导读|时隔1个月chatGPT4、文心一言的发布,AIGC又一次惊艳的出现在大家的视线。按照这个趋势,到年底AIGC将会有一个普遍的持续发展,真希望这一天早到来,为此勇哥也爆肝为该事业做一点微不足道的贡献,发布如下API,帮助开发者们相对遍历的开发和对接openai。因此阅读本文你会了解到最好用的java api、以及官方API的一些弊端是什么,进而少走弯路?以及SpringBoot如何快速对接OpenAi?
Openai官方推荐的JavaApi库是openai-java,该库缺失一些封装的经验,因此使用起来会有一些不便之处:
由于以上原因,勇哥自己重新封装了一下,支持以下功能,希望大家多多Star支持一下:
快速调用OpenAi官方 GPT-3 Api的Java库,更适合国内开发者,因为你懂的。Java libraries for using OpenAI's GPT-3 api.
Git地址 : https://gitee.com/miukoo/openai
更多的功能后续会持续提供
<dependency>
<groupId>cn.gjsm</groupId>
<artifactId>openai</artifactId>
<version>0.1.3</version>
</dependency>
// 实例化请求客户端 Instance the OpenAiClient
OpenAiClient openAiClient = OpenAiClientFactory.createClient(OPENAPI_TOKEN);
// 实例化发送的消息 Instance the message
ChatMessage chatMessage = new ChatMessage();
chatMessage.setRole("user");
chatMessage.setContent("今天天气怎么样?");
// 实例化发送的请求 Instance the request
ChatCompletionRequest request = ChatCompletionRequest.builder()
.messages(Arrays.asList(chatMessage))
.model("gpt-3.5-turbo")
.build();
// 执行请求 Execute request
Call<ChatCompletionResponse> chatCompletion = openAiClient.callChatCompletion(request);
Response<ChatCompletionResponse> response = chatCompletion.execute();
// 解析结果 Analysis results
if(response.isSuccessful()){
System.out.println(JSON.toJSONString(response.body()));
}
OpenAiClient openAiClient = OpenAiClientFactory.builder()
.readTimeout(Duration.ofMillis(openAiProperties.getTimeout()))
.connectTimeout(Duration.ofMillis(openAiProperties.getTimeout()))
.build()
.createClient(openAiProperties.getToken());
OpenAiClient openAiClient = OpenAiClientFactory.getInstance()
.createHttpProxyClient(openAiProperties.getToken(),"代理IP","代理端口");
框架是直接从环境变量中获取TOKEN,获取的变量名是OPENAPI_TOKEN。
OpenAiClientFactory.refreshToken("您的新TOKEN");
在程序启动命令中增加一下参数,可更好的支持本机代理 Add the following parameters to the program startup command to better support the native agent
-Djava.net.useSystemProxies=true
本项目是基于SpringBoot3.0封装的OpenAi快速开发类,支持49种场景调用。
Git地址:https://gitee.com/miukoo/openai-spring
注意:该项目还未提交到中央仓库,因此你需要下载源码到本地安装。
<dependency>
<groupId>cn.gjsm</groupId>
<artifactId>openai-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>
在application.yml中配置如下参数:
openai:
token: 你的秘钥
timeout: 5000 // 超时时间
@Autowired
OpenAiClient openAiClient;
@Test
public void test(){
// 实例化发送的消息 Instance the message
ChatMessage chatMessage = new ChatMessage();
chatMessage.setRole("user");
chatMessage.setContent("今天天气怎么样?");
// 实例化发送的请求 Instance the request
ChatCompletionRequest request = ChatCompletionRequest.builder()
.messages(Arrays.asList(chatMessage))
.model("gpt-3.5-turbo")
.build();
// 执行请求 Execute request
Call<ChatCompletionResponse> chatCompletion = openAiClient.callChatCompletion(request);
Response<ChatCompletionResponse> response = chatCompletion.execute();
// 解析结果 Analysis results
if(response.isSuccessful()){
System.out.println(JSON.toJSONString(response.body()));
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有