RAG API接口协议
本文档详细介绍Bella-RAG系统的API接口规范,包括知识检索、检索增强生成等核心功能的接口协议。
🔗 API基础信息
接口地址
- 云端版本:
https://rag.bella.top
认证方式
所有API接口都需要在请求头中提供认证信息:
Authorization: Bearer {OPEN_API_KEY}
通用请求头
Content-Type: application/json
Authorization: Bearer {OPEN_API_KEY}
X-BELLA-TRACE-ID: {链路追踪ID} (可选)
📄 文档索引接口
文档上传索引
接口地址: POST /api/file/stream/indexing
请求头部:
Content-Type: multipart/form-data
Authorization: Bearer {OPEN_API_KEY}
请求参数:
参数名称 | 类型 | 必需 | 描述 |
---|---|---|---|
file_id | string | 是 | 文件唯一标识 |
file_name | string | 是 | 文件名称 |
user | string | 是 | 用户标识 |
file | file | 是 | 上传的文件 |
请求示例:
curl --location 'http://localhost:8008/api/file/stream/indexing' \
--header 'Authorization: Bearer {OPEN_API_KEY}' \
--header 'Content-Type: multipart/form-data' \
--form 'file_id="FILE_ID"' \
--form 'file_name="document.pdf"' \
--form 'user="user_001"' \
--form 'file=@"/path/to/document.pdf"'
🔍 知识检索接口
基础检索
接口地址: POST /api/rag/search
请求参数:
参数名称 | 类型 | 必需 | 默认值 | 描述 |
---|---|---|---|---|
query | string | 是 | - | 检索查询 |
scope | array | 是 | - | 检索范围 |
scope.type | string | 是 | file | 范围类型:file、directory、space |
scope.ids | array | 是 | - | 文件ID列表 |
limit | integer | 否 | 3 | 检索数量限制 |
user | string | 是 | - | 用户标识 |
mode | string | 否 | normal | 检索模式 |
检索模式说明:
模式 | 描述 | 策略配置 |
---|---|---|
fast | 轻量搜索:追求速度,精度要求不高 | 语义检索,无重排器,最大补全策略 |
normal | 精准搜索:平衡速度和质量 | 语义检索,有重排器,最大补全策略 |
ultra | 全能搜索:高精度需求,支持图片理解 | 混合检索(向量+关键词),有重排器,上下文补全策略 |
请求示例:
{
"query": "机器学习的主要算法有哪些?",
"scope": [{
"type": "file",
"ids": ["file_123", "file_456"]
}],
"limit": 5,
"user": "user_00000000",
"mode": "normal"
}
响应数据:
字段名称 | 类型 | 描述 |
---|---|---|
code | number | 响应状态码 |
message | string | 响应消息 |
data.total | number | 结果总数 |
data.docs | array | 检索结果列表 |
data.docs[].type | string | 内容类型(默认text) |
data.docs[].text | string | 检索到的文本内容 |
data.docs[].score | number | 相关度得分 |
data.docs[].annotation | object | 文件元信息 |
响应示例:
{
"code": 0,
"message": "Success",
"data": {
"total": 10,
"docs": [
{
"type": "text",
"text": "召回文本内容...",
"score": 0.92,
"annotation": {
"file_id": "12345",
"file_name": "example_document.txt",
"paths": [[1, 12, 1], [1, 12, 2]]
}
}
]
}
}
💬 检索增强生成接口
RAG问答
接口地址: POST /api/rag/chat
请求参数:
参数名称 | 类型 | 必需 | 默认值 | 描述 |
---|---|---|---|---|
query | string | 是 | - | 用户问题 |
scope | array | 是 | - | 检索范围 |
user | string | 是 | - | 用户标识 |
response_type | string | 否 | blocking | 响应类型:blocking、stream |
timeout | number | 否 | - | 超时时间(秒) |
model | string | 否 | - | 生成模型 |
mode | string | 否 | normal | 检索模式 |
检索模式扩展:
模式 | 描述 | 特点 |
---|---|---|
fast | 轻量搜索 | 速度优先,资源消耗少 |
normal | 精准搜索 | 平衡效果与性能 |
ultra | 全能搜索 | 高精度,支持多模态 |
deep | 智能Agent搜索 | Deep RAG模式,支持复杂推理 |
请求示例:
{
"query": "机器学习的主要算法有哪些?",
"scope": [{
"type": "file",
"ids": ["file_123", "file_456"]
}],
"user": "user_00000000",
"response_type": "stream",
"model": "gpt-4o",
"mode": "deep"
}
📡 流式响应协议
响应类型
流式响应使用Server-Sent Events (SSE)格式,支持以下事件类型:
1. retrieval.completed - 检索完成
{
"id": "session_id",
"object": "retrieval.doc",
"doc": [
{
"type": "text",
"text": "检索到的文档内容...",
"annotation": {
"file_id": "file-123",
"file_name": "document.pdf",
"paths": [1, 2, 3]
},
"score": 0.85
}
]
}
2. message.delta - 消息增量
{
"id": "session_id",
"object": "message.delta",
"delta": {
"content": [
{
"type": "text",
"text": [
{
"value": "增量生成的文本内容",
"annotations": []
}
]
}
]
}
}
3. error - 错误事件
{
"id": "session_id",
"object": "error",
"error": {
"code": "unauthorized",
"type": "unauthorized",
"message": "ak is invalid"
}
}
4. message.sensitives - 敏感词检测
{
"id": "session_id",
"object": "message.sensitives",
"sensitives": [
{
"count": 1,
"detail": [
{
"offset": 60,
"length": "11",
"word": "敏感词",
"secTag": "aigc_phone",
"templateId": "aigc"
}
],
"type": "dataSec"
}
]
}
🤖 Deep RAG模式专有事件
Deep模式流式事件
1. plan.create - 计划创建
{
"id": "session_id",
"object": "plan.create",
"plan": [
{
"order": 1,
"description": "使用file_search工具查询相关文件",
"dependencies": []
}
],
"reasoning_content": "规划推理过程"
}
2. plan.step.start - 步骤开始
{
"id": "session_id",
"object": "plan.step.start",
"step": {
"order": 1,
"actions": [
{
"name": "file_search",
"params": {"question": "用户问题", "page": 1}
}
]
}
}
3. plan.step.complete - 步骤完成
{
"id": "session_id",
"object": "plan.step.complete",
"step": {
"step_order": 1,
"actions": [],
"status": "success",
"step_result": "执行结果"
}
}
4. plan.update - 计划更新
{
"id": "session_id",
"object": "plan.update",
"plan": [
{
"order": 1,
"description": "更新后的任务描述",
"status": 1,
"dependencies": []
}
]
}
5. plan.complete - 计划完成
{
"id": "session_id",
"object": "plan.complete",
"plan": [
{
"description": "已完成 的任务",
"status": 1,
"order": 1,
"result": "执行结果"
}
]
}