-
Notifications
You must be signed in to change notification settings - Fork 16
feat: [Orchestration] Enable local prompt templates #423
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
Changes from all commits
57b5287
971a878
0bae25c
bc3456b
4df5a1f
4468979
ca8c2a8
d3a7362
c3e4839
c024227
6063dd7
eab011b
b321102
48f86e2
2d566b9
da3f72d
e9e2294
cd9356a
13a14a8
88e9882
2fa8a80
8109eb8
f1c0ac6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| package com.sap.ai.sdk.orchestration; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
| import com.google.common.annotations.Beta; | ||
| import com.sap.ai.sdk.orchestration.model.ChatCompletionTool; | ||
| import com.sap.ai.sdk.orchestration.model.ChatMessage; | ||
|
|
@@ -9,6 +14,7 @@ | |
| import com.sap.ai.sdk.orchestration.model.Template; | ||
| import com.sap.ai.sdk.orchestration.model.TemplateResponseFormat; | ||
| import com.sap.ai.sdk.orchestration.model.TemplatingModuleConfig; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
@@ -35,14 +41,22 @@ | |
| @NoArgsConstructor(force = true, access = AccessLevel.PACKAGE) | ||
| @Beta | ||
| public class OrchestrationTemplate extends TemplateConfig { | ||
| @Nullable List<ChatMessage> template; | ||
| @Nullable Map<String, String> defaults; | ||
| @JsonProperty("template") | ||
| @Nullable | ||
| List<ChatMessage> template; | ||
|
|
||
| @JsonProperty("defaults") | ||
| @Nullable | ||
| Map<String, String> defaults; | ||
|
|
||
| @JsonProperty("response_format") | ||
| @With(AccessLevel.PRIVATE) | ||
| @Nullable | ||
| TemplateResponseFormat responseFormat; | ||
|
|
||
| @Nullable List<ChatCompletionTool> tools; | ||
| @JsonProperty("tools") | ||
| @Nullable | ||
| List<ChatCompletionTool> tools; | ||
|
|
||
| /** | ||
| * Create a low-level representation of the template. | ||
|
|
@@ -93,4 +107,45 @@ public OrchestrationTemplate withJsonResponse() { | |
| ResponseFormatJsonObject.create().type(ResponseFormatJsonObject.TypeEnum.JSON_OBJECT); | ||
| return this.withResponseFormat(responseFormatJsonObject); | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link Template} object from a JSON provided as String. | ||
| * | ||
| * @throws IOException if the JSON cannot be deserialized | ||
| * @param inputString the provided JSON | ||
| * @return A Template object representing the provided JSON | ||
| * @since 1.7.0 | ||
| */ | ||
| @Nullable | ||
| private OrchestrationTemplate fromJson(@Nonnull final String inputString) throws IOException { | ||
| final ObjectMapper objectMapper = | ||
| OrchestrationJacksonConfiguration.getOrchestrationObjectMapper(); | ||
| final JsonNode rootNode = objectMapper.readTree(inputString); | ||
| return objectMapper.treeToValue(rootNode.get("spec"), OrchestrationTemplate.class); | ||
| } | ||
|
Comment on lines
+120
to
+125
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method could principally be made public as an additional feature. I left it private because it is not part of the ticket, not tested on its own, and not aligned with JS SDK (which only has the yaml feature afaik). |
||
|
|
||
| /** | ||
| * Create a {@link Template} object from a YAML provided as String. | ||
| * | ||
| * @throws IOException if the YAML cannot be parsed or deserialized | ||
| * @param inputYaml the provided YAML | ||
| * @return A Template object representing the provided YAML | ||
| * @since 1.7.0 | ||
| */ | ||
| @Nullable | ||
| public OrchestrationTemplate fromYaml(@Nonnull final String inputYaml) throws IOException { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Question to reviewer) Pro: We might not be able to continue to keep this method/feature alive without additional work if the specs of prompt-registry and orchestration were to diverge at some point (see "Note 1" in the PR description). Con: Additional beta annotation for convenience code.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as discussed, since this API has no alternative to users, I would keep it non-Beta |
||
| final Object obj; | ||
| try { | ||
| final ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); | ||
| obj = yamlReader.readValue(inputYaml, Object.class); | ||
| } catch (JsonProcessingException ex) { | ||
| throw new IOException("Failed to parse the YAML input: " + ex.getMessage(), ex); | ||
| } | ||
| try { | ||
| final ObjectMapper jsonWriter = new ObjectMapper(); | ||
| return fromJson(jsonWriter.writeValueAsString(obj)); | ||
| } catch (JsonProcessingException ex) { | ||
| throw new IOException("Failed to deserialize the input: " + ex.getMessage(), ex); | ||
| } | ||
|
Comment on lines
+137
to
+149
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if these are the correct/best Exception types. I am open to suggestions :)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using two objectmappers, I was about to pitch something like... private static class TemplateYaml {
@JsonProperty("spec")
private OrchestrationTemplate spec;
}But that would've required additional customization to propagate the modules/rules along for Yaml Factory. |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.