You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: [Orchestration] Enable and test setting response_format (#328)
* Add e2e test using low-level classes
* Rename to jsonSchema
* Add simple unit test
* Add unit test for JSON object
* Improve unit test for JSON schema
* Add unit test for response format text
* Add e2e tests for all response formats
* Add docs, release notes, and links in sample app
* Small fixes
* Update release notes
* Rename unit test Functions
* Update release_notes.md
* Update docs/guides/ORCHESTRATION_CHAT_COMPLETION.md
Co-authored-by: Charles Dubois <[email protected]>
* Improve explanations
* Shorten unit tests
* Shorten unit tests more
* Update sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java
Co-authored-by: Charles Dubois <[email protected]>
* Update sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java
Co-authored-by: Charles Dubois <[email protected]>
* Update sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java
Co-authored-by: Charles Dubois <[email protected]>
* Add comment about future conveient layer to docs.
---------
Co-authored-by: Jonas Israel <[email protected]>
Co-authored-by: Charles Dubois <[email protected]>
-[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?");
var configWithTemplate = llmWithImageSupportConfig.withTemplateConfig(templatingConfig);
322
+
323
+
var prompt =
324
+
newOrchestrationPrompt(
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.
.description("Output schema for language translation.")));
364
+
var configWithTemplate = llmWithImageSupportConfig.withTemplateConfig(templatingConfig);
365
+
366
+
var prompt =newOrchestrationPrompt(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:
-[Convenient methods to create messages containing images and multiple text inputs](https://github.com/SAP/ai-sdk-java/tree/main/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md#add-images-and-multiple-text-inputs-to-a-message)
19
+
-[Enable setting the response format](https://github.com/SAP/ai-sdk-java/tree/main/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md#set-a-response-format)
"Well, this image features the logo of SAP, a software company, set against a gradient blue background transitioning from light to dark. The main color in the image is blue.");
"Well, this image features the logo of SAP, a software company, set against a gradient blue background transitioning from light to dark. The main color in the image is blue.");
"Well, this image features the logo of SAP, a software company, set against a gradient blue background transitioning from light to dark. The main color in the image is blue.");
0 commit comments