|
12 | 12 | - [Data Masking](#data-masking) |
13 | 13 | - [Grounding](#grounding) |
14 | 14 | - [Stream chat completion](#stream-chat-completion) |
| 15 | + - [Add images and multiple text inputs to a message](#add-images-and-multiple-text-inputs-to-a-message) |
| 16 | + - [Set a Response Format](#set-a-response-format) |
15 | 17 | - [Set Model Parameters](#set-model-parameters) |
16 | 18 | - [Using a Configuration from AI Launchpad](#using-a-configuration-from-ai-launchpad) |
17 | 19 |
|
@@ -300,6 +302,73 @@ Note, that only user and system messages are supported for multiple text inputs. |
300 | 302 | Please find [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java). |
301 | 303 |
|
302 | 304 |
|
| 305 | +## Set a Response Format |
| 306 | + |
| 307 | +It is possible to set the response format for the chat completion. Available options are using `JSON_OBJECT`, `JSON_SCHEMA`, and `TEXT`, where `TEXT` is the default behavior. |
| 308 | + |
| 309 | +### JSON_OBJECT |
| 310 | + |
| 311 | +Setting the response format to `JSON_OBJECT` tells the AI to respond with JSON, i.e., the response from the AI will be a string consisting of a valid JSON. This does, however, not guarantee that the response adheres to a specific structure (other than being valid JSON). |
| 312 | + |
| 313 | +```java |
| 314 | +var template = Message.user("What is 'apple' in German?"); |
| 315 | +var templatingConfig = |
| 316 | + Template.create() |
| 317 | + .template(List.of(template.createChatMessage())) |
| 318 | + .responseFormat( |
| 319 | + ResponseFormatJsonObject.create() |
| 320 | + .type(ResponseFormatJsonObject.TypeEnum.JSON_OBJECT)); |
| 321 | +var configWithTemplate = llmWithImageSupportConfig.withTemplateConfig(templatingConfig); |
| 322 | + |
| 323 | +var prompt = |
| 324 | + new OrchestrationPrompt( |
| 325 | + Message.system( |
| 326 | + "You are a language translator. Answer using the following JSON format: {\"language\": ..., \"translation\": ...}")); |
| 327 | +var response = client.chatCompletion(prompt, configWithTemplate).getContent(); |
| 328 | +``` |
| 329 | +Note, that it is necessary to tell the AI model to actually return a JSON object in the prompt. The result might not adhere exactly to the given JSON format, but it will be a JSON object. |
| 330 | + |
| 331 | + |
| 332 | +### JSON_SCHEMA |
| 333 | + |
| 334 | +If you want the response to not only consist of valid JSON but additionally adhere to a specific JSON schema, you can use `JSON_SCHEMA`. in order to do that, add a JSON schema to the configuration as shown below and the response will adhere to the given schema. |
| 335 | + |
| 336 | +```java |
| 337 | +var template = Message.user("Whats '%s' in German?".formatted(word)); |
| 338 | +var schema = |
| 339 | + Map.of( |
| 340 | + "type", |
| 341 | + "object", |
| 342 | + "properties", |
| 343 | + Map.of( |
| 344 | + "language", Map.of("type", "string"), |
| 345 | + "translation", Map.of("type", "string")), |
| 346 | + "required", |
| 347 | + List.of("language", "translation"), |
| 348 | + "additionalProperties", |
| 349 | + false); |
| 350 | + |
| 351 | +// Note, that we plan to add more convenient ways to add a JSON schema in the future. |
| 352 | +var templatingConfig = |
| 353 | + Template.create() |
| 354 | + .template(List.of(template.createChatMessage())) |
| 355 | + .responseFormat( |
| 356 | + ResponseFormatJsonSchema.create() |
| 357 | + .type(ResponseFormatJsonSchema.TypeEnum.JSON_SCHEMA) |
| 358 | + .jsonSchema( |
| 359 | + ResponseFormatJsonSchemaJsonSchema.create() |
| 360 | + .name("translation_response") |
| 361 | + .schema(schema) |
| 362 | + .strict(true) |
| 363 | + .description("Output schema for language translation."))); |
| 364 | +var configWithTemplate = llmWithImageSupportConfig.withTemplateConfig(templatingConfig); |
| 365 | + |
| 366 | +var prompt = new OrchestrationPrompt(Message.system("You are a language translator.")); |
| 367 | +var response = client.chatCompletion(prompt, configWithTemplate).getContent(); |
| 368 | +``` |
| 369 | + |
| 370 | +Please find [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java) |
| 371 | + |
303 | 372 | ## Set model parameters |
304 | 373 |
|
305 | 374 | Change your LLM configuration to add model parameters: |
|
0 commit comments