提交 f06b55c3 编写于 作者: G guoyiying

暂存

上级 47e5ae65
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectDictionaryState">
<dictionary name="guoyiying" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ASMSmaliIdeaPluginConfiguration">
<asm skipDebug="true" skipFrames="true" skipCode="false" expandFrames="false" />
<groovy codeStyle="LEGACY" />
</component>
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>
\ No newline at end of file
` 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

java:S1192"Define a constant instead of duplicating this literal "适用于对知识量、推理能力、创造力要求较高的场景" 3 times.(dz
\ No newline at end of file
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
[ java:S112""FDefine and throw a dedicated exception instead of using a generic one.(ûàÁä
Q java:S125"<This block of commented-out lines of code should be removed.(ÏÈÙ¨
\ No newline at end of file
p java:S116"VRename this field "task_status" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(¾»ƒ þÿÿÿÿ
l java:S116"RRename this field "task_id" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ÐÕìøÿÿÿÿ
j java:S116"URename this field "request_id" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ɦӓ
q java:S116"\Rename this field "completion_tokens" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(Ûð´ú
r java:S116"XRename this field "prompt_tokens" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(þ߉úÿÿÿÿ
l java:S116"WRename this field "total_tokens" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(Ú°Œ¤
\ No newline at end of file
z
java:S1149>"_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
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
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
<src/main/java/cn/bugstack/chatglm/session/Configuration.java,8/0/80065a9ed7ad9a18bdea4a3959ae77102039a220

Ssrc/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSessionFactory.java,5/0/504022ed192a0dca120d8b28d6fdb8fb081ed0cd
s
Csrc/main/java/cn/bugstack/chatglm/model/ChatCompletionResponse.java,b/3/b3a800729fe37c9b51785af6a5b06c5a7a664303
|
Lsrc/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSession.java,c/a/caf16d4635f38e5850291ca82ae50c9d36596102
x
Hsrc/main/java/cn/bugstack/chatglm/interceptor/OpenAiHTTPInterceptor.java,2/0/20550a4519469f3a2793d71d6bed550ad3656077
r
Bsrc/main/java/cn/bugstack/chatglm/model/ChatCompletionRequest.java,1/3/1371a282431555be400a63c5fee79c61bafb1ede
c
3src/test/java/cn/bugstack/chatglm/test/ApiTest.java,a/6/a6b382f8a9c04dede805112fe69360503af1bd22
b
2src/main/java/cn/bugstack/chatglm/model/Model.java,2/3/23d0cc0324fa072ab34e37f664df89e409da8705
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
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
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" \
......
......@@ -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","根据用户的文字描述生成图像,使用同步调用方式请求接口"),
;
......
......@@ -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 <a href="https://open.bigmodel.cn/usercenter/apikeys">ApiSecretKey</a>
* @github <a href="https://github.com/fuzhengwei/chatglm-sdk-java">chatglm-sdk-java</a>
* @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<ChatCompletionRequest.Tool>() {
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<ChatCompletionRequest.Prompt>() {
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<ChatCompletionRequest.Tool>() {
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<ChatCompletionRequest.Prompt>() {
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<ChatCompletionRequest.Prompt>() {
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<ChatCompletionRequest.Prompt>() {
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<ChatCompletionRequest.Prompt>() {
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<ChatCompletionRequest.Tool>() {
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<ChatCompletionRequest.Prompt>() {
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());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册