HTTP接口调用
功能描述
您可以通过HTTP调用接口来使用析言的问答能力,目前提供HTTP SSE协议。
前提条件
已开通析言服务并获得鉴权Token。目前获取鉴权Token的方法为:在阿里云官网右上角点击,选择售后服务,通过服务工单反馈给我们。
接口调用
POST https://xiyan-stream.biz.aliyun.com/service/api/chat
入参描述
传参方式 | 字段 | 数据类型 | 必选 | 描述 | 示例 |
Header | Content-Type | string | 是 | 请求类型:application/json | "Content-Type":"application/json" |
Authorization | string | 是 | 鉴权Token,例如:Bearer d1**2a | "Authorization":"Bearer d1**2a" | |
Body | sessionId | string | 否 | 会话唯一id,同一个会话session的多轮对话请求中的sessionId需要相同 | |
requestId | string | 否 | 单次请求的唯一id | ||
query | string | 是 | 本次对话请求的用户query | "查询最近3天的调用量" | |
sqlOnly | boolean | 否 | 当只希望服务端生成SQL,不需要执行SQL时,设置为true |
出参描述
字段 | 数据类型 | 描述 | 示例 |
data.event | string | 流式返回的事件类型,包括rewrite、selector、sql、sql_data、result、reject、error | |
data.sessionId | string | 同入参里的sessionId | |
data.requestId | string | 同入参里的requestId | |
data.rewrite | string | 对用户原始query进行改写的结果 | |
data.selector | array | 模型进行选表的结果,是由被选中的表名构成的数组 | ["table1", "table2"] |
data.sql | string | 模型生成的SQL查询语句 | "SELECT column_a FROM table_t" |
data.sqlData | object | 执行SQL查询语句后得到的查询结果数据 | |
data.sqlData.column | array | 查询结果数据表格的表头列名 | ["col1", "col2"] |
data.sqlData.data | array | 查询结果数据表格的内容 | |
data.sqlData.data[x] | object | 查询结果数据表格中每一行的数据,以key-value的形式组成,key是列名,value是数据值 | { "col1": "abc", "col2": "xyz" } |
data.visualization | object | 模型生成的可视化答案展示内容 | |
data.visualization.text | string | 可视化答案中作为回答文本的内容 | "最近3天的调用量分别为100,200,300" |
data.visualization.data | object | 可视化答案中作为图表展示的内容,如果模型判断该答案不需要通过图表展示,此字段为空。 该object中包含3个字段: plot_type:表示图表类型,候选值为bar、line、pie,分别表示柱状图、折线图和饼状图。 x_axis:表示x轴的标识,标识的值来自sqlData中的column数组中的值。 y_axis:表示y轴的标识,标识的值来自sqlData中的column数组中的值。 |
|
异常返回
字段 | 数据类型 | 描述 | 示例 |
data.sqlError | string | 当执行SQL报错时,会在该字段返回报错内容 | |
data.errorMessage | string | 当请求出错时,会在该字段返回报错消息,例如超过并发等 |
请求示例
curl http://xiyan-stream.biz.aliyun.com/service/api/chat \
-H 'Content-Type:application/json' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-X POST \
-d '{
"query": "最近3天的调用量",
"sessionId": "$uuid",
"requestId": "$uuid"
}'
返回示例(SSE流式返回)
event:rewrite
data:{"data":{"event":"rewrite","requestId":"e1103b74-4a80-4643-b032-71d149462fbb","rewrite":"注资的驿站站点及其各自的总收件数分别是多少?","sessionId":"session-e1103b74-4a80-4643-b032-71d149462fbb"}}
event:selector
data:{"data":{"event":"selector","requestId":"e1103b74-4a80-4643-b032-71d149462fbb","rewrite":"注资的驿站站点及其各自的总收件数分别是多少?","selector":["station_invdaily","station_info","user_info"],"sessionId":"session-e1103b74-4a80-4643-b032-71d149462fbb"}}
event:sql
data:{"data":{"event":"sql","requestId":"e1103b74-4a80-4643-b032-71d149462fbb","rewrite":"注资的驿站站点及其各自的总收件数分别是多少?","selector":["station_invdaily","station_info","user_info"],"sessionId":"session-e1103b74-4a80-4643-b032-71d149462fbb","sql":"SELECT `T2`.`stationname`, SUM(`T1`.`received_today`) FROM `station_invdaily` AS `T1` JOIN `station_info` AS `T2` ON `T1`.`stationid` = `T2`.`stationid` WHERE `T2`.`type` = 3 GROUP BY `T2`.`stationname`"}}
event:sql_data
data:{"data":{"event":"sql_data","requestId":"e1103b74-4a80-4643-b032-71d149462fbb","rewrite":"注资的驿站站点及其各自的总收件数分别是多少?","selector":["station_invdaily","station_info","user_info"],"sessionId":"session-e1103b74-4a80-4643-b032-71d149462fbb","sql":"SELECT `T2`.`stationname`, SUM(`T1`.`received_today`) FROM `station_invdaily` AS `T1` JOIN `station_info` AS `T2` ON `T1`.`stationid` = `T2`.`stationid` WHERE `T2`.`type` = 3 GROUP BY `T2`.`stationname`","sqlData":{"column":["stationname","SUM(T1.received_today)"],"data":[{"SUM(T1.received_today)":"5699","stationname":"西溪C区菜鸟驿站"},{"SUM(T1.received_today)":"4878","stationname":"云谷菜鸟驿站"},{"SUM(T1.received_today)":"4621","stationname":"八方城菜鸟驿站"}]}}}
event:result
data:{"data":{"event":"result","requestId":"e1103b74-4a80-4643-b032-71d149462fbb","rewrite":"注资的驿站站点及其各自的总收件数分别是多少?","selector":["station_invdaily","station_info","user_info"],"sessionId":"session-e1103b74-4a80-4643-b032-71d149462fbb","sql":"SELECT `T2`.`stationname`, SUM(`T1`.`received_today`) FROM `station_invdaily` AS `T1` JOIN `station_info` AS `T2` ON `T1`.`stationid` = `T2`.`stationid` WHERE `T2`.`type` = 3 GROUP BY `T2`.`stationname`","sqlData":{"column":["stationname","SUM(T1.received_today)"],"data":[{"SUM(T1.received_today)":"5699","stationname":"西溪C区菜鸟驿站"},{"SUM(T1.received_today)":"4878","stationname":"云谷菜鸟驿站"},{"SUM(T1.received_today)":"4621","stationname":"八方城菜鸟驿站"}]},"visualization":{"data":{"plot_type":"bar","x_axis":["stationname"],"y_axis":["SUM(T1.received_today)"]},"text":"注资的驿站站点及其各自的总收件数分别是:西溪C区菜鸟驿站总收件数为5699件,云谷菜鸟驿站总收件数为4878件,八方城菜鸟驿站总收件数为4621件。"}}}