This commit is contained in:
@@ -93,8 +93,8 @@ def create_agent_node(llm_with_tools, llm):
|
|||||||
# 告诉前端:新的一轮开始(如果流式)
|
# 告诉前端:新的一轮开始(如果流式)
|
||||||
if is_streaming:
|
if is_streaming:
|
||||||
await queue.put({
|
await queue.put({
|
||||||
"type": "turn_start",
|
"type": "node_start",
|
||||||
"turn": turn,
|
"node": "agent",
|
||||||
})
|
})
|
||||||
|
|
||||||
# 选择 LLM
|
# 选择 LLM
|
||||||
@@ -119,12 +119,11 @@ def create_agent_node(llm_with_tools, llm):
|
|||||||
full_content += chunk.content
|
full_content += chunk.content
|
||||||
await queue.put({
|
await queue.put({
|
||||||
"type": "llm_token",
|
"type": "llm_token",
|
||||||
"turn": turn,
|
"node": "agent",
|
||||||
"phase": "answering",
|
|
||||||
"token": chunk.content,
|
"token": chunk.content,
|
||||||
"reasoning_token": ""
|
"reasoning_token": ""
|
||||||
})
|
})
|
||||||
|
|
||||||
# 2. 处理 reasoning token
|
# 2. 处理 reasoning token
|
||||||
if hasattr(chunk, 'additional_kwargs') and chunk.additional_kwargs:
|
if hasattr(chunk, 'additional_kwargs') and chunk.additional_kwargs:
|
||||||
reasoning_content = chunk.additional_kwargs.get("reasoning_content", "")
|
reasoning_content = chunk.additional_kwargs.get("reasoning_content", "")
|
||||||
@@ -132,8 +131,7 @@ def create_agent_node(llm_with_tools, llm):
|
|||||||
full_reasoning_content += reasoning_content
|
full_reasoning_content += reasoning_content
|
||||||
await queue.put({
|
await queue.put({
|
||||||
"type": "llm_token",
|
"type": "llm_token",
|
||||||
"turn": turn,
|
"node": "agent",
|
||||||
"phase": "reasoning",
|
|
||||||
"token": "",
|
"token": "",
|
||||||
"reasoning_token": reasoning_content
|
"reasoning_token": reasoning_content
|
||||||
})
|
})
|
||||||
@@ -202,11 +200,13 @@ def create_agent_node(llm_with_tools, llm):
|
|||||||
# 发送工具开始事件(如果流式)
|
# 发送工具开始事件(如果流式)
|
||||||
if is_streaming:
|
if is_streaming:
|
||||||
await queue.put({
|
await queue.put({
|
||||||
"type": "tool_start",
|
"type": "custom",
|
||||||
"turn": turn,
|
"data": {
|
||||||
"tool": tool_name,
|
"type": "tool_start",
|
||||||
"args": tool_args,
|
"tool": tool_name,
|
||||||
"id": tool_id
|
"args": tool_args,
|
||||||
|
"id": tool_id
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
# 找到并执行对应工具
|
# 找到并执行对应工具
|
||||||
@@ -228,11 +228,13 @@ def create_agent_node(llm_with_tools, llm):
|
|||||||
# 发送工具结束事件(如果流式)
|
# 发送工具结束事件(如果流式)
|
||||||
if is_streaming:
|
if is_streaming:
|
||||||
await queue.put({
|
await queue.put({
|
||||||
"type": "tool_end",
|
"type": "custom",
|
||||||
"turn": turn,
|
"data": {
|
||||||
"tool": tool_name,
|
"type": "tool_end",
|
||||||
"id": tool_id,
|
"tool": tool_name,
|
||||||
"result": str(tool_result)
|
"id": tool_id,
|
||||||
|
"result": str(tool_result)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
# 构造 ToolMessage
|
# 构造 ToolMessage
|
||||||
@@ -248,14 +250,8 @@ def create_agent_node(llm_with_tools, llm):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# 没有工具调用,最终输出
|
# 没有工具调用,最终输出(不需要发 final_answer,因为 llm_token 已经发了)
|
||||||
info(f"[Agent] 第 {turn} 轮:完成,无工具调用")
|
info(f"[Agent] 第 {turn} 轮:完成,无工具调用")
|
||||||
if is_streaming:
|
|
||||||
await queue.put({
|
|
||||||
"type": "final_answer",
|
|
||||||
"turn": turn,
|
|
||||||
"content": full_content
|
|
||||||
})
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# 构建完整的 AIMessage 用于状态更新
|
# 构建完整的 AIMessage 用于状态更新
|
||||||
|
|||||||
Reference in New Issue
Block a user