From dfcfe1c521be0106aec4cf67c3b9e5d0a4048ec9 Mon Sep 17 00:00:00 2001 From: Jonas Israel Date: Wed, 8 Jul 2026 10:47:13 +0200 Subject: [PATCH] chore: [SpringAI] Improve e2e test stability --- .../sap/ai/sdk/app/services/OpenAiService.java | 7 +++++-- .../app/services/OpenAiServiceDeprecated.java | 11 +++++++---- .../sap/ai/sdk/app/services/WeatherMethod.java | 16 +++++----------- .../sdk/app/controllers/SpringAiOpenAiTest.java | 4 ++-- .../controllers/SpringAiOrchestrationTest.java | 4 ++-- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiService.java b/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiService.java index 1b84e56f6..726cea766 100644 --- a/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiService.java +++ b/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiService.java @@ -120,11 +120,14 @@ public OpenAiChatCompletionResponse chatCompletionToolExecution( final var messages = new ArrayList(); messages.add(OpenAiMessage.user("What's the weather in %s in %s?".formatted(location, unit))); + record WeatherRequest(String location, WeatherMethod.Unit unit) {} + // 1. Define the function final List tools = List.of( - OpenAiTool.forFunction(WeatherMethod::getCurrentWeather) - .withArgument(WeatherMethod.Request.class) + OpenAiTool.forFunction( + (WeatherRequest r) -> WeatherMethod.getCurrentWeather(r.location(), r.unit())) + .withArgument(WeatherRequest.class) .withName("weather") .withDescription("Get the weather for the given location")); diff --git a/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiServiceDeprecated.java b/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiServiceDeprecated.java index ceccf95da..b81df3efb 100644 --- a/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiServiceDeprecated.java +++ b/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiServiceDeprecated.java @@ -107,8 +107,10 @@ public OpenAiChatCompletionOutput chatCompletionImage(@Nonnull final String link public OpenAiChatCompletionOutput chatCompletionToolExecution( @Nonnull final String location, @Nonnull final String unit) { + record WeatherRequest(String location, WeatherMethod.Unit unit) {} + // 1. Define the function - final Map schemaMap = generateSchema(WeatherMethod.Request.class); + final Map schemaMap = generateSchema(WeatherRequest.class); final var function = new OpenAiChatCompletionFunction() .setName("weather") @@ -133,9 +135,10 @@ public OpenAiChatCompletionOutput chatCompletionToolExecution( // 2. Optionally, execute the function. final OpenAiChatToolCall toolCall = response.getChoices().get(0).getMessage().getToolCalls().get(0); - final WeatherMethod.Request arguments = - parseJson(toolCall.getFunction().getArguments(), WeatherMethod.Request.class); - final WeatherMethod.Response currentWeather = WeatherMethod.getCurrentWeather(arguments); + final WeatherRequest arguments = + parseJson(toolCall.getFunction().getArguments(), WeatherRequest.class); + final WeatherMethod.Response currentWeather = + WeatherMethod.getCurrentWeather(arguments.location(), arguments.unit()); final OpenAiChatMessage.OpenAiChatAssistantMessage assistantMessage = response.getChoices().get(0).getMessage(); diff --git a/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/WeatherMethod.java b/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/WeatherMethod.java index 9e1a7decd..cadc7b0c0 100644 --- a/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/WeatherMethod.java +++ b/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/WeatherMethod.java @@ -16,14 +16,6 @@ enum Unit { F } - /** - * Request for the weather - * - * @param location the city - * @param unit the unit of temperature - */ - record Request(String location, Unit unit) {} - /** * Response for the weather * @@ -35,8 +27,10 @@ record Response(double temp, Unit unit) {} @Nonnull @SuppressWarnings("unused") @Tool(description = "Get the weather in location") - static Response getCurrentWeather(@ToolParam @Nonnull final Request request) { - final int temperature = request.location.hashCode() % 30; - return new Response(temperature, request.unit); + static Response getCurrentWeather( + @ToolParam(description = "the city") @Nonnull final String location, + @ToolParam(description = "the unit of temperature") @Nonnull final Unit unit) { + final int temperature = location.hashCode() % 30; + return new Response(temperature, unit); } } diff --git a/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOpenAiTest.java b/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOpenAiTest.java index 46c6f199b..f4134a5a3 100644 --- a/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOpenAiTest.java +++ b/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOpenAiTest.java @@ -76,9 +76,9 @@ void testToolCallingWithoutExecution() { assertThat(toolCall1.name()).isEqualTo("getCurrentWeather"); assertThat(toolCall2.name()).isEqualTo("getCurrentWeather"); assertThat(toolCall1.arguments()) - .matches("\\{\"arg0\": \\{\"location\": \"[^\"]*Potsdam[^\"]*\", \"unit\": \"C\"}}"); + .matches("\\{\"arg0\": \"[^\"]*Potsdam[^\"]*\", \"arg1\": \"C\"}"); assertThat(toolCall2.arguments()) - .matches("\\{\"arg0\": \\{\"location\": \"[^\"]*Toulouse[^\"]*\", \"unit\": \"C\"}}"); + .matches("\\{\"arg0\": \"[^\"]*Toulouse[^\"]*\", \"arg1\": \"C\"}"); } @Test diff --git a/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOrchestrationTest.java b/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOrchestrationTest.java index 4727d0ad4..e080ccfc6 100644 --- a/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOrchestrationTest.java +++ b/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOrchestrationTest.java @@ -139,9 +139,9 @@ void testToolCallingWithoutExecution() { assertThat(toolCall1.name()).isEqualTo("getCurrentWeather"); assertThat(toolCall2.name()).isEqualTo("getCurrentWeather"); assertThat(toolCall1.arguments()) - .matches("\\{\"arg0\": \\{\"location\": \"[^\"]*Potsdam[^\"]*\", \"unit\": \"C\"}}"); + .matches("\\{\"arg0\": \"[^\"]*Potsdam[^\"]*\", \"arg1\": \"C\"}"); assertThat(toolCall2.arguments()) - .matches("\\{\"arg0\": \\{\"location\": \"[^\"]*Toulouse[^\"]*\", \"unit\": \"C\"}}"); + .matches("\\{\"arg0\": \"[^\"]*Toulouse[^\"]*\", \"arg1\": \"C\"}"); } @Test