Skip to main content

知识问答

基于知识库的智能问答,通过 SSE 流式输出,依次返回规划、工具调用、生成三个阶段

基于知识库的智能问答,通过 SSE 流式输出,依次返回规划、工具调用、生成三个阶段。

接口说明

  • 调用方式:HTTP REST,POST + application/json,响应为 SSE 流式输出。
  • Base URLhttps://{workspaceId}.cn-beijing.maas.aliyuncs.com,其中 {workspaceId} 为业务空间 ID。
  • 路径/api/v2/apps/knowledge/chat
  • 鉴权:请求头携带 Authorization: Bearer <API-Key>
  • 限流:默认用户维度 25 QPS。

请求语法

POST /api/v2/apps/knowledge/chat HTTP/1.1
Host: {workspaceId}.cn-beijing.maas.aliyuncs.com
Authorization: Bearer <API-Key>
Content-Type: application/json

请求参数

名称类型必填描述
agent_idstring知识问答服务实例 ID。在控制台知识问答页面创建并发布后获取。
querystring用户问题文本。
session_idstring会话 ID,用于多轮对话上下文维护。
imagesarray<string>图片 URL 列表,用于多模态问答。

响应格式

响应通过 SSE(Server-Sent Events)流式输出,依次返回三个阶段:
  1. 规划阶段:模型分析问题并制定检索策略
  2. 工具调用阶段:执行知识库检索
  3. 生成阶段:基于检索结果生成回答
每个 SSE 事件格式为:
event: result
data: {"type": "planning", "content": "..."}

event: result
data: {"type": "tool_call", "content": "..."}

event: result
data: {"type": "generation", "content": "..."}

请求示例

curl -X POST "https://{workspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/knowledge/chat" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "agent_id": "aid-xxxxxxxxxxxxxxxx",
    "query": "百炼平台支持哪些模型?"
}'

错误码

如果调用失败并返回报错信息,请参阅错误码文档进行解决。