大模型对话
更新时间:2024-04-28
接口描述
该接口用于在一轮对话中向大模型发送消息。
权限说明
Authorization需要填写密钥。
接口定义
URL | /v2/app/conversation/runs |
---|---|
Method | POST |
Content-Type | application/json |
Authorization | 请求签名(此签名为应用工作台密钥) |
请求结构
POST /v2/app/conversation/runs HTTP/1.1
HOST: qianfan.baidubce.com
Authorization: authorization string
Content-Type: application/json
{
"app_id": "85036d8f-239c-469c-b342-b62ca9d696f6",
"query": "根据文件中的数据,统计这几所学校小学生有多少",
"stream": true,
"conversation_id": "355a4f4e-a6d8-4dec-b840-7075030c6d22",
"file_ids": [
"cdd1e194-cfb7-4173-a154-795fae8535d9"
]
}
请求头域
除公共头域外,无其它特殊头域。
请求参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
app_id | string | 是 | app_id,来源于个人空间-应用-应用ID。 |
query | string | 是 | 用户query文字, 长度限制2000字符。 |
stream | bool | 是 | 是否以流式接口的形式返回数据,默认false。 |
conversation_id | string | 是 | 对话id,可通过新建会话接口创建。 |
file_ids | list[string] | 否 | 如果在对话中上传了文件,可以将文件id放入该字段,目前只处理第一个文件。 |
响应头域
除公共头域外,无其它特殊头域。
响应参数
字段 | 类型 | 必然存在 | 说明 |
---|---|---|---|
request_id | string | 是 | request_id便于追踪。 |
date | string | 是 | 消息返回时间的时间戳 UTC时间格式。 |
answer | string | 是 | 文字答案。 流式场景下是增量数据。 |
conversation_id | string | 是 | 对话id,可以用于后续调用,有效期为7天。 |
message_id | string | 是 | 消息id, 流式场景下多次推流message_id保持一致。 |
is_completion | boolean | 是 | 流式消息推送回答结果是否完结。 |
content | list[dict] | 是 | 节点输出信息相关内容。具体见下文content字段定义。 |
content字段
字段 | 类型 | 必然存在 | 说明 |
---|---|---|---|
event_code | int | 是 | 事件错误码 事件状态正常。 事件状出错时, 推送非0值,只代表事件状态,不代表整次对话状态。 |
event_message | string | 是 | 事件执行错误提示信息。 |
event_type | enum | 是 | 事件类型 rag: 知识问答 function_call: function_call 工具 ChatAgent: 闲聊或者总结 Workflow: 自定义工作流组件 其它工具: TTS、GetWeather、QueryArxiv、CodeInterpreter、WebPilot、WolframAlpha。全部列表可见 组件列表 部分。 |
event_id | string | 是 | 事件id。 |
event_status | enum | 是 | 事件执行状态: preparing:运行中 running:运行中 error:错误 done:执行完成 |
content_type | enum | 是 | 事件本次输出内容的类型。 status 状态事件,无数据输出 text 文本回答 image 图片 files 文件 function_call: function_call image: 图片url地址 audio:语音url地址 video: 视频url地址 |
outputs | object | 是 | 每个事件个性化输出内容的汇总字段, 根据事件的类型不同, outputs下内容不同。 |
outputs字段
说明: 每个事件类型的outputs字段均不相同。
字段名称 | 类型 | 描述 |
---|---|---|
outputs | Object | event_type 为 rag , 结果包括以下字段: text: string 普通文本 references list[object] 包含reference对象的数组, reference格式在下面定义 event_type 为 function_call , 结果包括以下字段: text: string 普通文本。function_call 的描述json event_type 为 Workflow,结果包括以下字段: text: string 普通文本 meta:json 自定义组件的描述信息 event_type 为其它组件名,可能会出现以下多种字段 urls: 外部链接 files: 工具生成的文件链接 image:工具生成的图片url audio:工具生成的语音url video:工具生成的视频url |
-- text | string | 各个事件都可能产生文字信息 |
-- meta | json | 自定义组件信息 |
-- references | list[object] | 通用类型 Reference的列表 |
-- urls | list[string] | 工具生成的文件链接列表 |
-- files | list[string] | 工具生成的可下载文件地址列表 |
-- image | string | 工具生成的图片url |
-- audio | string | 工具生成的语音url |
-- video | string | 工具生成的视频url |
reference参考信息
定义: 问答结果中的引用参考。 如大模型的回答引用了某切片, 需要在问答结果中标注出答案的来源位置。
字段 | 类型 | 必然存在 | 说明 |
---|---|---|---|
id | String | 是 | 这个id对应content信息的id |
from | String | 是 | 信息来源 |
url | String | 否 | BaiduSearch 的专用字段 |
content | String | 是 | 文字描述 通用字段,一般用来当做文档名或者链接的title使用 |
segment_id | String | 否 | 切片id 知识问答专有字段 |
document_id | String | 否 | 文档id 知识问答专有字段 |
dataset_id | String | 否 | 数据集id 知识问答专有字段 |
document_name | String | 否 | 文档名字 知识问答专有字段 |
function_call描述json
字段 | 类型 | 必然存在 | 说明 |
---|---|---|---|
arguments | String | 是 | 参数 |
component_code | String | 是 | 组件编码 |
component_name | String | 是 | 组件名称 |
meta自定义组件描述json
字段 | 类型 | 必然存在 | 说明 |
---|---|---|---|
workflow_id | String | 是 | 组件id |
workflow_code | String | 是 | 组件编码 |
workflow_name | String | 是 | 组件名称 |
desc | String | 是 | 组件描述 |
事件类型说明
事件编码 | 事件名称 |
---|---|
TableOCR | 表格文字识别 |
HandwriteOCR | 手写文字识别 |
BaiduSearch | 百度搜索 |
QRcodeOCR | 二维码识别 |
ImageUnderstand | 图像内容理解 |
TTS | 短文本在线合成-精品音库 |
VideoGet | 热门视频 |
WeatherQuery | 天气查询 |
Arxiv | Arxiv |
CodeInterpreter | Codeinterpreter |
WebPilot | WebPilot |
WolframAlpha | WolframAlpha |
NewsGet | 头条新闻 |
ECommerceProductInquiry | 电商商品查询 |
QueryFlights | 航班动态查询 |
QueryExpressPackage | 快递查询 |
BingImageSearch | 必应图片搜索 |
Excel2Figure | Excel转图表 |
GeneralOCR | 通用文字识别-高精度版 |
Translation | 文本翻译-通用版 |
ObjectRecognition | 通用物体和场景识别-高级版 |
MixCardOCR | 身份证混贴识别 |
ASR | 短语音识别-极速版 |
Text2Image | 文生图 |
PlantRecognition | 植物识别 |
AnimalRecognition | 动物识别 |
DocCropEnhance | 文档矫正增强 |
StyleRewrite | 风格转写 |
StyleWriting | 风格写作 |
SimilarQuestion | 相似问生成 |
ZhouGongDreamInterpreter | 周公解梦大师 |
LotteryResult | 彩票开奖结果 |
BaiduNovel | 百度小说 |
SportsEventInformation | 体育赛事信息 |
HealthAssistant | 健康小助手 |
DocFormatConverter | 文档格式转换 |
WebSummary | 网页内容总结 |
请求curl示例
curl --location 'https://qianfan.baidubce.com/v2/app/conversation/runs' \
--header 'Authorization: Bearer authorization string' \
--header 'Content-Type: application/json' \
--data '{
"app_id": "85036d8f-239c-469c-b342-b62ca9d696f6",
"query": "根据文件中的数据,统计这几所学校小学生有多少",
"stream": true,
"conversation_id": "355a4f4e-a6d8-4dec-b840-7075030c6d22",
"file_ids": [
"cdd1e194-cfb7-4173-a154-795fae8535d9"
]
}'
正确响应示例
HTTP/1.1 200 OK
// 一条 function_call事件类型的流式消息
{
"request_id": "a335502e-502d-426d-9e87-ea8ad47efc8d",
"date": "2024-04-26T09:11:13Z",
"answer": "",
"conversation_id": "1fdc9182-de2d-4c56-bf64-a72d98c2b59f",
"message_id": "66c1c8c5-d04a-4376-91ff-3a7285e698f0",
"is_completion": false,
"content": [
{
"event_code": 0,
"event_message": "",
"event_type": "function_call",
"event_id": "6",
"event_status": "done",
"content_type": "function_call",
"outputs": {
"text": {
"arguments": {
"query": "对'北京小学.xlsx'文件中的'count'列数据进行求和操作",
"upload_file": "北京小学.xlsx"
},
"component_code": "CodeInterpreter",
"component_name": "代码解释器"
}
}
}
]
}
// 一条 ChatAgent 事件类型的流式消息
{
"request_id": "a335502e-502d-426d-9e87-ea8ad47efc8d",
"date": "2024-04-26T09:11:59Z",
"answer": "北京小学.xlsx'文件中的数据,这几所学校小学生的总数为:430人。",
"conversation_id": "1fdc9182-de2d-4c56-bf64-a72d98c2b59f",
"message_id": "66c1c8c5-d04a-4376-91ff-3a7285e698f0",
"is_completion": false,
"content": [
{
"event_code": 0,
"event_message": "",
"event_type": "ChatAgent",
"event_id": "13",
"event_status": "running",
"content_type": "text",
"outputs": {
"text": "北京小学.xlsx'文件中的数据,这几所学校小学生的总数为:430人。"
}
}
]
}
// 成功开启流式消息后,有出现异常需要终止流,返回的异常消息
{
"request_id": "ae2225f7-1c2e-427a-a1ad-5413b762957d",
"code": "ChatError",
"message": "流式消息发生异常"
}
错误响应示例
HTTP/1.1 401 OK
{
"request_id": "ae2225f7-1c2e-427a-a1ad-5413b762957d",
"code": "PermissionDeniedError",
"message": "没有权限"
}