未验证 提交 aa0cfb88 编写于 作者: 小傅哥 提交者: GitHub

Update README.md

上级 643b87ce
...@@ -39,23 +39,12 @@ ...@@ -39,23 +39,12 @@
### 2.1 代码执行 ### 2.1 代码执行
```java ```java
import cn.bugstack.chatglm.model.*; /**
import cn.bugstack.chatglm.session.Configuration; * @author 小傅哥,微信:fustack
import cn.bugstack.chatglm.session.OpenAiSession; * @description 在官网申请 ApiSecretKey <a href="https://open.bigmodel.cn/usercenter/apikeys">ApiSecretKey</a>
import cn.bugstack.chatglm.session.OpenAiSessionFactory; * @github https://github.com/fuzhengwei
import cn.bugstack.chatglm.session.defaults.DefaultOpenAiSessionFactory; * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
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;
@Slf4j @Slf4j
public class ApiTest { public class ApiTest {
...@@ -66,7 +55,8 @@ public class ApiTest { ...@@ -66,7 +55,8 @@ public class ApiTest {
// 1. 配置文件 // 1. 配置文件
Configuration configuration = new Configuration(); Configuration configuration = new Configuration();
configuration.setApiHost("https://open.bigmodel.cn/"); configuration.setApiHost("https://open.bigmodel.cn/");
configuration.setApiSecretKey("4e087e4135306ef4a676f0cce3cee560.sgP2*****"); configuration.setApiSecretKey("d570f7c5d289cdac2abdfdc562e39f3f.trqz1dH8ZK6ED7Pg");
configuration.setLevel(HttpLoggingInterceptor.Level.BODY);
// 2. 会话工厂 // 2. 会话工厂
OpenAiSessionFactory factory = new DefaultOpenAiSessionFactory(configuration); OpenAiSessionFactory factory = new DefaultOpenAiSessionFactory(configuration);
// 3. 开启会话 // 3. 开启会话
...@@ -74,21 +64,44 @@ public class ApiTest { ...@@ -74,21 +64,44 @@ public class ApiTest {
} }
/** /**
* 流式对话 * 流式对话 & 关联上下文
*/ */
@Test @Test
public void test_completions() throws JsonProcessingException, InterruptedException { public void test_completions() throws JsonProcessingException, InterruptedException {
// 入参;模型、请求信息 // 入参;模型、请求信息
ChatCompletionRequest request = new ChatCompletionRequest(); 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<ChatCompletionRequest.Prompt>() { request.setPrompt(new ArrayList<ChatCompletionRequest.Prompt>() {
private static final long serialVersionUID = -7988151926241837899L; private static final long serialVersionUID = -7988151926241837899L;
{ {
add(ChatCompletionRequest.Prompt.builder() add(ChatCompletionRequest.Prompt.builder()
.role(Role.user.getCode()) .role(Role.user.getCode())
.content("写个java冒泡排序") .content("1+2")
.build()); .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 { ...@@ -109,6 +122,7 @@ public class ApiTest {
public void onClosed(EventSource eventSource) { public void onClosed(EventSource eventSource) {
log.info("对话完成"); log.info("对话完成");
} }
}); });
// 等待 // 等待
...@@ -116,7 +130,7 @@ public class ApiTest { ...@@ -116,7 +130,7 @@ public class ApiTest {
} }
/** /**
* 同步请求,future 模型,通过对流式的改造提供同步 * 同步请求
*/ */
@Test @Test
public void test_completions_future() throws ExecutionException, InterruptedException { public void test_completions_future() throws ExecutionException, InterruptedException {
...@@ -129,7 +143,7 @@ public class ApiTest { ...@@ -129,7 +143,7 @@ public class ApiTest {
{ {
add(ChatCompletionRequest.Prompt.builder() add(ChatCompletionRequest.Prompt.builder()
.role(Role.user.getCode()) .role(Role.user.getCode())
.content("写个java冒泡排序") .content("1+1")
.build()); .build());
} }
}); });
...@@ -141,7 +155,7 @@ public class ApiTest { ...@@ -141,7 +155,7 @@ public class ApiTest {
} }
/** /**
* 同步请求,官网自带的同步方法 * 同步请求
*/ */
@Test @Test
public void test_completions_sync() throws IOException { public void test_completions_sync() throws IOException {
...@@ -154,7 +168,7 @@ public class ApiTest { ...@@ -154,7 +168,7 @@ public class ApiTest {
{ {
add(ChatCompletionRequest.Prompt.builder() add(ChatCompletionRequest.Prompt.builder()
.role(Role.user.getCode()) .role(Role.user.getCode())
.content("写个java冒泡排序") .content("1+1")
.build()); .build());
} }
}); });
...@@ -163,6 +177,7 @@ public class ApiTest { ...@@ -163,6 +177,7 @@ public class ApiTest {
log.info("测试结果:{}", JSON.toJSONString(response)); log.info("测试结果:{}", JSON.toJSONString(response));
} }
} }
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册