From 78853a67e4ccdf18124dd63a74b0db083a2228d2 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 2 Feb 2025 23:35:58 -0800 Subject: [PATCH] refac --- backend/open_webui/utils/middleware.py | 44 +++++++++---------- .../Messages/Markdown/MarkdownTokens.svelte | 2 +- src/routes/+layout.svelte | 4 +- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 6c8db43cd..f39066a29 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -1103,10 +1103,15 @@ async def process_chat_response( elif block["type"] == "code_interpreter": attributes = block.get("attributes", {}) - lang = attributes.get("lang", "") - attribute_type = attributes.get("type", "") + output = block.get("output", {}) + + lang = attributes.get("lang", "") + + if output: + content = f'{content}
\nAnalyzed\n```{lang}\n{block["content"]}\n```\n```output\n{output}\n```\n
\n' + else: + content = f'{content}
\nAnalyzing...\n```{lang}\n{block["content"]}\n```\n
\n' - content = f"{content}```{lang if lang else attribute_type}\n{block['content']}\n```\n" else: block_content = str(block["content"]).strip() content = f"{content}{block['type']}: {block_content}\n" @@ -1205,7 +1210,7 @@ async def process_chat_response( DETECT_CODE_INTERPRETER = True reasoning_tags = ["think", "reason", "reasoning", "thought", "Thought"] - code_interpreter_tags = ["oi::code_interpreter"] + code_interpreter_tags = ["code_interpreter"] try: for event in events: @@ -1339,12 +1344,13 @@ async def process_chat_response( and retries < MAX_RETRIES ): retries += 1 + log.debug(f"Retrying code interpreter block: {retries}") try: if content_blocks[-1]["attributes"].get("type") == "code": output = await event_caller( { - "type": "execute:pyodide", + "type": "execute:python", "data": { "id": str(uuid4()), "code": content_blocks[-1]["content"], @@ -1354,15 +1360,7 @@ async def process_chat_response( except Exception as e: output = str(e) - content_blocks.append( - { - "type": "code_interpreter", - "attributes": { - "type": "output", - }, - "content": output, - } - ) + content_blocks[-1]["output"] = output content_blocks.append( { "type": "text", @@ -1370,6 +1368,15 @@ async def process_chat_response( } ) + await event_emitter( + { + "type": "chat:completion", + "data": { + "content": serialize_content_blocks(content_blocks), + }, + } + ) + try: res = await generate_chat_completion( request, @@ -1397,15 +1404,6 @@ async def process_chat_response( log.debug(e) break - await event_emitter( - { - "type": "chat:completion", - "data": { - "content": serialize_content_blocks(content_blocks), - }, - } - ) - title = Chats.get_chat_title_by_id(metadata["chat_id"]) data = { "done": True, diff --git a/src/lib/components/chat/Messages/Markdown/MarkdownTokens.svelte b/src/lib/components/chat/Messages/Markdown/MarkdownTokens.svelte index c295773a9..d44e60fa1 100644 --- a/src/lib/components/chat/Messages/Markdown/MarkdownTokens.svelte +++ b/src/lib/components/chat/Messages/Markdown/MarkdownTokens.svelte @@ -195,7 +195,7 @@ {/if} {:else if token.type === 'details'} - +
diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index ff517577c..5663e2040 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -248,8 +248,8 @@ tags.set(await getAllTags(localStorage.token)); } } else { - if (type === 'execute:pyodide') { - console.log('execute:pyodide', data); + if (type === 'execute:python') { + console.log('execute:python', data); executePythonAsWorker(data.id, data.code, cb); } }