Files
ailine/backend/app/deprecated/reasoning.py

121 lines
4.1 KiB
Python
Raw Normal View History

2026-05-05 00:54:04 +08:00
"""
React 推理节点
使用 intent.py 进行意图推理
"""
2026-05-05 23:17:00 +08:00
from typing import Optional
2026-05-05 00:54:04 +08:00
from datetime import datetime
2026-05-05 23:17:00 +08:00
from langchain_core.runnables.config import RunnableConfig
2026-05-05 00:54:04 +08:00
from backend.app.core.intent import react_reason_async, ReasoningResult, ReasoningAction
from ..state import MainGraphState
2026-05-06 01:15:52 +08:00
from backend.app.logger import info
2026-05-05 00:54:04 +08:00
from ._utils import dispatch_custom_event, make_react_event
2026-05-05 23:17:00 +08:00
async def react_reason_node(state: MainGraphState, config: Optional[RunnableConfig] = None) -> MainGraphState:
2026-05-05 00:54:04 +08:00
"""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
# ==================================================
# 原来的逻辑(第二次推理或没有保存结果时使用)
# ==================================================
2026-05-05 00:54:04 +08:00
# 步骤1: 准备上下文
context = {
"retrieved_docs": state.rag_docs,
2026-05-06 04:26:06 +08:00
"rag_confidence": getattr(state, "rag_confidence", 0.0),
"rag_attempts": getattr(state, "rag_attempts", 0),
2026-05-05 00:54:04 +08:00
"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
2026-05-05 00:54:04 +08:00
# 步骤5: 发送推理事件
await dispatch_custom_event(
"react_reasoning",
make_react_event(
state.reasoning_step,
result.action.name,
result.confidence,
result.reasoning
),
config
)
return state