Skip to content

Commit 1ff4d5a

Browse files
committed
Add documentation etc.
1 parent cec2591 commit 1ff4d5a

File tree

2 files changed

+12
-41
lines changed

2 files changed

+12
-41
lines changed

docs/guides/ORCHESTRATION_CHAT_COMPLETION.md

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -311,20 +311,12 @@ It is possible to set the response format for the chat completion. Available opt
311311
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).
312312

313313
```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);
314+
var configWithJsonResponse = config.withJsonResponse();
322315

323316
var prompt =
324317
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();
318+
Message.user("Some message."), Message.system("Answer using JSON."));
319+
var response = client.chatCompletion(prompt, configWithJsonResponse).getContent();
328320
```
329321
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.
330322

@@ -334,38 +326,17 @@ Note, that it is necessary to tell the AI model to actually return a JSON object
334326
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.
335327

336328
```java
337-
var template = Message.user("Whats '%s' in German?".formatted(word));
338329
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."));
330+
ResponseJsonSchema.from(MyClass.class)
331+
.withDescription("Output schema for the example class MyClass.")
332+
.withStrict(true);
333+
var configWithResponseSchema = config.withJsonSchemaResponse(schema);
334+
335+
var prompt =
336+
new OrchestrationPrompt(Message.user("Some message."));
367337
var response = client.chatCompletion(prompt, configWithTemplate).getContent();
368338
```
339+
Note, that the LLM will only exactly adhere to the given schema if you use `withStrict(true)`. Not all schemas are possible for OpenAI in strict mode. See [here](https://platform.openai.com/docs/guides/structured-outputs#supported-schemas) for more information.
369340

370341
Please find [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OrchestrationService.java)
371342

docs/release-notes/release_notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
### ✨ New Functionality
1414

15-
-
15+
- [Add new convenient methods to set the response format for Orchestration.](https://github.com/SAP/ai-sdk-java/tree/main/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md#set-a-response-format)
1616

1717
### 📈 Improvements
1818

0 commit comments

Comments
 (0)