您现在的位置是:首页 > 文章详情

简单易用的 OpenAi Java SDK 1.8.0 发布

日期:2023-08-16点击:270

推荐一套基于 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")) .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")) .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>
原文链接:https://www.oschina.net/news/253930/openai-java-sdk-1-8-0-released
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章