|
| 1 | +package com.plexpt.chatgpt; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.alibaba.fastjson.JSONObject; |
| 5 | +import com.plexpt.chatgpt.entity.chat.ChatChoice; |
| 6 | +import com.plexpt.chatgpt.entity.chat.ChatCompletion; |
| 7 | +import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse; |
| 8 | +import com.plexpt.chatgpt.entity.chat.ChatFunction; |
| 9 | +import com.plexpt.chatgpt.entity.chat.FunctionCallResult; |
| 10 | +import com.plexpt.chatgpt.entity.chat.Message; |
| 11 | +import com.plexpt.chatgpt.util.Proxys; |
| 12 | + |
| 13 | +import org.junit.Before; |
| 14 | + |
| 15 | +import java.net.Proxy; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +public class FunctionCallTest { |
| 21 | + |
| 22 | + private ChatGPT chatGPT; |
| 23 | + |
| 24 | + @Before |
| 25 | + public void before() { |
| 26 | + Proxy proxy = Proxys.http("127.0.0.1", 1080); |
| 27 | + |
| 28 | + chatGPT = ChatGPT.builder() |
| 29 | + .apiKey("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") |
| 30 | + .timeout(900) |
| 31 | + .proxy(proxy) |
| 32 | + .apiHost("https://api.openai.com/") //代理地址 |
| 33 | + .build() |
| 34 | + .init(); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @org.junit.Test |
| 39 | + public void chat() { |
| 40 | + List<ChatFunction> functions = new ArrayList<>(); |
| 41 | + ChatFunction function = new ChatFunction(); |
| 42 | + function.setName("getCurrentWeather"); |
| 43 | + function.setDescription("获取给定位置的当前天气"); |
| 44 | + function.setParameters(ChatFunction.ChatParameter.builder() |
| 45 | + .type("object") |
| 46 | + .required(Arrays.asList("location")) |
| 47 | + .properties(JSON.parseObject("{\n" + |
| 48 | + " \"location\": {\n" + |
| 49 | + " \"type\": \"string\",\n" + |
| 50 | + " \"description\": \"The city and state, e.g. San Francisco, " + |
| 51 | + "CA\"\n" + |
| 52 | + " },\n" + |
| 53 | + " \"unit\": {\n" + |
| 54 | + " \"type\": \"string\",\n" + |
| 55 | + " \"enum\": [\"celsius\", \"fahrenheit\"]\n" + |
| 56 | + " }\n" + |
| 57 | + " }")) |
| 58 | + .build()); |
| 59 | + functions.add(function); |
| 60 | + |
| 61 | + Message message = Message.of("上海的天气怎么样?"); |
| 62 | + ChatCompletion chatCompletion = ChatCompletion.builder() |
| 63 | + .model(ChatCompletion.Model.GPT_3_5_TURBO_0613.getName()) |
| 64 | + .messages(Arrays.asList(message)) |
| 65 | + .functions(functions) |
| 66 | + .maxTokens(8000) |
| 67 | + .temperature(0.9) |
| 68 | + .build(); |
| 69 | + ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion); |
| 70 | + ChatChoice choice = response.getChoices().get(0); |
| 71 | + Message res = choice.getMessage(); |
| 72 | + System.out.println(res); |
| 73 | + if ("function_call".equals(choice.getFinishReason())) { |
| 74 | + |
| 75 | + FunctionCallResult functionCall = res.getFunctionCall(); |
| 76 | + String functionCallName = functionCall.getName(); |
| 77 | + |
| 78 | + if ("getCurrentWeather".equals(functionCallName)) { |
| 79 | + String arguments = functionCall.getArguments(); |
| 80 | + JSONObject jsonObject = JSON.parseObject(arguments); |
| 81 | + String location = jsonObject.getString("location"); |
| 82 | + String unit = jsonObject.getString("unit"); |
| 83 | + String weather = getCurrentWeather(location, unit); |
| 84 | + |
| 85 | + callWithWeather(weather, res, functions); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + private void callWithWeather(String weather, Message res, List<ChatFunction> functions) { |
| 93 | + |
| 94 | + |
| 95 | + Message message = Message.of("上海的天气怎么样?"); |
| 96 | + Message function1 = Message.ofFunction(weather); |
| 97 | + function1.setName("getCurrentWeather"); |
| 98 | + ChatCompletion chatCompletion = ChatCompletion.builder() |
| 99 | + .model(ChatCompletion.Model.GPT_3_5_TURBO_0613.getName()) |
| 100 | + .messages(Arrays.asList(message, res, function1)) |
| 101 | + .functions(functions) |
| 102 | + .maxTokens(8000) |
| 103 | + .temperature(0.9) |
| 104 | + .build(); |
| 105 | + ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion); |
| 106 | + ChatChoice choice = response.getChoices().get(0); |
| 107 | + Message res2 = choice.getMessage(); |
| 108 | + //上海目前天气晴朗,气温为 22 摄氏度。 |
| 109 | + System.out.println(res2.getContent()); |
| 110 | + } |
| 111 | + |
| 112 | + public String getCurrentWeather(String location, String unit) { |
| 113 | + return "{ \"temperature\": 22, \"unit\": \"celsius\", \"description\": \"晴朗\" }"; |
| 114 | + } |
| 115 | +} |
0 commit comments