feat: 优化后的流式方案:双协程 + 结束哨兵 + turn/phase 元数据
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m26s
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m26s
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
"""
|
||||
极简 Agent 主图 - 用 LangGraph 原生 create_react_agent + 记忆节点
|
||||
极简 Agent 主图 - 自己的节点结构,更好控制流式
|
||||
"""
|
||||
|
||||
from langgraph.prebuilt import create_react_agent
|
||||
from langgraph.graph import StateGraph, START, END
|
||||
from langgraph.prebuilt import ToolNode
|
||||
from ..state import AgentState
|
||||
from ..nodes.memory_trigger import memory_trigger_node, set_mem0_client
|
||||
from ..nodes.agent import create_agent_node
|
||||
from backend.app.logger import info, warning
|
||||
from backend.app.tools import ALL_TOOLS
|
||||
|
||||
@@ -16,7 +17,7 @@ def build_agent_graph(
|
||||
max_steps: int = 10
|
||||
):
|
||||
"""
|
||||
构建包含记忆节点的 react agent 图
|
||||
构建包含记忆节点的 Agent 图
|
||||
|
||||
Args:
|
||||
chat_services: 模型服务字典
|
||||
@@ -24,7 +25,7 @@ def build_agent_graph(
|
||||
max_steps: 最大步数限制
|
||||
|
||||
Returns:
|
||||
编译好的 graph
|
||||
构建好的 StateGraph(未编译)
|
||||
"""
|
||||
# 获取主模型
|
||||
primary_model = chat_services.get("primary", next(iter(chat_services.values())))
|
||||
@@ -37,7 +38,8 @@ def build_agent_graph(
|
||||
async def init_state_node(state: AgentState):
|
||||
info("[Init State] 初始化状态,重置步数")
|
||||
return {
|
||||
"current_step": 0
|
||||
"current_step": 0,
|
||||
"max_steps": max_steps
|
||||
}
|
||||
|
||||
# ========== 2. 记忆节点(可选) ==========
|
||||
@@ -49,21 +51,39 @@ def build_agent_graph(
|
||||
except Exception as e:
|
||||
info(f"[Graph Builder] 记忆节点初始化失败: {e}")
|
||||
|
||||
# ========== 3. 创建 react agent 子图 ==========
|
||||
agent_runnable = create_react_agent(primary_model, ALL_TOOLS)
|
||||
# ========== 3. 核心节点 ==========
|
||||
llm_with_tools = primary_model.bind_tools(ALL_TOOLS)
|
||||
agent_node_fn = create_agent_node(llm_with_tools, primary_model)
|
||||
tool_node_fn = ToolNode(ALL_TOOLS)
|
||||
|
||||
# ========== 4. 构建主图 ==========
|
||||
# ========== 4. 条件边判断函数 ==========
|
||||
def should_continue(state: AgentState):
|
||||
"""判断是继续调用工具还是结束"""
|
||||
messages = state.messages
|
||||
last_message = messages[-1] if messages else None
|
||||
|
||||
if last_message and hasattr(last_message, 'tool_calls') and last_message.tool_calls:
|
||||
return "tools"
|
||||
|
||||
return "finalize"
|
||||
|
||||
# ========== 5. 完成节点 ==========
|
||||
async def finalize_node_simple(state: AgentState):
|
||||
info("[Finalize] 进入完成节点")
|
||||
return {}
|
||||
|
||||
# ========== 6. 构建图 ==========
|
||||
graph = StateGraph(AgentState)
|
||||
|
||||
graph.add_node("init_state", init_state_node)
|
||||
if retrieve_memory_node:
|
||||
graph.add_node("retrieve_memory", retrieve_memory_node)
|
||||
graph.add_node("memory_trigger", memory_trigger_node)
|
||||
graph.add_node("agent", agent_node_fn)
|
||||
graph.add_node("tools", tool_node_fn)
|
||||
graph.add_node("finalize", finalize_node_simple)
|
||||
|
||||
# 直接把 create_react_agent 的可运行对象作为节点
|
||||
graph.add_node("agent", agent_runnable)
|
||||
|
||||
# ========== 边的连接 ==========
|
||||
# ========== 7. 边的连接 ==========
|
||||
graph.add_edge(START, "init_state")
|
||||
|
||||
if retrieve_memory_node:
|
||||
@@ -73,7 +93,18 @@ def build_agent_graph(
|
||||
graph.add_edge("init_state", "memory_trigger")
|
||||
|
||||
graph.add_edge("memory_trigger", "agent")
|
||||
graph.add_edge("agent", END)
|
||||
|
||||
info("✅ [Graph Builder] 极简 Agent 图构建完成(用 create_react_agent)")
|
||||
graph.add_conditional_edges(
|
||||
"agent",
|
||||
should_continue,
|
||||
{
|
||||
"tools": "tools",
|
||||
"finalize": "finalize"
|
||||
}
|
||||
)
|
||||
|
||||
graph.add_edge("tools", "agent")
|
||||
graph.add_edge("finalize", END)
|
||||
|
||||
info("✅ [Graph Builder] 极简 Agent 图构建完成")
|
||||
return graph
|
||||
|
||||
@@ -67,7 +67,8 @@ def create_agent_node(llm_with_tools, llm):
|
||||
Returns:
|
||||
状态更新字典
|
||||
"""
|
||||
info(f"[Agent] 第 {state.current_step} 步推理")
|
||||
current_step = state.get("current_step", 0)
|
||||
info(f"[Agent] 第 {current_step} 步推理")
|
||||
|
||||
try:
|
||||
# 组装完整消息:系统提示 + 历史消息
|
||||
@@ -76,8 +77,8 @@ def create_agent_node(llm_with_tools, llm):
|
||||
info(f"[Agent] 消息数量: {len(full_messages)}, 最后一条: {type(full_messages[-1]).__name__}")
|
||||
|
||||
# 判断是否达到步数上限
|
||||
if state.current_step >= state.max_steps:
|
||||
info(f"[Agent] 达到步数上限 {state.max_steps},强制结束,不绑定工具")
|
||||
if current_step >= state.get("max_steps", 10):
|
||||
info(f"[Agent] 达到步数上限 {state.get('max_steps', 10)},强制结束,不绑定工具")
|
||||
current_llm = llm.bind_tools([])
|
||||
else:
|
||||
current_llm = llm_with_tools
|
||||
@@ -86,6 +87,9 @@ def create_agent_node(llm_with_tools, llm):
|
||||
|
||||
# 获取 token 队列
|
||||
token_queue = token_queue_var.get()
|
||||
if token_queue is None:
|
||||
error("[Agent] ❌ token_queue 为 None!")
|
||||
raise RuntimeError("token_queue 上下文变量未设置")
|
||||
|
||||
# 完整消息
|
||||
full_content = ""
|
||||
@@ -98,26 +102,28 @@ def create_agent_node(llm_with_tools, llm):
|
||||
# 处理 content
|
||||
if chunk.content:
|
||||
full_content += chunk.content
|
||||
if token_queue:
|
||||
await token_queue.put({
|
||||
"type": "llm_token",
|
||||
"node": "agent",
|
||||
"token": chunk.content,
|
||||
"reasoning_token": ""
|
||||
})
|
||||
await token_queue.put({
|
||||
"type": "llm_token",
|
||||
"node": "agent",
|
||||
"token": chunk.content,
|
||||
"reasoning_token": "",
|
||||
"turn": current_step,
|
||||
"phase": "answering" if not full_tool_calls else "thinking"
|
||||
})
|
||||
|
||||
# 处理 reasoning_content
|
||||
if hasattr(chunk, 'additional_kwargs') and chunk.additional_kwargs:
|
||||
reasoning_content = chunk.additional_kwargs.get("reasoning_content", "")
|
||||
if reasoning_content:
|
||||
full_reasoning_content += reasoning_content
|
||||
if token_queue:
|
||||
await token_queue.put({
|
||||
"type": "llm_token",
|
||||
"node": "agent",
|
||||
"token": "",
|
||||
"reasoning_token": reasoning_content
|
||||
})
|
||||
await token_queue.put({
|
||||
"type": "llm_token",
|
||||
"node": "agent",
|
||||
"token": "",
|
||||
"reasoning_token": reasoning_content,
|
||||
"turn": current_step,
|
||||
"phase": "thinking"
|
||||
})
|
||||
|
||||
# 处理 tool_calls
|
||||
if hasattr(chunk, 'tool_calls') and chunk.tool_calls:
|
||||
@@ -133,6 +139,14 @@ def create_agent_node(llm_with_tools, llm):
|
||||
break
|
||||
if not found:
|
||||
full_tool_calls.append(tc)
|
||||
# 发送工具调用开始事件
|
||||
await token_queue.put({
|
||||
"type": "tool_call_start",
|
||||
"tool": tc.get("name"),
|
||||
"args": tc.get("args"),
|
||||
"id": tc.get("id", ""),
|
||||
"turn": current_step
|
||||
})
|
||||
|
||||
# 构建完整的 AIMessage
|
||||
response = AIMessage(
|
||||
@@ -149,14 +163,21 @@ def create_agent_node(llm_with_tools, llm):
|
||||
# 返回状态更新
|
||||
return {
|
||||
"messages": [response],
|
||||
"current_step": state.current_step + 1,
|
||||
"llm_calls": state.llm_calls + 1
|
||||
"current_step": current_step + 1,
|
||||
"llm_calls": state.get("llm_calls", 0) + 1
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
error(f"[Agent] ❌ 第 {state.current_step} 步推理出错: {e}")
|
||||
error(f"[Agent] ❌ 第 {current_step} 步推理出错: {e}")
|
||||
import traceback
|
||||
error(f"[Agent] 堆栈: {traceback.format_exc()}")
|
||||
# 发送错误事件
|
||||
token_queue = token_queue_var.get()
|
||||
if token_queue:
|
||||
await token_queue.put({
|
||||
"type": "error",
|
||||
"message": str(e)
|
||||
})
|
||||
raise
|
||||
|
||||
return agent_node
|
||||
|
||||
Reference in New Issue
Block a user