muti tool_call

This commit is contained in:
朱潮 2025-12-14 09:29:19 +08:00
parent a41d45ccd9
commit 89f5b721ab

View File

@ -143,8 +143,8 @@ async def enhanced_generate_stream_response(
logger.info(f"Starting agent stream response") logger.info(f"Starting agent stream response")
chunk_id = 0 chunk_id = 0
message_tag = "" message_tag = ""
function_call = {}
function_name = "" function_name = ""
tool_args = ""
async for msg, metadata in agent.astream({"messages": messages}, stream_mode="messages"): async for msg, metadata in agent.astream({"messages": messages}, stream_mode="messages"):
new_content = "" new_content = ""
@ -153,10 +153,13 @@ async def enhanced_generate_stream_response(
if msg.tool_call_chunks: # 检查工具调用块 if msg.tool_call_chunks: # 检查工具调用块
if message_tag != "TOOL_CALL": if message_tag != "TOOL_CALL":
message_tag = "TOOL_CALL" message_tag = "TOOL_CALL"
function_name = ""
if msg.tool_call_chunks[0]["name"]: if msg.tool_call_chunks[0]["name"]:
function_name = msg.tool_call_chunks[0]["name"] function_name = msg.tool_call_chunks[0]["name"]
if msg.tool_call_chunks[0]["args"]: if function_name and msg.tool_call_chunks[0]["args"]:
tool_args += msg.tool_call_chunks[0]["args"] if function_name not in function_call:
function_call[function_name] = ""
function_call[function_name] += msg.tool_call_chunks[0]["args"]
elif len(msg.content) > 0: elif len(msg.content) > 0:
preamble_completed.set() preamble_completed.set()
await output_queue.put(("preamble_done", None)) await output_queue.put(("preamble_done", None))
@ -170,8 +173,9 @@ async def enhanced_generate_stream_response(
("finish_reason" in msg.response_metadata and msg.response_metadata["finish_reason"] == "tool_calls") or \ ("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") ("stop_reason" in msg.response_metadata and msg.response_metadata["stop_reason"] == "tool_use")
): ):
new_content = f"[{message_tag}] {function_name}\n{tool_args}" for function_name, args in function_call.items():
message_tag = "TOOL_CALL" new_content = f"[{message_tag}] {function_name}\n{args}"
message_tag = "TOOL_CALL_FINISH"
elif isinstance(msg, ToolMessage) and len(msg.content) > 0: elif isinstance(msg, ToolMessage) and len(msg.content) > 0:
message_tag = "TOOL_RESPONSE" message_tag = "TOOL_RESPONSE"
new_content = f"[{message_tag}] {msg.name}\n{msg.text}" new_content = f"[{message_tag}] {msg.name}\n{msg.text}"