推荐一套基于 SpringBoot 开发的全平台数据 (数据库管理工具) 功能比较完善,建议下载使用: https://github.com/EdurtIO/datacap 目前已经支持 40+ 多种数据源。国内首个应用 ChatGPT 到数据管理系统中项目。
推荐一套基于 SpringBoot 开发的简单、易用的开源权限管理平台,建议下载使用: https://github.com/devlive-community/authx。
OpenAI Java SDK 主要 为 Java 开发人员提供方便易用的 SDK 来与 OpenAI 的 API 进行交互。
- 支持 Stream
通过支持 Stream 我们实现了类似 ChaGPT 官网聊天逐步返回的效果。
以下示例我们都是通过在控制台中进行消息输出,如果需要其他输出方式可以自行支持,或者提交给我们,我们会优先支持。
创建 Completion
// 用于标记结束
CountDownLatch countDownLatch = new CountDownLatch(1);
ConsoleEventSourceListener listener = ConsoleEventSourceListener.builder()
.countDownLatch(countDownLatch)
.build();
try(OpenAiClient client=OpenAiClient.builder()
.apiKey(System.getProperty("openai.token"))
.listener(listener)
.build())
{
CompletionEntity configure = CompletionEntity.builder()
.prompt("How to create a stream completion")
.temperature(2D)
.build();
client.createCompletion(configure);
try {
countDownLatch.await();
}
catch (InterruptedException e) {
log.error("Interrupted while waiting", e);
}
}
创建 Chat Completion
// 用于标记结束
CountDownLatch countDownLatch = new CountDownLatch(1);
ConsoleEventSourceListener listener = ConsoleEventSourceListener.builder()
.countDownLatch(countDownLatch)
.build();
try(OpenAiClient client=OpenAiClient.builder()
.apiKey(System.getProperty("openai.token"))
.listener(listener)
.build())
{
List<MessageEntity> messages = Lists.newArrayList();
messages.add(MessageEntity.builder()
.content("Hello, my name is openai-java-sdk")
.build());
ChatEntity configure = ChatEntity.builder()
.messages(messages)
.build();
client.createChatCompletion(configure);
try {
countDownLatch.await();
}
catch (InterruptedException e) {
log.error("Interrupted while waiting", e);
}
}
目前我们提供了两个事件监听器
ConsoleEventSourceListener
: 主要用于处理控制台输出HttpServletEventSourceListener
: 主要用于处理 Servlet 输出
如何使用?
<properties>
<openai.version>1.8.0</openai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.devlive.sdk</groupId>
<artifactId>openai-java-sdk</artifactId>
<version>${openai.version}</version>
</dependency>
</dependencies>