SQLCoder-7B
SQLCoder-7B是由Defog研发、基于Mistral-7B微调的语言模型,用于将自然语言问题转换为SQL语句,具备优秀的生成效果。本文介绍了相关API。
功能介绍
调用本接口,发起一次文本续写请求。
在线调试
平台提供了 API在线调试平台-示例代码 ,用于帮助开发者调试接口,平台集成快速检索、查看开发文档、查看在线调用的请求内容和返回结果、复制和下载示例代码等功能,简单易用,更多内容请查看API在线调试介绍。
SDK调用
使用说明
- 本文内容适用Python、Go、Java和Node.js SDK,也可以查看续写Completions文档。
- SDK调用流程及鉴权介绍,请查看SDK安装及使用流程。
调用示例
- 指定支持预置服务的模型
使用model
字段,指定平台支持预置服务的模型,调用示例如下。
import os
import qianfan
# 使用安全认证AK/SK鉴权,通过环境变量方式初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"
comp = qianfan.Completion()
resp = comp.do(model="SQLCoder-7B", prompt="### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n")
print(resp["body"])
package main
import (
"context"
"fmt"
"os"
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
)
func main() {
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.Setenv("QIANFAN_ACCESS_KEY", "your_iam_ak")
os.Setenv("QIANFAN_SECRET_KEY", "your_iam_sk")
chat := qianfan.NewCompletion(
qianfan.WithModel("SQLCoder-7B"),
)
resp, _ := chat.Do( // Stream 启用流式返回,参数与 Do 相同
context.TODO(),
&qianfan.CompletionRequest{
Prompt: "### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n",
},
)
fmt.Println(resp.Result)
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.completion.CompletionResponse;
public class Demo {
public static void main(String[] args) {
// 使用安全认证AK/SK鉴权,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
Qianfan qianfan = new Qianfan("your_iam_ak", "your_iam_sk");
// 指定模型
CompletionResponse resp = qianfan.completion()
.model("SQLCoder-7B")
.prompt("### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n")
.execute();
System.out.print(resp.getResult());
}
}
import {Completions, setEnvVariable} from "@baiducloud/qianfan";
const client = new Completions({ QIANFAN_ACCESS_KEY: 'your_iam_ak', QIANFAN_SECRET_KEY: 'your_iam_sk' });
async function main() {
const resp = await client.completions({
prompt: '### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n',
}, 'SQLCoder-7B');
console.log(resp);
}
main();
- 用户自行发布的模型服务
对于用户快速部署自行发布的模型服务,通过使用endpoint
字段进行调用,示例如下。
import os
import qianfan
# 使用安全认证AK/SK鉴权,通过环境变量方式初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak"
os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk"
comp = qianfan.Completion()
# 使用自行发布的模型
resp = comp.do(endpoint="your_custom_endpoint", prompt="### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n")
# 可以通过resp["body"]获取接口返回的内容
print(resp["body"])
package main
import (
"context"
"fmt"
"os"
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
)
func main() {
// 使用安全认证AK/SK鉴权,通过环境变量初始化;替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
os.Setenv("QIANFAN_ACCESS_KEY", "your_iam_ak")
os.Setenv("QIANFAN_SECRET_KEY", "your_iam_sk")
chat := qianfan.NewCompletion(
qianfan.WithEndpoint("your_endpoint"),
)
resp, _ := chat.Do( // Stream 启用流式返回,参数与 Do 相同
context.TODO(),
&qianfan.CompletionRequest{
Prompt: "### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n",
},
)
fmt.Println(resp.Result)
}
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.completion.CompletionResponse;
public class Demo {
public static void main(String[] args) {
// 使用安全认证AK/SK鉴权,替换下列示例中参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
Qianfan qianfan = new Qianfan("your_iam_ak", "your_iam_sk");
// 使用自行发布的模型
CompletionResponse resp = qianfan.completion()
.endpoint("your_custom_endpoint")
.prompt("### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n")
.execute();
System.out.print(resp.getResult());
}
}
import {Completions, setEnvVariable} from "@baiducloud/qianfan";
const client = new Completions({ QIANFAN_ACCESS_KEY: 'your_iam_ak', QIANFAN_SECRET_KEY: 'your_iam_sk', Endpoint: 'your_endpoint' });
async function main() {
const resp = await client.completions({
prompt: '### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n',
});
console.log(resp);
}
main();
返回示例
{
"id": "as-1q2bqicwdp",
"object": "completion",
"created": 1711439139,
"result": "SELECT p.name, SUM(s.quantity * p.price) AS total_revenue FROM products p JOIN sales s ON p.product_id = s.product_id WHERE s.sale_date >= (CURRENT_DATE - interval '1 week') GROUP BY p.name;\n```",
"is_safe": 1,
"usage": {
"prompt_tokens": 360,
"completion_tokens": 52,
"total_tokens": 412
}
}
SELECT p.name, SUM(s.quantity * p.price) AS total_revenue FROM products p JOIN sales s ON p.product_id = s.product_id WHERE s.sale_date >= (CURRENT_DATE - interval '1 week') GROUP BY p.name;\n
SELECT p.name, SUM(s.quantity * p.price) AS total_revenue FROM products p JOIN sales s ON p.product_id = s.product_id WHERE s.sale_date >= (CURRENT_DATE - interval '1 week') GROUP BY p.name;\n
{
"id": "as-1q2bqicwdp",
"object": "completion",
"created": 1711439139,
"result": "SELECT p.name, SUM(s.quantity * p.price) AS total_revenue FROM products p JOIN sales s ON p.product_id = s.product_id WHERE s.sale_date >= (CURRENT_DATE - interval '1 week') GROUP BY p.name;\n```",
"is_safe": 1,
"usage": {
"prompt_tokens": 360,
"completion_tokens": 52,
"total_tokens": 412
}
}
请求参数
注意:以下为Python SDK参数说明,其他SDK参数相关说明请参考Go SDK-续写Completions参数相关说明、Java SDK参数相关说明、Node.js SDK参数相关说明。
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
prompt | string | 是 | 请求信息 |
model | string | 否 | 模型名称,用于指定平台支持预置服务的模型,说明: (1)如果调用预置服务,即调用本文API,该字段必填,且为固定值SQLCoder-7B (2)如果指定用户自行发布的模型服务,该字段不填写,需填写endpoint字段,详见参数endpoint说明 |
endpoint | string | 否 | 用于指定用户自行发布的模型服务。说明: (1)如果需指定用户自行发布的模型服务,如调用本文模型服务,endpoint字段为必填 (2)该字段值可以通过查看服务地址获取:打开模型服务-模型推理-我的服务页面,选择创建的服务-点击详情页查看服务地址,endpoint值为 https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/completions/ 后面的地址,如下图所示注意: 在创建服务页面,选择模型后,API地址会自动新增个后缀。例如选择某模型,输入API地址为“test1”,endpoint的取值即为“lbwxxxd4_test1”,如下图所示,如何发布服务请参考发布平台预置的模型服务 |
temperature | float | 否 | 说明: (1)较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定 (2)范围 (0, 1.0],不能为0 |
top_k | int | 否 | Top-K 采样参数,在每轮token生成时,保留k个概率最高的token作为候选。说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围:正整数 |
top_p | float | 否 | 说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围 [0, 1.0] |
penalty_score | float | 否 | 通过对已生成的token增加惩罚,减少重复生成的现象。说明: (1)值越大表示惩罚越大 (2)取值范围:[1.0, 2.0] |
stream | bool | 否 | 是否以流式接口的形式返回数据,默认false true:是,以流式接口的形式返回数据 false:否 |
retry_count | int | 否 | 重试次数,默认1次 |
request_timeout | float | 否 | 请求超时时间,默认60秒 |
backoff_factor | float | 否 | 请求重试参数,用于指定重试的策略,默认为0 |
stop | List[string] | 否 | 生成停止标识。当模型生成结果以stop中某个元素结尾时,停止文本生成。说明: (1)每个元素长度不超过20字符。 (2)最多4个元素 |
user_id | string | 否 | 表示最终用户的唯一标识符 |
返回参数
名称 | 类型 | 描述 |
---|---|---|
id | string | 本轮对话的id |
object | string | 回包类型,chat.completion:文本生成返回 |
created | int | 时间戳 |
sentence_id | int | 表示当前子句的序号。只有在流式接口模式下会返回该字段 |
is_end | bool | 表示当前子句是否是最后一句。只有在流式接口模式下会返回该字段 |
is_truncated | bool | 当前生成的结果是否被截断 |
result | string | 对话返回结果 |
need_clear_history | bool | 表示用户输入是否存在安全,是否关闭当前会话,清理历史会话信息 true:是,表示用户输入存在安全风险,建议关闭当前会话,清理历史会话信息 false:否,表示用户输入无安全风险 |
ban_round | int | 当need_clear_history为true时,此字段会告知第几轮对话有敏感信息,如果是当前问题,ban_round=-1 |
usage | usage | token统计信息 |
usage说明
名称 | 类型 | 描述 |
---|---|---|
prompt_tokens | int | 问题tokens数 |
completion_tokens | int | 回答tokens数 |
total_tokens | int | tokens总数 |
HTTP调用
鉴权说明
本文API,支持2种鉴权方式。不同鉴权方式,调用方式不同,使用Header、Query参数不同,详见本文请求说明。开发者可以选择以下任一种方式进行鉴权。
请求说明
- 基本信息
请求地址: https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/completions/sqlcoder_7b
请求方式: POST
- Header参数
根据不同鉴权方式,查看对应Header参数。
访问凭证access_token鉴权
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
Content-Type | string | 是 | 固定值application/json |
基于安全认证AK/SK进行签名计算鉴权
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
Content-Type | string | 是 | 固定值application/json |
x-bce-date | string | 否 | 当前时间,遵循ISO8601规范,格式如2016-04-06T08:23:49Z |
Authorization | string | 是 | 用于验证请求合法性的认证信息,更多内容请参考鉴权认证机制,签名工具可参考IAM签名工具 |
- Query参数
只有访问凭证access_token鉴权方式,需使用Query参数。
访问凭证access_token鉴权
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
access_token | string | 是 | 通过API Key和Secret Key获取的access_token,参考Access Token获取 |
- Body参数
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
prompt | string | 是 | 请求信息 |
stream | bool | 否 | 是否以流式接口的形式返回数据,默认false |
temperature | float | 否 | 说明: (1)较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定 (2)范围 (0, 1.0],不能为0 |
top_k | int | 否 | Top-K 采样参数,在每轮token生成时,保留k个概率最高的token作为候选。说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围:正整数 |
top_p | float | 否 | 说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围 [0, 1.0] |
penalty_score | float | 否 | 通过对已生成的token增加惩罚,减少重复生成的现象。说明: (1)值越大表示惩罚越大 (2)取值范围:[1.0, 2.0] |
stop | List(String) | 否 | 生成停止标识。当模型生成结果以stop中某个元素结尾时,停止文本生成。说明: (1)每个元素长度不超过20字符。 (2)最多4个元素 |
user_id | string | 否 | 表示最终用户的唯一标识符 |
响应说明
名称 | 类型 | 描述 |
---|---|---|
id | string | 本轮对话的id |
object | string | 回包类型,chat.completion:文本生成返回 |
created | int | 时间戳 |
sentence_id | int | 表示当前子句的序号。只有在流式接口模式下会返回该字段 |
is_end | bool | 表示当前子句是否是最后一句。只有在流式接口模式下会返回该字段 |
result | string | 对话返回结果 |
is_safe | bool | 1:表示输入内容无安全风险 0:表示输入内容有安全风险 |
usage | usage | token统计信息 |
usage说明
名称 | 类型 | 描述 |
---|---|---|
prompt_tokens | int | 问题tokens数 |
completion_tokens | int | 回答tokens数 |
total_tokens | int | tokens总数 |
请求示例
以访问凭证access_token鉴权方式为例,说明如何调用API,示例如下。
# 步骤一,获取access_token,替换下列示例中的应用API Key与应用Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]'
# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的“调用接口获取的access_token”
curl -X POST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/completions/sqlcoder_7b?access_token=[步骤一调用接口获取的access_token]' -d '{
"prompt": "### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n"
}' | iconv -f utf-8 -t utf-8
import requests
import json
def get_access_token():
"""
使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
"""
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
payload = json.dumps("")
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("access_token")
def main():
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/completions/sqlcoder_7b?access_token=" + get_access_token()
payload = json.dumps({
"prompt": "### Task\nGenerate a SQL query to answer the following question:\n`What is our total revenue by product in the last week?`\n\n### Database Schema\nThis query will run on a database whose schema is represented in this string:\nCREATE TABLE products (\n product_id INTEGER PRIMARY KEY, -- Unique ID for each product\n name VARCHAR(50), -- Name of the product\n price DECIMAL(10,2), -- Price of each unit of the product\n quantity INTEGER -- Current quantity in stock\n);\n\nCREATE TABLE customers (\n customer_id INTEGER PRIMARY KEY, -- Unique ID for each customer\n name VARCHAR(50), -- Name of the customer\n address VARCHAR(100) -- Mailing address of the customer\n);\n\nCREATE TABLE salespeople (\n salesperson_id INTEGER PRIMARY KEY, -- Unique ID for each salesperson\n name VARCHAR(50), -- Name of the salesperson\n region VARCHAR(50) -- Geographic sales region\n);\n\nCREATE TABLE sales (\n sale_id INTEGER PRIMARY KEY, -- Unique ID for each sale\n product_id INTEGER, -- ID of product sold\n customer_id INTEGER, -- ID of customer who made purchase\n salesperson_id INTEGER, -- ID of salesperson who made the sale\n sale_date DATE, -- Date the sale occurred\n quantity INTEGER -- Quantity of product sold\n);\n\nCREATE TABLE product_suppliers (\n supplier_id INTEGER PRIMARY KEY, -- Unique ID for each supplier\n product_id INTEGER, -- Product ID supplied\n supply_price DECIMAL(10,2) -- Unit price charged by supplier\n);\n\n-- sales.product_id can be joined with products.product_id\n-- sales.customer_id can be joined with customers.customer_id\n-- sales.salesperson_id can be joined with salespeople.salesperson_id\n-- product_suppliers.product_id can be joined with products.product_id\n\n### SQL\nGiven the database schema, here is the SQL query that answers `What is our total revenue by product in the last week?`:\n```sql\n"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
if __name__ == '__main__':
main()
响应示例
{
"id": "as-1q2bqicwdp",
"object": "chat.completion",
"created": 1711439139,
"result": "SELECT p.name, SUM(s.quantity * p.price) AS total_revenue FROM products p JOIN sales s ON p.product_id = s.product_id WHERE s.sale_date >= (CURRENT_DATE - interval '1 week') GROUP BY p.name;\n```",
"is_safe": 1,
"usage": {
"prompt_tokens": 360,
"completion_tokens": 52,
"total_tokens": 412
}
}
错误码
如果请求错误,服务器返回的JSON文本包含以下参数。
名称 | 描述 |
---|---|
error_code | 错误码 |
error_msg | 错误描述信息,帮助理解和解决发生的错误 |
例如Access Token失效返回以下内容,需要重新获取新的Access Token再次请求。
{
"error_code": 110,
"error_msg": "Access token invalid or no longer valid"
}
更多相关错误码,请查看错误码说明。