|
6 | 6 | import com.sap.ai.sdk.orchestration.model.LLMModuleConfig; |
7 | 7 | import com.sap.ai.sdk.orchestration.model.MaskingModuleConfig; |
8 | 8 | import com.sap.ai.sdk.orchestration.model.OutputFilteringConfig; |
| 9 | +import com.sap.ai.sdk.orchestration.model.ResponseFormatJsonObject; |
| 10 | +import com.sap.ai.sdk.orchestration.model.ResponseFormatJsonSchema; |
| 11 | +import com.sap.ai.sdk.orchestration.model.ResponseFormatJsonSchemaJsonSchema; |
| 12 | +import com.sap.ai.sdk.orchestration.model.Template; |
| 13 | +import com.sap.ai.sdk.orchestration.model.TemplateRef; |
| 14 | +import com.sap.ai.sdk.orchestration.model.TemplateResponseFormat; |
9 | 15 | import com.sap.ai.sdk.orchestration.model.TemplatingModuleConfig; |
10 | 16 | import java.util.ArrayList; |
11 | 17 | import java.util.Arrays; |
| 18 | +import java.util.List; |
12 | 19 | import java.util.Objects; |
13 | 20 | import javax.annotation.Nonnull; |
14 | 21 | import javax.annotation.Nullable; |
@@ -61,7 +68,9 @@ public class OrchestrationModuleConfig { |
61 | 68 | * @link <a href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/templating">SAP |
62 | 69 | * AI Core: Orchestration - Templating</a> |
63 | 70 | */ |
64 | | - @Nullable TemplatingModuleConfig templateConfig; |
| 71 | + @With(AccessLevel.NONE) |
| 72 | + @Nullable |
| 73 | + TemplatingModuleConfig templateConfig; |
65 | 74 |
|
66 | 75 | /** |
67 | 76 | * A masking configuration to pseudonymous or anonymize sensitive data in the input. |
@@ -211,4 +220,97 @@ public OrchestrationModuleConfig withGrounding( |
211 | 220 | @Nonnull final GroundingProvider groundingProvider) { |
212 | 221 | return this.withGroundingConfig(groundingProvider.createConfig()); |
213 | 222 | } |
| 223 | + |
| 224 | + /** |
| 225 | + * Creates a new configuration with the given template configuration. |
| 226 | + * |
| 227 | + * @link <a href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/templating">SAP |
| 228 | + * AI Core: Orchestration - Templating</a> |
| 229 | + * @param templateConfig |
| 230 | + * @return A new configuration with the given template configuration. |
| 231 | + */ |
| 232 | + @Nonnull |
| 233 | + public OrchestrationModuleConfig withTemplateConfig( |
| 234 | + @Nullable final TemplatingModuleConfig templateConfig) { |
| 235 | + |
| 236 | + // If new templateConfig is a TemplateRef, use it. |
| 237 | + if (templateConfig instanceof TemplateRef) { |
| 238 | + return new OrchestrationModuleConfig( |
| 239 | + llmConfig, templateConfig, maskingConfig, filteringConfig, groundingConfig); |
| 240 | + } |
| 241 | + |
| 242 | + // Make sure old responseFormat is only overwritten if new templateConfig has one set. |
| 243 | + var newTemplate = (Template) templateConfig; |
| 244 | + TemplateResponseFormat responseFormat = null; |
| 245 | + if (this.templateConfig instanceof Template oldTemplate) { |
| 246 | + responseFormat = oldTemplate.getResponseFormat(); |
| 247 | + } |
| 248 | + if (newTemplate != null && newTemplate.getResponseFormat() == null) { |
| 249 | + newTemplate.setResponseFormat(responseFormat); |
| 250 | + } |
| 251 | + if (newTemplate == null && responseFormat != null) { |
| 252 | + newTemplate = Template.create().template(List.of()); |
| 253 | + newTemplate.setResponseFormat(responseFormat); |
| 254 | + } |
| 255 | + |
| 256 | + return new OrchestrationModuleConfig( |
| 257 | + llmConfig, newTemplate, maskingConfig, filteringConfig, groundingConfig); |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * Creates a new configuration with the given response schema. |
| 262 | + * |
| 263 | + * @link <a |
| 264 | + * href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/structured-output">SAP |
| 265 | + * AI Core: Orchestration - Structured Output</a> |
| 266 | + * @param schema |
| 267 | + * @return A new configuration with the given response schema. |
| 268 | + * @since 1.4.0 |
| 269 | + */ |
| 270 | + @Nonnull |
| 271 | + public OrchestrationModuleConfig withResponseJsonSchema( |
| 272 | + @Nonnull final ResponseJsonSchema schema) { |
| 273 | + val responseFormatJsonSchema = |
| 274 | + ResponseFormatJsonSchema.create() |
| 275 | + .type(ResponseFormatJsonSchema.TypeEnum.JSON_SCHEMA) |
| 276 | + .jsonSchema( |
| 277 | + ResponseFormatJsonSchemaJsonSchema.create() |
| 278 | + .name(schema.getName()) |
| 279 | + .schema(schema.getSchemaMap()) |
| 280 | + .strict(schema.getIsStrict()) |
| 281 | + .description(schema.getDescription())); |
| 282 | + |
| 283 | + if (this.templateConfig instanceof Template template) { |
| 284 | + template.setResponseFormat(responseFormatJsonSchema); |
| 285 | + return this.withTemplateConfig(template); |
| 286 | + } |
| 287 | + val templatingConfig = |
| 288 | + Template.create().template(List.of()).responseFormat(responseFormatJsonSchema); |
| 289 | + return this.withTemplateConfig(templatingConfig); |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * Creates a new configuration where the response format is set to JSON object. |
| 294 | + * |
| 295 | + * @link <a |
| 296 | + * href="https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/structured-output">SAP |
| 297 | + * AI Core: Orchestration - Structured Output</a> |
| 298 | + * @return A new configuration with response format JSON object. |
| 299 | + * @since 1.4.0 |
| 300 | + */ |
| 301 | + @Nonnull |
| 302 | + public OrchestrationModuleConfig withJsonResponse() { |
| 303 | + if (this.templateConfig instanceof Template template) { |
| 304 | + template.setResponseFormat( |
| 305 | + ResponseFormatJsonObject.create().type(ResponseFormatJsonObject.TypeEnum.JSON_OBJECT)); |
| 306 | + return this.withTemplateConfig(template); |
| 307 | + } |
| 308 | + val templatingConfig = |
| 309 | + Template.create() |
| 310 | + .template(List.of()) |
| 311 | + .responseFormat( |
| 312 | + ResponseFormatJsonObject.create() |
| 313 | + .type(ResponseFormatJsonObject.TypeEnum.JSON_OBJECT)); |
| 314 | + return this.withTemplateConfig(templatingConfig); |
| 315 | + } |
214 | 316 | } |
0 commit comments