demo工程(csdn上傳總是報錯461, 只好使用百度網盤了)
鏈接:https://pan.baidu.com/s/1EXbQDBMMNh1pyMIKwCmnow?pwd=7891
提取碼:7891
注冊百度智能云賬號并申請文心千帆大模型資格
https://login.bce.baidu.com/
https://cloud.baidu.com/product/wenxinworkshop
創(chuàng)建應用用于獲取access_token
創(chuàng)建應用成功后,可以獲取到API Key和Secret Key
獲取access_token
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API Key】&client_secret=【Secret Key】'
開通付費服務
api調用是按token(字數)收費的,不開通收費無法使用。
發(fā)送對話請求
curl -XPOST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=[步驟一調用接口獲取的access_token]' -d '{
"messages": [
{"role":"user","content":"介紹一下你自己"}
]
}'
Java發(fā)送http方式調用
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* api文檔 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11
*
*/
public class BaiduWenXinChat {
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().connectTimeout(5, TimeUnit.SECONDS) // 連接超時設置為20秒
.writeTimeout(15, TimeUnit.SECONDS) // 寫入超時設置為30秒
.readTimeout(20, TimeUnit.SECONDS) // 讀取超時設置為30秒
.build();
static Gson gson = new Gson();
// 歷史對話信息
Map<String, List<ChatMsg>> mapChatList = new HashMap<String, List<ChatMsg>>();
public static void main(String[] args) throws Exception {
/* 獲取acessToken:
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API Key】&client_secret=【Secret Key】'
*/
String accessToken = "24.621fe77e232121321213213213213213c6b";
String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token="
+ accessToken;
String msg = "介紹下你自己";
// 結合prompt增強的當前待發(fā)送信息
ChatMsg chatMsg = new ChatMsg();
chatMsg.setRole("user");
chatMsg.setContent(msg);
// 當前發(fā)送消息數組
List<ChatMsg> messages = new ArrayList<ChatMsg>();
messages.add(chatMsg);
String messagesJson = gson.toJson(messages);
String content = "{\"messages\":" + messagesJson + "}";
long start = System.currentTimeMillis();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, content);
System.out.println(content);
Request request = new Request.Builder().url(url).method("POST", body)
.addHeader("Content-Type", "application/json").build();
Response response = HTTP_CLIENT.newCall(request).execute();
String responseText = response.body().string();
System.out.println("response返回: \n" + responseText);
long end = System.currentTimeMillis();
System.out.println("該回答花費時間為:" + (end - start) / 1000.0 + "秒");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(responseText);
String answer = rootNode.get("result").asText();
System.out.println(answer);
}
}
class ChatMsg {
private String role;
private String content;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
demo工程(csdn上傳總是報錯461, 只好使用百度網盤了)
鏈接:https://pan.baidu.com/s/1EXbQDBMMNh1pyMIKwCmnow?pwd=7891
提取碼:7891文章來源:http://www.zghlxwxcb.cn/news/detail-707645.html
參考
https://blog.csdn.net/qq_30299877/article/details/131917097
https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11
https://cloud.baidu.com/qianfandev/topic/267178
https://blog.csdn.net/shy_snow/article/details/132759686文章來源地址http://www.zghlxwxcb.cn/news/detail-707645.html
到了這里,關于百度千帆大模型文心一言api調用的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!