tool_call_chunks
This commit is contained in:
parent
559040f7ce
commit
edb895bbf9
@ -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已生成, 开始流式输出")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user