mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-12 04:15:25 +00:00
refac
This commit is contained in:
parent
5b3eae3855
commit
1bc9711afd
1 changed files with 110 additions and 100 deletions
|
|
@ -1284,124 +1284,134 @@ async def process_chat_response(
|
||||||
# Non-streaming response
|
# Non-streaming response
|
||||||
if not isinstance(response, StreamingResponse):
|
if not isinstance(response, StreamingResponse):
|
||||||
if event_emitter:
|
if event_emitter:
|
||||||
if isinstance(response, dict) or isinstance(response, JSONResponse):
|
try:
|
||||||
|
if isinstance(response, dict) or isinstance(response, JSONResponse):
|
||||||
|
if isinstance(response, list) and len(response) == 1:
|
||||||
|
# If the response is a single-item list, unwrap it #17213
|
||||||
|
response = response[0]
|
||||||
|
|
||||||
if isinstance(response, JSONResponse) and isinstance(
|
if isinstance(response, JSONResponse) and isinstance(
|
||||||
response.body, bytes
|
response.body, bytes
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
response_data = json.loads(response.body.decode("utf-8"))
|
response_data = json.loads(response.body.decode("utf-8"))
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
response_data = {"error": {"detail": "Invalid JSON response"}}
|
response_data = {
|
||||||
else:
|
"error": {"detail": "Invalid JSON response"}
|
||||||
response_data = response
|
}
|
||||||
|
|
||||||
if "error" in response_data:
|
|
||||||
error = response_data.get("error")
|
|
||||||
|
|
||||||
if isinstance(error, dict):
|
|
||||||
error = error.get("detail", error)
|
|
||||||
else:
|
else:
|
||||||
error = str(error)
|
response_data = response
|
||||||
|
|
||||||
Chats.upsert_message_to_chat_by_id_and_message_id(
|
if "error" in response_data:
|
||||||
metadata["chat_id"],
|
error = response_data.get("error")
|
||||||
metadata["message_id"],
|
|
||||||
{
|
|
||||||
"error": {"content": error},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if isinstance(error, str) or isinstance(error, dict):
|
|
||||||
await event_emitter(
|
|
||||||
{
|
|
||||||
"type": "chat:message:error",
|
|
||||||
"data": {"error": {"content": error}},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if "selected_model_id" in response_data:
|
if isinstance(error, dict):
|
||||||
Chats.upsert_message_to_chat_by_id_and_message_id(
|
error = error.get("detail", error)
|
||||||
metadata["chat_id"],
|
else:
|
||||||
metadata["message_id"],
|
error = str(error)
|
||||||
{
|
|
||||||
"selectedModelId": response_data["selected_model_id"],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
choices = response_data.get("choices", [])
|
|
||||||
if choices and choices[0].get("message", {}).get("content"):
|
|
||||||
content = response_data["choices"][0]["message"]["content"]
|
|
||||||
|
|
||||||
if content:
|
|
||||||
await event_emitter(
|
|
||||||
{
|
|
||||||
"type": "chat:completion",
|
|
||||||
"data": response_data,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
title = Chats.get_chat_title_by_id(metadata["chat_id"])
|
|
||||||
|
|
||||||
await event_emitter(
|
|
||||||
{
|
|
||||||
"type": "chat:completion",
|
|
||||||
"data": {
|
|
||||||
"done": True,
|
|
||||||
"content": content,
|
|
||||||
"title": title,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Save message in the database
|
|
||||||
Chats.upsert_message_to_chat_by_id_and_message_id(
|
Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata["chat_id"],
|
metadata["chat_id"],
|
||||||
metadata["message_id"],
|
metadata["message_id"],
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"error": {"content": error},
|
||||||
"content": content,
|
},
|
||||||
|
)
|
||||||
|
if isinstance(error, str) or isinstance(error, dict):
|
||||||
|
await event_emitter(
|
||||||
|
{
|
||||||
|
"type": "chat:message:error",
|
||||||
|
"data": {"error": {"content": error}},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if "selected_model_id" in response_data:
|
||||||
|
Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
|
metadata["chat_id"],
|
||||||
|
metadata["message_id"],
|
||||||
|
{
|
||||||
|
"selectedModelId": response_data["selected_model_id"],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Send a webhook notification if the user is not active
|
choices = response_data.get("choices", [])
|
||||||
if not get_active_status_by_user_id(user.id):
|
if choices and choices[0].get("message", {}).get("content"):
|
||||||
webhook_url = Users.get_user_webhook_url_by_id(user.id)
|
content = response_data["choices"][0]["message"]["content"]
|
||||||
if webhook_url:
|
|
||||||
await post_webhook(
|
if content:
|
||||||
request.app.state.WEBUI_NAME,
|
await event_emitter(
|
||||||
webhook_url,
|
{
|
||||||
f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}",
|
"type": "chat:completion",
|
||||||
{
|
"data": response_data,
|
||||||
"action": "chat",
|
}
|
||||||
"message": content,
|
)
|
||||||
|
|
||||||
|
title = Chats.get_chat_title_by_id(metadata["chat_id"])
|
||||||
|
|
||||||
|
await event_emitter(
|
||||||
|
{
|
||||||
|
"type": "chat:completion",
|
||||||
|
"data": {
|
||||||
|
"done": True,
|
||||||
|
"content": content,
|
||||||
"title": title,
|
"title": title,
|
||||||
"url": f"{request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}",
|
|
||||||
},
|
},
|
||||||
)
|
}
|
||||||
|
)
|
||||||
|
|
||||||
await background_tasks_handler()
|
# Save message in the database
|
||||||
|
Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
|
metadata["chat_id"],
|
||||||
|
metadata["message_id"],
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": content,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
if events and isinstance(events, list):
|
# Send a webhook notification if the user is not active
|
||||||
extra_response = {}
|
if not get_active_status_by_user_id(user.id):
|
||||||
for event in events:
|
webhook_url = Users.get_user_webhook_url_by_id(user.id)
|
||||||
if isinstance(event, dict):
|
if webhook_url:
|
||||||
extra_response.update(event)
|
await post_webhook(
|
||||||
else:
|
request.app.state.WEBUI_NAME,
|
||||||
extra_response[event] = True
|
webhook_url,
|
||||||
|
f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}",
|
||||||
|
{
|
||||||
|
"action": "chat",
|
||||||
|
"message": content,
|
||||||
|
"title": title,
|
||||||
|
"url": f"{request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
response_data = {
|
await background_tasks_handler()
|
||||||
**extra_response,
|
|
||||||
**response_data,
|
|
||||||
}
|
|
||||||
|
|
||||||
if isinstance(response, dict):
|
if events and isinstance(events, list):
|
||||||
response = response_data
|
extra_response = {}
|
||||||
if isinstance(response, JSONResponse):
|
for event in events:
|
||||||
response = JSONResponse(
|
if isinstance(event, dict):
|
||||||
content=response_data,
|
extra_response.update(event)
|
||||||
headers=response.headers,
|
else:
|
||||||
status_code=response.status_code,
|
extra_response[event] = True
|
||||||
)
|
|
||||||
|
response_data = {
|
||||||
|
**extra_response,
|
||||||
|
**response_data,
|
||||||
|
}
|
||||||
|
|
||||||
|
if isinstance(response, dict):
|
||||||
|
response = response_data
|
||||||
|
if isinstance(response, JSONResponse):
|
||||||
|
response = JSONResponse(
|
||||||
|
content=response_data,
|
||||||
|
headers=response.headers,
|
||||||
|
status_code=response.status_code,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
log.debug(f"Error occurred while processing request: {e}")
|
||||||
|
pass
|
||||||
|
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue