OpenWebUI 通过 Pipeline 对接 Dify Workflow
本文介绍如何在 OpenWebUI 中安装并配置 Pipelines 插件,并通过 Python 脚本调用 Dify 的工作流接口。
1. 安装 Pipeline 容器
使用 Docker 运行 Pipelines 服务:
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
2. 对接 OpenWebUI
在 OpenWebUI 管理界面中启用 Pipelines 插件。配置完成后,确保密码设置正确(默认示例为 0p3n-w3bu!)。
[图:OpenWebUI 插件配置界面]
3. 进入 Pipeline 容器配置
如需本地调试或自定义开发,可参考以下步骤:
- 确保已安装 Python 3.11(官方唯一支持版本)。
- 克隆管道存储库:
git clone https://github.com/open-webui/pipelines.git cd pipelines - 安装依赖项:
pip install -r requirements.txt - 启动服务器:
sh ./start.sh - 将客户端上的 OpenAI URL 设置为 Pipelines URL,以解锁全部功能并集成自定义工作流。
4. 上传 Dify Python 脚本
编写 Python 脚本以对接 Dify API。注意:使用前请修改脚本中的 api_url、api_key 及输入输出变量名。
方案一:块输出模式
此模式等待工作流完全结束后返回最终结果。
from typing import List, Union, Generator, Iterator, Optional
import requests, json
class Pipeline:
def __init__(self):
self.name = "IT_AI 智能知识平台"
# 请替换为实际的 Dify API 地址
self.api_url = "YOUR_DIFY_WORKFLOW_URL"
# 请替换为实际的 Dify API Key
self.api_key =
.api_request_stream =
.verify_ssl =
.debug =
():
()
():
()
() -> :
.debug:
()
body
() -> :
.debug:
()
body
() -> [, Generator, Iterator]:
()
.debug:
()
response_mode = .api_request_stream
headers = {
: ,
:
}
data = {
: {: user_message},
: response_mode,
: body.get(, {}).get(, )
}
:
response = requests.post(
.api_url, headers=headers, json=data,
stream=.api_request_stream, verify=.verify_ssl
)
response.status_code == :
final_output =
line response.iter_lines():
line final_output:
line = line.decode().strip()
.debug:
()
line.startswith():
json_str = line[():]
:
json_data = json.loads(json_str)
json_data.get() == :
outputs = json_data.get(, {}).get(, {})
full_text = outputs.get(, )
full_text:
final_output =
full_text
json.JSONDecodeError e:
()
final_output full_text:
full_text
:
Exception e:

