如何在 Open WebUI 中显示 DeepSeek-R1 的思考过程

书接上回,咱们部署好了 DeepSeek-R1 之后,发现没办法显示思考过程,只能等结果出来之后查看结果,这个体感上就会感觉响应很慢。
查看了 open-webui 的 issues,发现已经有人提过这个问题,并且已有解决方案,那就是通过 Pipelines 来实现。具体步骤如下:
1. 部署 pipelines 容器
- docker run 命令:
docker run -d -p 9099:9099 --add-host=host.docker.internal:host-gateway -v pipelines:/app/pipelines --name pipelines --restart always ghcr.io/open-webui/pipelines:main
- docker-compose.yml 配置:
services:
open-webui-pipelines:
container_name: open-webui-pipelines
image: ghcr.io/open-webui/pipelines:main
network_mode: bridge
ports:
- '9099:9099'
volumes:
- ./pipelines:/app/pipelines
restart: always
2. 配置管道连接
- 在
管理员设置
->外部连接
中按+
添加一个连接。

- 管道地址通过内网 IP、hostname 或域名等,加上前面 pipelines 容器的端口号
9099
;密钥是固定的0p3n-w3bu!
。

3. 创建函数
- 访问 DeepSeek R1 Function 函数页面,通过
Get
按钮完成注册。

- 注册完成之后,在弹出的页面中输入自己的站点地址自动跳转并导入函数。

- 也可以手动复制该函数代码,然后点击
+
加号手动添加。

- 以下是我在
1.2.10
版本上修改过的代码,支持了模型显示名的配置,这样方便与原始版本区分
"""
title: DeepSeek R1
author: zgccrui
description: 在OpwenWebUI中显示DeepSeek R1模型的思维链 - 仅支持0.5.6及以上版本
version: 1.2.10
licence: MIT
"""
import json
import httpx
import re
from typing import AsyncGenerator, Callable, Awaitable
from pydantic import BaseModel, Field
import asyncio
class Pipe:
class Valves(BaseModel):
DEEPSEEK_API_BASE_URL: str = Field(
default="https://api.deepseek.com/v1",
description="DeepSeek API的基础请求地址",
)
DEEPSEEK_API_KEY: str = Field(
default="", description="用于身份验证的DeepSeek API密钥,可从控制台获取"
)
DEEPSEEK_API_MODEL: str = Field(
default="deepseek-reasoner",
description="API请求的模型名称,默认为 deepseek-reasoner",
)
DEEPSEEK_MODEL_DISPLAY_NAME: str = Field(
default="deepseek-reasoner-fix",
description="模型名称,默认为 deepseek-reasoner-fix",
)
def __init__(self):
self.valves = self.Valves()
self.data_prefix = "data:"
self.emitter = None
def pipes(self):
return [
{
"id": self.valves.DEEPSEEK_API_MODEL,
"name": self.valves.DEEPSEEK_MODEL_DISPLAY_NAME,
}
]
async def pipe(
self, body: dict, __event_emitter__: Callable[[dict], Awaitable[None]] = None
) -> AsyncGenerator[str, None]:
"""主处理管道(已移除缓冲)"""
thinking_state = {"thinking": -1} # 使用字典来存储thinking状态
self.emitter = __event_emitter__
# 验证配置
if not self.valves.DEEPSEEK_API_KEY:
yield json.dumps({"error": "未配置API密钥"}, ensure_ascii=False)
return
# 准备请求参数
headers = {
"Authorization": f"Bearer {self.valves.DEEPSEEK_API_KEY}",
"Content-Type": "application/json",
}
try:
# 模型ID提取
model_id = body["model"].split(".", 1)[-1]
payload = {**body, "model": model_id}
# 处理消息以防止连续的相同角色
messages = payload["messages"]
i = 0
while i < len(messages) - 1:
if messages[i]["role"] == messages[i + 1]["role"]:
# 插入具有替代角色的占位符消息
alternate_role = (
"assistant" if messages[i]["role"] == "user" else "user"
)
messages.insert(
i + 1,
{"role": alternate_role, "content": "[Unfinished thinking]"},
)
i += 1
# yield json.dumps(payload, ensure_ascii=False)
# 发起API请求
async with httpx.AsyncClient(http2=True) as client:
async with client.stream(
"POST",
f"{self.valves.DEEPSEEK_API_BASE_URL}/chat/completions",
json=payload,
headers=headers,
timeout=300,
) as response:
# 错误处理
if response.status_code != 200:
error = await response.aread()
yield self._format_error(response.status_code, error)
return
# 流式处理响应
async for line in response.aiter_lines():
if not line.startswith(self.data_prefix):
continue
# 截取 JSON 字符串
json_str = line[len(self.data_prefix) :]
# 去除首尾空格后检查是否为结束标记
if json_str.strip() == "[DONE]":
return
try:
data = json.loads(json_str)
except json.JSONDecodeError as e:
# 格式化错误信息,这里传入错误类型和详细原因(包括出错内容和异常信息)
error_detail = f"解析失败 - 内容:{json_str},原因:{e}"
yield self._format_error("JSONDecodeError", error_detail)
return
choice = data.get("choices", [{}])[0]
# 结束条件判断
if choice.get("finish_reason"):
return
# 状态机处理
state_output = await self._update_thinking_state(
choice.get("delta", {}), thinking_state
)
if state_output:
yield state_output # 直接发送状态标记
if state_output == "<think>":
yield "\n"
# 内容处理并立即发送
content = self._process_content(choice["delta"])
if content:
if content.startswith("<think>"):
match = re.match(r"^<think>", content)
if match:
content = re.sub(r"^<think>", "", content)
yield "<think>"
await asyncio.sleep(0.1)
yield "\n"
elif content.startswith("</think>"):
match = re.match(r"^</think>", content)
if match:
content = re.sub(r"^</think>", "", content)
yield "</think>"
await asyncio.sleep(0.1)
yield "\n"
yield content
except Exception as e:
yield self._format_exception(e)
async def _update_thinking_state(self, delta: dict, thinking_state: dict) -> str:
"""更新思考状态机(简化版)"""
state_output = ""
# 状态转换:未开始 -> 思考中
if thinking_state["thinking"] == -1 and delta.get("reasoning_content"):
thinking_state["thinking"] = 0
state_output = "<think>"
# 状态转换:思考中 -> 已回答
elif (
thinking_state["thinking"] == 0
and not delta.get("reasoning_content")
and delta.get("content")
):
thinking_state["thinking"] = 1
state_output = "\n</think>\n\n"
return state_output
def _process_content(self, delta: dict) -> str:
"""直接返回处理后的内容"""
return delta.get("reasoning_content", "") or delta.get("content", "")
def _format_error(self, status_code: int, error: bytes) -> str:
# 如果 error 已经是字符串,则无需 decode
if isinstance(error, str):
error_str = error
else:
error_str = error.decode(errors="ignore")
try:
err_msg = json.loads(error_str).get("message", error_str)[:200]
except Exception as e:
err_msg = error_str[:200]
return json.dumps(
{"error": f"HTTP {status_code}: {err_msg}"}, ensure_ascii=False
)
def _format_exception(self, e: Exception) -> str:
"""异常格式化保持不变"""
err_type = type(e).__name__
return json.dumps({"error": f"{err_type}: {str(e)}"}, ensure_ascii=False)
- 函数配置中,Api URL、Api Key、模型名字和上个教程一样,输入阿里云百炼对应的即可。

- 启用该函数

4. 设置新模型
- 前往
管理员设置
->模型
,将新的deepseek-r1-fix
设为启用,还可手动修改 Logo 图片等。

- 然后就可以在对话中看到思考过程了,出现「正在思考」的时候,可以点击旁边的箭头展开思考过程。

5. 联网搜索 Error searching 的问题
目前使用下来发现一个问题,配置了 Pipeline 之后,虽然能够正常显示深度思考过程了,但是使用「联网搜索」会出现 Error searching
的问题。
目前发现一个临时解决方案,就是去 管理员设置
-> 界面
中关闭 网页搜索关键词生成
。初步判断是经过了 Pipeline 的时候,会导致联网搜索的关键词生成出现问题,从而传到搜索引擎的是空的内容,导致搜索失败。临时关闭搜索关键词生成,导致的问题是用户输入内容会直接作为搜索引擎的搜索关键词,不过这个倒能接受。
2025-02-18 更新:发现一个完美的解决方案,在 管理员设置
-> 界面
-> 外部模型
中选择一个其他模型(非配置了 Pipeline 的这个模型,例如 qwen-max-latest
或 deepseek-v3
)即可,尽量不要用 deepseek-r1
,会比较慢。


评论区