跳到主要内容

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_idstring文件唯一标识
file_namestring文件名称
userstring用户标识
filefile上传的文件

请求示例:

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

请求参数:

参数名称类型必需默认值描述
querystring-检索查询
scopearray-检索范围
scope.typestringfile范围类型:file、directory、space
scope.idsarray-文件ID列表
limitinteger3检索数量限制
userstring-用户标识
modestringnormal检索模式

检索模式说明:

模式描述策略配置
fast轻量搜索:追求速度,精度要求不高语义检索,无重排器,最大补全策略
normal精准搜索:平衡速度和质量语义检索,有重排器,最大补全策略
ultra全能搜索:高精度需求,支持图片理解混合检索(向量+关键词),有重排器,上下文补全策略

请求示例:

{
"query": "机器学习的主要算法有哪些?",
"scope": [{
"type": "file",
"ids": ["file_123", "file_456"]
}],
"limit": 5,
"user": "user_00000000",
"mode": "normal"
}

响应数据:

字段名称类型描述
codenumber响应状态码
messagestring响应消息
data.totalnumber结果总数
data.docsarray检索结果列表
data.docs[].typestring内容类型(默认text)
data.docs[].textstring检索到的文本内容
data.docs[].scorenumber相关度得分
data.docs[].annotationobject文件元信息

响应示例:

{
"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

请求参数:

参数名称类型必需默认值描述
querystring-用户问题
scopearray-检索范围
userstring-用户标识
response_typestringblocking响应类型:blocking、stream
timeoutnumber-超时时间(秒)
modelstring-生成模型
modestringnormal检索模式

检索模式扩展:

模式描述特点
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": "执行结果"
}
]
}

6. heartbeat - 心跳包

{
"id": "session_id",
"object": "heartbeat"
}

心跳包说明:

  • 每10秒自动发送一次
  • 防止长时间计算导致连接超时
  • 客户端可以忽略或用于连接监控

📝 非流式响应格式

普通模式响应

{
"code": 0,
"message": "Success",
"data": {
"content": [
{
"type": "text",
"text": [
{
"value": "回答内容",
"annotations": [
{
"type": "file_citation",
"file_citation": {
"paths": [1, 2, 3],
"file_id": "file-123",
"file_name": "example.docx"
}
}
]
}
]
}
]
}
}

Deep模式响应

{
"content": [
{
"type": "text",
"text": [
{
"value": "基于规划执行的回答内容",
"annotations": []
}
]
}
],
"plan": {
"steps": [
{
"description": "执行的步骤描述",
"status": 1,
"order": 1,
"result": "步骤执行结果"
}
]
}
}

⚠️ 错误码说明

错误码类型描述解决方案
401unauthorizedAPI密钥无效检查Authorization头部
400bad_request请求参数错误验证请求参数格式
404not_found文件不存在确认文件ID正确
500internal_error服务器内部错误联系技术支持
429rate_limit请求频率超限降低请求频率

🔧 集成示例

Python示例

import requests
import json

# 基础检索
def search_knowledge(query, file_ids):
url = "http://localhost:8008/api/rag/search"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
data = {
"query": query,
"scope": [{"type": "file", "ids": file_ids}],
"user": "user_001",
"mode": "ultra"
}

response = requests.post(url, headers=headers, json=data)
return response.json()

# 流式问答
def chat_stream(query, file_ids):
url = "http://localhost:8008/api/rag/chat"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
data = {
"query": query,
"scope": [{"type": "file", "ids": file_ids}],
"user": "user_001",
"response_type": "stream",
"mode": "deep"
}

response = requests.post(url, headers=headers, json=data, stream=True)
for line in response.iter_lines():
if line:
event_data = line.decode('utf-8')
if event_data.startswith('data: '):
try:
data = json.loads(event_data[6:])
print(f"Event: {data.get('object')}")
yield data
except json.JSONDecodeError:
continue

JavaScript示例

// 基础检索
async function searchKnowledge(query, fileIds) {
const response = await fetch('http://localhost:8008/api/rag/search', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: query,
scope: [{ type: 'file', ids: fileIds }],
user: 'user_001',
mode: 'ultra'
})
});

return await response.json();
}

// 流式问答
async function chatStream(query, fileIds) {
const response = await fetch('http://localhost:8008/api/rag/chat', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: query,
scope: [{ type: 'file', ids: fileIds }],
user: 'user_001',
response_type: 'stream',
mode: 'deep'
})
});

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: ')) {
try {
const data = JSON.parse(line.slice(6));
console.log('Event:', data.object);
// 处理不同类型的事件
} catch (e) {
// 忽略解析错误
}
}
}
}
}

🔗 相关文档