Remove unnecessary if statement in test code

* Remove unnecessary if statement in 'toolFunctionCall' test code from OpenAiApiToolFunctionCallIT
* Reformat using java-format plugin
This commit is contained in:
Seongmin Kim
2024-08-20 04:40:41 +09:00
committed by GitHub
parent c2b6e85608
commit 58c292ab87

View File

@@ -102,50 +102,46 @@ public class OpenAiApiToolFunctionCallIT {
ChatCompletionMessage responseMessage = chatCompletion.getBody().choices().get(0).message();
// Check if the model wanted to call a function
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);
// 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);
// 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);
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, null));
}
// extend conversation with function response.
messages.add(new ChatCompletionMessage("" + weatherResponse.temp() + weatherRequest.unit(), Role.TOOL,
functionName, toolCall.id(), null, 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");
;
}
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) {