修复循环推理bug
This commit is contained in:
@@ -151,6 +151,20 @@ class ReactIntentReasoner:
|
||||
result.reasoning = "已获取足够信息,直接回答"
|
||||
return result
|
||||
|
||||
# 检查 RAG 是否多次失败(reasoning_history 中有失败的 RAG 记录)
|
||||
# 失败的 RAG 记录特征:confidence = 0.0
|
||||
rag_history = context.get("reasoning_history", [])
|
||||
rag_fail_count = sum(
|
||||
1 for h in rag_history
|
||||
if h.get("action") in ("RETRIEVE_RAG", "RE_RETRIEVE_RAG") and h.get("confidence", 1.0) == 0.0
|
||||
)
|
||||
if rag_fail_count >= 2:
|
||||
# RAG 多次失败,应该直接回答而不是继续重试
|
||||
result.action = ReasoningAction.DIRECT_RESPONSE
|
||||
result.confidence = 0.7
|
||||
result.reasoning = f"RAG 已尝试 {rag_fail_count} 次均失败,知识库无相关内容,直接基于常识回答"
|
||||
return result
|
||||
|
||||
# 策略1:尝试使用 LLM 推理
|
||||
try:
|
||||
llm_result = await self._reason_with_llm(query, context)
|
||||
@@ -347,7 +361,7 @@ _reasoner: Optional[ReactIntentReasoner] = None
|
||||
_small_reasoner: Optional[ReactIntentReasoner] = None
|
||||
|
||||
|
||||
def _get_reasoner(use_small_llm: bool = False) -> ReactIntentReasoner:
|
||||
def _get_reasoner(use_small_llm: bool = True) -> ReactIntentReasoner:
|
||||
"""
|
||||
获取推理器实例
|
||||
|
||||
@@ -371,7 +385,7 @@ def _get_reasoner(use_small_llm: bool = False) -> ReactIntentReasoner:
|
||||
async def react_reason_async(
|
||||
query: str,
|
||||
context: Optional[Dict[str, Any]] = None,
|
||||
use_small_llm: bool = False
|
||||
use_small_llm: bool = True
|
||||
) -> ReasoningResult:
|
||||
"""
|
||||
便捷函数:异步 React 推理(推荐使用)
|
||||
|
||||
Reference in New Issue
Block a user