流式基本流程已通过修改了yield和return的冲突导致的问题

This commit is contained in:
Dong_master
2025-07-04 03:26:44 +08:00
parent 4005a8a3e2
commit 68cdd163d3
8 changed files with 323 additions and 117 deletions
+25 -19
View File
@@ -24,25 +24,30 @@ class LocalAgentRunner(runner.RequestRunner):
pending_tool_calls = []
req_messages = query.prompt.messages.copy() + query.messages.copy() + [query.user_message]
is_stream = query.adapter.is_stream_output_supported()
try:
is_stream = query.adapter.is_stream
except AttributeError:
is_stream = False
# while True:
# pass
if not is_stream:
# 非流式输出,直接请求
# print(123)
msg = await query.use_llm_model.requester.invoke_llm(
query,
query.use_llm_model,
req_messages,
query.use_funcs,
is_stream,
extra_args=query.use_llm_model.model_entity.extra_args,
)
yield msg
final_msg = msg
print(final_msg)
else:
# 流式输出,需要处理工具调用
tool_calls_map: dict[str, llm_entities.ToolCall] = {}
async for msg in await query.use_llm_model.requester.invoke_llm(
async for msg in query.use_llm_model.requester.invoke_llm_stream(
query,
query.use_llm_model,
req_messages,
@@ -51,20 +56,20 @@ class LocalAgentRunner(runner.RequestRunner):
extra_args=query.use_llm_model.model_entity.extra_args,
):
yield msg
if msg.tool_calls:
for tool_call in msg.tool_calls:
if tool_call.id not in tool_calls_map:
tool_calls_map[tool_call.id] = llm_entities.ToolCall(
id=tool_call.id,
type=tool_call.type,
function=llm_entities.FunctionCall(
name=tool_call.function.name if tool_call.function else '',
arguments=''
),
)
if tool_call.function and tool_call.function.arguments:
# 流式处理中,工具调用参数可能分多个chunk返回,需要追加而不是覆盖
tool_calls_map[tool_call.id].function.arguments += tool_call.function.arguments
# if msg.tool_calls:
# for tool_call in msg.tool_calls:
# if tool_call.id not in tool_calls_map:
# tool_calls_map[tool_call.id] = llm_entities.ToolCall(
# id=tool_call.id,
# type=tool_call.type,
# function=llm_entities.FunctionCall(
# name=tool_call.function.name if tool_call.function else '',
# arguments=''
# ),
# )
# if tool_call.function and tool_call.function.arguments:
# # 流式处理中,工具调用参数可能分多个chunk返回,需要追加而不是覆盖
# tool_calls_map[tool_call.id].function.arguments += tool_call.function.arguments
final_msg = llm_entities.Message(
role=msg.role,
content=msg.all_content,
@@ -105,7 +110,7 @@ class LocalAgentRunner(runner.RequestRunner):
if is_stream:
tool_calls_map = {}
async for msg in await query.use_llm_model.requester.invoke_llm(
async for msg in await query.use_llm_model.requester.invoke_llm_stream(
query,
query.use_llm_model,
req_messages,
@@ -130,10 +135,11 @@ class LocalAgentRunner(runner.RequestRunner):
tool_calls_map[tool_call.id].function.arguments += tool_call.function.arguments
final_msg = llm_entities.Message(
role=msg.role,
content=all_content,
content=msg.all_content,
tool_calls=list(tool_calls_map.values()),
)
else:
print("非流式")
# 处理完所有调用,再次请求
msg = await query.use_llm_model.requester.invoke_llm(
query,