From edb895bbf9bec1e27a7e44f5af7d1ef57516706f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Sun, 14 Dec 2025 21:21:00 +0800 Subject: [PATCH] tool_call_chunks --- routes/chat.py | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/routes/chat.py b/routes/chat.py index 3e922b3..9efade5 100644 --- a/routes/chat.py +++ b/routes/chat.py @@ -143,26 +143,21 @@ async def enhanced_generate_stream_response( logger.info(f"Starting agent stream response") chunk_id = 0 message_tag = "" - function_call = {} - tool_id = "" async for msg, metadata in agent.astream({"messages": messages}, stream_mode="messages"): new_content = "" + if isinstance(msg, AIMessageChunk): - # 判断是否有工具调用 - if msg.tool_call_chunks: # 检查工具调用块 - if message_tag != "TOOL_CALL": - message_tag = "TOOL_CALL" - if msg.tool_call_chunks[0]["name"]: - tool_id = msg.tool_call_chunks[0]["id"] - if tool_id not in function_call: - function_call[tool_id] = { - "function_name": msg.tool_call_chunks[0]["name"], - "arguments": "" - } - if tool_id and tool_id in function_call and msg.tool_call_chunks[0]["args"]: - function_call[tool_id]["arguments"] += msg.tool_call_chunks[0]["args"] - elif len(msg.content) > 0: + # 处理工具调用 + if msg.tool_call_chunks: + message_tag = "TOOL_CALL" + for tool_call_chunk in msg.tool_call_chunks: + if tool_call_chunk["name"]: + new_content = f"[{message_tag}] {tool_call_chunk['name']}\n" + new_content += tool_call_chunk['args'] + + # 处理文本内容 + elif msg.content: preamble_completed.set() await output_queue.put(("preamble_done", None)) meta_message_tag = metadata.get("message_tag", "ANSWER") @@ -170,19 +165,13 @@ async def enhanced_generate_stream_response( message_tag = meta_message_tag new_content = f"[{meta_message_tag}]\n" new_content += msg.text - elif message_tag == "TOOL_CALL" and \ - ( - ("finish_reason" in msg.response_metadata and msg.response_metadata["finish_reason"] == "tool_calls") or \ - ("stop_reason" in msg.response_metadata and msg.response_metadata["stop_reason"] == "tool_use") - ): - for args in function_call.values(): - new_content += f"[{message_tag}] {args['function_name']}\n{args['arguments']}\n" - message_tag = "TOOL_CALL_FINISH" - elif isinstance(msg, ToolMessage) and len(msg.content) > 0: + + # 处理工具响应 + elif isinstance(msg, ToolMessage) and msg.content: message_tag = "TOOL_RESPONSE" new_content = f"[{message_tag}] {msg.name}\n{msg.text}\n" - # 只有当有新内容时才发送chunk + # 发送内容块 if new_content: if chunk_id == 0: logger.info(f"Agent首个Token已生成, 开始流式输出")