目录
功能特性
最简使用
进阶使用
函数调用(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();
福利:想要的资料全都有 ,全免费,没有魔法和套路
关注公众号:资源充电吧
点击小卡片关注下,回复:学习