GPT TEXT TO SQL 接口文档
作用:将自然语言转化为可执行的SQL语句, 集成GPT能力, 可多轮对话
接口信息:
初始化上下文
PATH:/df/rest/gpt/start
METHOD: POST
Header 请求参数
json
Authorization: Bearer M2M5YzY2TE0ZmEwMDNZjY1MDRkMGNkNWIuYTUxYThkZjJkGQ3NGZlZmJlZmIMGM2YTA3MDhlNg=
Body 请求参数
json
{
"language": "chinese",
"model": {
"type": "mysql",
"version": "string",
"tables": [
{
"tableDisplayName": "string",
"tableName": "string",
"columns": [
{
"columnDisplayName": null,
"columnName": null,
"dataType": null,
"aggregation": null
}
]
}
],
"relations": [
{
"conditions": [
{
"dstColName": null,
"srcColName": null
}
],
"dimensionTable": "string",
"factTable": "string",
"joinType": "LEFT JOIN"
}
]
}
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
---|---|---|---|---|
Authorization | header | string | 是 | bearer token |
body | body | object | 否 | none |
» language | body | string | 是 | 语言环境 |
» model | body | object | 是 | none |
»» type | body | string | 是 | 数据库类型 |
»» version | body | string | 是 | 数据库版本 |
»» tables | body | [object] | 是 | 表结构列表 |
»»» tableDisplayName | body | string | 否 | 表显示名 |
»»» tableName | body | string | 否 | 表原始名 |
»»» columns | body | [object] | 否 | 表列列表 |
»»»» columnDisplayName | body | string | 是 | 列显示名 |
»»»» columnName | body | string | 是 | 列原始名 |
»»»» dataType | body | string | 是 | 列数据类型 |
»»»» aggregation | body | string | 是 | 列聚合方式 |
»» relations | body | [object] | 是 | 表关联关系列表 |
»»» conditions | body | [object] | 否 | 关联条件 |
»»»» dstColName | body | string | 否 | dimension 表关联列原始名 |
»»»» srcColName | body | string | 否 | fact 表关联列原始名 |
»»» dimensionTable | body | string | 否 | dimension 表原始名 |
»»» factTable | body | string | 否 | fact 表原始名 |
»»» joinType | body | string | 否 | 关联类型 |
枚举值
language:语言环境
值 | 含义 |
---|---|
chinese | 中文 |
english | 英文 |
model.type:数据库类型
值 | 含义 |
---|---|
mysql | mysql数据库 |
clickhouse | clickhouse数据库 |
impala | impala数据库 |
dataType:列数据类型
值 | 含义 |
---|---|
boolean | 布尔类型 |
int | 整型 |
bigint | 长整型 |
double | 浮点型 |
string | 字符串 |
timestamp | 时间戳 |
date | 日期类型 |
time | 时间类型 |
aggregation:列聚合方式
值 | 含义 |
---|---|
SUM | 求和 |
AVERAGE | 平均值 |
MIN | 最小值 |
MAX | 最大值 |
COUNT | 数量 |
COUNT_DISTINCT | 去重数量 |
VARIANCE | 方差 |
STD_DEVIATION | 标准差 |
NONE | 无 |
joinType:表关联方式
值 | 含义 |
---|---|
LEFT JOIN | 左关联 |
RIGHT JOIN | 右关联 |
INNER JOIN | 内关联 |
FULL JOIN | 全关联 |
返回示例
200 Response
json
{
"errCode": 0,
"exception": "string",
"data": "string"
}
返回数据结构
状态码 200
名称 | 类型 | 必选 | 约束 | 中文名 | 说明 |
---|---|---|---|---|---|
» errCode | integer | true | none | none | |
» exception | string | true | none | none | |
» data | string | true | none | chat id:通过/df/rest/gpt/start接口获取 |
对话查询
PATH:/df/rest/gpt/chat
METHOD: POST
Header 请求参数
json
Authorization: Bearer M2M5YzY2TE0ZmEwMDNZjY1MDRkMGNkNWIuYTUxYThkZjJkGQ3NGZlZmJlZmIMGM2YTA3MDhlNg=
Body 请求参数
json
{
"input": "string",
"chatId": "string"
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
---|---|---|---|---|
Authorization | header | string | 是 | bearer token |
body | body | object | 否 | none |
» input | body | string | 是 | 用户输入的问题 |
» chatId | body | string | 是 | 对话id |
返回示例
200 Response
json
{
"errCode": 0,
"exception": "string",
"data": {
"type": "sql",
"content": "string",
"question": "string"
}
}
返回数据结构
状态码 200
名称 | 类型 | 必选 | 约束 | 说明 |
---|---|---|---|---|
» errCode | integer | true | none | none |
» exception | string | true | none | none |
» data | object | true | none | none |
»» type | string | true | none | 类型 |
»» content | string | true | none | none |
»» question | string | true | none | 解析后的问题, 当type为sql时才有值 |
枚举值
type:返回结果类型
值 | 含义 |
---|---|
sql | sql语句 |
error | 错误信息 |
assistant | gpt交互信息 |
Bearer token 获取方式如下图:

curl 样例:
json
curl -X POST "https://test-dev.datafocus.ai/df/rest/gpt/start" -H "Authorization: Bearer M2M5YzY2ZTE3ZGQzNGI0ZmEwMDNlZjY1MDRkMGNkNWIuYTUxYThkZjJkMGQ3NGZlZmJlZmI1MGM2YTA3MDhlNzg=" -H "Content-Type:application/json" -d '{"language": "chinese", "model":{"type":"mysql","version":"8.0","tables":[{"tableDisplayName":"test","tableName":"test","columns":[{"columnDisplayName":"name","columnName":"name","dataType":"string","aggregation":"","samples":["张医师","陶医师","毛医师"]},{"columnDisplayName":"address","columnName":"address","dataType":"string","aggregation":"","samples":["内科-传染病科","内科-呼吸内科","内科-神经内科"]},{"columnDisplayName":"age","columnName":"age","dataType":"int","aggregation":"SUM"},{"columnDisplayName":"date","columnName":"date","dataType":"timestamp","aggregation":"","samples":["1921-08-10 00:00:00.000","1922-10-24 00:00:00.000","1926-10-24 00:00:00.000"]}]}],"relations":[]}}'
curl 返回结果:
{
"data": "dee0f117e771412ba152581a57e4488d",
"errCode": 0,
"exception": "",
"msgParams": null,
"promptMsg": null,
"success": true
}
-------------------------
curl -X POST "https://test-dev.datafocus.ai/df/rest/gpt/chat" -H "Authorization: Bearer M2M5YzY2ZTE3ZGQzNGI0ZmEwMDNlZjY1MDRkMGNkNWIuYTUxYThkZjJkMGQ3NGZlZmJlZmI1MGM2YTA3MDhlNzg=" -H "Content-Type:application/json" -d '{"input":"去年 age","chatId":"dee0f117e771412ba152581a57e4488d"}'
curl 返回结果:
{
"data": {
"content": "select tbl_1873666373101797381.`age` as col_11709116884877520050 from `test` tbl_1873666373101797381 where (((tbl_1873666373101797381.`date` >= '2023-01-01 00:00:00.000') and (tbl_1873666373101797381.`date` < '2024-01-01 00:00:00.000')) = true) order by tbl_1873666373101797381.`age`",
"question": "去年 age的分布",
"type": "sql"
},
"errCode": 0,
"exception": "",
"msgParams": null,
"promptMsg": null,
"success": true
}
-------------------------
curl -X POST "https://test-dev.datafocus.ai/df/rest/gpt/chat" -H "Authorization: Bearer M2M5YzY2ZTE3ZGQzNGI0ZmEwMDNlZjY1ZmI1MGM2YTA3MDhlNzg=" -H "Content-Type:application/json" -d '{"input":"今年呢?","chatId":"dee0f117e771412ba152581a57e4488d"}'
curl 返回结果:
{
"data": {
"content": "select tbl_1873666373101797381.`age` as col_11709116884877520050 from `test` tbl_1873666373101797381 where (((tbl_1873666373101797381.`date` >= '2024-01-01 00:00:00.000') and (tbl_1873666373101797381.`date` < '2025-01-01 00:00:00.000')) = true) order by tbl_1873666373101797381.`age`",
"question": "今年 age的分布",
"type": "sql"
},
"errCode": 0,
"exception": "",
"msgParams": null,
"promptMsg": null,
"success": true
}