feat: 优化后的流式方案:双协程 + 结束哨兵 + turn/phase 元数据
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m26s

This commit is contained in:
2026-05-07 02:21:09 +08:00
parent 58a2c8c081
commit eb33203b5c
4 changed files with 343 additions and 106 deletions

View File

@@ -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