Skip to content

Commit 77a80db

Browse files
committed
📝 更新函数调用文档
1 parent 2c7a576 commit 77a80db

File tree

5 files changed

+220
-4
lines changed

5 files changed

+220
-4
lines changed

README.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ maven
6666
<dependency>
6767
<groupId>com.github.plexpt</groupId>
6868
<artifactId>chatgpt</artifactId>
69-
<version>4.1.0</version>
69+
<version>4.1.1</version>
7070
</dependency>
7171
```
7272

7373
gradle
7474
```
75-
implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.1.0'
75+
implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.1.1'
7676
```
7777

7878

@@ -127,6 +127,95 @@ implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.1.0'
127127
Message res = response.getChoices().get(0).getMessage();
128128
System.out.println(res);
129129

130+
```
131+
### 函数调用(Function Call)
132+
133+
```java
134+
//国内需要代理 国外不需要
135+
Proxy proxy = Proxys.http("127.0.0.1", 1080);
136+
137+
chatGPT = ChatGPT.builder()
138+
.apiKey("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")
139+
.timeout(900)
140+
.proxy(proxy)
141+
.apiHost("https://api.openai.com/") //代理地址
142+
.build()
143+
.init();
144+
145+
List<ChatFunction> functions = new ArrayList<>();
146+
ChatFunction function = new ChatFunction();
147+
function.setName("getCurrentWeather");
148+
function.setDescription("获取给定位置的当前天气");
149+
function.setParameters(ChatFunction.ChatParameter.builder()
150+
.type("object")
151+
.required(Arrays.asList("location"))
152+
.properties(JSON.parseObject("{\n" +
153+
" \"location\": {\n" +
154+
" \"type\": \"string\",\n" +
155+
" \"description\": \"The city and state, e.g. San Francisco, " +
156+
"CA\"\n" +
157+
" },\n" +
158+
" \"unit\": {\n" +
159+
" \"type\": \"string\",\n" +
160+
" \"enum\": [\"celsius\", \"fahrenheit\"]\n" +
161+
" }\n" +
162+
" }"))
163+
.build());
164+
functions.add(function);
165+
166+
Message message = Message.of("上海的天气怎么样?");
167+
ChatCompletion chatCompletion = ChatCompletion.builder()
168+
.model(ChatCompletion.Model.GPT_3_5_TURBO_0613.getName())
169+
.messages(Arrays.asList(message))
170+
.functions(functions)
171+
.maxTokens(8000)
172+
.temperature(0.9)
173+
.build();
174+
ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);
175+
ChatChoice choice = response.getChoices().get(0);
176+
Message res = choice.getMessage();
177+
System.out.println(res);
178+
if ("function_call".equals(choice.getFinishReason())) {
179+
180+
FunctionCallResult functionCall = res.getFunctionCall();
181+
String functionCallName = functionCall.getName();
182+
183+
if ("getCurrentWeather".equals(functionCallName)) {
184+
String arguments = functionCall.getArguments();
185+
JSONObject jsonObject = JSON.parseObject(arguments);
186+
String location = jsonObject.getString("location");
187+
String unit = jsonObject.getString("unit");
188+
String weather = getCurrentWeather(location, unit);
189+
190+
callWithWeather(weather, res, functions);
191+
}
192+
}
193+
194+
195+
private void callWithWeather(String weather, Message res, List<ChatFunction> functions) {
196+
197+
198+
Message message = Message.of("上海的天气怎么样?");
199+
Message function1 = Message.ofFunction(weather);
200+
function1.setName("getCurrentWeather");
201+
ChatCompletion chatCompletion = ChatCompletion.builder()
202+
.model(ChatCompletion.Model.GPT_3_5_TURBO_0613.getName())
203+
.messages(Arrays.asList(message, res, function1))
204+
.functions(functions)
205+
.maxTokens(8000)
206+
.temperature(0.9)
207+
.build();
208+
ChatCompletionResponse response = chatGPT.chatCompletion(chatCompletion);
209+
ChatChoice choice = response.getChoices().get(0);
210+
Message res2 = choice.getMessage();
211+
//上海目前天气晴朗,气温为 22 摄氏度。
212+
System.out.println(res2.getContent());
213+
}
214+
215+
public String getCurrentWeather(String location, String unit) {
216+
return "{ \"temperature\": 22, \"unit\": \"celsius\", \"description\": \"晴朗\" }";
217+
}
218+
130219
```
131220

132221
### 流式使用

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.plexpt</groupId>
77
<artifactId>chatgpt</artifactId>
8-
<version>4.1.0</version>
8+
<version>4.1.1</version>
99
<name>chatgpt</name>
1010
<description>ChatGPT4.0、 ChatGPT Java SDK.</description>
1111
<url>https://chat.plexpt.com</url>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.plexpt.chatgpt.entity.chat;
2+
3+
4+
import lombok.Data;
5+
6+
@Data
7+
public class FunctionCallResult {
8+
9+
String name;
10+
11+
String arguments;
12+
}

src/main/java/com/plexpt/chatgpt/entity/chat/Message.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Message {
2626
private String name;
2727

2828
@JsonProperty("function_call")
29-
private String functionCall;
29+
private FunctionCallResult functionCall;
3030

3131
public Message(String role, String content) {
3232
this.role = role;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)