AuraK 提供了一套完整的开放 API,允许外部系统通过 API Key 认证访问知识库功能。
基础 URL: http://localhost:3001
支持两种认证方式:
x-api-key: kb_your_api_key_here
x-tenant-id: your_tenant_id # 可选,指定租户
Authorization: Bearer kb_your_api_key_here
x-tenant-id: your_tenant_id # 可选,指定租户
x-tenant-id,将使用用户的默认租户x-tenant-id,将验证用户是否属于该租户?tenantId=xxx 查询参数指定租户与知识库进行对话,支持流式响应。
POST /api/v1/chat
请求头:
Content-Type: application/json
x-api-key: kb_your_api_key_here
请求体:
{
"message": "什么是机器学习?",
"stream": true,
"history": [
{
"role": "user",
"content": "你好"
},
{
"role": "assistant",
"content": "你好!有什么我可以帮助你的吗?"
}
],
"userLanguage": "zh"
}
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
message |
string | ✅ | 用户消息 |
stream |
boolean | ❌ | 是否使用流式响应,默认 true |
history |
array | ❌ | 历史对话记录 |
userLanguage |
string | ❌ | 用户语言,支持 zh、en、ja |
响应(流式):
data: {"type":"status","data":{"stage":"searching","message":"正在搜索知识库..."}}
data: {"type":"status","data":{"stage":"search_complete","message":"找到 3 条相关信息"}}
data: {"type":"sources","data":[{"fileName":"机器学习入门.pdf","content":"机器学习是人工智能的一个分支...","score":0.95,"chunkIndex":2,"fileId":"xxx"}]}
data: {"type":"content","data":"机器学习是"}
data: {"type":"content","data":"人工智能的一个"}
data: {"type":"content","data":"重要分支..."}
data: [DONE]
响应类型说明:
| 类型 | 说明 |
|---|---|
status |
状态信息(搜索中、搜索完成、生成中) |
sources |
引用来源 |
content |
AI 生成的回答内容 |
thinking |
深度思考过程(仅部分模型支持) |
historyId |
对话历史 ID |
响应(非流式):
{
"message": "机器学习是人工智能的一个重要分支...",
"sources": [
{
"fileName": "机器学习入门.pdf",
"content": "机器学习是人工智能的一个分支...",
"score": 0.95,
"chunkIndex": 2,
"fileId": "xxx"
}
],
"historyId": "hist_xxx"
}
不使用知识库的简单聊天。
POST /chat
请求体:
{
"message": "你好",
"stream": true
}
搜索知识库内容。
POST /api/v1/search
请求体:
{
"query": "机器学习",
"topK": 5,
"similarityThreshold": 0.7
}
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
query |
string | ✅ | 搜索查询 |
topK |
number | ❌ | 返回结果数量,默认 5 |
similarityThreshold |
number | ❌ | 相似度阈值,默认 0.7 |
响应:
{
"results": [
{
"fileName": "机器学习入门.pdf",
"content": "机器学习是人工智能的一个分支...",
"score": 0.95,
"chunkIndex": 2,
"fileId": "xxx"
}
],
"total": 1
}
获取知识库中的所有文件。
GET /api/v1/knowledge-bases
响应:
{
"files": [
{
"id": "file_xxx",
"name": "机器学习入门.pdf",
"size": 1024000,
"status": "completed",
"createdAt": "2024-01-01T00:00:00.000Z"
}
],
"total": 1
}
上传文件到知识库。
POST /api/v1/knowledge-bases/upload
请求头:
Content-Type: multipart/form-data
x-api-key: kb_your_api_key_here
请求体:
file: [文件]
mode: fast
chunkSize: 1000
chunkOverlap: 200
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
File | ✅ | 要上传的文件 |
mode |
string | ❌ | 处理模式:fast(快速)或 precise(精确) |
chunkSize |
number | ❌ | 分块大小,默认 1000 |
chunkOverlap |
number | ❌ | 分块重叠,默认 200 |
响应:
{
"id": "file_xxx",
"name": "机器学习入门.pdf",
"size": 1024000,
"status": "indexing",
"message": "文件上传成功,正在处理中"
}
GET /api/v1/knowledge-bases/:id
响应:
{
"id": "file_xxx",
"name": "机器学习入门.pdf",
"size": 1024000,
"status": "completed",
"chunkCount": 50,
"createdAt": "2024-01-01T00:00:00.000Z"
}
DELETE /api/v1/knowledge-bases/:id
响应:
{
"message": "文件删除成功"
}
{
"statusCode": 400,
"message": "错误描述",
"error": "Bad Request"
}
| 状态码 | 说明 |
|---|---|
| 400 | 请求参数错误 |
| 401 | 认证失败(API Key 无效或缺失) |
| 403 | 权限不足 |
| 404 | 资源不存在 |
| 500 | 服务器内部错误 |
// 流式聊天
const response = await fetch('http://localhost:3001/api/v1/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'kb_your_api_key_here'
},
body: JSON.stringify({
message: '什么是机器学习?',
stream: true
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') break;
const parsed = JSON.parse(data);
console.log(parsed);
}
}
}
import requests
# 非流式聊天
response = requests.post(
'http://localhost:3001/api/v1/chat',
headers={
'Content-Type': 'application/json',
'x-api-key': 'kb_your_api_key_here'
},
json={
'message': '什么是机器学习?',
'stream': False
}
)
data = response.json()
print(data['message'])
# 流式聊天
curl -X POST http://localhost:3001/api/v1/chat \
-H "Content-Type: application/json" \
-H "x-api-key: kb_your_api_key_here" \
-d '{"message": "什么是机器学习?", "stream": true}'
# 上传文件
curl -X POST http://localhost:3001/api/v1/knowledge-bases/upload \
-H "x-api-key: kb_your_api_key_here" \
-F "file=@/path/to/file.pdf" \
-F "mode=fast"
# 搜索知识库
curl -X POST http://localhost:3001/api/v1/search \
-H "Content-Type: application/json" \
-H "x-api-key: kb_your_api_key_here" \
-d '{"query": "机器学习", "topK": 5}'
上传文件时支持以下格式:
目前暂无速率限制,但建议:
A: 登录系统后,进入 设置 → API Key 标签页即可查看和复制。
A: 在 API Key 管理页面点击"重新生成 API Key"按钮。注意:旧 Key 将立即失效。
A: 支持中文(zh)、英文(en)、日文(ja),通过 userLanguage 参数指定。
A: 取决于文件大小和处理模式:
A: 使用 GET /api/v1/knowledge-bases/:id 接口查看文件状态。