Files
ailine/backend/app/main_graph/nodes/reasoning.py
root 47ccac5b35
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 6m22s
优化:复用 hybrid_router 的推理结果,避免重复 LLM 调用
2026-05-06 18:50:22 +08:00

121 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
React 推理节点
使用 intent.py 进行意图推理
"""
from typing import Optional
from datetime import datetime
from langchain_core.runnables.config import RunnableConfig
from backend.app.core.intent import react_reason_async, ReasoningResult, ReasoningAction
from ..state import MainGraphState
from backend.app.logger import info
from ._utils import dispatch_custom_event, make_react_event
async def react_reason_node(state: MainGraphState, config: Optional[RunnableConfig] = None) -> MainGraphState:
"""React 模式推理节点:判断下一步做什么"""
state.current_phase = "react_reasoning"
state.reasoning_step += 1
info(f"[推理] 第 {state.reasoning_step} 次推理开始")
# ==================================================
# 优化:如果是第一次推理,检查 hybrid_router 的结果!
# 避免重复推理!
# ==================================================
if state.reasoning_step == 1 and state.hybrid_router.decision and state.hybrid_router.decision.reasoning_result:
# 有保存的推理结果,直接复用!
decision = state.hybrid_router.decision
result: ReasoningResult = decision.reasoning_result
info(f"[推理] 第1次推理复用 hybrid_router 结果: action={result.action.name}, confidence={result.confidence}")
if result.reasoning:
info(f"[推理] 推理过程: {result.reasoning}")
# 记录推理历史
state.reasoning_history.append({
"step": state.reasoning_step,
"action": result.action.name,
"confidence": result.confidence,
"reasoning": result.reasoning,
"timestamp": datetime.now().isoformat()
})
# 更新状态
state.react_reasoning.last_reasoning = {
"action": result.action.name,
"confidence": result.confidence,
"reasoning": result.reasoning
}
state.react_reasoning.reasoning_result = result
state.last_action = result.action.name
# 发送推理事件
await dispatch_custom_event(
"react_reasoning",
make_react_event(
state.reasoning_step,
result.action.name,
result.confidence,
result.reasoning
),
config
)
return state
# ==================================================
# 原来的逻辑(第二次推理或没有保存结果时使用)
# ==================================================
# 步骤1: 准备上下文
context = {
"retrieved_docs": state.rag_docs,
"rag_confidence": getattr(state, "rag_confidence", 0.0),
"rag_attempts": getattr(state, "rag_attempts", 0),
"previous_actions": [h.get("action") for h in state.reasoning_history],
"reasoning_history": state.reasoning_history,
"messages": state.messages,
"errors": state.errors
}
# 步骤2: 执行推理
result: ReasoningResult = await react_reason_async(state.user_query, context)
info(f"[推理] 推理结果: action={result.action.name}, confidence={result.confidence}")
if result.reasoning:
info(f"[推理] 推理过程: {result.reasoning}")
# 步骤3: 记录推理历史
state.reasoning_history.append({
"step": state.reasoning_step,
"action": result.action.name,
"confidence": result.confidence,
"reasoning": result.reasoning,
"timestamp": datetime.now().isoformat()
})
# 步骤4: 更新状态 - 只使用新的结构化字段
state.react_reasoning.last_reasoning = {
"action": result.action.name,
"confidence": result.confidence,
"reasoning": result.reasoning
}
state.react_reasoning.reasoning_result = result
state.last_action = result.action.name
# 步骤5: 发送推理事件
await dispatch_custom_event(
"react_reasoning",
make_react_event(
state.reasoning_step,
result.action.name,
result.confidence,
result.reasoning
),
config
)
return state