|
| 1 | +package com.sap.ai.sdk.orchestration; |
| 2 | + |
| 3 | +import static lombok.AccessLevel.NONE; |
| 4 | +import static lombok.AccessLevel.PRIVATE; |
| 5 | + |
| 6 | +import com.google.common.annotations.Beta; |
| 7 | +import com.google.common.collect.Lists; |
| 8 | +import com.sap.ai.sdk.orchestration.model.EmbeddingsInput; |
| 9 | +import com.sap.ai.sdk.orchestration.model.EmbeddingsInputText; |
| 10 | +import com.sap.ai.sdk.orchestration.model.EmbeddingsModelConfig; |
| 11 | +import com.sap.ai.sdk.orchestration.model.EmbeddingsModuleConfigs; |
| 12 | +import com.sap.ai.sdk.orchestration.model.EmbeddingsOrchestrationConfig; |
| 13 | +import com.sap.ai.sdk.orchestration.model.EmbeddingsPostRequest; |
| 14 | +import com.sap.ai.sdk.orchestration.model.MaskingModuleConfigProviders; |
| 15 | +import java.util.List; |
| 16 | +import javax.annotation.Nonnull; |
| 17 | +import javax.annotation.Nullable; |
| 18 | +import lombok.AllArgsConstructor; |
| 19 | +import lombok.Getter; |
| 20 | +import lombok.Value; |
| 21 | +import lombok.With; |
| 22 | +import lombok.experimental.Tolerate; |
| 23 | + |
| 24 | +/** |
| 25 | + * Represents a request for generating embeddings through the SAP AI Core Orchestration service. |
| 26 | + * |
| 27 | + * @since 1.12.0 |
| 28 | + */ |
| 29 | +@Beta |
| 30 | +@Value |
| 31 | +@AllArgsConstructor(access = PRIVATE) |
| 32 | +public class OrchestrationEmbeddingRequest { |
| 33 | + |
| 34 | + /** The embedding model to use for generating vector representations. */ |
| 35 | + @Nonnull OrchestrationEmbeddingModel model; |
| 36 | + |
| 37 | + /** The list of text inputs to be converted into embeddings. */ |
| 38 | + @Nonnull List<String> inputs; |
| 39 | + |
| 40 | + /** Optional masking providers for data privacy and security. */ |
| 41 | + @With(value = PRIVATE) |
| 42 | + @Nullable |
| 43 | + List<MaskingProvider> masking; |
| 44 | + |
| 45 | + /** Optional embedding input type classification to optimize embedding generation. */ |
| 46 | + @With(value = PRIVATE) |
| 47 | + @Getter(NONE) |
| 48 | + @Nullable |
| 49 | + EmbeddingsInput.TypeEnum inputType; |
| 50 | + |
| 51 | + /** |
| 52 | + * Create an embedding request using fluent API starting with model selection. |
| 53 | + * |
| 54 | + * <pre>{@code |
| 55 | + * OrchestrationEmbeddingRequest.forModel(myModel).forInputs("text to embed"); |
| 56 | + * }</pre> |
| 57 | + * |
| 58 | + * @param model the embedding model to use |
| 59 | + * @return a step for specifying inputs |
| 60 | + */ |
| 61 | + @Nonnull |
| 62 | + public static InputStep forModel(@Nonnull final OrchestrationEmbeddingModel model) { |
| 63 | + return inputs -> new OrchestrationEmbeddingRequest(model, List.copyOf(inputs), null, null); |
| 64 | + } |
| 65 | + |
| 66 | + /** Builder step for specifying text inputs to embed. */ |
| 67 | + @FunctionalInterface |
| 68 | + public interface InputStep { |
| 69 | + |
| 70 | + /** |
| 71 | + * Specifies text inputs to be embedded. |
| 72 | + * |
| 73 | + * @param inputs the text strings to embed |
| 74 | + * @return a new embedding request instance |
| 75 | + */ |
| 76 | + @Nonnull |
| 77 | + OrchestrationEmbeddingRequest forInputs(@Nonnull final List<String> inputs); |
| 78 | + |
| 79 | + /** |
| 80 | + * Specifies multiple text inputs using variable arguments. |
| 81 | + * |
| 82 | + * @param firstInput string to embed |
| 83 | + * @param inputs optional additional strings to embed |
| 84 | + * @return a new embedding request instance |
| 85 | + */ |
| 86 | + @Nonnull |
| 87 | + default OrchestrationEmbeddingRequest forInputs( |
| 88 | + @Nonnull final String firstInput, @Nonnull final String... inputs) { |
| 89 | + return forInputs(Lists.asList(firstInput, inputs)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Adds data masking providers to enable detection and masking of sensitive information. |
| 95 | + * |
| 96 | + * @param maskingProvider the primary masking provider |
| 97 | + * @param maskingProviders additional masking providers |
| 98 | + * @return a new request instance with the specified masking providers |
| 99 | + * @see MaskingProvider |
| 100 | + */ |
| 101 | + @Tolerate |
| 102 | + @Nonnull |
| 103 | + public OrchestrationEmbeddingRequest withMasking( |
| 104 | + @Nonnull final MaskingProvider maskingProvider, |
| 105 | + @Nonnull final MaskingProvider... maskingProviders) { |
| 106 | + return withMasking(Lists.asList(maskingProvider, maskingProviders)); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Configures this request to optimize embeddings for document content. |
| 111 | + * |
| 112 | + * @return a new request instance configured for document embedding |
| 113 | + */ |
| 114 | + @Nonnull |
| 115 | + public OrchestrationEmbeddingRequest asDocument() { |
| 116 | + return withInputType(EmbeddingsInput.TypeEnum.DOCUMENT); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Configures this request to optimize embeddings for general text content. |
| 121 | + * |
| 122 | + * @return a new request instance configured for text embedding |
| 123 | + */ |
| 124 | + @Nonnull |
| 125 | + public OrchestrationEmbeddingRequest asText() { |
| 126 | + return withInputType(EmbeddingsInput.TypeEnum.TEXT); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Configures this request to optimize embeddings for query content. |
| 131 | + * |
| 132 | + * @return a new request instance configured for query embedding |
| 133 | + */ |
| 134 | + @Nonnull |
| 135 | + public OrchestrationEmbeddingRequest asQuery() { |
| 136 | + return withInputType(EmbeddingsInput.TypeEnum.QUERY); |
| 137 | + } |
| 138 | + |
| 139 | + @Nonnull |
| 140 | + EmbeddingsPostRequest createEmbeddingsPostRequest() { |
| 141 | + |
| 142 | + final var input = |
| 143 | + EmbeddingsInput.create().text(EmbeddingsInputText.create(inputs)).type(inputType); |
| 144 | + final var embeddingsModelConfig = |
| 145 | + EmbeddingsModelConfig.create().model(model.createEmbeddingsModelDetails()); |
| 146 | + final var modules = |
| 147 | + EmbeddingsOrchestrationConfig.create() |
| 148 | + .modules(EmbeddingsModuleConfigs.create().embeddings(embeddingsModelConfig)); |
| 149 | + |
| 150 | + if (masking != null) { |
| 151 | + final var dpiConfigs = masking.stream().map(MaskingProvider::createConfig).toList(); |
| 152 | + modules.getModules().setMasking(MaskingModuleConfigProviders.create().providers(dpiConfigs)); |
| 153 | + } |
| 154 | + return EmbeddingsPostRequest.create().config(modules).input(input); |
| 155 | + } |
| 156 | +} |
0 commit comments