diff --git a/README.md b/README.md index d80e3a843c6cdabbb81a165fe54660968712d85c..cebc9e1f982c6b9759155c1844df4ff16a084f89 100644 --- a/README.md +++ b/README.md @@ -39,23 +39,12 @@ ### 2.1 代码执行 ```java -import cn.bugstack.chatglm.model.*; -import cn.bugstack.chatglm.session.Configuration; -import cn.bugstack.chatglm.session.OpenAiSession; -import cn.bugstack.chatglm.session.OpenAiSessionFactory; -import cn.bugstack.chatglm.session.defaults.DefaultOpenAiSessionFactory; -import com.alibaba.fastjson.JSON; -import com.fasterxml.jackson.core.JsonProcessingException; -import lombok.extern.slf4j.Slf4j; -import okhttp3.sse.EventSource; -import okhttp3.sse.EventSourceListener; -import org.junit.Before; -import org.junit.Test; - -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.concurrent.CountDownLatch; - +/** + * @author 小傅哥,微信:fustack + * @description 在官网申请 ApiSecretKey ApiSecretKey + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ @Slf4j public class ApiTest { @@ -66,7 +55,8 @@ public class ApiTest { // 1. 配置文件 Configuration configuration = new Configuration(); configuration.setApiHost("https://open.bigmodel.cn/"); - configuration.setApiSecretKey("4e087e4135306ef4a676f0cce3cee560.sgP2*****"); + configuration.setApiSecretKey("d570f7c5d289cdac2abdfdc562e39f3f.trqz1dH8ZK6ED7Pg"); + configuration.setLevel(HttpLoggingInterceptor.Level.BODY); // 2. 会话工厂 OpenAiSessionFactory factory = new DefaultOpenAiSessionFactory(configuration); // 3. 开启会话 @@ -74,21 +64,44 @@ public class ApiTest { } /** - * 流式对话 + * 流式对话 & 关联上下文 */ @Test public void test_completions() throws JsonProcessingException, InterruptedException { // 入参;模型、请求信息 ChatCompletionRequest request = new ChatCompletionRequest(); - request.setModel(Model.CHATGLM_LITE); // 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.setPrompt(new ArrayList() { private static final long serialVersionUID = -7988151926241837899L; { add(ChatCompletionRequest.Prompt.builder() .role(Role.user.getCode()) - .content("写个java冒泡排序") + .content("1+2") + .build()); + + add(ChatCompletionRequest.Prompt.builder() + .role(Role.user.getCode()) + .content("Okay") + .build()); + + /* system 和 user 为一组出现。如果有参数类型为 system 则 system + user 一组一起传递。*/ + add(ChatCompletionRequest.Prompt.builder() + .role(Role.system.getCode()) + .content("1+1=2") + .build()); + + add(ChatCompletionRequest.Prompt.builder() + .role(Role.user.getCode()) + .content("Okay") + .build()); + + add(ChatCompletionRequest.Prompt.builder() + .role(Role.user.getCode()) + .content("1+2") .build()); + } }); @@ -109,6 +122,7 @@ public class ApiTest { public void onClosed(EventSource eventSource) { log.info("对话完成"); } + }); // 等待 @@ -116,7 +130,7 @@ public class ApiTest { } /** - * 同步请求,future 模型,通过对流式的改造提供同步 + * 同步请求 */ @Test public void test_completions_future() throws ExecutionException, InterruptedException { @@ -129,7 +143,7 @@ public class ApiTest { { add(ChatCompletionRequest.Prompt.builder() .role(Role.user.getCode()) - .content("写个java冒泡排序") + .content("1+1") .build()); } }); @@ -141,7 +155,7 @@ public class ApiTest { } /** - * 同步请求,官网自带的同步方法 + * 同步请求 */ @Test public void test_completions_sync() throws IOException { @@ -154,7 +168,7 @@ public class ApiTest { { add(ChatCompletionRequest.Prompt.builder() .role(Role.user.getCode()) - .content("写个java冒泡排序") + .content("1+1") .build()); } }); @@ -163,6 +177,7 @@ public class ApiTest { log.info("测试结果:{}", JSON.toJSONString(response)); } + } ```