From f06b55c3a07175d3956b85c8c6a991c45aa945e7 Mon Sep 17 00:00:00 2001 From: guoyiying <845840011@qq.com> Date: Tue, 15 Oct 2024 17:17:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 6 + .idea/dictionaries | 6 + .idea/encodings.xml | 7 + .idea/misc.xml | 16 ++ .../1371a282431555be400a63c5fee79c61bafb1ede | 4 + .../20550a4519469f3a2793d71d6bed550ad3656077 | 0 .../23d0cc0324fa072ab34e37f664df89e409da8705 | 3 + .../43f58b3cf3c113838eb40f9fc9c0edc56c5ce3c2 | 8 + .../442292b8a7efeabbe4cc176709b833b1792140ec | 0 .../49b2c5cc830b5396d25cf459acdc664db5ea259f | 0 .../504022ed192a0dca120d8b28d6fdb8fb081ed0cd | 0 .../80065a9ed7ad9a18bdea4a3959ae77102039a220 | 3 + .../8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d | 0 .../a6b382f8a9c04dede805112fe69360503af1bd22 | 0 .../b3a800729fe37c9b51785af6a5b06c5a7a664303 | 7 + .../caf16d4635f38e5850291ca82ae50c9d36596102 | 5 + .../eed25e69ed20d4c064c8126684dd827de075171b | 2 + .idea/sonarlint/issuestore/index.pb | 27 ++ .idea/vcs.xml | 6 + curl.sh | 15 + docs/curl/curl.sh | 2 +- .../java/cn/bugstack/chatglm/model/Model.java | 1 + .../cn/bugstack/chatglm/test/ApiTest.java | 259 ++---------------- 23 files changed, 143 insertions(+), 234 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/dictionaries create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/sonarlint/issuestore/1/3/1371a282431555be400a63c5fee79c61bafb1ede create mode 100644 .idea/sonarlint/issuestore/2/0/20550a4519469f3a2793d71d6bed550ad3656077 create mode 100644 .idea/sonarlint/issuestore/2/3/23d0cc0324fa072ab34e37f664df89e409da8705 create mode 100644 .idea/sonarlint/issuestore/4/3/43f58b3cf3c113838eb40f9fc9c0edc56c5ce3c2 create mode 100644 .idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec create mode 100644 .idea/sonarlint/issuestore/4/9/49b2c5cc830b5396d25cf459acdc664db5ea259f create mode 100644 .idea/sonarlint/issuestore/5/0/504022ed192a0dca120d8b28d6fdb8fb081ed0cd create mode 100644 .idea/sonarlint/issuestore/8/0/80065a9ed7ad9a18bdea4a3959ae77102039a220 create mode 100644 .idea/sonarlint/issuestore/8/e/8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d create mode 100644 .idea/sonarlint/issuestore/a/6/a6b382f8a9c04dede805112fe69360503af1bd22 create mode 100644 .idea/sonarlint/issuestore/b/3/b3a800729fe37c9b51785af6a5b06c5a7a664303 create mode 100644 .idea/sonarlint/issuestore/c/a/caf16d4635f38e5850291ca82ae50c9d36596102 create mode 100644 .idea/sonarlint/issuestore/e/e/eed25e69ed20d4c064c8126684dd827de075171b create mode 100644 .idea/sonarlint/issuestore/index.pb create mode 100644 .idea/vcs.xml create mode 100644 curl.sh diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..8bf4d45 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,6 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/dictionaries b/.idea/dictionaries new file mode 100644 index 0000000..7b85999 --- /dev/null +++ b/.idea/dictionaries @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e88ef54 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/1/3/1371a282431555be400a63c5fee79c61bafb1ede b/.idea/sonarlint/issuestore/1/3/1371a282431555be400a63c5fee79c61bafb1ede new file mode 100644 index 0000000..c51b852 --- /dev/null +++ b/.idea/sonarlint/issuestore/1/3/1371a282431555be400a63c5fee79c61bafb1ede @@ -0,0 +1,4 @@ + +` java:S112S"FDefine and throw a dedicated exception instead of using a generic one.(҂ +L +java:S1874"6Remove this use of "CHATGLM_6B_SSE"; it is deprecated.(濍 \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/2/0/20550a4519469f3a2793d71d6bed550ad3656077 b/.idea/sonarlint/issuestore/2/0/20550a4519469f3a2793d71d6bed550ad3656077 new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/2/3/23d0cc0324fa072ab34e37f664df89e409da8705 b/.idea/sonarlint/issuestore/2/3/23d0cc0324fa072ab34e37f664df89e409da8705 new file mode 100644 index 0000000..f499e77 --- /dev/null +++ b/.idea/sonarlint/issuestore/2/3/23d0cc0324fa072ab34e37f664df89e409da8705 @@ -0,0 +1,3 @@ + + +java:S1192"Define a constant instead of duplicating this literal "适用于对知识量、推理能力、创造力要求较高的场景" 3 times.(dz \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/4/3/43f58b3cf3c113838eb40f9fc9c0edc56c5ce3c2 b/.idea/sonarlint/issuestore/4/3/43f58b3cf3c113838eb40f9fc9c0edc56c5ce3c2 new file mode 100644 index 0000000..04554c0 --- /dev/null +++ b/.idea/sonarlint/issuestore/4/3/43f58b3cf3c113838eb40f9fc9c0edc56c5ce3c2 @@ -0,0 +1,8 @@ + +l +java:S1104"QMake cache a static final constant or non-public and provide accessors if needed.( +F +java:S1444"+Make this "public static cache" field final( +t java:S115"ZRename this constant name to match the regular expression '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.( +P +java:S1118":Add a private constructor to hide the implicit public one.(ĵ \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec b/.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/4/9/49b2c5cc830b5396d25cf459acdc664db5ea259f b/.idea/sonarlint/issuestore/4/9/49b2c5cc830b5396d25cf459acdc664db5ea259f new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/5/0/504022ed192a0dca120d8b28d6fdb8fb081ed0cd b/.idea/sonarlint/issuestore/5/0/504022ed192a0dca120d8b28d6fdb8fb081ed0cd new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/8/0/80065a9ed7ad9a18bdea4a3959ae77102039a220 b/.idea/sonarlint/issuestore/8/0/80065a9ed7ad9a18bdea4a3959ae77102039a220 new file mode 100644 index 0000000..8f98439 --- /dev/null +++ b/.idea/sonarlint/issuestore/8/0/80065a9ed7ad9a18bdea4a3959ae77102039a220 @@ -0,0 +1,3 @@ + +[ java:S112""FDefine and throw a dedicated exception instead of using a generic one.( +Q java:S125""_Replace the synchronized class "StringBuffer" by an unsynchronized one such as "StringBuilder".( +V +java:S1128"@Remove this unused import 'java.util.concurrent.CountDownLatch'.( \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/e/e/eed25e69ed20d4c064c8126684dd827de075171b b/.idea/sonarlint/issuestore/e/e/eed25e69ed20d4c064c8126684dd827de075171b new file mode 100644 index 0000000..30f9227 --- /dev/null +++ b/.idea/sonarlint/issuestore/e/e/eed25e69ed20d4c064c8126684dd827de075171b @@ -0,0 +1,2 @@ + +t java:S115"ZRename this constant name to match the regular expression '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.( \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 0000000..2ce6794 --- /dev/null +++ b/.idea/sonarlint/issuestore/index.pb @@ -0,0 +1,27 @@ + +9 + README.md,8/e/8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d +7 +curl.sh,4/9/49b2c5cc830b5396d25cf459acdc664db5ea259f +a +1src/main/java/cn/bugstack/chatglm/IOpenAiApi.java,e/e/eed25e69ed20d4c064c8126684dd827de075171b +m +=src/main/java/cn/bugstack/chatglm/utils/BearerTokenUtils.java,4/3/43f58b3cf3c113838eb40f9fc9c0edc56c5ce3c2 +7 +pom.xml,4/4/442292b8a7efeabbe4cc176709b833b1792140ec +l + + + + + + \ No newline at end of file diff --git a/curl.sh b/curl.sh new file mode 100644 index 0000000..47ec327 --- /dev/null +++ b/curl.sh @@ -0,0 +1,15 @@ +curl -X POST \ + -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsInNpZ25fdHlwZSI6IlNJR04ifQ.eyJhcGlfa2V5IjoiNmJiNzIzOGQwMWM1MmE0Y2UzMDA1MTMzODExNjhjNGUiLCJleHAiOjE3MjU1OTk2NjUyNjAsInRpbWVzdGFtcCI6MTcyNTU5Nzg2NTI2MH0._3QMXjRy99JB-1lqfMViw_32JsopECius3UtiwL29Lk" \ + -H "Content-Type: application/json" \ + -H "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)" \ + -d '{ + "model":"glm-4-flash", + "stream": "true", + "messages": [ + { + "role": "user", + "content": "写个java冒泡排序" + } + ] + }' \ + https://open.bigmodel.cn/api/paas/v4/chat/completions \ No newline at end of file diff --git a/docs/curl/curl.sh b/docs/curl/curl.sh index 948905b..72b74b8 100644 --- a/docs/curl/curl.sh +++ b/docs/curl/curl.sh @@ -1,5 +1,5 @@ curl -X POST \ - -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsInNpZ25fdHlwZSI6IlNJR04ifQ.eyJhcGlfa2V5IjoiNzdkNDg2YTMyOGQ1ZGRlNTBiOTE0Y2UxMDJlNzFkNGYiLCJleHAiOjE2OTczNTU1NTgwMTQsInRpbWVzdGFtcCI6MTY5NzM1Mzc1ODAxNH0.KNOXxlwULWcvNZ9CvdxGLgYlf4pDNA0az2FoFV7DX7Q" \ + -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsInNpZ25fdHlwZSI6IlNJR04ifQ.eyJhcGlfa2V5IjoiNmJiNzIzOGQwMWM1MmE0Y2UzMDA1MTMzODExNjhjNGUiLCJleHAiOjE3MDEwNjY5NjIxNzgsInRpbWVzdGFtcCI6MTcwMTA2NTE2MjE3OH0.wuefgqSSw5P6kvuOhlqvOFaejw2j1hkPjAAs1QHL6ao" \ -H "Content-Type: application/json" \ -H "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)" \ -H "Accept: text/event-stream" \ diff --git a/src/main/java/cn/bugstack/chatglm/model/Model.java b/src/main/java/cn/bugstack/chatglm/model/Model.java index 7df64e3..476d615 100644 --- a/src/main/java/cn/bugstack/chatglm/model/Model.java +++ b/src/main/java/cn/bugstack/chatglm/model/Model.java @@ -28,6 +28,7 @@ public enum Model { /** 智谱AI 24年01月发布 */ GLM_3_5_TURBO("glm-3-turbo","适用于对知识量、推理能力、创造力要求较高的场景"), GLM_4("glm-4","适用于复杂的对话交互和深度内容创作设计的场景"), + GLM_4_FLASH("glm-4-flash","免费开放的大语言模型"), GLM_4V("glm-4v","根据输入的自然语言指令和图像信息完成任务,推荐使用 SSE 或同步调用方式请求接口"), COGVIEW_3("cogview-3","根据用户的文字描述生成图像,使用同步调用方式请求接口"), ; diff --git a/src/test/java/cn/bugstack/chatglm/test/ApiTest.java b/src/test/java/cn/bugstack/chatglm/test/ApiTest.java index 7415afd..dd30389 100644 --- a/src/test/java/cn/bugstack/chatglm/test/ApiTest.java +++ b/src/test/java/cn/bugstack/chatglm/test/ApiTest.java @@ -9,8 +9,6 @@ import cn.bugstack.chatglm.utils.BearerTokenUtils; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import lombok.extern.slf4j.Slf4j; -import okhttp3.Response; -import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.sse.EventSource; import okhttp3.sse.EventSourceListener; import org.apache.commons.lang3.StringUtils; @@ -18,7 +16,6 @@ import org.jetbrains.annotations.Nullable; import org.junit.Before; import org.junit.Test; -import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; @@ -27,7 +24,7 @@ import java.util.concurrent.ExecutionException; /** * @author 小傅哥,微信:fustack * @description 在官网申请 ApiSecretKey ApiSecretKey - * @github chatglm-sdk-java + * @github https://github.com/fuzhengwei * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! */ @Slf4j @@ -40,8 +37,7 @@ public class ApiTest { // 1. 配置文件 Configuration configuration = new Configuration(); configuration.setApiHost("https://open.bigmodel.cn/"); - configuration.setApiSecretKey("39580e34e175019c230fdd519817b381.*****"); - configuration.setLevel(HttpLoggingInterceptor.Level.BODY); + configuration.setApiSecretKey("6bb7238d01c52a4ce300513381168c4e.6eVqwo1tsctUgyzl"); // 2. 会话工厂 OpenAiSessionFactory factory = new DefaultOpenAiSessionFactory(configuration); // 3. 开启会话 @@ -49,206 +45,69 @@ public class ApiTest { } /** - * 流式对话; - * 1. 默认 isCompatible = true 会兼容新旧版数据格式 - * 2. GLM_3_5_TURBO、GLM_4 支持联网等插件 + * 流式对话 */ @Test public void test_completions() throws Exception { - CountDownLatch countDownLatch = new CountDownLatch(1); - // 入参;模型、请求信息 ChatCompletionRequest request = new ChatCompletionRequest(); - request.setModel(Model.GLM_3_5_TURBO); // chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro + request.setModel(Model.CHATGLM_TURBO); // chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro request.setIncremental(false); - request.setIsCompatible(true); // 是否对返回结果数据做兼容,24年1月发布的 GLM_3_5_TURBO、GLM_4 模型,与之前的模型在返回结果上有差异。开启 true 可以做兼容。 - // 24年1月发布的 glm-3-turbo、glm-4 支持函数、知识库、联网功能 - request.setTools(new ArrayList() { - private static final long serialVersionUID = -7988151926241837899L; - - { - add(ChatCompletionRequest.Tool.builder() - .type(ChatCompletionRequest.Tool.Type.web_search) - .webSearch(ChatCompletionRequest.Tool.WebSearch.builder().enable(true).searchQuery("小傅哥").build()) - .build()); - } - }); request.setPrompt(new ArrayList() { private static final long serialVersionUID = -7988151926241837899L; { add(ChatCompletionRequest.Prompt.builder() .role(Role.user.getCode()) - .content("小傅哥的是谁") + .content("1+2") .build()); - } - }); - - // 请求 - openAiSession.completions(request, new EventSourceListener() { - @Override - public void onEvent(EventSource eventSource, @Nullable String id, @Nullable String type, String data) { - ChatCompletionResponse response = JSON.parseObject(data, ChatCompletionResponse.class); - log.info("测试结果 onEvent:{}", response.getData()); - // type 消息类型,add 增量,finish 结束,error 错误,interrupted 中断 - if (EventType.finish.getCode().equals(type)) { - ChatCompletionResponse.Meta meta = JSON.parseObject(response.getMeta(), ChatCompletionResponse.Meta.class); - log.info("[输出结束] Tokens {}", JSON.toJSONString(meta)); - } - } - - @Override - public void onClosed(EventSource eventSource) { - log.info("对话完成"); - countDownLatch.countDown(); - } - - @Override - public void onFailure(EventSource eventSource, @Nullable Throwable t, @Nullable Response response) { - log.info("对话异常"); - countDownLatch.countDown(); - } - }); - // 等待 - countDownLatch.await(); - } - - /** - * 流式对话; - * 1. 与 test_completions 测试类相比,只是设置 isCompatible = false 这样就是使用了新的数据结构。onEvent 处理接收数据有差异 - * 2. 不兼容旧版格式的话,仅支持 GLM_3_5_TURBO、GLM_4 其他模型会有解析错误 - */ - @Test - public void test_completions_new() throws Exception { - CountDownLatch countDownLatch = new CountDownLatch(1); - - // 入参;模型、请求信息 - ChatCompletionRequest request = new ChatCompletionRequest(); - request.setModel(Model.GLM_4V); // GLM_3_5_TURBO、GLM_4 - request.setIsCompatible(false); - // 24年1月发布的 glm-3-turbo、glm-4 支持函数、知识库、联网功能 - request.setTools(new ArrayList() { - private static final long serialVersionUID = -7988151926241837899L; - - { - add(ChatCompletionRequest.Tool.builder() - .type(ChatCompletionRequest.Tool.Type.web_search) - .webSearch(ChatCompletionRequest.Tool.WebSearch.builder().enable(true).searchQuery("小傅哥").build()) + /*add(ChatCompletionRequest.Prompt.builder() + .role(Role.user.getCode()) + .content("Okay") .build()); - } - }); - request.setMessages(new ArrayList() { - private static final long serialVersionUID = -7988151926241837899L; - { + *//* system 和 user 为一组出现。如果有参数类型为 system 则 system + user 一组一起传递。*//* add(ChatCompletionRequest.Prompt.builder() - .role(Role.user.getCode()) - .content("小傅哥的是谁") + .role(Role.system.getCode()) + .content("1+1=2") .build()); - } - }); - // 请求 - openAiSession.completions(request, new EventSourceListener() { - @Override - public void onEvent(EventSource eventSource, @Nullable String id, @Nullable String type, String data) { - if ("[DONE]".equals(data)) { - log.info("[输出结束] Tokens {}", JSON.toJSONString(data)); - return; - } - - ChatCompletionResponse response = JSON.parseObject(data, ChatCompletionResponse.class); - log.info("测试结果:{}", JSON.toJSONString(response)); - } - - @Override - public void onClosed(EventSource eventSource) { - log.info("对话完成"); - countDownLatch.countDown(); - } - - @Override - public void onFailure(EventSource eventSource, @Nullable Throwable t, @Nullable Response response) { - log.error("对话失败", t); - countDownLatch.countDown(); - } - }); - - // 等待 - countDownLatch.await(); - } - - /** - * 模型编码:glm-4v - * 根据输入的自然语言指令和图像信息完成任务,推荐使用 SSE 或同步调用方式请求接口 - * https://open.bigmodel.cn/dev/api#glm-4v - */ - @Test - public void test_completions_4v() throws Exception { - CountDownLatch countDownLatch = new CountDownLatch(1); - // 入参;模型、请求信息 - ChatCompletionRequest request = new ChatCompletionRequest(); - request.setModel(Model.GLM_4V); // GLM_3_5_TURBO、GLM_4 - request.setStream(true); - request.setMessages(new ArrayList() { - private static final long serialVersionUID = -7988151926241837899L; - - { - // content 字符串格式 -// add(ChatCompletionRequest.Prompt.builder() -// .role(Role.user.getCode()) -// .content("这个图片写了什么") -// .build()); - - // content 对象格式 add(ChatCompletionRequest.Prompt.builder() .role(Role.user.getCode()) - .content(ChatCompletionRequest.Prompt.Content.builder() - .type(ChatCompletionRequest.Prompt.Content.Type.text.getCode()) - .text("这是什么图片") - .build()) + .content("Okay") .build()); - // content 对象格式,上传图片;图片支持url、basde64 add(ChatCompletionRequest.Prompt.builder() .role(Role.user.getCode()) - .content(ChatCompletionRequest.Prompt.Content.builder() - .type(ChatCompletionRequest.Prompt.Content.Type.image_url.getCode()) - .imageUrl(ChatCompletionRequest.Prompt.Content.ImageUrl.builder().url("https://bugstack.cn/images/article/project/chatgpt/chatgpt-extra-231011-01.png").build()) - .build()) - .build()); + .content("1+2") + .build());*/ + } }); + // 请求 openAiSession.completions(request, new EventSourceListener() { @Override public void onEvent(EventSource eventSource, @Nullable String id, @Nullable String type, String data) { - if ("[DONE]".equals(data)) { - log.info("[输出结束] Tokens {}", JSON.toJSONString(data)); - return; - } - ChatCompletionResponse response = JSON.parseObject(data, ChatCompletionResponse.class); - log.info("测试结果:{}", JSON.toJSONString(response)); + log.info("测试结果 onEvent:{}", response.getData()); + // type 消息类型,add 增量,finish 结束,error 错误,interrupted 中断 + if (EventType.finish.getCode().equals(type)) { + ChatCompletionResponse.Meta meta = JSON.parseObject(response.getMeta(), ChatCompletionResponse.Meta.class); + log.info("[输出结束] Tokens {}", JSON.toJSONString(meta)); + } } @Override public void onClosed(EventSource eventSource) { log.info("对话完成"); - countDownLatch.countDown(); } - @Override - public void onFailure(EventSource eventSource, @Nullable Throwable t, @Nullable Response response) { - log.error("对话失败", t); - countDownLatch.countDown(); - } }); // 等待 - countDownLatch.await(); - + new CountDownLatch(1).await(); } /** @@ -258,14 +117,14 @@ public class ApiTest { public void test_completions_future() throws Exception { // 入参;模型、请求信息 ChatCompletionRequest request = new ChatCompletionRequest(); - request.setModel(Model.CHATGLM_TURBO); // chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro + request.setModel(Model.CHATGLM_LITE); // chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro request.setPrompt(new ArrayList() { private static final long serialVersionUID = -7988151926241837899L; { add(ChatCompletionRequest.Prompt.builder() .role(Role.user.getCode()) - .content("1+1") + .content("写个java冒泡排序") .build()); } }); @@ -276,79 +135,13 @@ public class ApiTest { log.info("测试结果:{}", response); } - /** - * 同步请求 - */ - @Test - public void test_completions_sync_01() throws Exception { - // 入参;模型、请求信息 - ChatCompletionRequest request = new ChatCompletionRequest(); - request.setModel(Model.GLM_3_5_TURBO); // chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro - request.setPrompt(new ArrayList() { - private static final long serialVersionUID = -7988151926241837899L; - - { - add(ChatCompletionRequest.Prompt.builder() - .role(Role.user.getCode()) - .content("小傅哥是谁") - .build()); - } - }); - - // 24年1月发布的 glm-3-turbo、glm-4 支持函数、知识库、联网功能 - request.setTools(new ArrayList() { - private static final long serialVersionUID = -7988151926241837899L; - - { - add(ChatCompletionRequest.Tool.builder() - .type(ChatCompletionRequest.Tool.Type.web_search) - .webSearch(ChatCompletionRequest.Tool.WebSearch.builder().enable(true).searchQuery("小傅哥").build()) - .build()); - } - }); - - ChatCompletionSyncResponse response = openAiSession.completionsSync(request); - - log.info("测试结果:{}", JSON.toJSONString(response)); - } - - @Test - public void test_completions_sync_02() throws Exception { - // 入参;模型、请求信息 - ChatCompletionRequest request = new ChatCompletionRequest(); - request.setModel(Model.GLM_3_5_TURBO); // chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro - request.setPrompt(new ArrayList() { - private static final long serialVersionUID = -7988151926241837899L; - - { - add(ChatCompletionRequest.Prompt.builder() - .role(Role.user.getCode()) - .content("1+1") - .build()); - } - }); - - ChatCompletionSyncResponse response = openAiSession.completionsSync(request); - - log.info("测试结果:{}", JSON.toJSONString(response)); - System.out.println(response.getChoices().get(0).getMessage().getContent()); - } - - @Test - public void test_genImages() throws Exception { - ImageCompletionRequest request = new ImageCompletionRequest(); - request.setModel(Model.COGVIEW_3); - request.setPrompt("画个小狗"); - ImageCompletionResponse response = openAiSession.genImages(request); - log.info("测试结果:{}", JSON.toJSONString(response)); - } @Test public void test_curl() { // 1. 配置文件 Configuration configuration = new Configuration(); configuration.setApiHost("https://open.bigmodel.cn/"); - configuration.setApiSecretKey("39580e34e175019c230fdd519817b381.*****"); + configuration.setApiSecretKey("6bb7238d01c52a4ce300513381168c4e.6eVqwo1tsctUgyzl"); // 2. 获取Token String token = BearerTokenUtils.getToken(configuration.getApiKey(), configuration.getApiSecret()); -- GitLab