StreamQuery - 流式对话
更新时间:2024-11-04
API访问域名
请求方式 | POST | 说明 |
---|---|---|
URI | https://api-ngd.baidu.com | |
后缀接口 | core/v5/stream/query |
说明
- 在流式对话模式中,系统会返回多个chunk(片段)返回。每一个 chunk 可能包含最终答案的一部分内容,系统在生成部分内容之时就将其传递给用户,而不必等到完整答案全部生成完毕。在该模式中,系统在逐步产出回答的过程里,同步地将回答的一部分逐次发送给用户,以此确保较为快速的首字输出,确保交互的即时性。
适用场景:
- 要求快速响应的场景:适用于实时互动场景,如在线游戏的客服支持或者商家的售后客服支持,客服需要对用户提出的问题及时反馈。
- 不依赖完整答案进行决策的场景:若用户仅查询一些简单信息,而无需完整分析时,可使用流式对话。
流式对话多次返回示例:
data:{"sessionId":"xxx","queryId":"xxx","replyStatus":200,"answer":[{"chunkId":0,"status":"running","topicId":"","blockId":"","nodeId":"","name":null,"taskFlowSolved":null,"reply":{"type":1,"text":"写爱情","textList":null,"clarifyGuide":null,"replySource":"CHITCHAT","showDocumentSource":null,"documents":null,"tableQAInfo":null,"docDebug":null,"faqSearch":null,"slotCollectInfo":null,"plugin":null,"url":null,"buttons":null,"intentName":null},"instruction":null,"statisticsInfo":null,"debugReplyType":"chitchat","debugNodeName":null,"debugInputParam":null,"debugCost":null,"debugResult":null}],"variables":{"last_response":"","last_user_response":"帮我写一首关于爱情的诗","triggered_intent":["智能写诗_plugin"],"last_intent":""},"endTime":null,"recommendList":["帮我写首诗","为什么还没下班"],"rewriteQuery":"帮我写一首关于爱情的诗","intent":null,"replyInfo":null,"needIgnoreDialogHistory":false,"agentType":1,"qps":1,"totalTokens":0,"statisticsInfoMap":{"unMatch":{"nodeId":"unMatch","type":"unMatch","prompt":null,"output":null,"instruction":null,"errorCode":null,"errorMessage":null,"entityName":null}}}
data:{"sessionId":"xxx","queryId":"xxx","replyStatus":200,"answer":[{"chunkId":0,"status":"running","topicId":"","blockId":"","nodeId":"","name":null,"taskFlowSolved":null,"reply":{"type":1,"text":"诗啊","textList":null,"clarifyGuide":null,"replySource":"CHITCHAT","showDocumentSource":null,"documents":null,"tableQAInfo":null,"docDebug":null,"faqSearch":null,"slotCollectInfo":null,"plugin":null,"url":null,"buttons":null,"intentName":null},"instruction":null,"statisticsInfo":null,"debugReplyType":"chitchat","debugNodeName":null,"debugInputParam":null,"debugCost":null,"debugResult":null}],"variables":{"last_response":"","last_user_response":"帮我写一首关于爱情的诗","triggered_intent":["智能写诗_plugin"],"last_intent":""},"endTime":null,"recommendList":["帮我写首诗","为什么还没下班"],"rewriteQuery":"帮我写一首关于爱情的诗","intent":null,"replyInfo":null,"needIgnoreDialogHistory":false,"agentType":1,"qps":1,"totalTokens":0,"statisticsInfoMap":{"unMatch":{"nodeId":"unMatch","type":"unMatch","prompt":null,"output":null,"instruction":null,"errorCode":null,"errorMessage":null,"entityName":null}}}
data:{"sessionId":"xxx","queryId":"xxx","replyStatus":200,"answer":[{"chunkId":0,"status":"running","topicId":"","blockId":"","nodeId":"","name":null,"taskFlowSolved":null,"reply":{"type":1,"text":",这","textList":null,"clarifyGuide":null,"replySource":"CHITCHAT","showDocumentSource":null,"documents":null,"tableQAInfo":null,"docDebug":null,"faqSearch":null,"slotCollectInfo":null,"plugin":null,"url":null,"buttons":null,"intentName":null},"instruction":null,"statisticsInfo":null,"debugReplyType":"chitchat","debugNodeName":null,"debugInputParam":null,"debugCost":null,"debugResult":null}],"variables":{"last_response":"","last_user_response":"帮我写一首关于爱情的诗","triggered_intent":["智能写诗_plugin"],"last_intent":""},"endTime":null,"recommendList":["帮我写首诗","为什么还没下班"],"rewriteQuery":"帮我写一首关于爱情的诗","intent":null,"replyInfo":null,"needIgnoreDialogHistory":false,"agentType":1,"qps":1,"totalTokens":0,"statisticsInfoMap":{"unMatch":{"nodeId":"unMatch","type":"unMatch","prompt":null,"output":null,"instruction":null,"errorCode":null,"errorMessage":null,"entityName":null}}}
data:{"sessionId":"xxx","queryId":"xxx","replyStatus":200,"answer":[{"chunkId":0,"status":"running","topicId":"","blockId":"","nodeId":"","name":null,"taskFlowSolved":null,"reply":{"type":1,"text":"真是太浪漫","textList":null,"clarifyGuide":null,"replySource":"CHITCHAT","showDocumentSource":null,"documents":null,"tableQAInfo":null,"docDebug":null,"faqSearch":null,"slotCollectInfo":null,"plugin":null,"url":null,"buttons":null,"intentName":null},"instruction":null,"statisticsInfo":null,"debugReplyType":"chitchat","debugNodeName":null,"debugInputParam":null,"debugCost":null,"debugResult":null}],"variables":{"last_response":"","last_user_response":"帮我写一首关于爱情的诗","triggered_intent":["智能写诗_plugin"],"last_intent":""},"endTime":null,"recommendList":["帮我写首诗","为什么还没下班"],"rewriteQuery":"帮我写一首关于爱情的诗","intent":null,"replyInfo":null,"needIgnoreDialogHistory":false,"agentType":1,"qps":1,"totalTokens":0,"statisticsInfoMap":{"unMatch":{"nodeId":"unMatch","type":"unMatch","prompt":null,"output":null,"instruction":null,"errorCode":null,"errorMessage":null,"entityName":null}}}
data:{"sessionId":"xxx","queryId":"xxx","replyStatus":200,"answer":[{"chunkId":0,"status":"running","topicId":"","blockId":"","nodeId":"","name":null,"taskFlowSolved":null,"reply":{"type":1,"text":"了!","textList":null,"clarifyGuide":null,"replySource":"CHITCHAT","showDocumentSource":null,"documents":null,"tableQAInfo":null,"docDebug":null,"faqSearch":null,"slotCollectInfo":null,"plugin":null,"url":null,"buttons":null,"intentName":null},"instruction":null,"statisticsInfo":null,"debugReplyType":"chitchat","debugNodeName":null,"debugInputParam":null,"debugCost":null,"debugResult":null}],"variables":{"last_response":"","last_user_response":"帮我写一首关于爱情的诗","triggered_intent":["智能写诗_plugin"],"last_intent":""},"endTime":null,"recommendList":["帮我写首诗","为什么还没下班"],"rewriteQuery":"帮我写一首关于爱情的诗","intent":null,"replyInfo":null,"needIgnoreDialogHistory":false,"agentType":1,"qps":1,"totalTokens":0,"statisticsInfoMap":{"unMatch":{"nodeId":"unMatch","type":"unMatch","prompt":null,"output":null,"instruction":null,"errorCode":null,"errorMessage":null,"entityName":null}}}
请求参数
Header参数
名称 | 类型 | 必填 | 中文名 | 说明 |
---|---|---|---|---|
token | string | 是 | bot_access_token | access_token: access_token是bot的属性,通过bot管理列表获取 |
Uid | string | 否 | c 端用户id | |
Username | string | 否 | c 端用户名称 | |
ACTIVE | boolean | 否 | 调用环境 | online/offline 分别代表线上环境和测试环境公有云环境该参数不生效 |
Body参数
名称 | 类型 | 必填 | 中文名 | 说明 |
---|---|---|---|---|
queryText | string | 否 | 用户问题 | 请求的问题,比如示例中的:你好 |
sessionId | string | 否 | 会话id | 用于标识一个访问者的会话和保持上下文信息,在首轮对话中可以为空。 |
chatSessionLatitudeSwitch | boolean | 否 | 单次session闲聊 | 该字段控制单次session闲聊是否开启,默认值:true |
variables | object | 否 | 变量 | 对话中使用的变量,填写后会覆盖当前中对话中已有的变量值。格式为MAP(key 是变量名称,value是变量值)。 |
topicId | string | 否 | 主题id | 对话测试时使用,执行指定的topic,和nodeId配合使用 |
engines | array | 否 | 使用的对话引擎 | String类型,FAQ("faq", "知识库-FAQ"),DOC_QA("docQa", "知识库-文档问答"),TASK_FLOW("taskflow", "流程画布"),TASK_BASED("task_based", "意图识别"),为null 或taskflow是支持所有引擎 |
multimodal | array | 否 | 多模态输入 | 当前支持图片,不能单独使用,需要和queryText 一起传递,目前仅标准版支持 |
multimodal \ fileKey | string | 是 | 文件唯一标识 | 使用/open/api/v2/file/upload接口上传后返回的key |
multimodal \ type | string | 是 | 文件类型 | 1 :图片,目前只支持图片,上传一张图片 |
startId | string | 否 | 节点id | 对话测试时使用,执行指定的节点id。和topicId配合使用 |
channel | string | 否 | 渠道 | 渠道管理中的渠道标识,传递该参数,可以命中FAQ中配置的条件答案 |
button | object | 否 | 按钮 | 按钮步骤会回复所有按钮的id,名称集合,点击按钮时需传递该字段 |
button \ id | string | 是 | 按钮id | |
button \ name | string | 是 | 按钮名称 | |
collect | boolean | 是 | 对话数据收集 | 是否收集会话数据,用于统计、标注 |
响应参数
名称 | 类型 | 必填 | 中文名 | 说明 |
---|---|---|---|---|
sessionId | string | 是 | 会话ID | 用于标识本轮会话的唯一ID |
queryId | string | 是 | 对话ID | 用于标识本次对话的唯一ID |
replyStatus | integer | 是 | 回复状态 | 回复状态,默认成功SUCCESS(200, "成功");FAIL(4008001, "失败"); |
answer | array | 是 | 回答列表 | 包含各个回答块的数组 |
answer \ chunkId | integer | 是 | 流式chunkId | 流式会话将AI的回答分块(chunk)返回,一次返回一个chunk,该参数标识一次对话中的唯一chunk |
answer \ status | string | 是 | 流式返回数据状态 | RUNNING("running")表示还有未传完的chunk;DONE("done")表示当前chunk为该轮对话中的最后一个chunk; |
answer \ topicId | string | 否 | 主题ID | 对应的主题ID |
answer \ blockId | string | 否 | 区块ID | 对应的区块ID |
answer \ nodeId | string | 否 | 节点ID | 对应的节点ID |
answer \ reply | object | 否 | 回复 | 包含回复相关详细信息的对象 |
answer \ reply \ type | integer | 是 | 回复类型 | 1 (文本)3(富文本)8(列表) |
answer \ reply \ text | string | 是 | 文本 | 文本/富文本答案 |
answer \ reply \ textList | array | 否 | 文本列表 | 回复的文本列表 |
answer \ reply \ buttons | array | 否 | 按钮列表 | replySource是BUTTONS 时该字段有值 |
answer \ reply \ buttons \ id | string | 否 | 按钮id | |
answer \ reply \ buttons \ name | string | 否 | 按钮名称 | |
answer \ reply \ clarifyGuide | string | 否 | 澄清指南 | 提供澄清相关的指南和信息 |
answer \ reply \ replySource | string | 是 | 回复来源 | FAQ("faq", "知识库-FAQ"),DOC_QA("docQa", "知识库-文档"),TEXT_REPLY("text", "文本回复"),INTENT_CLARIFY("intentClarify", "意图澄清"),LARGE_MODEL_REPLY("largeModelReply", "大模型回复"),COLLECT_SLOT_ENTITY("collectSlotEntity", "收集槽位实体"),CHITCHAT("chitchat", "闲聊"),UNMATCHED("unmatch", "未匹配固定话术"),SENSITIVE("sensitive", "敏感拒答话术"),SYSTEM("system", "系统异常"),INSTRUCTION("instruction", "指令"); |
answer \ reply \ showDocumentSource | boolean | 否 | 显示文档源 | 是否显示文档来源 |
answer \ reply \ documents | array | 是 | 文档 | 知识问答信息,replySource是DOC_QA 该字段有值 |
answer \ reply \ documents \ document_id | string | 是 | 文档ID | 文档的唯一标识符 |
answer \ reply \ documents \ document_name | string | 是 | 文档名 | 文档的名称 |
answer \ reply \ documents \ knowledge_type | string | 是 | 文档类型 | 文档类型:doc 普通文档, web 网页知识 |
answer \ reply \ documents \ web_url | string | 是 | 文档链接 | 文档链接,当knowledgeType 是 web 时有值 |
answer \ reply \ documents \ data | array | 是 | 数据 | 文档的数据内容 |
answer \ reply \ documents \ data \ segment_id | string | 是 | 段落ID | 段落的唯一标识符 |
answer \ reply \ documents \ data \ segment_content | string | 是 | 段落内容 | 段落的文本内容 |
answer \ reply \ documents \ data \ score | string | 是 | 得分 | 段落相关性得分 |
answer \ reply \ documents \ data \ segment_coord | string | 是 | 段落坐标 | 段落在文档中的位置坐标 |
answer \ reply \ documents \ data \ data | array | 是 | 数据 | 段落中的句子数据 |
answer \ reply \ documents \ data \ data \ sentence_id | string | 是 | 句子ID | 句子的唯一标识符 |
answer \ reply \ documents \ data \ data \ sentence_content | string | 是 | 句子内容 | 句子的文本内容 |
answer \ reply \ documents \ data \ data \ score | string | 是 | 得分 | 句子相关性得分 |
answer \ reply \ docDebug | object | 否 | 文档调试 | 提供文档调试相关信息,replySource是DOC_QA 该字段有值 |
answer \ reply \ faqSearch | array | 否 | FAQ搜索 | faq 检索信息,replySource是FAQ 该字段有值 |
answer \ reply \ slotCollectInfo | object | 否 | 槽收集信息 | 收集槽信息 |
answer \ reply \ slotCollectInfo \ waitCollectResultList | array | 是 | 待收集结果列表 | 列出待收集的结果 |
answer \ reply \ slotCollectInfo \ waitCollectResultList \ name | string | 是 | 名称 | 待收集的槽名称 |
answer \ reply \ slotCollectInfo \ waitCollectResultList \ result | string | 是 | 结果 | 已收集的结果 |
answer \ reply \ slotCollectInfo \ waitCollectResultList \ option | array | 否 | 可选值 | 可选的答案 |
answer \ reply \ slotCollectInfo \ waitCollectResultList \ option \ optionSize | integer | 否 | 选项数量 | 选项数量 |
answer \ reply \ slotCollectInfo \ waitCollectResultList \ option \ clarity | string | 否 | 清晰度 | 澄清话术 |
answer \ reply \ slotCollectInfo \ waitCollectResultList \ option \ inputExample | string | 是 | 输入示例 | 输入的示例 |
answer \ reply \ slotCollectInfo \ waitCollectResultList \ option \ outputExample | string | 是 | 输出示例 | 输出的示例 |
answer \ reply \ slotCollectInfo \ hasCollectResultList | array | 是 | 已收集结果列表 | 列出已收集的结果 |
instruction | object | 否 | 指令 | 发出指令后有值,指令详细信息 Map<String, Map<String, String>>, 外层key为指令标识,value为指令参数map |
variables | object | 是 | 变量 | Map<String, Object>存储会话期间的各种变量信息 |
endTimestring | 否 | 结束时间 | 会话结束时间 | |
recommendList | array | 是 | 推荐列表 | 提供可能的操作或信息的推荐列表 |
rewriteQuery | string | 是 | 重写查询 | 用户输入的重写版本 |
intent | array | 是 | 意图 | 列出此次会话涉及的意图 |
intent \ _effect | integer | 是 | 影响 | 表示此意图的效果 |
intent \ id | string | 是 | ID | 意图的唯一标识符 |
intent \ name | string | 是 | 名称 | 意图的名称 |
intent \ description | string | 否 | 描述 | 意图的描述 |
intent \ agentId | string | 否 | 代理ID | 代理的ID |
intent \ created | string | 否 | 创建时间 | 意图创建时间 |
intent \ updated | string | 否 | 更新时间 | 意图最后更新时间 |
intent \ confidence | float | 是 | 置信度 | 意图识别的置信度 |
intent \ source | string | 是 | 来源 | 意图识别的来源 |
intent \ threshold | float | 是 | 阈值 | 意图识别的阈值 |
intent \ system | boolean | 是 | 系统意图 | 是否为系统预定义的意图 |
intent \ needClarify | boolean | 是 | 需要澄清 | 是否需要澄清此意图 |
intent \ alias | string | 否 | 别名 | 意图的别名 |
intent \ nameZh | string | 是 | 中文名称 | 意图的中文名称 |
intent \ examples | array | 否 | 示例 | 意图的示例 |
intent \ hasActiveCopy | boolean | 是 | 活跃副本 | 意图是否有活跃的副本 |
intent \ templateStr | string | 否 | 模板字符串 | 意图的模板字符串 |
intent \ createdUserName | string | 否 | 创建者用户名 | 创建此意图的用户的用户名 |
intent \ createdUserId | string | 否 | 创建者用户ID | 创建此意图的用户的用户ID |
intent \ lastEditUserName | string | 否 | 最后编辑用户名 | 最后编辑此意图的用户的用户名 |
intent \ lastEditUserId | string | 否 | 最后编辑用户ID | 最后编辑此意图的用户的用户ID |
intent \ version | integer | 是 | 版本 | 意图的版本 |
intent \ industryIntent | boolean | 是 | 行业意图 | 是否为特定行业的意图 |
intent \ knnExampleStr | string | 是 | KNN示例字符串 | 用于KNN匹配的示例字符串 |
intent \ extra | object | 否 | 额外信息 | 包含额外信息的对象 |
intent \ recSource | string | 否 | 推荐来源 | 意图推荐的来源 |
intent \ noResponse | boolean | 是 | 无响应 | 是否未响应 |
请求示例
curl --location 'https://keyue.cloud.baidu.com/online/core/v5/stream/query'
--header 'token: xxxx'
--header 'Content-Type: application/json'
--data '{
"queryText": "你好",
"sessionId": "ecb95fcc-0e49-4ab0-b026-c20a8aac1585",
"multimodal": {"fileKey":"xxxx", "type":"1"},
"variables": {
"homeAddress": "chengdu"
}
}'
响应示例
{
"sessionId": "02329c01-7720-45e9-b19c-6dfacec87b81",
"queryId": "39ad613a-a089-4019-baa4-8f48558b5f63",
"replyStatus": 200,
"answer": [
{
"chunkId": 0,
"status": "done",
"topicId": "",
"blockId": "",
"nodeId": "",
"reply": null,
"instruction": null
}
],
"variables": {
"last_response": "",
"last_user_response": "我要租车",
"LLM_rewrite_query": "我要租车",
"triggered_intent": [
"我要租车",
"我要租车"
],
"last_intent": "我要租车"
},
"endTime": null,
"recommendList": [
"今天天气怎么样"
],
"rewriteQuery": "我要租车",
"intent": [
{
"_effect": 0,
"id": "946f7bf7-0d29-11ef-81bf-fa20202f5a70",
"name": "h4f4e8f6",
"description": "",
"agentId": null,
"created": null,
"updated": null,
"confidence": 1.0,
"source": "knnExactly",
"threshold": 0.0,
"system": false,
"needClarify": false,
"alias": null,
"nameZh": "我要租车",
"examples": null,
"hasActiveCopy": false,
"templateStr": null,
"createdUserName": null,
"createdUserId": null,
"lastEditUserName": null,
"lastEditUserId": null,
"version": 0,
"industryIntent": false,
"knnExampleStr": "我要租车",
"extra": null,
"recSource": null,
"noResponse": false
}
]
}
{
"sessionId": "948c2eef-02e4-46ff-b9d1-e38833b7764a",
"queryId": "f794f6fe-2483-46a6-86b8-711fd9627ff7",
"replyStatus": 200,
"answer": [{
"chunkId": 2,
"status": "done",
"topicId": "a52cce91-f235-4c6e-96b0-fdb026c0c09c",
"blockId": "xRGRfnMdWCTIxRoUtWAzqK37SuUxnur_ThW_",
"nodeId": "v8tdXcXokpltzHq5NrhXnNEEfjM0s6_wgQxy",
"reply": {
"type": 1,
"text": "这是一个文本节点",
"textList": null,
"clarifyGuide": null,
"replySource": "TEXT_REPLY",
"showDocumentSource": null,
"documents": null,
"docDebug": null,
"faqSearch": null,
"slotCollectInfo": null,
"instruction": null
},
"instruction": null
}],
"variables": {
"date": "2024-05-08",
"last_intent": "",
"week": "3",
"last_task_prompt": "这是一个文本节点",
"time": "19:28:50",
"last_response": "这是一个文本节点",
"last_user_response": ""
},
"endTime": null,
"recommendList": [],
"rewriteQuery": null,
"intent": null
}