文末送资料|跟着开源学技术-ChatGPT开源项目-chatgpt-java

2024-04-25 20:52

本文主要是介绍文末送资料|跟着开源学技术-ChatGPT开源项目-chatgpt-java,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

目录

功能特性

最简使用

进阶使用

函数调用(Function Call)

流式使用

流式配合Spring SseEmitter使用

多KEY自动轮询

大家好,我是充电君

   今天带着大家来看个Java版本的ChatGPT。这个开源项目就是chatgpt-java。

Github: https://github.com/PlexPt/chatgpt-java

Chatgpt-Java是基于OpenAi官方api开发的Java版OpenAi SDK,帮助Java开发者更加快速、简洁、灵活高效的将OpenAi继承到项目中。ChatGPT Java 版本,OpenAI ChatGPT 的逆向工程 SDK,可扩展用于聊天机器人等。

功能特性

功能特性
GPT 3.5支持
GPT 4.0支持
函数调用支持
流式对话支持
阻塞式对话支持
前端
上下文支持
计算Token用jtokkit
多KEY轮询支持
代理支持
反向代理支持

maven

<dependency><groupId>com.github.plexpt</groupId><artifactId>chatgpt</artifactId><version>4.4.0</version>
</dependency>

gradle

implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.4.0'

最简使用

//国内需要代理Proxy proxy = Proxys.http("127.0.0.1", 1081);//socks5 代理// Proxy proxy = Proxys.socks5("127.0.0.1", 1080);ChatGPT chatGPT = ChatGPT.builder().apiKey("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").proxy(proxy).apiHost("https://api.openai.com/") //反向代理地址.build().init();String res = chatGPT.chat("写一段七言绝句诗,题目是:火锅!");

System.out.println(res);

进阶使用

   //国内需要代理 国外不需要Proxy proxy = Proxys.http("127.0.0.1", 1080);ChatGPT chatGPT = ChatGPT.builder().apiKey("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").proxy(proxy).timeout(900).apiHost("https://api.openai.com/") //反向代理地址.build().init();Message system = Message.ofSystem("你现在是一个诗人,专门写七言绝句");Message message = Message.of("写一段七言绝句诗,题目是:火锅!");ChatCompletion chatCompletion = ChatCompletion.builder().model(ChatCompletion.Model.GPT_3_5_TURBO.getName()).messages(Arrays.asList(system, message)).maxTokens(3000).temperature(0.9).build();ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);Message res = response.getChoices().get(0).getMessage();System.out.println(res);
   

函数调用(Function Call)

//国内需要代理 国外不需要Proxy proxy = Proxys.http("127.0.0.1", 1080);chatGPT = ChatGPT.builder().apiKey("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").timeout(900).proxy(proxy).apiHost("https://api.openai.com/") //代理地址.build().init();List<ChatFunction> functions = new ArrayList<>();ChatFunction function = new ChatFunction();function.setName("getCurrentWeather");function.setDescription("获取给定位置的当前天气");function.setParameters(ChatFunction.ChatParameter.builder().type("object").required(Arrays.asList("location")).properties(JSON.parseObject("{\n" +"          \"location\": {\n" +"            \"type\": \"string\",\n" +"            \"description\": \"The city and state, e.g. San Francisco, " +"CA\"\n" +"          },\n" +"          \"unit\": {\n" +"            \"type\": \"string\",\n" +"            \"enum\": [\"celsius\", \"fahrenheit\"]\n" +"          }\n" +"        }")).build());functions.add(function);Message message = Message.of("上海的天气怎么样?");ChatCompletion chatCompletion = ChatCompletion.builder().model(ChatCompletion.Model.GPT_3_5_TURBO_0613.getName()).messages(Arrays.asList(message)).functions(functions).maxTokens(8000).temperature(0.9).build();ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);ChatChoice choice = response.getChoices().get(0);Message res = choice.getMessage();System.out.println(res);if ("function_call".equals(choice.getFinishReason())) {FunctionCallResult functionCall = res.getFunctionCall();String functionCallName = functionCall.getName();if ("getCurrentWeather".equals(functionCallName)) {String arguments = functionCall.getArguments();JSONObject jsonObject = JSON.parseObject(arguments);String location = jsonObject.getString("location");String unit = jsonObject.getString("unit");String weather = getCurrentWeather(location, unit);callWithWeather(weather, res, functions);}}private void callWithWeather(String weather, Message res, List<ChatFunction> functions) {Message message = Message.of("上海的天气怎么样?");Message function1 = Message.ofFunction(weather);function1.setName("getCurrentWeather");ChatCompletion chatCompletion = ChatCompletion.builder().model(ChatCompletion.Model.GPT_3_5_TURBO_0613.getName()).messages(Arrays.asList(message, res, function1)).functions(functions).maxTokens(8000).temperature(0.9).build();ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);ChatChoice choice = response.getChoices().get(0);Message res2 = choice.getMessage();//上海目前天气晴朗,气温为 22 摄氏度。System.out.println(res2.getContent());}public String getCurrentWeather(String location, String unit) {return "{ \"temperature\": 22, \"unit\": \"celsius\", \"description\": \"晴朗\" }";}

流式使用

    //国内需要代理 国外不需要Proxy proxy = Proxys.http("127.0.0.1", 1080);ChatGPTStream chatGPTStream = ChatGPTStream.builder().timeout(600).apiKey("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").proxy(proxy).apiHost("https://api.openai.com/").build().init();ConsoleStreamListener listener = new ConsoleStreamListener();Message message = Message.of("写一段七言绝句诗,题目是:火锅!");ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();
chatGPTStream.streamChatCompletion(chatCompletion, listener);、

流式配合Spring SseEmitter使用

参考 SseStreamListener

你可能在找这个,参考Demo https://github.com/PlexPt/chatgpt-online-springboot

@GetMapping("/chat/sse")@CrossOriginpublic SseEmitter sseEmitter(String prompt) {//国内需要代理 国外不需要Proxy proxy = Proxys.http("127.0.0.1", 1080);ChatGPTStream chatGPTStream = ChatGPTStream.builder().timeout(600).apiKey("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").proxy(proxy).apiHost("https://api.openai.com/").build().init();SseEmitter sseEmitter = new SseEmitter(-1L);SseStreamListener listener = new SseStreamListener(sseEmitter);Message message = Message.of(prompt);listener.setOnComplate(msg -> {//回答完成,可以做一些事情});chatGPTStream.streamChatCompletion(Arrays.asList(message), listener);return sseEmitter;}

多KEY自动轮询

只需替换chatGPT构造部分

chatGPT = ChatGPT.builder().apiKeyList(// 从数据库或其他地方取出多个KEYArrays.asList("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","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","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","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",)).timeout(900).proxy(proxy).apiHost("https://api.openai.com/") //代理地址.build()

.init();

图片

福利:想要的资料全都有 ,全免费,没有魔法和套路

关注公众号:资源充电吧


点击小卡片关注下,回复:学习

这篇关于文末送资料|跟着开源学技术-ChatGPT开源项目-chatgpt-java的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/935767

相关文章

springboot jsp jar 部署支持指定 jsp 位置

1. spring boot 支持 jsp pom: <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>javax.servlet</groupId><artifact

Druid 的整合 springboot mybaits

创建 SpringBoot 项目        在整合 Druid 之前,需要先创建一个 SpringBoot 和 MyBatis 的项目,先来观察一下,它默认是否使用了数据库连接池,使用了什么数据库连接池。然后,再来整合 Druid 这款数据库连接池到项目当中。        创建 SpringBoot 和 Mybatis 的项目很简单,通过向导即可完成( 完整项目项目www.fhadmin

SpringBoot Filter 过滤

1.通过扫描注解完成Filter组件注册 创建一个类,实现Filter接口,实现doFilter()方法 在该类使用注解@WebFilter,设置filterName与urlPatterns 在doFilter中编写代码 编写启动类:增加注解@ServletComponentScan /*** SpringBoot整合Filter 方式一 项目 www.fhadmin.org*

springboot 引用 jar 包分离部署

maven 打包时依赖 jar 包分离, pom 添加 <!--依赖复制到lib--> <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>copy</id><phase>package</ph

spring cloud zuul网关路由 报 500

经过仔细检查,全部配置映射都没问题,springboot-admin 监控中心所有服务状态全部显示绿色正常 但就是通过 zuul 访问服务提示 500错误 这个情况,一般是请求超时所致 调整请求超时时间即可 zuul服务本地报错提示:com.netflix.zuul.exception.ZuulException: Forwarding error 原始配置示例: hystrix:co

springboot和传统springmvc区别

一、概念 1、Spring Spring是一个开源容器框架,可以接管web层,业务层,dao层,持久层的组件,并且可以配置各种bean,和维护bean与bean之间的关系。其核心就是控制反转(IOC),和面向切面(AOP),简单的说就是一个分层的轻量级开源框架。 2、SpringMVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flo

java接口中 定义 private 私有方法

在传统的Java编程中,被广为人知的一个知识点是:java Interface接口中不能定义private私有方法。只允许我们定义public访问权限的方法、抽象方法或静态方法。但是从Java 9 开始,Interface 接口中允许定义私有方法和私有静态方法。下面我们就来为大家介绍其语法规则,和为什么要有这样的设计。 其实在Java 8之前,还有另一个被广为人之的知识点:接口中所有的方法必须

spring boot 修改打包后 jar 或者 war 的项目名称

配置下 pom.xml 文件  <build><finalName>fhadmin</finalName><plugins><plugin><!-- springboot项目www.fhadmin.org --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><

大尺寸测量技术 外径宽度长度 多种大尺寸测量仪应用

在现代工业和科学领域中,大尺寸测量技术扮演着至关重要的角色。无论是管材、棒材、板材还是型材等,都有一些尺寸大的产品在生产生活中扮演着重要角色,然而这些大尺寸产品的检测,又无疑面临着诸多困难,而智能在线大尺寸测量仪提供了强有力的支持。 针对多种大尺寸产品的测量,蓝鹏有诸多解决方案,通过双测头平行光原理、光电广角测头原理、激光测量原理、机器视觉原理等,为产线提供高度精确和可靠的精密仪器。可对外径、宽度

【第16章】spring-mvc之多文件上传

文章目录 前言一、文件大小限制二、前端三、后端总结 前言 本章在上篇文件的上传基础上就行扩展,多文件上传、多线程处理,所有的文件上传成功则返回成功。 一、文件大小限制 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"xmlns:xsi=