From 58c292ab875a2be061263fe43fff18a033b6f1fb Mon Sep 17 00:00:00 2001 From: Seongmin Kim <44383895+fing9@users.noreply.github.com> Date: Tue, 20 Aug 2024 04:40:41 +0900 Subject: [PATCH] Remove unnecessary if statement in test code * Remove unnecessary if statement in 'toolFunctionCall' test code from OpenAiApiToolFunctionCallIT * Reformat using java-format plugin --- .../api/tool/OpenAiApiToolFunctionCallIT.java | 58 +++++++++---------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/tool/OpenAiApiToolFunctionCallIT.java b/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/tool/OpenAiApiToolFunctionCallIT.java index 1753b6eaca..0a02361080 100644 --- a/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/tool/OpenAiApiToolFunctionCallIT.java +++ b/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/tool/OpenAiApiToolFunctionCallIT.java @@ -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 chatCompletion2 = completionApi - .chatCompletionEntity(functionResponseRequest); + ResponseEntity 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"); + ; }