From c36c63f1e8bb005db9e91bc66d8d7801d618c548 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 5 Feb 2025 02:10:28 -0800 Subject: [PATCH 1/4] fix: tag --- backend/open_webui/utils/middleware.py | 33 ++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 6e4a875b4..68979e647 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1233,6 +1233,16 @@ async def process_chat_response( ].replace(match.group(0), "") if not content_blocks[-1]["content"]: content_blocks.pop() + + if not content_blocks: + # Append the new block + content_blocks.append( + { + "type": "text", + "content": "", + } + ) + # Append the new block content_blocks.append( { @@ -1461,14 +1471,23 @@ async def process_chat_response( log.debug("Error: ", e) continue - # Clean up the last text block - if content_blocks[-1]["type"] == "text": - content_blocks[-1]["content"] = content_blocks[-1][ - "content" - ].strip() + if content_blocks: + # Clean up the last text block + if content_blocks[-1]["type"] == "text": + content_blocks[-1]["content"] = content_blocks[-1][ + "content" + ].strip() - if not content_blocks[-1]["content"]: - content_blocks.pop() + if not content_blocks[-1]["content"]: + content_blocks.pop() + + if not content_blocks: + content_blocks.append( + { + "type": "text", + "content": "", + } + ) if response_tool_calls: tool_calls.append(response_tool_calls) From 90cd6f272fb79739a2f5c77a522965683d6daa2a Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 5 Feb 2025 02:33:40 -0800 Subject: [PATCH 2/4] fix: tag support --- backend/open_webui/utils/middleware.py | 69 ++++++++++++++++++-------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 68979e647..54a302d3d 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1231,18 +1231,10 @@ async def process_chat_response( content_blocks[-1]["content"] = content_blocks[-1][ "content" ].replace(match.group(0), "") + if not content_blocks[-1]["content"]: content_blocks.pop() - if not content_blocks: - # Append the new block - content_blocks.append( - { - "type": "text", - "content": "", - } - ) - # Append the new block content_blocks.append( { @@ -1258,6 +1250,7 @@ async def process_chat_response( tag = content_blocks[-1]["tag"] # Match end tag e.g., end_tag_pattern = rf"" + if re.search(end_tag_pattern, content): block_content = content_blocks[-1]["content"] # Strip start and end tags from the content @@ -1265,9 +1258,23 @@ async def process_chat_response( block_content = re.sub( start_tag_pattern, "", block_content ).strip() - block_content = re.sub( - end_tag_pattern, "", block_content - ).strip() + + end_tag_regex = re.compile(end_tag_pattern, re.DOTALL) + split_content = end_tag_regex.split(block_content, maxsplit=1) + + # Content inside the tag + block_content = ( + split_content[0].strip() if split_content else "" + ) + + # Leftover content (everything after ``) + leftover_content = ( + split_content[1].strip() if len(split_content) > 1 else "" + ) + + print(f"block_content: {block_content}") + print(f"leftover_content: {leftover_content}") + if block_content: end_flag = True content_blocks[-1]["content"] = block_content @@ -1280,19 +1287,31 @@ async def process_chat_response( content_blocks.append( { "type": "text", - "content": "", + "content": leftover_content, } ) - # Clean processed content - content = re.sub( - rf"<{tag}(.*?)>(.|\n)*?", - "", - content, - flags=re.DOTALL, - ) + else: + end_flag = True # Remove the block if content is empty content_blocks.pop() + + if leftover_content: + content_blocks.append( + { + "type": "text", + "content": leftover_content, + } + ) + + # Clean processed content + content = re.sub( + rf"<{tag}(.*?)>(.|\n)*?", + "", + content, + flags=re.DOTALL, + ) + return content, content_blocks, end_flag message = Chats.get_message_by_id_and_message_id( @@ -1358,6 +1377,7 @@ async def process_chat_response( try: data = json.loads(data) + print(data) if "selected_model_id" in data: model_id = data["selected_model_id"] @@ -1412,6 +1432,15 @@ async def process_chat_response( 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 ) From bffbf7e2384f0291da1ebfc2c90da8f893e07609 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 5 Feb 2025 02:35:16 -0800 Subject: [PATCH 3/4] doc: changelog --- CHANGELOG.md | 6 ++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf288e680..6437143f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.5.9] - 2025-02-05 + +### Fixed + +- **💡 "Think" Tag Display Issue**: Resolved a bug where the "Think" tag was not functioning correctly, ensuring proper visualization of the model's reasoning process before delivering responses. + ## [0.5.8] - 2025-02-05 ### Added diff --git a/package-lock.json b/package-lock.json index 074ff21c1..f5087c798 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "open-webui", - "version": "0.5.8", + "version": "0.5.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "open-webui", - "version": "0.5.8", + "version": "0.5.9", "dependencies": { "@codemirror/lang-javascript": "^6.2.2", "@codemirror/lang-python": "^6.1.6", diff --git a/package.json b/package.json index d2c4795c6..bf3bb4639 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "open-webui", - "version": "0.5.8", + "version": "0.5.9", "private": true, "scripts": { "dev": "npm run pyodide:fetch && vite dev --host", From 13c7d965158c92ed8888bfe8efb054f2935c1a40 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 5 Feb 2025 02:38:05 -0800 Subject: [PATCH 4/4] refac --- backend/open_webui/utils/middleware.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 54a302d3d..a91e166c0 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1252,6 +1252,8 @@ async def process_chat_response( end_tag_pattern = rf"" if re.search(end_tag_pattern, content): + end_flag = True + block_content = content_blocks[-1]["content"] # Strip start and end tags from the content start_tag_pattern = rf"<{tag}(.*?)>" @@ -1276,7 +1278,6 @@ async def process_chat_response( print(f"leftover_content: {leftover_content}") if block_content: - end_flag = True content_blocks[-1]["content"] = block_content content_blocks[-1]["ended_at"] = time.time() content_blocks[-1]["duration"] = int( @@ -1290,9 +1291,7 @@ async def process_chat_response( "content": leftover_content, } ) - else: - end_flag = True # Remove the block if content is empty content_blocks.pop() @@ -1377,7 +1376,6 @@ async def process_chat_response( try: data = json.loads(data) - print(data) if "selected_model_id" in data: model_id = data["selected_model_id"]