推理优化
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m36s

This commit is contained in:
2026-05-06 04:26:06 +08:00
parent 1260bef5cb
commit ef6fbc1521
12 changed files with 313 additions and 129 deletions

View File

@@ -130,41 +130,60 @@ class ReactIntentReasoner:
retrieved_docs = context.get("retrieved_docs", [])
messages = context.get("messages", [])
# 关键修改:不要在第一次 rag_retrieve 后就直接回答,允许再推理一次
# 让推理逻辑有机会判断 RAG 结果好不好,要不要再检索或转 web search
# 获取 RAG 相关状态
previous_actions = context.get("previous_actions", [])
rag_count = previous_actions.count("RETRIEVE_RAG") # 修复:大写
web_search_count = previous_actions.count("web_search")
rag_count = previous_actions.count("RETRIEVE_RAG")
rag_attempts = context.get("rag_attempts", rag_count)
rag_confidence = context.get("rag_confidence", 0.0)
retrieved_docs = context.get("retrieved_docs", [])
# 如果已经有检索文档了,直接回答
if retrieved_docs and len(retrieved_docs) > 0:
result.action = ReasoningAction.DIRECT_RESPONSE
result.confidence = 0.95
result.reasoning = "已获取检索文档,直接回答"
return result
# 只有当 rag 或 web search 已经超过 1 次,或者已经有推理在 rag 之后,才直接回答
if rag_count >= 2 or web_search_count >= 1:
result.action = ReasoningAction.DIRECT_RESPONSE
result.confidence = 0.95
result.reasoning = "已获取足够信息,直接回答"
return result
web_search_count = previous_actions.count("web_search")
# 检查 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 retrieved_docs and len(retrieved_docs) > 0:
if rag_confidence >= 0.6:
# 置信度足够高,直接回答
result.action = ReasoningAction.DIRECT_RESPONSE
result.confidence = 0.95
result.reasoning = f"已获取检索文档,置信度={rag_confidence:.2f},直接回答"
return result
elif rag_attempts >= 2 or rag_fail_count >= 2:
# 尝试次数已够或多次失败,放弃 RAG转向联网搜索
result.action = ReasoningAction.WEB_SEARCH
result.confidence = 0.8
result.reasoning = f"RAG 置信度={rag_confidence:.2f} < 0.6,且已尝试 {rag_attempts} 次,转向联网搜索"
result.metadata["need_web_search"] = True
result.metadata["search_query"] = query
return result
else:
# 置信度不够但还有尝试机会,再查一次
result.action = ReasoningAction.RETRIEVE_RAG
result.confidence = 0.8
result.reasoning = f"已获取检索文档但置信度={rag_confidence:.2f} < 0.6,可再尝试一次"
result.retrieval_config.need_retrieval = True
result.retrieval_config.retrieval_query = query
return result
# 如果 RAG 已多次失败且无文档,直接回答(基于常识)
if rag_fail_count >= 2:
# RAG 多次失败,应该直接回答而不是继续重试
result.action = ReasoningAction.DIRECT_RESPONSE
result.confidence = 0.7
result.reasoning = f"RAG 已尝试 {rag_fail_count} 次均失败,知识库无相关内容,直接基于常识回答"
return result
# 如果 web search 已执行过,直接回答
if web_search_count >= 1:
result.action = ReasoningAction.DIRECT_RESPONSE
result.confidence = 0.95
result.reasoning = "已获取联网搜索结果,直接回答"
return result
# 策略1尝试使用 LLM 推理
try:
llm_result = await self._reason_with_llm(query, context)
@@ -194,6 +213,10 @@ class ReactIntentReasoner:
context_parts = []
if context.get("retrieved_docs"):
context_parts.append(f"- 已检索文档: {len(context['retrieved_docs'])}")
if context.get("rag_confidence") is not None:
context_parts.append(f"- RAG 置信度: {context['rag_confidence']:.2f}")
if context.get("rag_attempts"):
context_parts.append(f"- RAG 尝试次数: {context['rag_attempts']}")
if context.get("previous_actions"):
context_parts.append(f"- 历史动作: {context['previous_actions']}")
@@ -202,7 +225,7 @@ class ReactIntentReasoner:
return f"""你是一个专业的意图推理助手。请分析用户的查询,决定下一步应该做什么。
可选动作:
1. DIRECT_RESPONSE - 直接回答(闲聊、打招呼、不需要额外信息)
1. DIRECT_RESPONSE - 直接回答(闲聊、打招呼、不需要额外信息,或已有足够信息
2. RETRIEVE_RAG - 需要查询知识库(询问知识、政策、文档等)
3. RE_RETRIEVE_RAG - 需要重新检索(之前的结果不够,或者用户明确说"再查查""更多"
4. WEB_SEARCH - 需要联网搜索(询问最新资讯、热点、实时信息、知识库中没有的内容)
@@ -212,6 +235,12 @@ class ReactIntentReasoner:
- news_analysis: 资讯、新闻、热点分析相关
6. CLARIFY - 需要澄清用户的问题(问题不明确)
判断规则:
- 如果 RAG 置信度 >= 0.6 且有检索文档,应返回 DIRECT_RESPONSE
- 如果 RAG 置信度 < 0.6 且尝试次数 < 2可返回 RETRIEVE_RAG 再试一次
- 如果 RAG 置信度 < 0.6 且尝试次数 >= 2应返回 WEB_SEARCH
- 如果已联网搜索过,应返回 DIRECT_RESPONSE
用户查询: {query}
当前上下文:
{context_str}