Browse Source

feat: add support for reasoning_content

CorbinChen 2 months ago
parent
commit
516f1cf67b
1 changed files with 30 additions and 2 deletions
  1. 30 2
      backend/open_webui/utils/middleware.py

+ 30 - 2
backend/open_webui/utils/middleware.py

@@ -1553,10 +1553,38 @@ async def process_chat_response(
                                                         ] += delta_arguments
 
                                     value = delta.get("content")
-
+                                    reasoning_content = delta.get("reasoning_content")
+                                    if reasoning_content:
+                                        if not content_blocks or content_blocks[-1]["type"] != "reasoning":
+                                            reasoning_block = {
+                                                "type": "reasoning",
+                                                "start_tag": "think",
+                                                "end_tag": "/think",
+                                                "attributes": {"type": "reasoning_content"},
+                                                "content": "",
+                                                "started_at": time.time()
+                                            }
+                                            content_blocks.append(reasoning_block)
+                                        else:
+                                            reasoning_block = content_blocks[-1]
+                                        reasoning_block["content"] += reasoning_content
+                                        data = {
+                                            "content": serialize_content_blocks(content_blocks)
+                                        }
                                     if value:
-                                        content = f"{content}{value}"
+                                        if content_blocks and content_blocks[-1]["type"] == "reasoning" and content_blocks[-1].get("attributes", {}).get("type") == "reasoning_content":
+                                            reasoning_block = content_blocks[-1]
+                                            reasoning_block["attributes"] = {}
+                                            reasoning_block["ended_at"] = time.time()
+                                            reasoning_block["duration"] = int(reasoning_block["ended_at"] - reasoning_block["started_at"])
 
+                                            content_blocks.append(
+                                                {
+                                                    "type": "text",
+                                                    "content": "",
+                                                }
+                                            )
+                                        content = f"{content}{value}"
                                         if not content_blocks:
                                             content_blocks.append(
                                                 {