Springboot集成OpenAi chatgpt.5官方开源项目opanAI-Java
前言
本文主要介绍Springboot集成openai-java完成openai官方接口的调用,官方有多种语言的demo示例
OPEAI开源openai-java项目地址:https://github/TheoKanning/openai-java
准备工作
必要的前提
本文主要介绍Springboot集成openai-java完成openai官方接口的调用,官方有多种语言的demo示例
OPEAI开源openai-java项目地址:https://github/TheoKanning/openai-java
必要的前提,要使用chatgpt必须要魔法
- 魔法
- openai帐号(需要apiKey)
- springbootmaven的项目
1、maven中引入openai-java
目前用的版本是0.12.0
<!-- openai -->
<dependency>
<groupId></groupId>
<artifactId>api</artifactId>
<version>${openai.version}</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>client</artifactId>
<version>${openai.version}</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>service</artifactId>
<version>${openai.version}</version>
</dependency>
2、配置
测试我把配置信息放在了yml中
openai:
proxyHost: 127.0.0.1
proxyPort: 7890
keys:
- sk-xxxxxxxxxxxxxxxxxxxxxxxx
配置类
@Configuration
@ConfigurationProperties(prefix = openai)
public class OpenAiModel {
/**
* 代理地址
*/
private static String proxyHost;
/**
* 代理端口
*/
private static Integer proxyPort;
/**
* openai apikey
*/
private static List<String> keys;
// 省略 get set
}
、使用
调用api的核心类是OpenAiService,不清楚是不是魔法的问题,我直接调用会ping不通,请求超时,必须设置代理。
/**
* openAiService 工厂
* @author WuHao
* @since 202/5/24 10:00
*/
public class AiServiceFactory {
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(10L);
public static OpenAiService createService() {
String token = Optional.ofullable(OpenAiModel.getKeys()).orElseThrow(() -> new RuntimeException(ApiKey不能为空,请检查参数配置)).stream().findFirst().orElse(null);
Assert.notEmpty(token,() -> new RuntimeException(ApiKey不能为空,请检查参数配置));
ObjectMapper mapper = OpenAiService.defaultObjectMapper();
// 设置代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(OpenAiModel.getProxyHost(), OpenAiModel.getProxyPort()));
OkHttpClient client = OpenAiService.defaultClient(token, DEFAULT_TIMEOUT).newBuilder()
.proxy(proxy)
.build();
Retrofit retrofit = OpenAiService.defaultRetrofit(client, mapper);
return new OpenAiService(retrofit.create(OpenAiApi.class), client.dispatcher().executorService());
}
}
测试类
@GetMapping(/testChat)
public String testChat() throws UnsupportedEncodingException {
OpenAiService service = AiServiceFactory.createService();
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), URLDecoder.decode(取一个个字的中文名字,要求姓氏为吴, UTF-8));
messages.add(systemMessage);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
.builder()
.model(gpt-.5-turbo)
.messages(messages)
.n(1)
.maxTokens(50)
.logitBias(new HashMap<>())
.build();
service.streamChatCompletion(chatCompletionRequest)
.doOnError(Throwable::printStackTrace)
.blockingForEach(System.err::println);
service.shutdownExecutor();
return null;
输出结果
流式输出的api可以与sse推送消息,后来写了一个测试页面,实现了打字机的效果,页面十分潦草,将就着看看…………
接口的其他使用方式可下载openai-java源码自行理解,目前我也在学习当中
上述配置中代理指的是魔法的代理地址,先开启魔法
1、网络 -》 右键 属性
2、到Internet选项
、连接 -》 局域网设置
地址 对应的配置 proxyHost
端口 对应的配置 proxyPort
gitee
github
开源项目HugAi聊天知识库,为爱发电:HugAi聊天知识库
ps
没事可以来我的破站逛逛~~欢迎大家
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-09 14:19:36
推荐阅读
留言与评论(共有 14 条评论) |
本站网友 产检项目 | 17分钟前 发表 |
UTF-8)); messages.add(systemMessage); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest .builder() .model(gpt-.5-turbo) .messages(messages) .n(1) .maxTokens(50) .logitBias(new HashMap<>()) .build(); service.streamChatCompletion(chatCompletionRequest) .doOnError(Throwable | |
本站网友 我们傻傻的 | 24分钟前 发表 |
URLDecoder.decode(取一个个字的中文名字,要求姓氏为吴 | |
本站网友 李航 | 3分钟前 发表 |
- sk-xxxxxxxxxxxxxxxxxxxxxxxx 配置类 @Configuration @ConfigurationProperties(prefix = openai) public class OpenAiModel { /** * 代理地址 */ private static String proxyHost; /** * 代理端口 */ private static Integer proxyPort; /** * openai apikey */ private static List<String> keys; // 省略 get set } | |
本站网友 点穴减肥 | 15分钟前 发表 |
OpenAiModel.getProxyPort())); OkHttpClient client = OpenAiService.defaultClient(token | |
本站网友 cuus | 16分钟前 发表 |
7890 keys | |
本站网友 大麦茶怎么泡 | 20分钟前 发表 |
00 */ public class AiServiceFactory { private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(10L); public static OpenAiService createService() { String token = Optional.ofullable(OpenAiModel.getKeys()).orElseThrow(() -> new RuntimeException(ApiKey不能为空,请检查参数配置)).stream().findFirst().orElse(null); Assert.notEmpty(token | |
本站网友 山水倾城 | 13分钟前 发表 |
printStackTrace) .blockingForEach(System.err | |
本站网友 丽时美发 | 28分钟前 发表 |
本站网友 银耳红枣枸杞汤 | 12分钟前 发表 |
new InetSocketAddress(OpenAiModel.getProxyHost() | |
本站网友 阿卡巴大帝 | 9分钟前 发表 |
127.0.0.1 proxyPort | |
本站网友 十滴水2 | 7分钟前 发表 |
- sk-xxxxxxxxxxxxxxxxxxxxxxxx 配置类 @Configuration @ConfigurationProperties(prefix = openai) public class OpenAiModel { /** * 代理地址 */ private static String proxyHost; /** * 代理端口 */ private static Integer proxyPort; /** * openai apikey */ private static List<String> keys; // 省略 get set } | |
本站网友 崇州房产网 | 27分钟前 发表 |
网络 -》 右键 属性 2 | |
本站网友 金牛教育在线 | 10分钟前 发表 |
URLDecoder.decode(取一个个字的中文名字,要求姓氏为吴 |