Skip to content

Commit

Permalink
Remove unnecessary if statement in test code
Browse files Browse the repository at this point in the history
* Remove unnecessary if statement in 'toolFunctionCall' test code from OpenAiApiToolFunctionCallIT
* Reformat using java-format plugin
  • Loading branch information
fing9 committed Aug 19, 2024
1 parent c2b6e85 commit 58c292a
Showing 1 changed file with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,49 +102,45 @@ public void toolFunctionCall() {

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);
var functionResponseRequest = new ChatCompletionRequest(messages, "gpt-4o", 0.5f);

ResponseEntity<ChatCompletion> chatCompletion2 = completionApi
.chatCompletionEntity(functionResponseRequest);
ResponseEntity<ChatCompletion> chatCompletion2 = completionApi.chatCompletionEntity(functionResponseRequest);

logger.info("Final response: " + chatCompletion2.getBody());
logger.info("Final response: " + chatCompletion2.getBody());

assertThat(chatCompletion2.getBody().choices()).isNotEmpty();
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");
;
}
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");
;

}

Expand Down

0 comments on commit 58c292a

Please sign in to comment.