-
Notifications
You must be signed in to change notification settings - Fork 608
/
OpenAiApiToolFunctionCallIT.java
160 lines (129 loc) · 5.92 KB
/
OpenAiApiToolFunctionCallIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright 2023 - 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.openai.api.tool;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage.Role;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage.ToolCall;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest.ToolChoiceBuilder;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest;
import org.springframework.ai.openai.api.OpenAiApi.FunctionTool.Type;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Based on the OpenAI Function Calling tutorial:
* https://platform.openai.com/docs/guides/function-calling/parallel-function-calling
*
* @author Christian Tzolov
*/
@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
public class OpenAiApiToolFunctionCallIT {
private final Logger logger = LoggerFactory.getLogger(OpenAiApiToolFunctionCallIT.class);
MockWeatherService weatherService = new MockWeatherService();
OpenAiApi completionApi = new OpenAiApi(System.getenv("OPENAI_API_KEY"));
@SuppressWarnings("null")
@Test
public void toolFunctionCall() {
// Step 1: send the conversation and available functions to the model
var message = new ChatCompletionMessage("What's the weather like in San Francisco, Tokyo, and Paris?",
Role.USER);
var functionTool = new OpenAiApi.FunctionTool(Type.FUNCTION,
new OpenAiApi.FunctionTool.Function("Get the weather in location. Return temperature in Celsius.",
"getCurrentWeather", ModelOptionsUtils.jsonToMap("""
{
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state e.g. San Francisco, CA"
},
"lat": {
"type": "number",
"description": "The city latitude"
},
"lon": {
"type": "number",
"description": "The city longitude"
},
"unit": {
"type": "string",
"enum": ["C", "F"]
}
},
"required": ["location", "lat", "lon", "unit"]
}
""")));
List<ChatCompletionMessage> messages = new ArrayList<>(List.of(message));
ChatCompletionRequest chatCompletionRequest = new ChatCompletionRequest(messages, "gpt-4o",
List.of(functionTool), ToolChoiceBuilder.AUTO);
// List.of(functionTool), ToolChoiceBuilder.FUNCTION("getCurrentWeather"));
ResponseEntity<ChatCompletion> chatCompletion = completionApi.chatCompletionEntity(chatCompletionRequest);
assertThat(chatCompletion.getBody()).isNotNull();
assertThat(chatCompletion.getBody().choices()).isNotEmpty();
ChatCompletionMessage responseMessage = chatCompletion.getBody().choices().get(0).message();
assertThat(responseMessage.role()).isEqualTo(Role.ASSISTANT);
assertThat(responseMessage.toolCalls()).isNotNull();
// Check if the model wanted to call a function
if (responseMessage.toolCalls() != null) {
// extend conversation with assistant's reply.
messages.add(responseMessage);
// Send the info for each function call and function response to the model.
for (ToolCall toolCall : responseMessage.toolCalls()) {
var functionName = toolCall.function().name();
if ("getCurrentWeather".equals(functionName)) {
MockWeatherService.Request weatherRequest = fromJson(toolCall.function().arguments(),
MockWeatherService.Request.class);
MockWeatherService.Response weatherResponse = weatherService.apply(weatherRequest);
// extend conversation with function response.
messages.add(new ChatCompletionMessage("" + weatherResponse.temp() + weatherRequest.unit(),
Role.TOOL, functionName, toolCall.id(), null));
}
}
var functionResponseRequest = new ChatCompletionRequest(messages, "gpt-4o", 0.5f);
ResponseEntity<ChatCompletion> chatCompletion2 = completionApi
.chatCompletionEntity(functionResponseRequest);
logger.info("Final response: " + chatCompletion2.getBody());
assertThat(chatCompletion2.getBody().choices()).isNotEmpty();
assertThat(chatCompletion2.getBody().choices().get(0).message().role()).isEqualTo(Role.ASSISTANT);
assertThat(chatCompletion2.getBody().choices().get(0).message().content()).contains("San Francisco")
.containsAnyOf("30.0°C", "30°C");
assertThat(chatCompletion2.getBody().choices().get(0).message().content()).contains("Tokyo")
.containsAnyOf("10.0°C", "10°C");
;
assertThat(chatCompletion2.getBody().choices().get(0).message().content()).contains("Paris")
.containsAnyOf("15.0°C", "15°C");
;
}
}
private static <T> T fromJson(String json, Class<T> targetClass) {
try {
return new ObjectMapper().readValue(json, targetClass);
}
catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}