from 公众号:程序员more
OpenWebUI通过HTTP方式提供对外接口,使得开发者可以通过HTTP方式快速对接拥有RAG本事的模子基座。
01 OpenWebUI配置app key
OpenWebUI利用BearerToken机制对 API 请求进行身份验证。从 Open WebUI 中的“设置>帐户”获取 API 密钥,或者利用 JWT(JSON Web 令牌)进行身份验证。如下图获取API Key
此中JWT是偶然效性限制,API密钥是永世的。
02 API利用说明
注意每次请求都必要将API KEY密钥设置到HTTP请求头
Authorization: Bearer eyJhbGci***
基础接口功能包罗列出在OpenWebUI注册的模子和模子进行谈天。
接口作用
| 列出所有已经配置在OpenWebUI的模子
| 地址
| /api/models
| 方法
| GET
| 请求示例
| 127.0.0.1:3000/api/models
| 相应结果
| - { "data": [{ "id": "deepseek-r1:1.5b", "name": "deepseek-r1:1.5b", "object": "model", "created": 1741313196, "owned_by": "ollama", "ollama": { "name": "deepseek-r1:1.5b", "model": "deepseek-r1:1.5b", "modified_at": "2025-02-26T09:59:46.3066414+08:00", "size": 1117322599, "digest": "a42b25d8c10a841bd24724309898ae851466696a7d7f3a0a408b895538ccbc96", "details": { "parent_model": "", "format": "gguf", "family": "qwen2", "families": ["qwen2"], "parameter_size": "1.8B", "quantization_level": "Q4_K_M" }, "urls": [0] }, "actions": [] }]}
复制代码 | 以下是简单的谈天调用,文章下面会先容关联知识库的谈天调用方式
接口作用
| 调用后端模子进行谈天
| 地址
| /api/chat/completions
| 方法
| POST
| 请求示例
|
- { "model": "deepseek-r1:1.5b", "messages": [ { "role": "user", "content": "你好啊" } ]}
复制代码 | 参数说明
| model
| /api/models接口返回的模子id
| role
| user=⽤户输⼊
assistant=AI助⼿的复兴
system=体系,即设定AI的⾏为
tool=⼯具调⽤返回值
| content
| 提问内容
| 相应结果
|
- { "id": "deepseek-r1:1.5b-09322e4f-ba07-42fc-894c-c64424240670", "created": 1741313942, "model": "deepseek-r1:1.5b", "choices": [ { "index": 0, "logprobs": null, "finish_reason": "stop", "message": { "content": "<think>\n\n</think>\n\n你好!很高兴见到你,有什么我可以帮忙的吗?", "role": "assistant" } } ], "object": "chat.completion", "usage": { "response_token/s": 22.73, "prompt_token/s": 41.32, "total_duration": 890745200, "load_duration": 19290900, "prompt_eval_count": 5, "prompt_tokens": 5, "prompt_eval_duration": 121000000, "eval_count": 17, "completion_tokens": 17, "eval_duration": 748000000, "approximate_total": "0h0m0s", "total_tokens": 22, "completion_tokens_details": { "reasoning_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0 } }}
复制代码 | 参数说明
| message.content
| 相应内容
| 谈天中利用流式相应
接口作用
| 在谈天中利用流式相应,结果不是一次性返回,而是分段返回,参数中设置stream即是true
| 地址
| /api/chat/completions
| 方法
| POST
| 请求示例
| - { "model": "deepseek-r1:1.5b", "messages": [ { "role": "user", "content": "什么是java" } ], <strong>"stream":true</strong>}
复制代码 | 相应结果
|
数据以流式相应:
data: {"id": "deepseek-r1:1.5b-fc918971-baab-4f2f-a359-cafb9c104ab2", "created": 1741331179, "model": "deepseek-r1:1.5b", "choices": [{"index": 0, "logprobs": null, "finish_reason": null, "delta": {"content": "\u7684\u5173\u952e"}}], "object": "chat.completion.chunk"}
……
data: {"id": "deepseek-r1:1.5b-0fae48d0-162d-4cef-ba42-54ef12a27c4f", "created": 1741331179, "model": "deepseek-r1:1.5b", "choices": [{"index": 0, "logprobs": null, "finish_reason": null, "delta": {"content": "\u3002"}}], "object": "chat.completion.chunk"}
……
data: {"id": "deepseek-r1:1.5b-33a7f78c-0052-47d7-a674-0560458b8107", "created": 1741331179, "model": "deepseek-r1:1.5b", "choices": [{"index": 0, "logprobs": null, "finish_reason": "stop", "delta": {}}], "object": "chat.completion.chunk", "usage": {"response_token/s": 20.83, "prompt_token/s": 52.63, "total_duration": 99595060200, "load_duration": 1483943500, "prompt_eval_count": 5, "prompt_tokens": 5, "prompt_eval_duration": 95000000, "eval_count": 2042, "completion_tokens": 2042, "eval_duration": 98014000000, "approximate_total": "0h1m39s", "total_tokens": 2047, "completion_tokens_details": {"reasoning_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0}}}
……
data: [DONE]
| 请求中附带知识库文件或者知识库集合
接口作用
| 请求中附带知识库文件或者知识库集合
| 地址
| /api/chat/completions
| 方法
| POST
| 请求参数指定知识文件
| - { "model": "gpt-4-turbo", "messages": [ {"role": "user", "content": "Explain the concepts in this document."} ], "files": [ {"type": "file", "id": "your-file-id-here"} ]}
复制代码 | 请求参数指定知识集合
| - { "model": "gpt-4-turbo", "messages": [ {"role": "user", "content": "Provide insights on the historical perspectives covered in the collection."} ], "files": [ {"type": "collection", "id": "your-collection-id-here"} ]}
复制代码 | 接下来相识知识库上传相干API。
03 RAG部门
Retrieval Augmented Generation (RAG) 功能通过整合外部来源的数据来增强相应。下面将说明知识库创建与知识库数据上传相干接口,如没有说明同一的请求头Content-Type=application/json
上传知识库的流程:
1. 创建知识库,接口:/api/v1/knowledge/create;
2. 上传知识库文件,接口:/api/v1/files/
3. 关联知识库与知识库文件,接口:/api/v1/knowledge/{知识库id}/file/add
接口与参数的简单说明
创建知识库
| 接口
| POST /api/v1/knowledge/create
| 请求参数
| {"name":"知识库名称","description":"知识库说明","access_control":null}
| 相应结果
| - { "id": "知识库Id:e13f471f-cc6c-4df1-9210-8eb586bf8b6b", "user_id": "e42bf055-03e4-421e-894d-c18a58b6b73d", "name": "test", "description": "for test", "data": null, "meta": null, "access_control": null, "created_at": 1741317031, "updated_at": 1741317031, "files": null}
复制代码 | 上传知识库文件
| 接口
| POST /api/v1/files/
| 请求参数
| 文件以二进制流上传
请求头:content-type:multipart/form-data;
| 相应结果
| - { "id": "77cb080c-5de8-4ffb-9304-d658e44a4dfb", "user_id": "e42bf055-03e4-421e-894d-c18a58b6b73d", "hash": "93737981707e6327ca3adf45a7ae11bfd33bc7fcbb0de82c85c390efa1a2f01c", "filename": "日志.txt", "data": { "content": "文件上传内容" }, "meta": { "name": "日志.txt", "content_type": "text/plain", "size": 5587, "data": {}, "collection_name": "file-77cb080c-5de8-4ffb-9304-d658e44a4dfb" }, "created_at": 1741317221, "updated_at": 1741317221}
复制代码 |
上传知识库文件
| 接口
| POST /api/v1/knowledge/{id}/file/add
id对应/api/v1/knowledge/create接口相应的知识库id
| 请求参数
| {"file_id":"77cb080c-5de8-4ffb-9304-d658e44a4dfb"}
file_id对应/api/v1/files接口相应的id
| 相应结果
| - { "id": "e13f471f-cc6c-4df1-9210-8eb586bf8b6b", "user_id": "e42bf055-03e4-421e-894d-c18a58b6b73d", "name": "test", "description": "for test", "data": { "file_ids": [ "77cb080c-5de8-4ffb-9304-d658e44a4dfb" ] }, "meta": null, "access_control": null, "created_at": 1741317031, "updated_at": 1741317226, "files": [ { "id": "77cb080c-5de8-4ffb-9304-d658e44a4dfb", "user_id": "e42bf055-03e4-421e-894d-c18a58b6b73d", "hash": "93737981707e6327ca3adf45a7ae11bfd33bc7fcbb0de82c85c390efa1a2f01c", "filename": "日志.txt", "path": "/app/backend/data/uploads/77cb080c-5de8-4ffb-9304-d658e44a4dfb_日志.txt", "data": { "content": "文件内容" }, "meta": { "name": "日志.txt", "content_type": "text/plain", "size": 5587, "data": {}, "collection_name": "e13f471f-cc6c-4df1-9210-8eb586bf8b6b" }, "access_control": null, "created_at": 1741317221, "updated_at": 1741317221 } ]}
复制代码 | 假如想实现多轮会话,可以看/api/v1/chats/{chat_id},它重要负责管理会话,包罗查询(GET)、新增(POST),重要通过chat_id关联/api/chat/completions。
假如想相识其他的接口我们可以通过在浏览器打开调试窗口查看网络调用情况。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |