Skip to main content

🔗 API 端点

本指南提供了如何有效与 API 端点交互以实现无缝集成和使用我们模型自动化的基本信息。请注意,这是一个实验性设置,可能会在未来进行更新以进行增强。

身份验证

为了确保对 API 的安全访问,需要进行身份验证 🛡️。您可以使用 Bearer Token 机制对您的 API 请求进行身份验证。从 Open WebUI 的 设置 > 账户 中获取您的 API 密钥,或者,使用 JWT(JSON Web Token)进行身份验证。

重要 API 端点

📜 检索所有模型

  • 端点: GET /api/models

  • 描述: 获取通过 Open WebUI 创建或添加的所有模型。

  • 示例:

    curl -H "Authorization: Bearer YOUR_API_KEY" http://localhost:3000/api/models

💬 聊天完成

  • 端点: POST /api/chat/completions

  • 描述: 作为 OpenAI API 兼容的聊天完成端点,用于 Open WebUI 上的模型,包括 Ollama 模型、OpenAI 模型和 Open WebUI 函数模型。

  • Curl 示例:

    curl -X POST http://localhost:3000/api/chat/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "llama3.1",
    "messages": [
    {
    "role": "user",
    "content": "为什么天空是蓝色的?"
    }
    ]
    }'
  • Python 示例:

    import requests

    def chat_with_model(token):
    url = 'http://localhost:3000/api/chat/completions'
    headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json'
    }
    data = {
    "model": "granite3.1-dense:8b",
    "messages": [
    {
    "role": "user",
    "content": "为什么天空是蓝色的?"
    }
    ]
    }
    response = requests.post(url, headers=headers, json=data)
    return response.json()

🦙 Ollama API 代理支持

如果您想直接与 Ollama 模型交互——包括嵌入生成或原始提示流——Open WebUI 通过代理路由提供对原生 Ollama API 的透明直通。

🔁 生成完成(流式)

curl http://localhost:3000/ollama/api/generate -d '{
"model": "llama3.2",
"prompt": "为什么天空是蓝色的?"
}'

📦 列出可用模型

curl http://localhost:3000/ollama/api/tags

🧠 生成嵌入

curl -X POST http://localhost:3000/ollama/api/embed -d '{
"model": "llama3.2",
"input": ["Open WebUI 很棒!", "让我们生成嵌入。"]
}'

这非常适合使用 Open WebUI 后面的 Ollama 模型构建搜索索引、检索系统或自定义管道。

🧩 检索增强生成(RAG)

检索增强生成(RAG)功能允许您通过结合来自外部来源的数据来增强响应。下面,您将找到通过 API 管理文件和知识集合的方法,以及如何在聊天完成中有效使用它们。

上传文件

要在 RAG 响应中使用外部数据,您首先需要上传文件。上传文件的内容会自动提取并存储在向量数据库中。

  • 端点: POST /api/v1/files/

  • Curl 示例:

    curl -X POST -H "Authorization: Bearer YOUR_API_KEY" -H "Accept: application/json" \
    -F "file=@/path/to/your/file" http://localhost:3000/api/v1/files/
  • Python 示例:

    import requests

    def upload_file(token, file_path):
    url = 'http://localhost:3000/api/v1/files/'
    headers = {
    'Authorization': f'Bearer {token}',
    'Accept': 'application/json'
    }
    files = {'file': open(file_path, 'rb')}
    response = requests.post(url, headers=headers, files=files)
    return response.json()

将文件添加到知识集合

上传后,您可以将文件分组到知识集合中,或在聊天中单独引用它们。

  • 端点: POST /api/v1/knowledge/{id}/file/add

  • Curl 示例:

    curl -X POST http://localhost:3000/api/v1/knowledge/{knowledge_id}/file/add \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"file_id": "your-file-id-here"}'
  • Python 示例:

    import requests

    def add_file_to_knowledge(token, knowledge_id, file_id):
    url = f'http://localhost:3000/api/v1/knowledge/{knowledge_id}/file/add'
    headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json'
    }
    data = {'file_id': file_id}
    response = requests.post(url, headers=headers, json=data)
    return response.json()

在聊天完成中使用文件和集合

您可以在 RAG 查询中引用单个文件或整个集合,以获得丰富的响应。

在聊天完成中使用单个文件

当您希望聊天模型的响应专注于特定文件的内容时,此方法非常有用。

  • 端点: POST /api/chat/completions

  • Curl 示例:

    curl -X POST http://localhost:3000/api/chat/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "gpt-4-turbo",
    "messages": [
    {"role": "user", "content": "解释此文档中的概念。"}
    ],
    "files": [
    {"type": "file", "id": "your-file-id-here"}
    ]
    }'
  • Python 示例:

    import requests

    def chat_with_file(token, model, query, file_id):
    url = 'http://localhost:3000/api/chat/completions'
    headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json'
    }
    data = {
    'model': model,
    'messages': [
    {'role': 'user', 'content': query}
    ],
    'files': [
    {'type': 'file', 'id': file_id}
    ]
    }
    response = requests.post(url, headers=headers, json=data)
    return response.json()
在聊天完成中使用知识集合

要利用整个知识集合的聚合数据,您可以在查询中指定集合 ID。

  • Curl 示例:

    curl -X POST http://localhost:3000/api/chat/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "gpt-4-turbo",
    "messages": [
    {"role": "user", "content": "基于知识库总结关键点。"}
    ],
    "files": [
    {"type": "collection", "id": "your-collection-id-here"}
    ]
    }'
  • Python 示例:

    import requests

    def chat_with_collection(token, model, query, collection_id):
    url = 'http://localhost:3000/api/chat/completions'
    headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json'
    }
    data = {
    'model': model,
    'messages': [
    {'role': 'user', 'content': query}
    ],
    'files': [
    {'type': 'collection', 'id': collection_id}
    ]
    }
    response = requests.post(url, headers=headers, json=data)
    return response.json()

其他有用端点

🎯 模型详情

  • 端点: GET /api/models/{model_id}
  • 描述: 获取特定模型的详细信息。

📋 聊天历史

  • 端点: GET /api/chats
  • 描述: 检索用户的聊天历史。

🗂️ 文件管理

  • 列出文件: GET /api/v1/files/
  • 删除文件: DELETE /api/v1/files/{file_id}
  • 下载文件: GET /api/v1/files/{file_id}/download

📚 知识管理

  • 创建知识集合: POST /api/v1/knowledge/
  • 列出知识集合: GET /api/v1/knowledge/
  • 删除知识集合: DELETE /api/v1/knowledge/{knowledge_id}

有关完整的 API 文档,请参考 Open WebUI 实例上的 /docs 端点,或查看我们的 GitHub 仓库 获取最新信息。