Skip to content

Commit a6a209b

Browse files
committed
Create schema with jackson
1 parent fd6acdc commit a6a209b

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

sample-code/spring-app/pom.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
<dependency>
170170
<groupId>com.fasterxml.jackson.core</groupId>
171171
<artifactId>jackson-core</artifactId>
172-
<scope>provided</scope>
172+
<scope>compile</scope>
173173
</dependency>
174174
<!-- scope "test" -->
175175
<dependency>
@@ -186,6 +186,15 @@
186186
<groupId>com.sap.cloud.sdk.cloudplatform</groupId>
187187
<artifactId>cloudplatform-connectivity</artifactId>
188188
</dependency>
189+
<dependency>
190+
<groupId>com.fasterxml.jackson.module</groupId>
191+
<artifactId>jackson-module-jsonSchema</artifactId>
192+
<version>2.18.2</version>
193+
</dependency>
194+
<dependency>
195+
<groupId>io.vavr</groupId>
196+
<artifactId>vavr</artifactId>
197+
</dependency>
189198
</dependencies>
190199

191200
<build>
@@ -220,6 +229,20 @@
220229
<skipAddThirdParty>true</skipAddThirdParty>
221230
</configuration>
222231
</plugin>
232+
<plugin>
233+
<groupId>org.apache.maven.plugins</groupId>
234+
<artifactId>maven-dependency-plugin</artifactId>
235+
<executions>
236+
<execution>
237+
<id>analyze</id>
238+
<configuration>
239+
<ignoredDependencies>
240+
<ignoredDependency>com.fasterxml.jackson.core:jackson-core</ignoredDependency>
241+
</ignoredDependencies>
242+
</configuration>
243+
</execution>
244+
</executions>
245+
</plugin>
223246
</plugins>
224247
</build>
225248
</project>

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiService.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionTool.ToolType.FUNCTION;
77

88
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
910
import com.sap.ai.sdk.core.AiCoreService;
1011
import com.sap.ai.sdk.foundationmodels.openai.OpenAiClient;
1112
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionDelta;
@@ -16,6 +17,7 @@
1617
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatMessage;
1718
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiEmbeddingOutput;
1819
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiEmbeddingParameters;
20+
import io.vavr.control.Try;
1921
import java.util.ArrayList;
2022
import java.util.List;
2123
import java.util.Map;
@@ -125,18 +127,20 @@ public OpenAiChatCompletionOutput chatCompletionTools(final int months) {
125127
@Nonnull
126128
public OpenAiChatCompletionOutput chatCompletionToolExecution(
127129
@Nonnull final String location, @Nonnull final String unit) {
130+
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));
138+
128139
final var function =
129140
new OpenAiChatCompletionFunction()
130141
.setName("weather")
131142
.setDescription("Get the weather for the given location")
132-
.setParameters(
133-
Map.of(
134-
"type",
135-
"object",
136-
"properties",
137-
Map.of(
138-
"location", Map.of("type", "string"),
139-
"unit", Map.of("type", "string", "enum", List.of("C", "F")))));
143+
.setParameters(schemaMap);
140144
final var tool = new OpenAiChatCompletionTool().setType(FUNCTION).setFunction(function);
141145

142146
final var messages = new ArrayList<OpenAiChatMessage>();

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/services/OpenAiServiceV2.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fasterxml.jackson.core.JsonProcessingException;
99
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
1011
import com.sap.ai.sdk.core.AiCoreService;
1112
import com.sap.ai.sdk.foundationmodels.openai.OpenAiChatCompletionDelta;
1213
import com.sap.ai.sdk.foundationmodels.openai.OpenAiChatCompletionRequest;
@@ -27,6 +28,7 @@
2728
import com.sap.ai.sdk.foundationmodels.openai.generated.model.CreateChatCompletionRequest;
2829
import com.sap.ai.sdk.foundationmodels.openai.generated.model.CreateChatCompletionResponse;
2930
import com.sap.ai.sdk.foundationmodels.openai.generated.model.FunctionObject;
31+
import io.vavr.control.Try;
3032
import java.util.List;
3133
import java.util.Map;
3234
import java.util.stream.Stream;
@@ -132,18 +134,19 @@ public OpenAiChatCompletionResponse chatCompletionTools(final int months) {
132134
public CreateChatCompletionResponse chatCompletionToolExecution(
133135
@Nonnull final String location, @Nonnull final String unit) {
134136

137+
final var schemaMap =
138+
Try.of(() -> new JsonSchemaGenerator(JACKSON).generateSchema(WeatherMethod.Request.class))
139+
.map(schema -> JACKSON.convertValue(schema, Map.class))
140+
.getOrElseThrow(
141+
e ->
142+
new IllegalArgumentException(
143+
"Could not generate schema for WeatherMethod.Request", e));
144+
135145
final var function =
136146
new FunctionObject()
137147
.name("weather")
138148
.description("Get the weather for the given location")
139-
.parameters(
140-
Map.of(
141-
"type",
142-
"object",
143-
"properties",
144-
Map.of(
145-
"location", Map.of("type", "string"),
146-
"unit", Map.of("type", "string", "enum", List.of("C", "F")))));
149+
.parameters(schemaMap);
147150

148151
final var tool = new ChatCompletionTool().type(FUNCTION).function(function);
149152

0 commit comments

Comments
 (0)