All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m36s
主要改进: 1. 新增 json_parser.py - 统一的 JSON 解析工具 - 支持多种格式(纯 JSON、markdown、文本中的 JSON) - 多层 fallback 策略 - 安全的字段提取函数 2. 优化 intent.py 和 hybrid_router.py - 使用新的 json_parser - 优化 Prompt,更清晰的格式要求 - 更好的错误处理 3. 改进 state.py - 新增结构化状态字段 - ReactReasoningState、HybridRouterState、FastPathState - 向后兼容旧的 debug_info 4. 更新各节点模块 - 同时更新旧字段保持兼容 - reasoning.py - 更新 state.react_reasoning - hybrid_router.py - 更新 state.hybrid_router - fast_paths.py - 更新 state.fast_path
80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
"""
|
|
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
|
|
from ...main_graph.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} 次推理开始")
|
|
|
|
# 步骤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: 更新调试信息(同时更新旧的 debug_info 和新的结构化字段)
|
|
state.debug_info["last_reasoning"] = {
|
|
"action": result.action.name,
|
|
"confidence": result.confidence,
|
|
"reasoning": result.reasoning
|
|
}
|
|
state.debug_info["reasoning_result"] = result
|
|
state.last_action = result.action.name
|
|
|
|
# 更新新的结构化字段
|
|
state.react_reasoning.last_reasoning = {
|
|
"action": result.action.name,
|
|
"confidence": result.confidence,
|
|
"reasoning": result.reasoning
|
|
}
|
|
state.react_reasoning.reasoning_result = result
|
|
|
|
# 步骤5: 发送推理事件
|
|
await dispatch_custom_event(
|
|
"react_reasoning",
|
|
make_react_event(
|
|
state.reasoning_step,
|
|
result.action.name,
|
|
result.confidence,
|
|
result.reasoning
|
|
),
|
|
config
|
|
)
|
|
|
|
return state
|