🤖 RAG Chatbot API

基于阿里云通义千问 + 向量数据库的智能问答系统接口文档

🔗 Base URL: http://localhost:8000

💬 聊天接口

POST /api/chat 核心接口

发送消息并获取AI回复,支持RAG知识库检索增强。
知识库来源:用户私有分析报告 (analysis_sections) - 通过 user_id 关联查询

请求参数

参数类型必填说明
messagestring必填用户消息内容
session_idstring可选会话ID,用于保持上下文
use_ragboolean可选是否启用RAG检索,默认true
user_idstring必填用户ID (uuid),用于查询用户私有分析报告

请求示例

{
  "message": "请介绍一下公司的营销方案",
  "session_id": "abc123",
  "use_rag": true,
  "user_id": "34e3911e-4959-4710-bde2-da59abf743e2"
}

响应示例

{
  "response": "根据知识库,公司的营销方案包括...",
  "session_id": "abc123"
}
POST /api/chat/stream 流式响应

流式聊天接口,支持Server-Sent Events (SSE) 实时返回生成内容。

请求参数

/api/chat 相同

响应格式 (SSE)

data: {"content": "根据"}

data: {"content": "知识库"}

data: {"done": true, "session_id": "abc123"}
POST /api/search

搜索用户私有分析报告(关键词匹配)。

请求参数

参数类型必填说明
querystring必填搜索查询文本
top_kinteger可选返回结果数量,默认5
user_idstring必填用户ID (uuid),搜索该用户的私有报告

请求示例

{
  "query": "营销策略",
  "top_k": 3,
  "user_id": "34e3911e-4959-4710-bde2-da59abf743e2"
}

响应示例

[
  {
    "document": {
      "id": 1,
      "content": "相关文档内容...",
      "metadata": null,
      "created_at": "2025-12-12T10:00:00Z"
    },
    "similarity": 0.89
  }
]

📋 问题模板管理

GET /api/templates 模板接口

获取所有问题模板列表,供大模型和前端使用。

响应示例

{
  "templates": [
    {
      "id": "uuid-xxx",
      "question": "请介绍公司的主要业务",
      "description": "查询公司业务介绍",
      "category": "公司信息",
      "order": 0
    }
  ],
  "total": 1
}
POST /api/templates 管理员

创建新的问题模板(管理员接口)。

请求参数

参数类型必填说明
questionstring必填问题模板内容
descriptionstring可选模板描述
categorystring可选分类
orderinteger可选排序顺序,默认0

请求示例

{
  "question": "公司有哪些产品?",
  "description": "查询产品列表",
  "category": "产品信息",
  "order": 1
}
PUT /api/templates/{template_id} 管理员

修改问题模板(管理员接口)。

请求参数

参数类型必填说明
questionstring可选问题模板内容
descriptionstring可选模板描述
categorystring可选分类
orderinteger可选排序顺序
DELETE /api/templates/{template_id} 管理员

删除问题模板(管理员接口)。

POST /api/templates/batch 管理员

批量设置问题模板,会覆盖所有现有模板(管理员接口)。

请求示例

[
  {"question": "公司有哪些产品?"},
  {"question": "如何联系客服?"},
  {"question": "退换货政策是什么?"}
]

📌 其他接口

GET /

返回聊天界面首页。

GET /docs

Swagger UI 交互式 API 文档,支持在线测试接口。

GET /redoc

ReDoc 格式的 API 文档。

⚠️ 错误码说明

状态码说明
200请求成功
400请求参数错误
422请求体格式错误
500服务器内部错误(如API调用失败)

RAG Chatbot API v1.0.0 | 基于 FastAPI 构建

使用阿里云 DashScope API (通义千问 + text-embedding-v2)