|
@@ -65,6 +65,7 @@ from open_webui.env import (
|
|
|
SRC_LOG_LEVELS,
|
|
|
GLOBAL_LOG_LEVEL,
|
|
|
BYPASS_MODEL_ACCESS_CONTROL,
|
|
|
+ ENABLE_REALTIME_CHAT_SAVE,
|
|
|
)
|
|
|
from open_webui.constants import TASKS
|
|
|
|
|
@@ -928,6 +929,10 @@ async def process_chat_response(
|
|
|
|
|
|
# Handle as a background task
|
|
|
async def post_response_handler(response, events):
|
|
|
+
|
|
|
+ assistant_message = get_last_assistant_message(form_data["messages"])
|
|
|
+ content = assistant_message if assistant_message else ""
|
|
|
+
|
|
|
try:
|
|
|
for event in events:
|
|
|
await event_emitter(
|
|
@@ -946,9 +951,6 @@ async def process_chat_response(
|
|
|
},
|
|
|
)
|
|
|
|
|
|
- assistant_message = get_last_assistant_message(form_data["messages"])
|
|
|
- content = assistant_message if assistant_message else ""
|
|
|
-
|
|
|
async for line in response.body_iterator:
|
|
|
line = line.decode("utf-8") if isinstance(line, bytes) else line
|
|
|
data = line
|
|
@@ -977,7 +979,6 @@ async def process_chat_response(
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
-
|
|
|
value = (
|
|
|
data.get("choices", [])[0]
|
|
|
.get("delta", {})
|
|
@@ -987,14 +988,19 @@ async def process_chat_response(
|
|
|
if value:
|
|
|
content = f"{content}{value}"
|
|
|
|
|
|
- # Save message in the database
|
|
|
- Chats.upsert_message_to_chat_by_id_and_message_id(
|
|
|
- metadata["chat_id"],
|
|
|
- metadata["message_id"],
|
|
|
- {
|
|
|
+ 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": content,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ data = {
|
|
|
"content": content,
|
|
|
- },
|
|
|
- )
|
|
|
+ }
|
|
|
|
|
|
except Exception as e:
|
|
|
done = "data: [DONE]" in line
|
|
@@ -1003,6 +1009,16 @@ async def process_chat_response(
|
|
|
if done:
|
|
|
data = {"done": True, "content": content, "title": title}
|
|
|
|
|
|
+ if not 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": content,
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
# Send a webhook notification if the user is not active
|
|
|
if (
|
|
|
get_user_id_from_session_pool(metadata["session_id"])
|
|
@@ -1036,6 +1052,16 @@ async def process_chat_response(
|
|
|
print("Task was cancelled!")
|
|
|
await event_emitter({"type": "task-cancelled"})
|
|
|
|
|
|
+ if not 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": content,
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
if response.background is not None:
|
|
|
await response.background()
|
|
|
|