3 Star 66 Fork 25

walter / baidu-model-java-sdk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
BaiduServiceTest.java 24.46 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
package baidumodel.service;
import baidumodel.constant.ChatRoleConstant;
import baidumodel.entity.chat.*;
import baidumodel.listener.BaiduEventSourceListener;
import baidumodel.util.TokenUtil;
import cn.hutool.json.JSONUtil;
import okhttp3.OkHttpClient;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @author: wuchenxi
* @date: 2023-07-29 15:41:41
*/
public class BaiduServiceTest {
private final String apiKey = "RMj5LYyoo0hh5Nlm3DzI5oqS";
private final String secretKey = "2Uyc0O19Wjo8jbbTnubRdQjMSav9R2x3";
@Test
public void testErnieBotTurbo() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieBotTurboParam param = ErnieBotTurboParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
ErnieBotTurboResponse ernieBotTurboResponse = baiduService.ernieBotTurbo(param, token);
System.out.println(JSONUtil.toJsonStr(ernieBotTurboResponse));
}
@Test
public void testErnieBotTurboStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieBotTurboParam param = ErnieBotTurboParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.ernieBotTurboStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testBloomz7b() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Bloomz7bParam param = Bloomz7bParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
Bloomz7bResponse bloomz7bResponse = baiduService.bloomz7b(param, token);
System.out.println(JSONUtil.toJsonStr(bloomz7bResponse));
}
@Test
public void testBloomz7bStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Bloomz7bParam param = Bloomz7bParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.bloomz7bStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testErnieBot() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieBotParam param = ErnieBotParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
ErnieBotResponse ernieBotResponse = baiduService.ernieBot(param, token);
System.out.println(JSONUtil.toJsonStr(ernieBotResponse));
}
@Test
public void testErnieBotStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieBotParam param = ErnieBotParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.ernieBotStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testOkhttpClient() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
BaiduService baiduService = new BaiduService(apiKey, secretKey, client.build());
ErnieBotParam param = ErnieBotParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.ernieBotStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testErnieBot4() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieBot4Param param = ErnieBot4Param.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
ErnieBot4Response ernieBotResponse = baiduService.ernieBot4(param, token);
System.out.println(JSONUtil.toJsonStr(ernieBotResponse));
}
@Test
public void testErnieBot4Stream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieBot4Param param = ErnieBot4Param.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.ernieBot4Stream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testPromptTemplate() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Map<String, Object> map = new HashMap<>();
map.put("number", 2);
map.put("content", "如何写一个好提纲");
PromptTemplateParam param = PromptTemplateParam.builder()
.id(1966)
.values(map)
.build();
PromptTemplateResponse promptTemplateResponse = baiduService.promptTemplate(param, baiduService.getToken());
System.out.println(JSONUtil.toJsonStr(promptTemplateResponse));
}
@Test
public void testEmbeddingV1() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
EmbeddingV1Param param = EmbeddingV1Param.builder()
.input(Collections.singletonList("文本向量"))
.user_id("1")
.build();
EmbeddingV1Response embeddingV1Response = baiduService.embeddingV1(param, baiduService.getToken());
System.out.println(JSONUtil.toJsonStr(embeddingV1Response));
}
@Test
public void testVisualGLM6B() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
VisualGLM6BParam param = VisualGLM6BParam.builder()
.apiName("apiName")
.prompt("生成一张小猫的图片")
.height(500)
.width(500)
.build();
VisualGLM6BResponse visualGLM6BResponse = baiduService.visualGLM6B(param, baiduService.getToken());
System.out.println(JSONUtil.toJsonStr(visualGLM6BResponse));
}
@Test
public void testLinlyChineseLLaMA() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
LinlyChineseLLaMAParam param = LinlyChineseLLaMAParam.builder()
.apiName("apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
LinlyChineseLLaMAResponse linlyChineseLLaMAResponse = baiduService.linlyChineseLLaMA(param, token);
System.out.println(JSONUtil.toJsonStr(linlyChineseLLaMAResponse));
}
@Test
public void testLinlyChineseLLaMAStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
LinlyChineseLLaMAParam param = LinlyChineseLLaMAParam.builder()
.apiName("your apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.linlyChineseLLaMAStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testChatGLM26B() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ChatGLM26BParam param = ChatGLM26BParam.builder()
.apiName("apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
ChatGLM26BResponse chatGLM26BResponse = baiduService.chatGLM26B(param, token);
System.out.println(JSONUtil.toJsonStr(chatGLM26BResponse));
}
@Test
public void testChatGLM26BStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ChatGLM26BParam param = ChatGLM26BParam.builder()
.apiName("your apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.chatGLM26BStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testOpenLLaMA7B() {BaiduService baiduService = new BaiduService(apiKey, secretKey);
OpenLLaMA7BParam param = OpenLLaMA7BParam.builder()
.apiName("apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
OpenLLaMA7BResponse response = baiduService.openLLaMA7B(param, token);
System.out.println(JSONUtil.toJsonStr(response));
}
@Test
public void testOpenLLaMA7BStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
OpenLLaMA7BParam param = OpenLLaMA7BParam.builder()
.apiName("your apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.openLLaMA7BStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testFalcon7B() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Falcon7BParam param = Falcon7BParam.builder()
.apiName("apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
Falcon7BResponse response = baiduService.falcon7B(param, token);
System.out.println(JSONUtil.toJsonStr(response));
}
@Test
public void testFalcon7BStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Falcon7BParam param = Falcon7BParam.builder()
.apiName("your apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.falcon7BStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testDolly12B() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Dolly12BParam param = Dolly12BParam.builder()
.apiName("apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
Dolly12BResponse response = baiduService.dolly12B(param, token);
System.out.println(JSONUtil.toJsonStr(response));
}
@Test
public void testDolly12BStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Dolly12BParam param = Dolly12BParam.builder()
.apiName("your apiName")
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.dolly12BStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testLlama2_70bChat() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Llama2ChatParam param = Llama2ChatParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
LlamaChatResponse response = baiduService.Llama2_70bChat(param, token);
System.out.println(JSONUtil.toJsonStr(response));
}
@Test
public void testLlama2_7bChat() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Llama2ChatParam param = Llama2ChatParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
LlamaChatResponse response = baiduService.Llama2_7bChat(param, token);
System.out.println(JSONUtil.toJsonStr(response));
}
@Test
public void testLlama2_13bChat() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Llama2ChatParam param = Llama2ChatParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
LlamaChatResponse response = baiduService.Llama2_13bChat(param, token);
System.out.println(JSONUtil.toJsonStr(response));
}
@Test
public void testLlama2_70bChatStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Llama2ChatParam param = Llama2ChatParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.Llama2_70bChatStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testLlama2_7bChatStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Llama2ChatParam param = Llama2ChatParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.Llama2_7bChatStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testLlama2_13bChatStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
Llama2ChatParam param = Llama2ChatParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.Llama2_13bChatStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testStableDiffusionXL() throws IOException {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
StableDiffusionXLParam param = StableDiffusionXLParam.builder()
.prompt("画一只可爱的小猫咪")
.negative_prompt("背景不要白色")
.steps(null)
.size("512x512")
.n(1)
.sampler_index("Euler a")
.user_id("1")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
StableDiffusionXLResponse response = baiduService.stableDiffusionXL(param, token);
byte[] imageBytes = Base64.getDecoder().decode(response.getData().get(0).getB64_image());
Files.write(Paths.get("output.png"), imageBytes, StandardOpenOption.CREATE_NEW);
System.out.println(JSONUtil.toJsonStr(response));
}
@Test
public void testErnieSpeed128K() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieSpeed128KParam param = ErnieSpeed128KParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.max_output_tokens(1024)
.user_id("test")
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
ErnieSpeed128KResponse ernieBotTurboResponse = baiduService.ernieSpeed128K(param, token);
System.out.println(JSONUtil.toJsonStr(ernieBotTurboResponse));
}
@Test
public void testErnieSpeed128KStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieSpeed128KParam param = ErnieSpeed128KParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.max_output_tokens(1024)
.user_id("test")
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.ernieSpeed128KStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
@Test
public void testErnieSpeed8K() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieSpeed8KParam param = ErnieSpeed8KParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.max_output_tokens(1024)
.user_id("test")
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
ErnieSpeed8KResponse ernieBotTurboResponse = baiduService.ernieSpeed8K(param, token);
System.out.println(JSONUtil.toJsonStr(ernieBotTurboResponse));
}
@Test
public void testErnieSpeed8KStream() {
BaiduService baiduService = new BaiduService(apiKey, secretKey);
ErnieSpeed8KParam param = ErnieSpeed8KParam.builder()
.messages(Collections.singletonList(BaiduChatMessage.builder()
.role(ChatRoleConstant.USER)
.content("你是谁呀?")
.build()))
.temperature(0.95F)
.penalty_score(1.0F)
.top_p(0.8F)
.max_output_tokens(1024)
.user_id("test")
.system("你是一个贴心小助手")
.build();
String token = TokenUtil.getToken(apiKey, secretKey);
// 通过 BaiduEventSourceListener#onEvent 方法接收数据
baiduService.ernieSpeed8KStream(param, token, new BaiduEventSourceListener());
while (true) {
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/gitwcx/baidu-model-java-sdk.git
git@gitee.com:gitwcx/baidu-model-java-sdk.git
gitwcx
baidu-model-java-sdk
baidu-model-java-sdk
master

搜索帮助

Bbcd6f05 5694891 0cc6727d 5694891