DeepSeek13-open-webui Pipelines编写和部署

发布于:2025-06-24 ⋅ 阅读:(13) ⋅ 点赞:(0)

如何编写一个pipeline,并启动运行

以下是编写并启动运行一个 Pipeline 的简明指南,基于 Open WebUI Pipelines 框架,包含核心步骤和示例代码:

一、核心概念

  1. 环境要求

    • 确保已安装 Python 3.11(官方唯一支持版本)。
    • 安装 Open WebUI Pipelines 依赖:
      git clone https://github.com/open-webui/pipelines.git
      cd pipelines
      pip install -r requirements.txt
      
  2. 理解Pipeline 类型

  • Filter:预处理请求或后处理响应(如日志记录、内容过滤)。
  • Pipe:处理核心逻辑(如函数调用、RAG 流程)。
  • 运行方式:独立服务,通过 OpenAI API 兼容的客户端(如 Open WebUI)连接调用。

二、编写 Pipeline 代码

示例:Filter 管道(简单的请求日志 Filter)
# pipelines/log_filter.py
from pipelines import Filter, PipelineRequest, PipelineResponse

class LogFilter(Filter):
    name = "log_filter"  # 唯一标识
    description = "记录请求和响应日志"

    def process_request(self, request: PipelineRequest) -> PipelineRequest:
        """请求预处理:打印日志"""
        print(f"[请求] {request.model} | 内容:{request.messages[0]['content'][:50]}...")
        return request  # 必须返回处理后的请求

    def process_response(self, response: PipelineResponse) -> PipelineResponse:
        """响应后处理:打印日志"""
        print(f"[响应] 生成内容长度:{len(response.choices[0]['message']['content'])}")
        return response  # 必须返回处理后的响应
代码解析
  1. 继承 Filter 类
    • process_request:在请求发送到 OpenAI API 前执行。
    • process_response:在接收到 API 响应后执行。
  2. 关键属性
    • name:管道唯一名称,用于配置和识别。
    • description:管道功能描述。
  3. 数据结构
    • PipelineRequest:包含请求的完整信息(模型、消息、参数等)。
    • PipelineResponse:包含 API 响应的完整内容。

三、编写 Pipe 管道(示例:函数调用处理)

若需处理复杂逻辑(如函数调用、RAG),可编写 Pipe 类型 管道:

# pipelines/function_call_pipe.py
from pipelines import Pipe, PipelineRequest, PipelineResponse

class FunctionCallPipe(Pipe):
    name = "function_call_pipe"
    description = "处理函数调用逻辑"

    def process(self, request: PipelineRequest) -> PipelineResponse:
        """核心逻辑:解析函数调用并返回结果"""
        if request.function_calls:
            # 模拟函数执行(实际需调用外部工具)
            tool_result = {"status": "success", "data": "调用工具返回的数据"}
            return PipelineResponse(
                choices=[{
                    "message": {
                        "content": "工具调用完成",
                        "function_call": {"name": "tool", "parameters": tool_result}
                    }
                }]
            )
        return request  # 无函数调用时透传请求

四、启动 Pipeline 服务

方式 1:使用 Docker(推荐)
# 启动基础容器(默认端口 9099)
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

# 添加自定义管道(通过 URL 加载)
docker run -d -p 9099:9099 \
  -e PIPELINES_URLS="https://raw.githubusercontent.com/your-username/your-repo/main/log_filter.py" \
  # 其他参数同上
方式 2:本地运行(开发调试)
  1. 克隆仓库并安装依赖
    git clone https://github.com/open-webui/pipelines.git
    cd pipelines
    pip install -r requirements.txt  # Python 3.11 必需
    
  2. 启动服务
    sh ./start.sh  # 或 python main.py
    

五、连接到 Open WebUI(客户端配置)

  1. 添加 API 连接

    • 进入 Open WebUI 管理面板 > 设置 > Connections,点击 + 添加新连接。
    • 设置 API URLhttp://localhost:9099API 密钥0p3n-w3bu!(默认值)。
    • 若 Open WebUI 在 Docker 中,URL 改为 http://host.docker.internal:9099
  2. 启用管道

    • 进入 管理面板 > 设置 > Pipelines,选择已安装的管道(如 log_filter),启用并保存配置。

六、验证运行

  1. 发送测试请求
    在 Open WebUI 聊天框中输入文本,触发 API 请求,观察控制台日志:

    [请求] gpt-3.5-turbo | 内容:用户输入的问题...
    [响应] 生成内容长度:150
    
  2. 调试技巧

    • 使用 print() 或 Python 日志库(如 logging)输出调试信息。
    • 通过 Docker 日志查看:docker logs pipelines

七、注意事项

  1. 安全风险
    • 禁止加载不可信来源的管道(支持任意代码执行)。
    • 生产环境需限制管道的网络权限。
  2. 依赖管理
    • 自定义管道如需额外库,需在 requirements.txt 中声明,并构建自定义 Docker 镜像。
  3. 官方资源

© 著作权归作者所有


网站公告

今日签到

点亮在社区的每一天
去签到