以下是简单的聊天调用,文章下面会介绍关联知识库的聊天调用方式。
| 接口作用 | 调用后端模型进行聊天 |
| --- | --- |
| 地址 | /api/chat/completions |
| 方法 | POST |
| 请求示例 | ```json
{
"model": "deepseek-r1:1.5b",
"messages": [
{
"role": "user",
"content": "你好啊"
}
]
}
``` |
| 参数说明 | model: /api/models 接口返回的模型 id<br>role: user=用户输入, assistant=AI 助手的回复, system=系统设定, tool=工具调用返回值<br>content: 提问内容 |
| 响应结果 | ```json
{
"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": "\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
}
}
}
``` |
### 聊天中使用流式响应
| 接口作用 | 在聊天中使用流式响应,结果不是一次性返回,而是分段返回,参数中设置 stream 等于 true |
| --- | --- |
| 地址 | /api/chat/completions |
| 方法 | POST |
| 请求示例 | ```json
{
"model": "deepseek-r1:1.5b",
"messages": [
{
"role": "user",
"content": "什么是 java"
}
],
"stream": true
}
``` |
| 响应结果 | 数据以流式响应:<br>```text
data: {"id": "deepseek-r1:1.5b-fc918971-baab-4f2f-a359-cafb9c104ab2", ... "delta": {"content": "的关键"}}
data: {"id": "...", ... "delta": {"content": "。"}}
data: {"id": "...", ... "finish_reason": "stop", ... "usage": {...}}
data: [DONE]
``` |
### 请求中附带知识库文件或者知识库集合
| 接口作用 | 请求中附带知识库文件或者知识库集合 |
| --- | --- |
| 地址 | /api/chat/completions |
| 方法 | POST |
| 请求参数指定知识文件 | ```json
{
"model": "gpt-4-turbo",
"messages": [
{"role": "user", "content": "Explain the concepts in this document."}
],
"files": [
{"type": "file", "id": "your-file-id-here"}
]
}
``` |
| 请求参数指定知识集合 | ```json
{
"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} |
| 响应结果 | ```json
{
"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/ |
| 请求参数 | 文件以二进制流上传<br>请求头:content-type: multipart/form-data; |
| 响应结果 | ```json
{
"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 |
| 请求参数 | {"file_id":"77cb080c-5de8-4ffb-9304-d658e44a4dfb"}<br>(file_id 对应/api/v1/files 接口响应的 id) |
| 响应结果 | ```json
{
"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。
如果想了解其他的接口我们可以通过在浏览器打开调试窗口查看网络调用情况。

