mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-22 12:26:08 +00:00
fix: In the runner, every 8 tokens yield
This commit is contained in:
@@ -189,8 +189,7 @@ class OpenAIChatCompletions(requester.LLMAPIRequester):
|
|||||||
delta_message.is_final = True
|
delta_message.is_final = True
|
||||||
delta_message.content = current_content
|
delta_message.content = current_content
|
||||||
|
|
||||||
if chunk_idx % 64 == 0 or delta_message.is_final:
|
yield delta_message
|
||||||
yield delta_message
|
|
||||||
# return
|
# return
|
||||||
|
|
||||||
async def _closure(
|
async def _closure(
|
||||||
|
|||||||
@@ -195,5 +195,4 @@ class GiteeAIChatCompletions(chatcmpl.OpenAIChatCompletions):
|
|||||||
delta_message.is_final = True
|
delta_message.is_final = True
|
||||||
delta_message.content = current_content
|
delta_message.content = current_content
|
||||||
|
|
||||||
if chunk_idx % 64 == 0 or delta_message.is_final:
|
yield delta_message
|
||||||
yield delta_message
|
|
||||||
|
|||||||
@@ -286,8 +286,7 @@ class ModelScopeChatCompletions(requester.LLMAPIRequester):
|
|||||||
delta_message.is_final = True
|
delta_message.is_final = True
|
||||||
delta_message.content = current_content
|
delta_message.content = current_content
|
||||||
|
|
||||||
if chunk_idx % 64 == 0 or delta_message.is_final:
|
yield delta_message
|
||||||
yield delta_message
|
|
||||||
# return
|
# return
|
||||||
|
|
||||||
async def invoke_llm(
|
async def invoke_llm(
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ class LocalAgentRunner(runner.RequestRunner):
|
|||||||
else:
|
else:
|
||||||
# 流式输出,需要处理工具调用
|
# 流式输出,需要处理工具调用
|
||||||
tool_calls_map: dict[str, llm_entities.ToolCall] = {}
|
tool_calls_map: dict[str, llm_entities.ToolCall] = {}
|
||||||
|
msg_idx = 0
|
||||||
async for msg in query.use_llm_model.requester.invoke_llm_stream(
|
async for msg in query.use_llm_model.requester.invoke_llm_stream(
|
||||||
query,
|
query,
|
||||||
query.use_llm_model,
|
query.use_llm_model,
|
||||||
@@ -54,7 +55,9 @@ class LocalAgentRunner(runner.RequestRunner):
|
|||||||
query.use_funcs,
|
query.use_funcs,
|
||||||
extra_args=query.use_llm_model.model_entity.extra_args,
|
extra_args=query.use_llm_model.model_entity.extra_args,
|
||||||
):
|
):
|
||||||
yield msg
|
msg_idx = msg_idx + 1
|
||||||
|
if msg_idx % 8 == 0 or msg.is_final:
|
||||||
|
yield msg
|
||||||
if msg.tool_calls:
|
if msg.tool_calls:
|
||||||
for tool_call in msg.tool_calls:
|
for tool_call in msg.tool_calls:
|
||||||
if tool_call.id not in tool_calls_map:
|
if tool_call.id not in tool_calls_map:
|
||||||
@@ -115,19 +118,19 @@ class LocalAgentRunner(runner.RequestRunner):
|
|||||||
extra_args=query.use_llm_model.model_entity.extra_args,
|
extra_args=query.use_llm_model.model_entity.extra_args,
|
||||||
):
|
):
|
||||||
yield msg
|
yield msg
|
||||||
if msg.tool_calls:
|
if msg.tool_calls:
|
||||||
for tool_call in msg.tool_calls:
|
for tool_call in msg.tool_calls:
|
||||||
if tool_call.id not in tool_calls_map:
|
if tool_call.id not in tool_calls_map:
|
||||||
tool_calls_map[tool_call.id] = llm_entities.ToolCall(
|
tool_calls_map[tool_call.id] = llm_entities.ToolCall(
|
||||||
id=tool_call.id,
|
id=tool_call.id,
|
||||||
type=tool_call.type,
|
type=tool_call.type,
|
||||||
function=llm_entities.FunctionCall(
|
function=llm_entities.FunctionCall(
|
||||||
name=tool_call.function.name if tool_call.function else '', arguments=''
|
name=tool_call.function.name if tool_call.function else '', arguments=''
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
if tool_call.function and tool_call.function.arguments:
|
if tool_call.function and tool_call.function.arguments:
|
||||||
# 流式处理中,工具调用参数可能分多个chunk返回,需要追加而不是覆盖
|
# 流式处理中,工具调用参数可能分多个chunk返回,需要追加而不是覆盖
|
||||||
tool_calls_map[tool_call.id].function.arguments += tool_call.function.arguments
|
tool_calls_map[tool_call.id].function.arguments += tool_call.function.arguments
|
||||||
final_msg = llm_entities.Message(
|
final_msg = llm_entities.Message(
|
||||||
role=msg.role,
|
role=msg.role,
|
||||||
content=msg.all_content,
|
content=msg.all_content,
|
||||||
|
|||||||
Reference in New Issue
Block a user