mirror of
https://github.com/open-webui/open-webui
synced 2025-06-26 18:26:48 +00:00
refac
This commit is contained in:
parent
46c4da4864
commit
b964b175d4
@ -1474,129 +1474,136 @@ async def process_chat_response(
|
|||||||
extra_params=extra_params,
|
extra_params=extra_params,
|
||||||
)
|
)
|
||||||
|
|
||||||
if "selected_model_id" in data:
|
if data:
|
||||||
model_id = data["selected_model_id"]
|
if "selected_model_id" in data:
|
||||||
Chats.upsert_message_to_chat_by_id_and_message_id(
|
model_id = data["selected_model_id"]
|
||||||
metadata["chat_id"],
|
Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata["message_id"],
|
metadata["chat_id"],
|
||||||
{
|
metadata["message_id"],
|
||||||
"selectedModelId": model_id,
|
{
|
||||||
},
|
"selectedModelId": model_id,
|
||||||
)
|
},
|
||||||
else:
|
|
||||||
choices = data.get("choices", [])
|
|
||||||
if not choices:
|
|
||||||
continue
|
|
||||||
|
|
||||||
delta = choices[0].get("delta", {})
|
|
||||||
delta_tool_calls = delta.get("tool_calls", None)
|
|
||||||
|
|
||||||
if delta_tool_calls:
|
|
||||||
for delta_tool_call in delta_tool_calls:
|
|
||||||
tool_call_index = delta_tool_call.get("index")
|
|
||||||
|
|
||||||
if tool_call_index is not None:
|
|
||||||
if (
|
|
||||||
len(response_tool_calls)
|
|
||||||
<= tool_call_index
|
|
||||||
):
|
|
||||||
response_tool_calls.append(
|
|
||||||
delta_tool_call
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
delta_name = delta_tool_call.get(
|
|
||||||
"function", {}
|
|
||||||
).get("name")
|
|
||||||
delta_arguments = delta_tool_call.get(
|
|
||||||
"function", {}
|
|
||||||
).get("arguments")
|
|
||||||
|
|
||||||
if delta_name:
|
|
||||||
response_tool_calls[
|
|
||||||
tool_call_index
|
|
||||||
]["function"]["name"] += delta_name
|
|
||||||
|
|
||||||
if delta_arguments:
|
|
||||||
response_tool_calls[
|
|
||||||
tool_call_index
|
|
||||||
]["function"][
|
|
||||||
"arguments"
|
|
||||||
] += delta_arguments
|
|
||||||
|
|
||||||
value = delta.get("content")
|
|
||||||
|
|
||||||
if value:
|
|
||||||
content = f"{content}{value}"
|
|
||||||
|
|
||||||
if not content_blocks:
|
|
||||||
content_blocks.append(
|
|
||||||
{
|
|
||||||
"type": "text",
|
|
||||||
"content": "",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
content_blocks[-1]["content"] = (
|
|
||||||
content_blocks[-1]["content"] + value
|
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
choices = data.get("choices", [])
|
||||||
|
if not choices:
|
||||||
|
continue
|
||||||
|
|
||||||
if DETECT_REASONING:
|
delta = choices[0].get("delta", {})
|
||||||
content, content_blocks, _ = (
|
delta_tool_calls = delta.get("tool_calls", None)
|
||||||
tag_content_handler(
|
|
||||||
"reasoning",
|
if delta_tool_calls:
|
||||||
reasoning_tags,
|
for delta_tool_call in delta_tool_calls:
|
||||||
content,
|
tool_call_index = delta_tool_call.get(
|
||||||
content_blocks,
|
"index"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if tool_call_index is not None:
|
||||||
|
if (
|
||||||
|
len(response_tool_calls)
|
||||||
|
<= tool_call_index
|
||||||
|
):
|
||||||
|
response_tool_calls.append(
|
||||||
|
delta_tool_call
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
delta_name = delta_tool_call.get(
|
||||||
|
"function", {}
|
||||||
|
).get("name")
|
||||||
|
delta_arguments = (
|
||||||
|
delta_tool_call.get(
|
||||||
|
"function", {}
|
||||||
|
).get("arguments")
|
||||||
|
)
|
||||||
|
|
||||||
|
if delta_name:
|
||||||
|
response_tool_calls[
|
||||||
|
tool_call_index
|
||||||
|
]["function"][
|
||||||
|
"name"
|
||||||
|
] += delta_name
|
||||||
|
|
||||||
|
if delta_arguments:
|
||||||
|
response_tool_calls[
|
||||||
|
tool_call_index
|
||||||
|
]["function"][
|
||||||
|
"arguments"
|
||||||
|
] += delta_arguments
|
||||||
|
|
||||||
|
value = delta.get("content")
|
||||||
|
|
||||||
|
if value:
|
||||||
|
content = f"{content}{value}"
|
||||||
|
|
||||||
|
if not content_blocks:
|
||||||
|
content_blocks.append(
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"content": "",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
content_blocks[-1]["content"] = (
|
||||||
|
content_blocks[-1]["content"] + value
|
||||||
)
|
)
|
||||||
|
|
||||||
if DETECT_CODE_INTERPRETER:
|
if DETECT_REASONING:
|
||||||
content, content_blocks, end = (
|
content, content_blocks, _ = (
|
||||||
tag_content_handler(
|
tag_content_handler(
|
||||||
"code_interpreter",
|
"reasoning",
|
||||||
code_interpreter_tags,
|
reasoning_tags,
|
||||||
content,
|
content,
|
||||||
content_blocks,
|
content_blocks,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
if end:
|
if DETECT_CODE_INTERPRETER:
|
||||||
break
|
content, content_blocks, end = (
|
||||||
|
tag_content_handler(
|
||||||
if DETECT_SOLUTION:
|
"code_interpreter",
|
||||||
content, content_blocks, _ = (
|
code_interpreter_tags,
|
||||||
tag_content_handler(
|
content,
|
||||||
"solution",
|
content_blocks,
|
||||||
solution_tags,
|
)
|
||||||
content,
|
|
||||||
content_blocks,
|
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
if ENABLE_REALTIME_CHAT_SAVE:
|
if end:
|
||||||
# Save message in the database
|
break
|
||||||
Chats.upsert_message_to_chat_by_id_and_message_id(
|
|
||||||
metadata["chat_id"],
|
if DETECT_SOLUTION:
|
||||||
metadata["message_id"],
|
content, content_blocks, _ = (
|
||||||
{
|
tag_content_handler(
|
||||||
|
"solution",
|
||||||
|
solution_tags,
|
||||||
|
content,
|
||||||
|
content_blocks,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if ENABLE_REALTIME_CHAT_SAVE:
|
||||||
|
# Save message in the database
|
||||||
|
Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
|
metadata["chat_id"],
|
||||||
|
metadata["message_id"],
|
||||||
|
{
|
||||||
|
"content": serialize_content_blocks(
|
||||||
|
content_blocks
|
||||||
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
data = {
|
||||||
"content": serialize_content_blocks(
|
"content": serialize_content_blocks(
|
||||||
content_blocks
|
content_blocks
|
||||||
),
|
),
|
||||||
},
|
}
|
||||||
)
|
|
||||||
else:
|
|
||||||
data = {
|
|
||||||
"content": serialize_content_blocks(
|
|
||||||
content_blocks
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
await event_emitter(
|
await event_emitter(
|
||||||
{
|
{
|
||||||
"type": "chat:completion",
|
"type": "chat:completion",
|
||||||
"data": data,
|
"data": data,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
done = "data: [DONE]" in line
|
done = "data: [DONE]" in line
|
||||||
if done:
|
if done:
|
||||||
@ -2000,7 +2007,9 @@ async def process_chat_response(
|
|||||||
form_data=event,
|
form_data=event,
|
||||||
extra_params=extra_params,
|
extra_params=extra_params,
|
||||||
)
|
)
|
||||||
yield wrap_item(json.dumps(event))
|
|
||||||
|
if event:
|
||||||
|
yield wrap_item(json.dumps(event))
|
||||||
|
|
||||||
async for data in original_generator:
|
async for data in original_generator:
|
||||||
data, _ = await process_filter_functions(
|
data, _ = await process_filter_functions(
|
||||||
@ -2010,7 +2019,9 @@ async def process_chat_response(
|
|||||||
form_data=data,
|
form_data=data,
|
||||||
extra_params=extra_params,
|
extra_params=extra_params,
|
||||||
)
|
)
|
||||||
yield data
|
|
||||||
|
if data:
|
||||||
|
yield data
|
||||||
|
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
stream_wrapper(response.body_iterator, events),
|
stream_wrapper(response.body_iterator, events),
|
||||||
|
Loading…
Reference in New Issue
Block a user