Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
KnowledgePlanet
road-map
chatglm-sdk-java
比较版本
a2affabbdd221e79614c58f9708ffd0580e711ba...643b87ce4c74472111d1df1b51fdc50db598dbca
chatglm-sdk-java
项目概览
KnowledgePlanet
/
road-map
/
chatglm-sdk-java
通知
198
Star
15
Fork
7
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
chatglm-sdk-java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
源分支
643b87ce4c74472111d1df1b51fdc50db598dbca
选择Git版本
...
目标分支
a2affabbdd221e79614c58f9708ffd0580e711ba
选择Git版本
比较
Commits (8)
https://gitcode.net/KnowledgePlanet/road-map/chatglm-sdk-java/-/commit/044cc852a093f0ab8104cd8d1a54bba081c021d6
feat:发布到maven仓库 v1.0
2023-12-05T22:40:22+08:00
小傅哥
184172133@qq.com
https://gitcode.net/KnowledgePlanet/road-map/chatglm-sdk-java/-/commit/8f9b300d5429f07c201e0dfa649534994eb7b70c
feat:发布到maven仓库 v1.0
2023-12-09T08:25:24+08:00
小傅哥
184172133@qq.com
https://gitcode.net/KnowledgePlanet/road-map/chatglm-sdk-java/-/commit/1b69e40d18b7c0b9e15c13e7966354986d46d169
Update README.md
2023-12-12T17:12:07+08:00
小傅哥
fuzhengwei@users.noreply.github.com
https://gitcode.net/KnowledgePlanet/road-map/chatglm-sdk-java/-/commit/399606ee801c8fd97d7c81ab0e0afe9dcac9ff85
feat:add chatglm sync method
2023-12-14T17:26:11+08:00
maxuan
maxuan123456
https://gitcode.net/KnowledgePlanet/road-map/chatglm-sdk-java/-/commit/d3f14eeda91ca6c30d48a4a777f1a2e882416ac8
Merge pull request #3 from max-holo/dev_max
2023-12-14T17:37:35+08:00
小傅哥
fuzhengwei@users.noreply.github.com
feat:add chatglm sync method
https://gitcode.net/KnowledgePlanet/road-map/chatglm-sdk-java/-/commit/7cd52d195fd6bbc71862a11278ef67db7ca8a31b
Update README.md
2023-12-14T17:38:57+08:00
小傅哥
fuzhengwei@users.noreply.github.com
https://gitcode.net/KnowledgePlanet/road-map/chatglm-sdk-java/-/commit/ebdf6589ffaa119800a203206209755e0453bd2a
Update pom.xml
2023-12-14T17:44:40+08:00
小傅哥
fuzhengwei@users.noreply.github.com
https://gitcode.net/KnowledgePlanet/road-map/chatglm-sdk-java/-/commit/643b87ce4c74472111d1df1b51fdc50db598dbca
feat:v1.1 发布,文档更新
2023-12-14T22:06:49+08:00
小傅哥
184172133@qq.com
隐藏空白更改
内联
并排
Showing
8 changed file
with
266 addition
and
16 deletion
+266
-16
README.md
README.md
+68
-3
pom.xml
pom.xml
+100
-5
src/main/java/cn/bugstack/chatglm/IOpenAiApi.java
src/main/java/cn/bugstack/chatglm/IOpenAiApi.java
+5
-0
src/main/java/cn/bugstack/chatglm/interceptor/OpenAiHTTPInterceptor.java
...n/bugstack/chatglm/interceptor/OpenAiHTTPInterceptor.java
+1
-2
src/main/java/cn/bugstack/chatglm/model/ChatCompletionSyncResponse.java
...cn/bugstack/chatglm/model/ChatCompletionSyncResponse.java
+42
-0
src/main/java/cn/bugstack/chatglm/session/OpenAiSession.java
src/main/java/cn/bugstack/chatglm/session/OpenAiSession.java
+4
-0
src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSession.java
...gstack/chatglm/session/defaults/DefaultOpenAiSession.java
+19
-4
src/test/java/cn/bugstack/chatglm/test/ApiTest.java
src/test/java/cn/bugstack/chatglm/test/ApiTest.java
+27
-2
未找到文件。
README.md
浏览文件 @
643b87ce
...
...
@@ -20,13 +20,13 @@
-
申请ApiKey:
[
https://open.bigmodel.cn/usercenter/apikeys
](
https://open.bigmodel.cn/usercenter/apikeys
)
- 注册申请开通,即可获得 ApiKey
-
运行环境:JDK 1.8+
-
maven pom -
`
暂时测试阶段,未推送到Maven中央仓库,需要下载代码本地 install 后使用
`
-
maven pom -
`
已发布到Maven仓库
`
```
pom
<dependency>
<groupId>cn.bugstack</groupId>
<artifactId>chatglm-sdk-java</artifactId>
<version>1.
0-SNAPSHOT
</version>
<version>1.
1
</version>
</dependency>
```
...
...
@@ -39,6 +39,23 @@
### 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
;
@Slf4j
public
class
ApiTest
{
...
...
@@ -98,6 +115,54 @@ public class ApiTest {
new
CountDownLatch
(
1
).
await
();
}
/**
* 同步请求,future 模型,通过对流式的改造提供同步
*/
@Test
public
void
test_completions_future
()
throws
ExecutionException
,
InterruptedException
{
// 入参;模型、请求信息
ChatCompletionRequest
request
=
new
ChatCompletionRequest
();
request
.
setModel
(
Model
.
CHATGLM_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
(
"写个java冒泡排序"
)
.
build
());
}
});
CompletableFuture
<
String
>
future
=
openAiSession
.
completions
(
request
);
String
response
=
future
.
get
();
log
.
info
(
"测试结果:{}"
,
response
);
}
/**
* 同步请求,官网自带的同步方法
*/
@Test
public
void
test_completions_sync
()
throws
IOException
{
// 入参;模型、请求信息
ChatCompletionRequest
request
=
new
ChatCompletionRequest
();
request
.
setModel
(
Model
.
CHATGLM_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
(
"写个java冒泡排序"
)
.
build
());
}
});
ChatCompletionSyncResponse
response
=
openAiSession
.
completionsSync
(
request
);
log
.
info
(
"测试结果:{}"
,
JSON
.
toJSONString
(
response
));
}
}
```
...
...
@@ -215,4 +280,4 @@ chatglm:
-
📝 博客:
[
bugstack.cn
](
https://bugstack.cn/
)
- 足够硬核,内容老狠了!
-
📺 视频:
[
B站 小傅哥の码场
](
https://space.bilibili.com/15637440
)
-
💌 微信:
[
fustack
](
https://bugstack.cn/images/personal/fustack.png
)
- 备注来意
-
🐾 我的编程知识星球:
[
实战生产级项目、手写框架级源码,可以向我 1对1 提问,解答技术/职场/规划问题
](
https://bugstack.cn/md/zsxq/introduce.html
)
\ No newline at end of file
-
🐾 我的编程知识星球:
[
实战生产级项目、手写框架级源码,可以向我 1对1 提问,解答技术/职场/规划问题
](
https://bugstack.cn/md/zsxq/introduce.html
)
pom.xml
浏览文件 @
643b87ce
...
...
@@ -6,11 +6,11 @@
<groupId>
cn.bugstack
</groupId>
<artifactId>
chatglm-sdk-java
</artifactId>
<version>
1.
0-SNAPSHOT
</version>
<version>
1.
1
</version>
<name>
chatglm-sdk-java
</name>
<description>
OpenAI Java SDK, ZhiPuAi ChatGLM Java SDK .
</description>
<url>
https://
open.bigmodel.cn/
</url>
<description>
OpenAI Java SDK, ZhiPuAi ChatGLM Java SDK .
Copyright © 2023 bugstack虫洞栈 All rights reserved. 版权所有(C)小傅哥 https://github.com/fuzhengwei/chatglm-sdk-java
</description>
<url>
https://
github.com/fuzhengwei/chatglm-sdk-java
</url>
<licenses>
<license>
...
...
@@ -27,7 +27,7 @@
<email>
184172133@qq.com
</email>
<url>
https://github.com/fuzhengwei
</url>
<organization>
chatglm ai
</organization>
<organizationUrl>
https://
open.bigmodel.cn/
</organizationUrl>
<organizationUrl>
https://
github.com/fuzhengwei/chatglm-sdk-java
</organizationUrl>
<roles>
<role>
architect
</role>
<role>
developer
</role>
...
...
@@ -155,4 +155,99 @@
</plugins>
</build>
</project>
\ No newline at end of file
<profiles>
<profile>
<!--注意,此id必须与setting.xml中指定的一致,不要自作聪明改它名字-->
<id>
release
</id>
<activation>
<activeByDefault>
true
</activeByDefault>
</activation>
<build>
<!--发布到中央SNAPSHOT仓库插件-->
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-release-plugin
</artifactId>
<version>
2.5.3
</version>
<configuration>
<autoVersionSubmodules>
true
</autoVersionSubmodules>
<useReleaseProfile>
false
</useReleaseProfile>
<releaseProfiles>
release
</releaseProfiles>
<goals>
deploy
</goals>
</configuration>
</plugin>
<!--生成源码插件-->
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-source-plugin
</artifactId>
<version>
2.2.1
</version>
<executions>
<execution>
<id>
attach-sources
</id>
<goals>
<goal>
jar-no-fork
</goal>
</goals>
</execution>
</executions>
</plugin>
<!--生成API文档插件-->
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-javadoc-plugin
</artifactId>
<version>
2.9.1
</version>
<executions>
<execution>
<id>
attach-javadocs
</id>
<goals>
<goal>
jar
</goal>
</goals>
<configuration>
<additionalparam>
-Xdoclint:none
</additionalparam>
<!-- /usr/libexec/java_home -V -->
<javadocExecutable>
/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home/bin/javadoc
</javadocExecutable>
</configuration>
</execution>
</executions>
</plugin>
<!--gpg插件-->
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-gpg-plugin
</artifactId>
<version>
1.5
</version>
<executions>
<execution>
<id>
sign-artifacts
</id>
<phase>
verify
</phase>
<goals>
<goal>
sign
</goal>
</goals>
<configuration>
<keyname>
fuzhengwei
</keyname>
<passphrase>
123456
</passphrase>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<!--注意,此id必须与setting.xml中指定的一致-->
<id>
ossrh
</id>
<url>
https://s01.oss.sonatype.org/content/repositories/snapshots
</url>
</snapshotRepository>
<repository>
<id>
release
</id>
<url>
https://s01.oss.sonatype.org/content/repositories/releases/
</url>
</repository>
</distributionManagement>
</profile>
</profiles>
</project>
src/main/java/cn/bugstack/chatglm/IOpenAiApi.java
浏览文件 @
643b87ce
...
...
@@ -2,6 +2,7 @@ package cn.bugstack.chatglm;
import
cn.bugstack.chatglm.model.ChatCompletionRequest
;
import
cn.bugstack.chatglm.model.ChatCompletionResponse
;
import
cn.bugstack.chatglm.model.ChatCompletionSyncResponse
;
import
io.reactivex.Single
;
import
retrofit2.http.Body
;
import
retrofit2.http.POST
;
...
...
@@ -16,8 +17,12 @@ import retrofit2.http.Path;
public
interface
IOpenAiApi
{
String
v3_completions
=
"api/paas/v3/model-api/{model}/sse-invoke"
;
String
v3_completions_sync
=
"api/paas/v3/model-api/{model}/invoke"
;
@POST
(
v3_completions
)
Single
<
ChatCompletionResponse
>
completions
(
@Path
(
"model"
)
String
model
,
@Body
ChatCompletionRequest
chatCompletionRequest
);
@POST
(
v3_completions_sync
)
Single
<
ChatCompletionSyncResponse
>
completions
(
@Body
ChatCompletionRequest
chatCompletionRequest
);
}
src/main/java/cn/bugstack/chatglm/interceptor/OpenAiHTTPInterceptor.java
浏览文件 @
643b87ce
...
...
@@ -30,14 +30,13 @@ public class OpenAiHTTPInterceptor implements Interceptor {
public
@NotNull
Response
intercept
(
Chain
chain
)
throws
IOException
{
// 1. 获取原始 Request
Request
original
=
chain
.
request
();
// 2. 构建请求
Request
request
=
original
.
newBuilder
()
.
url
(
original
.
url
())
.
header
(
"Authorization"
,
"Bearer "
+
BearerTokenUtils
.
getToken
(
configuration
.
getApiKey
(),
configuration
.
getApiSecret
()))
.
header
(
"Content-Type"
,
Configuration
.
JSON_CONTENT_TYPE
)
.
header
(
"User-Agent"
,
Configuration
.
DEFAULT_USER_AGENT
)
.
header
(
"Accept"
,
Configuration
.
SSE_CONTENT_TYPE
)
.
header
(
"Accept"
,
null
!=
original
.
header
(
"Accept"
)
?
original
.
header
(
"Accept"
)
:
Configuration
.
SSE_CONTENT_TYPE
)
.
method
(
original
.
method
(),
original
.
body
())
.
build
();
...
...
src/main/java/cn/bugstack/chatglm/model/ChatCompletionSyncResponse.java
0 → 100644
浏览文件 @
643b87ce
package
cn.bugstack.chatglm.model
;
import
lombok.Data
;
import
java.util.List
;
/**
* 同步调用响应
* @author max
* @date 2023/12/14 15:41
*/
@Data
public
class
ChatCompletionSyncResponse
{
private
Integer
code
;
private
String
msg
;
private
Boolean
success
;
private
ChatGLMData
data
;
@Data
public
static
class
ChatGLMData
{
private
List
<
Choice
>
choices
;
private
String
task_status
;
private
Usage
usage
;
private
String
task_id
;
private
String
request_id
;
}
@Data
public
static
class
Usage
{
private
int
completion_tokens
;
private
int
prompt_tokens
;
private
int
total_tokens
;
}
@Data
public
static
class
Choice
{
private
String
role
;
private
String
content
;
}
}
src/main/java/cn/bugstack/chatglm/session/OpenAiSession.java
浏览文件 @
643b87ce
...
...
@@ -2,10 +2,12 @@ package cn.bugstack.chatglm.session;
import
cn.bugstack.chatglm.model.ChatCompletionRequest
;
import
cn.bugstack.chatglm.model.ChatCompletionResponse
;
import
cn.bugstack.chatglm.model.ChatCompletionSyncResponse
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
okhttp3.sse.EventSource
;
import
okhttp3.sse.EventSourceListener
;
import
java.io.IOException
;
import
java.util.concurrent.CompletableFuture
;
/**
...
...
@@ -20,4 +22,6 @@ public interface OpenAiSession {
CompletableFuture
<
String
>
completions
(
ChatCompletionRequest
chatCompletionRequest
)
throws
InterruptedException
;
ChatCompletionSyncResponse
completionsSync
(
ChatCompletionRequest
chatCompletionRequest
)
throws
IOException
;
}
src/main/java/cn/bugstack/chatglm/session/defaults/DefaultOpenAiSession.java
浏览文件 @
643b87ce
...
...
@@ -3,20 +3,19 @@ package cn.bugstack.chatglm.session.defaults;
import
cn.bugstack.chatglm.IOpenAiApi
;
import
cn.bugstack.chatglm.model.ChatCompletionRequest
;
import
cn.bugstack.chatglm.model.ChatCompletionResponse
;
import
cn.bugstack.chatglm.model.ChatCompletionSyncResponse
;
import
cn.bugstack.chatglm.model.EventType
;
import
cn.bugstack.chatglm.session.Configuration
;
import
cn.bugstack.chatglm.session.OpenAiSession
;
import
com.alibaba.fastjson.JSON
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
lombok.extern.slf4j.Slf4j
;
import
okhttp3.MediaType
;
import
okhttp3.Request
;
import
okhttp3.RequestBody
;
import
okhttp3.Response
;
import
okhttp3.*
;
import
okhttp3.sse.EventSource
;
import
okhttp3.sse.EventSourceListener
;
import
org.jetbrains.annotations.Nullable
;
import
java.io.IOException
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.CountDownLatch
;
...
...
@@ -94,4 +93,20 @@ public class DefaultOpenAiSession implements OpenAiSession {
return
future
;
}
@Override
public
ChatCompletionSyncResponse
completionsSync
(
ChatCompletionRequest
chatCompletionRequest
)
throws
IOException
{
// 构建请求信息
Request
request
=
new
Request
.
Builder
()
.
url
(
configuration
.
getApiHost
().
concat
(
IOpenAiApi
.
v3_completions_sync
).
replace
(
"{model}"
,
chatCompletionRequest
.
getModel
().
getCode
()))
.
header
(
"Accept"
,
Configuration
.
APPLICATION_JSON
)
.
post
(
RequestBody
.
create
(
MediaType
.
parse
(
"application/json"
),
chatCompletionRequest
.
toString
()))
.
build
();
OkHttpClient
okHttpClient
=
configuration
.
getOkHttpClient
();
Response
response
=
okHttpClient
.
newCall
(
request
).
execute
();
if
(!
response
.
isSuccessful
()){
new
RuntimeException
(
"Request failed"
);
}
return
JSON
.
parseObject
(
response
.
body
().
string
(),
ChatCompletionSyncResponse
.
class
);
}
}
src/test/java/cn/bugstack/chatglm/test/ApiTest.java
浏览文件 @
643b87ce
...
...
@@ -17,6 +17,7 @@ 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
;
...
...
@@ -119,14 +120,14 @@ public class ApiTest {
public
void
test_completions_future
()
throws
ExecutionException
,
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
.
setPrompt
(
new
ArrayList
<
ChatCompletionRequest
.
Prompt
>()
{
private
static
final
long
serialVersionUID
=
-
7988151926241837899L
;
{
add
(
ChatCompletionRequest
.
Prompt
.
builder
()
.
role
(
Role
.
user
.
getCode
())
.
content
(
"
写个java冒泡排序
"
)
.
content
(
"
1+1
"
)
.
build
());
}
});
...
...
@@ -137,6 +138,30 @@ public class ApiTest {
log
.
info
(
"测试结果:{}"
,
response
);
}
/**
* 同步请求
*/
@Test
public
void
test_completions_sync
()
throws
IOException
{
// 入参;模型、请求信息
ChatCompletionRequest
request
=
new
ChatCompletionRequest
();
request
.
setModel
(
Model
.
CHATGLM_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
));
}
@Test
public
void
test_curl
()
{
...
...