ERNIE Speed-AppBuilder
-
The model version is ERNIE-Speed-AppBuilder-8K-0322
Create chat
matters needing attention
-
This API supports two types Authentication method 。 Different authentication methods have different calling methods, and different header and query parameters are used. See the request description in this article for details. Developers can choose one of the following methods for authentication.
Online debugging
Request Description
essential information
Header parameter
-
Access credential access_token authentication
|
|
|
|
---|---|---|---|
|
|
|
|
-
Signature calculation authentication based on security authentication AK/SK
|
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Query parameter
-
Access credential access_token authentication
|
|
|
|
---|---|---|---|
|
|
|
|
Body parameter
|
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---|---|---|
|
|
|
|
|
|
Response description
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
|
-
In synchronous mode, the response parameter is the complete json package of the above fields. -
In streaming mode, the response parameter of each field is data: {Response parameter}.
Example
Request example (single round)
#Step 1, obtain access_token and replace the API Key and Secret Key in the following example
curl ' https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= [API Key]&client_secret=[Secret Key]'
#Step 2, call the API of this article, and use the access_token obtained in step 1 to replace the "access_token obtained by calling the interface" in the following example“
curl -XPOST https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ai_apaas?access_token = [ Step 1 Call the access_token obtained from the interface ] -d '{ "messages": [ {"role": "user", "content": "Hello"} ] }' | iconv -f utf-8 -t utf-8
import requests import json def get_access_token ( ) :
""" Use API Key, Secret Key to obtain access_token, and replace the application API Key and application Secret Key in the following example """ url = " https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= [Application API Key]&client_secret=[Application 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/chat/ai_apaas?access_token= " + get_access_token ( ) payload = json . dumps ( {
"messages" : [
{
"role" : "user" ,
"content" : "Hello"
}
]
} ) headers = {
'Content-Type' : 'application/json'
} response = requests . request ( "POST" , url , headers = headers , data = payload )
print ( response . text )
if __name__ == '__main__' : main ( )
Response example (single round)
HTTP/ one point one two hundred OK Date : Thu , twenty-three Mar two thousand and twenty-three 03 : twelve : 03 GMT Content-Type : application/json; charset=utf -8 Statement : AI-generated {
"id" : "as-vfqpucnn5t" ,
"object" : "chat.completion" ,
"created" : one billion seven hundred and nine million seven hundred and twelve thousand nine hundred and eighty ,
"result" : "Hello, do you have any questions that I can help you answer?" ,
"is_truncated" : false ,
"need_clear_history" : false ,
"usage" : {
"prompt_tokens" : one ,
"completion_tokens" : ten ,
"total_tokens" : eleven
}
}
Request example (multiple rounds)
#Step 1, obtain access_token and replace the API Key and Secret Key in the following example
curl ' https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= [API Key]&client_secret=[Secret Key]'
#Step 2, call the API of this article, and use the access_token obtained in step 1 to replace the "access_token obtained by calling the interface" in the following example“
curl -XPOST ' https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ai_apaas?access_token= [Step 1 Call the access_token obtained from the interface] ' -d '{ "messages": [ {"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hello, what questions do you need my help to answer?"}, {"role": "user", "content": "Where can I go on weekends when I am in Shanghai?"}, {"role": "assistant", "content": "Shanghai is a city full of vitality and cultural atmosphere. There are many places suitable for weekend travel. Here are some places worth recommending: \n \n 1. Shanghai Science and Technology Museum: Shanghai Science and Technology Museum is one of the largest science and technology museums in mainland China, with many interactive exhibitions and scientific laboratories, which can let you know the development history and latest achievements of science and technology. \n 2. Shanghai Museum: Shanghai Museum is a museum integrating art, history and culture. It has many exquisite works of art and cultural relics, which can give you an in-depth understanding of the cultural history of China and the world. \n 3. The Bund: The Bund is one of the landmarks of Shanghai, with many high-rise buildings and bustling commercial blocks. It is a good place to enjoy the beautiful scenery of the Huangpu River and the city. \n 4. Shanghai Disneyland: Shanghai Disneyland is one of the largest Disneyland theme parks in the world. There are many wonderful amusement projects and performances that can let you enjoy entertainment and joy. \n 5. Shanghai Wildlife Park: Shanghai Wildlife Park is a wildlife park with the theme of nature conservation. It has many rare animals and plants, which can let you get close to nature. \n \n These places are all good places to visit on weekends in Shanghai, and you can choose appropriate itineraries according to your interests and needs. "}, {"role": "user", "content": "Introduction to Beijing"} ] }' | iconv -f utf-8 -t utf-8
import requests import json def get_access_token ( ) :
""" Use API Key, Secret Key to obtain access_token, and replace the application API Key and application Secret Key in the following example """ url = " https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= [Application API Key]&client_secret=[Application 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/chat/ai_apaas?access_token= " + get_access_token ( ) payload = json . dumps ( {
"messages" : [
{
"role" : "user" ,
"content" : "Hello"
} ,
{
"role" : "assistant" ,
"content" : "Hello, do you have any questions that I can help you answer?"
} ,
{
"role" : "user" ,
"content" : "Where can I go on weekends when I'm in Shanghai?"
} ,
{
"role" : "assistant" ,
"content" : "Shanghai is a city full of vitality and cultural atmosphere. There are many places suitable for weekend travel. Here are some places worth recommending: n n1 Shanghai Science and Technology Museum: Shanghai Science and Technology Museum is one of the largest science and technology museums in mainland China. It has many interactive exhibitions and scientific laboratories, which can let you know the development history and latest achievements of science and technology. \N2. Shanghai Museum: The Shanghai Museum is a museum integrating art, history and culture. It has many exquisite works of art and cultural relics, which can give you an in-depth understanding of the cultural history of China and the world. \N3. The Bund: The Bund is one of the landmarks of Shanghai, with many high-rise buildings and bustling commercial blocks. It is a good place to enjoy the beautiful scenery of the Huangpu River and the city. \N4. Shanghai Disneyland: Shanghai Disneyland is one of the largest Disneyland theme parks in the world. There are many wonderful amusement projects and performances that can let you enjoy entertainment and joy. \N5. Shanghai Wildlife Park: Shanghai Wildlife Park is a wildlife park with the theme of nature protection. It has many rare animals and plants, which can let you get close to nature. \N n These places are all good places to visit on weekends in Shanghai. You can choose the appropriate itinerary according to your interests and needs. "
} ,
{
"role" : "user" ,
"content" : "Introduction to Beijing"
}
]
} ) headers = {
'Content-Type' : 'application/json'
} response = requests . request ( "POST" , url , headers = headers , data = payload )
print ( response . text )
if __name__ == '__main__' : main ( )
Response example (multiple rounds)
HTTP/ one point one two hundred OK Date : Mon , twelve Apr two thousand and twenty-one 06 : twenty-seven : fifty-five GMT Content-Type : application/json; charset=utf -8 Statement : AI-generated {
"id" : "as-yp9zh3c76n" ,
"object" : "chat.completion" ,
"created" : one billion seven hundred and nine million seven hundred and thirteen thousand one hundred and three ,
"result" : "Beijing is the capital of the People's Republic of China, located in the north of the North China Plain, and the center of China's political, cultural and international exchanges. Beijing has 16 municipal districts, with a total area of about 16410 square kilometers and a permanent population of about 21.5 million. n nBeijing is an ancient capital of China, with rich historical and cultural heritage. Historically, as the capital of many dynasties, it has many historical sites and cultural attractions. At the same time, Beijing is also one of the economic centers of China, with numerous high-tech enterprises and financial institutions, which is the economic core of China. \N n Mandarin is the main language in Beijing, but there are also many dialects and minority languages. Beijing cuisine is mainly northern cuisine, and there are also many restaurants with international cuisine. The traffic in Beijing is mainly subway and bus, and there are also highways and railway networks to facilitate the traffic with surrounding cities. " ,
"is_truncated" : false ,
"need_clear_history" : false ,
"usage" : {
"prompt_tokens" : two hundred and forty ,
"completion_tokens" : one hundred and fifty-one ,
"total_tokens" : three hundred and ninety-one
}
}
Request example (streaming)
#Step 1, obtain access_token and replace the API Key and Secret Key in the following example
curl ' https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= [API Key]&client_secret=[Secret Key]'
#Step 2, call the API of this article, and use the access_token obtained in step 1 to replace the "access_token obtained by calling the interface" in the following example“
curl -XPOST ' https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ai_apaas?access_token= [Step 1 Call the access_token obtained from the interface] ' -d '{ "messages": [ {"role": "user", "content": "recommend me some self driving routes"} ], "stream": true }'
import requests import json def get_access_token ( ) :
""" Use API Key, Secret Key to obtain access_token, and replace the application API Key and application Secret Key in the following example """ url = " https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= [Application API Key]&client_secret=[Application 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/chat/ai_apaas?access_token= " + get_access_token ( ) payload = json . dumps ( {
"messages" : [
{
"role" : "user" ,
"content" : "Recommend me some self driving routes"
}
] ,
"stream" : True
} ) headers = {
'Content-Type' : 'application/json'
} response = requests . request ( "POST" , url , headers = headers , data = payload , stream = True )
for line in response . iter_lines ( ) :
print ( line . decode ( "UTF-8" ) )
if __name__ == '__main__' : main ( )
Response example (streaming)
HTTP/1.1 200 OK Date: Mon, 12 Apr 2021 06:27:55 GMT Content-Type: text/event-stream; charset=utf-8 Cache-Control: no-cache Statement: AI-generated Data: {"id": "as-ywwpgx4dt7", "object": "chat. completion", "created": 1680166793, "sentence_id": 0, "is_end": false, "is_trunned": false, "result": "Here are some recommended routes for self driving tours: n n1.", "need_clear_history": false, "usage": {"prompt_tokens": 11, "completion_tokens": 16, "total _tokens": 27}} Data: {"id": "as-ywwpgx4dt7", "object": "chat. completion", "created": 1680166794, "sentence_id": 1, "is_end": false, "is_trunced": false, "result": "The most beautiful landscape avenue in mainland China: the Sichuan Tibet line, starting from Chengdu, along the Sichuan Tibet highway all the way west, passing through Daocheng Yading, Litang, Batang, Mangkang and other beautiful scenery, and finally arriving at Lhasa.", "need_clear_history" :false,"usage":{"prompt_tokens":11,"completion_tokens":50,"total_tokens":77}} data: {"id":"as-ywwpgx4dt7","object":"chat.completion","created":1680166795,"sentence_id":2,"is_end":false,"is_truncated":false,"result":"\n2. Silk Road: This is a road that runs through the east and west of China, starting from Shanghai, passing through Xi'an, Lanzhou, Urumqi and other cities, and finally reaching Kashgar. ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":43,"total_tokens":120}} Data: {"id": "as-ywwpgx4dt7", "object": "chat. completion", "created": 1680166796, "sentence_id": 3, "is_end": false, "is_trunced": false, "result": "You can enjoy the different scenery of northern and southern China along the way. n3 Northwest Ring Road: Starting from Chengdu, it passes through Dujiangyan, Qingcheng Mountain, Danba, Tagong Grassland, Xindu Bridge and other scenic spots, and finally arrives at Xining. ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":56,"total_tokens":176}} Data: {"id": "as-ywwpgx4dt7", "object": "chat. completion", "created": 1680166799, "sentence_id": 4, "is_end": false, "is_trunced": false, "result": "This route can enjoy the magnificent scenery of northwest China. n4 Northeast Ring Road: It starts from Shenyang, passes Changchun, Jilin, Yanji and other cities, and finally reaches Harbin. ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":48,"total_tokens":224}} Data: {"id": "as-ywwpgx4dt7", "object": "chat. completion", "created": 1680166800, "sentence_id": 5, "is_end": false, "is_truncated": false, "result": "You can enjoy the vast grasslands and forests in Northeast China along the way. n5 Southwest Ring Road: Starting from Chengdu, it passes Ya'an, Kangding, Litang, Daocheng Yading and other scenic spots, and finally arrives at Shangri La. ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":54,"total_tokens":278}} Data: {"id": "as-ywwpgx4dt7", "object": "chat. completion", "created": 1680166802, "sentence_id": 6, "is_end": false, "is_truncated": false, "result": "This route can appreciate the high mountains, canyons and ethnic cultures in southwest China. n6 Hainan Ring Road: starting from Sanya, passing Haikou, Wenchang, Wanning and other cities, and finally arriving at Sanya. ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":52,"total_tokens":330}} Data: {"id": "as-ywwpgx4dt7", "object": "chat. completion", "created": 1680166803, "sentence_id": 7, "is_end": false, "is_truncated": false, "result": "This route can enjoy the coastline and tropical rainforests in southern China. n7 Yunnan Tibet line: starting from Kunming, passing Dali, Lijiang, Shangri La and other scenic spots, and finally arriving at Lhasa, Tibet. ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":52,"total_tokens":382}} Data: {"id": "as-ywwpgx4dt7", "object": "chat. completion", "created": 1680166804, "sentence_id": 8, "is_end": true, "is_trunned": false, "result": "This route can appreciate the natural scenery and cultural landscape of southwest China and Tibet.", "need_clear_history": false, "usage": {"prompt_tokens": 11, "completion_tokens": 30, "total_tokens": 412}}
Error code
|
|
---|---|
|
|
|
|
{ "error_code": 110, "error_msg": "Access token invalid or no longer valid" }