generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: [Orchestration] Convenience for response format #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b146cfd
Use library to generate schemas from java classes
Jonas-Isr 1611a08
Create ResponseJsonSchema and adapt OrchestrationModuleConfig
Jonas-Isr d7c5039
Add additional tests, reset TestCoverage
Jonas-Isr cec2591
Merge branch 'main' into orch-schema-convenience
Jonas-Isr 1ff4d5a
Add documentation etc.
Jonas-Isr 79865aa
Small fixes
Jonas-Isr 0520339
Improve documentation
Jonas-Isr b9d5d9a
Requested changes
Jonas-Isr 28f8c63
Merge branch 'main' into orch-schema-convenience
Jonas-Isr 6eb21bd
Update orchestration/src/main/java/com/sap/ai/sdk/orchestration/Orche…
Jonas-Isr 9b1482d
Small fix
Jonas-Isr b7824d0
WIP
Jonas-Isr 5ee6cea
Introduce new class TemplateConfig and adapt tests
Jonas-Isr 731972a
Adding javadocs and annotations
Jonas-Isr fbf4944
Add tests
Jonas-Isr 168bd71
Update docs/guides/ORCHESTRATION_CHAT_COMPLETION.md
Jonas-Isr 7c79e3a
Merge remote-tracking branch 'origin/orch-schema-convenience' into or…
Jonas-Isr 57cc75f
Merge branch 'main' into orch-schema-convenience
Jonas-Isr 537f28a
Small changes
Jonas-Isr 403cf50
Merge branch 'main' into orch-schema-convenience
Jonas-Isr a4ceb8d
Merge branch 'main' into orch-schema-convenience
Jonas-Isr a21cb6b
Update orchestration/src/main/java/com/sap/ai/sdk/orchestration/Respo…
Jonas-Isr ffcb22e
Formatting
bot-sdk-js 42d7d22
Use functional interfaces instead of class (#357)
newtork 6fe5fe7
Requested changes
Jonas-Isr fea3bc0
change @since to 1.4.0
Jonas-Isr b736fea
Merge branch 'main' into orch-schema-convenience
Jonas-Isr f633d90
Merge branch 'main' into orch-schema-convenience
Jonas-Isr 18c5dfd
rename factory methods
Jonas-Isr 2f99bb8
small fix
Jonas-Isr 81dd4ab
Merge branch 'main' into orch-schema-convenience
Jonas-Isr 6d1b700
small fix.finalfinal
Jonas-Isr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
orchestration/src/main/java/com/sap/ai/sdk/orchestration/ResponseJsonSchema.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package com.sap.ai.sdk.orchestration; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.github.victools.jsonschema.generator.Option; | ||
| import com.github.victools.jsonschema.generator.OptionPreset; | ||
| import com.github.victools.jsonschema.generator.SchemaGenerator; | ||
| import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; | ||
| import com.github.victools.jsonschema.generator.SchemaVersion; | ||
| import com.github.victools.jsonschema.module.jackson.JacksonModule; | ||
| import com.github.victools.jsonschema.module.jackson.JacksonOption; | ||
| import com.sap.ai.sdk.orchestration.model.Template; | ||
| import com.sap.ai.sdk.orchestration.model.TemplateResponseFormat; | ||
| import java.lang.reflect.Type; | ||
| import java.util.Map; | ||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Value; | ||
| import lombok.With; | ||
| import lombok.val; | ||
|
|
||
| /** | ||
| * The schema object to use for the response format parameter in {@link OrchestrationModuleConfig}. | ||
| * | ||
| * @since 1.4.0 | ||
| */ | ||
| @Value | ||
| @AllArgsConstructor(access = AccessLevel.PACKAGE) | ||
| @With | ||
| public class ResponseJsonSchema { | ||
| @Nonnull Map<String, Object> schemaMap; | ||
| @Nonnull String name; | ||
| @Nullable String description; | ||
|
|
||
| @With(AccessLevel.NONE) | ||
| @Nullable | ||
| Boolean isStrict; | ||
Jonas-Isr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Create a new instance of {@link ResponseJsonSchema} with the given strictness. | ||
| * | ||
| * @return A new ResponseJsonSchema instance with the given strictness | ||
| * @since 1.4.0 | ||
| */ | ||
| @Nonnull | ||
| public ResponseJsonSchema withStrict(@Nullable final Boolean isStrict) { | ||
| return new ResponseJsonSchema(schemaMap, name, description, isStrict); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new instance of {@link ResponseJsonSchema} with the given schema map and name. | ||
| * | ||
| * @param schemaMap The schema map | ||
| * @param name The name of the schema | ||
| * @return The new instance of {@link ResponseJsonSchema} | ||
| * @since 1.4.0 | ||
| */ | ||
| @Nonnull | ||
| public static ResponseJsonSchema of( | ||
| @Nonnull final Map<String, Object> schemaMap, @Nonnull final String name) { | ||
| return new ResponseJsonSchema(schemaMap, name, null, null); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new instance of {@link ResponseJsonSchema} from a given class. | ||
| * | ||
| * @param classType The class to generate the schema from | ||
| * @return The new instance of {@link ResponseJsonSchema} | ||
| * @since 1.4.0 | ||
| */ | ||
| @Nonnull | ||
| public static ResponseJsonSchema from(@Nonnull final Type classType) { | ||
| val module = | ||
| new JacksonModule( | ||
| JacksonOption.RESPECT_JSONPROPERTY_REQUIRED, JacksonOption.RESPECT_JSONPROPERTY_ORDER); | ||
| val generator = | ||
| new SchemaGenerator( | ||
| new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON) | ||
| .without(Option.SCHEMA_VERSION_INDICATOR) | ||
| .with(Option.FORBIDDEN_ADDITIONAL_PROPERTIES_BY_DEFAULT) | ||
| .with(module) | ||
| .build()); | ||
| val jsonSchema = generator.generateSchema(classType); | ||
| val mapper = new ObjectMapper(); | ||
| final Map<String, Object> schemaMap = mapper.convertValue(jsonSchema, new TypeReference<>() {}); | ||
Jonas-Isr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val schemaName = ((Class<?>) classType).getSimpleName() + "-Schema"; | ||
Jonas-Isr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return new ResponseJsonSchema(schemaMap, schemaName, null, null); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new instance of a {@link Template} that is a copy of the given one but with the given | ||
| * response format. | ||
| * | ||
| * @param originalTemplate The template to copy | ||
| * @param responseFormat The response format to set in the copy | ||
| * @return The new instance of {@link Template} | ||
| */ | ||
| @Nonnull | ||
| static Template newTemplateWithResponseFormat( | ||
| @Nonnull final Template originalTemplate, | ||
| @Nullable final TemplateResponseFormat responseFormat) { | ||
| val newTemplate = Template.create().template(originalTemplate.getTemplate()); | ||
| newTemplate.setDefaults(originalTemplate.getDefaults()); | ||
| newTemplate.setTools(originalTemplate.getTools()); | ||
| for (val customFieldName : originalTemplate.getCustomFieldNames()) { | ||
| newTemplate.setCustomField(customFieldName, originalTemplate.getCustomField(customFieldName)); | ||
| } | ||
| newTemplate.setResponseFormat(responseFormat); | ||
| return newTemplate; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.