|
5 | 5 | import static com.sap.ai.sdk.foundationmodels.openai.OpenAiModel.TEXT_EMBEDDING_3_SMALL; |
6 | 6 | import static com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionTool.ToolType.FUNCTION; |
7 | 7 |
|
| 8 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 9 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 10 | +import com.fasterxml.jackson.databind.JsonMappingException; |
8 | 11 | import com.fasterxml.jackson.databind.ObjectMapper; |
9 | 12 | import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; |
10 | 13 | import com.sap.ai.sdk.core.AiCoreService; |
|
17 | 20 | import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatMessage; |
18 | 21 | import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiEmbeddingOutput; |
19 | 22 | import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiEmbeddingParameters; |
20 | | -import io.vavr.control.Try; |
21 | 23 | import java.util.ArrayList; |
22 | 24 | import java.util.List; |
23 | 25 | import java.util.Map; |
@@ -128,13 +130,14 @@ public OpenAiChatCompletionOutput chatCompletionTools(final int months) { |
128 | 130 | public OpenAiChatCompletionOutput chatCompletionToolExecution( |
129 | 131 | @Nonnull final String location, @Nonnull final String unit) { |
130 | 132 |
|
131 | | - final var schemaMap = |
132 | | - Try.of(() -> new JsonSchemaGenerator(JACKSON).generateSchema(WeatherMethod.Request.class)) |
133 | | - .map(schema -> JACKSON.convertValue(schema, Map.class)) |
134 | | - .getOrElseThrow( |
135 | | - e -> |
136 | | - new IllegalArgumentException( |
137 | | - "Could not generate schema for WeatherMethod.Request", e)); |
| 133 | + final var jsonSchemaGenerator = new JsonSchemaGenerator(JACKSON); |
| 134 | + Map<String, Object> schemaMap; |
| 135 | + try { |
| 136 | + final var schema = jsonSchemaGenerator.generateSchema(WeatherMethod.Request.class); |
| 137 | + schemaMap = JACKSON.convertValue(schema, new TypeReference<>() {}); |
| 138 | + } catch (JsonMappingException e) { |
| 139 | + throw new IllegalArgumentException("Could not generate schema for WeatherMethod.Request", e); |
| 140 | + } |
138 | 141 |
|
139 | 142 | final var function = |
140 | 143 | new OpenAiChatCompletionFunction() |
@@ -163,7 +166,7 @@ public OpenAiChatCompletionOutput chatCompletionToolExecution( |
163 | 166 | JACKSON.readValue(toolCall.getFunction().getArguments(), WeatherMethod.Request.class); |
164 | 167 | final var toolResponse = new WeatherMethod().getCurrentWeather(weatherRequest); |
165 | 168 | toolResponseJson = JACKSON.writeValueAsString(toolResponse); |
166 | | - } catch (Exception e) { |
| 169 | + } catch (JsonProcessingException e) { |
167 | 170 | throw new IllegalArgumentException("Error parsing tool call arguments", e); |
168 | 171 | } |
169 | 172 |
|
|
0 commit comments