From 1635dcb69b3552d6369af9b6254745176ed8d9f4 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 3 Feb 2025 16:07:43 -0800 Subject: [PATCH 1/4] refac --- backend/open_webui/utils/middleware.py | 8 +++++++- src/lib/components/chat/Chat.svelte | 24 ++++++++++++------------ src/lib/utils/index.ts | 13 ++++++++++--- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 11e36cecf..644ec9014 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1120,7 +1120,7 @@ async def process_chat_response( output = html.escape(json.dumps(output)) if raw: - content = f'{content}
\nAnalyzed\n```{lang}\n{block["content"]}\n```\n```output\n{output}\n```\n
\n' + content = f'{content}\n{block["content"]}\n\n```output\n{output}\n```\n' else: content = f'{content}
\nAnalyzed\n```{lang}\n{block["content"]}\n```\n
\n' else: @@ -1312,6 +1312,12 @@ async def process_chat_response( ) if end: + data = { + "content": serialize_content_blocks( + content_blocks + ), + } + break if ENABLE_REALTIME_CHAT_SAVE: diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 89fedb919..4e8d002dc 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -45,7 +45,7 @@ promptTemplate, splitStream, sleep, - removeDetailsWithReasoning, + removeDetails, getPromptVariables } from '$lib/utils'; @@ -1338,8 +1338,17 @@ parentId: string, { modelId = null, modelIdx = null, newChat = false } = {} ) => { - const _chatId = JSON.parse(JSON.stringify($chatId)); + // If modelId is provided, use it, else use selected model + let selectedModelIds = modelId + ? [modelId] + : atSelectedModel !== undefined + ? [atSelectedModel.id] + : selectedModels; + // Create response messages for each selected model + const responseMessageIds: Record = {}; + + const _chatId = JSON.parse(JSON.stringify($chatId)); // Create new chat if newChat is true and first user message if ( newChat && @@ -1351,15 +1360,6 @@ await saveChatHandler(_chatId); } - // If modelId is provided, use it, else use selected model - let selectedModelIds = modelId - ? [modelId] - : atSelectedModel !== undefined - ? [atSelectedModel.id] - : selectedModels; - - // Create response messages for each selected model - const responseMessageIds: Record = {}; for (const [_modelIdx, modelId] of selectedModelIds.entries()) { const model = $models.filter((m) => m.id === modelId).at(0); @@ -1515,7 +1515,7 @@ : undefined, ...createMessagesList(history, responseMessageId).map((message) => ({ ...message, - content: removeDetailsWithReasoning(message.content) + content: removeDetails(message.content, ['reasoning', 'code_interpreter']) })) ] .filter((message) => message?.content?.trim()) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 2ccc1bf5d..0a5b72e36 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -668,8 +668,15 @@ export const cleanText = (content: string) => { return removeFormattings(removeEmojis(content.trim())); }; -export const removeDetailsWithReasoning = (content) => { - return content.replace(/]*>.*?<\/details>/gis, '').trim(); +export const removeDetails = (content, types) => { + for (const type of types) { + content = content.replace( + new RegExp(`]*>.*?<\\/details>`, 'gis'), + '' + ); + } + + return content; }; // This regular expression matches code blocks marked by triple backticks @@ -741,7 +748,7 @@ export const extractSentencesForAudio = (text: string) => { }; export const getMessageContentParts = (content: string, split_on: string = 'punctuation') => { - content = removeDetailsWithReasoning(content); + content = removeDetails(content, ['reasoning', 'code_interpreter']); const messageContentParts: string[] = []; switch (split_on) { From 943a57474fdeab1c43490fb50591ab400ee90967 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 3 Feb 2025 16:14:00 -0800 Subject: [PATCH 2/4] refac --- backend/open_webui/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/open_webui/constants.py b/backend/open_webui/constants.py index cb65e0d77..86d87a2c3 100644 --- a/backend/open_webui/constants.py +++ b/backend/open_webui/constants.py @@ -57,7 +57,7 @@ class ERROR_MESSAGES(str, Enum): ) FILE_NOT_SENT = "FILE_NOT_SENT" - FILE_NOT_SUPPORTED = "Oops! It seems like the file format you're trying to upload is not supported. Please upload a file with a supported format (e.g., JPG, PNG, PDF, TXT) and try again." + FILE_NOT_SUPPORTED = "Oops! It seems like the file format you're trying to upload is not supported. Please upload a file with a supported format and try again." NOT_FOUND = "We could not find what you're looking for :/" USER_NOT_FOUND = "We could not find what you're looking for :/" From 31c176a485642d2bc860ff833a06f6db13127798 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 3 Feb 2025 16:18:07 -0800 Subject: [PATCH 3/4] refac --- backend/open_webui/utils/middleware.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 644ec9014..71e32c755 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1107,9 +1107,15 @@ async def process_chat_response( reasoning_duration = block.get("duration", None) if reasoning_duration: - content = f'{content}
\nThought for {reasoning_duration} seconds\n{reasoning_display_content}\n
\n' + if raw: + content = f'{content}<{block["tag"]}>{block["content"]}\n' + else: + content = f'{content}
\nThought for {reasoning_duration} seconds\n{reasoning_display_content}\n
\n' else: - content = f'{content}
\nThinking…\n{reasoning_display_content}\n
\n' + if raw: + content = f'{content}<{block["tag"]}>{block["content"]}\n' + else: + content = f'{content}
\nThinking…\n{reasoning_display_content}\n
\n' elif block["type"] == "code_interpreter": attributes = block.get("attributes", {}) @@ -1124,7 +1130,10 @@ async def process_chat_response( else: content = f'{content}
\nAnalyzed\n```{lang}\n{block["content"]}\n```\n
\n' else: - content = f'{content}
\nAnalyzing...\n```{lang}\n{block["content"]}\n```\n
\n' + if raw: + content = f'{content}\n{block["content"]}\n\n' + else: + content = f'{content}
\nAnalyzing...\n```{lang}\n{block["content"]}\n```\n
\n' else: block_content = str(block["content"]).strip() From df07d671ce17548bd28f8147e6ca6a0c8e22fe90 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 3 Feb 2025 16:21:44 -0800 Subject: [PATCH 4/4] refac --- backend/open_webui/utils/middleware.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 71e32c755..ffccdca87 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1321,12 +1321,6 @@ async def process_chat_response( ) if end: - data = { - "content": serialize_content_blocks( - content_blocks - ), - } - break if ENABLE_REALTIME_CHAT_SAVE: @@ -1370,6 +1364,15 @@ async def process_chat_response( if not content_blocks[-1]["content"]: content_blocks.pop() + await event_emitter( + { + "type": "chat:completion", + "data": { + "content": serialize_content_blocks(content_blocks), + }, + } + ) + if response.background: await response.background()