|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.inference; |
| 11 | + |
| 12 | +import org.elasticsearch.core.Nullable; |
| 13 | +import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper; |
| 14 | +import org.elasticsearch.xcontent.ConstructingObjectParser; |
| 15 | +import org.elasticsearch.xcontent.ParseField; |
| 16 | +import org.elasticsearch.xcontent.ToXContentObject; |
| 17 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 18 | +import org.elasticsearch.xcontent.XContentParser; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.ElementType; |
| 24 | +import static org.elasticsearch.inference.TaskType.COMPLETION; |
| 25 | +import static org.elasticsearch.inference.TaskType.RERANK; |
| 26 | +import static org.elasticsearch.inference.TaskType.SPARSE_EMBEDDING; |
| 27 | +import static org.elasticsearch.inference.TaskType.TEXT_EMBEDDING; |
| 28 | + |
| 29 | +/** |
| 30 | + * Defines the base settings required to configure an inference endpoint. |
| 31 | + * |
| 32 | + * These settings are immutable and describe the input and output types that the endpoint will handle. |
| 33 | + * They capture the essential properties of an inference model, ensuring the endpoint is correctly configured. |
| 34 | + * |
| 35 | + * Key properties include: |
| 36 | + * <ul> |
| 37 | + * <li>{@code taskType} - Specifies the type of task the model performs, such as classification or text embeddings.</li> |
| 38 | + * <li>{@code dimensions}, {@code similarity}, and {@code elementType} - These settings are applicable only when |
| 39 | + * the {@code taskType} is {@link TaskType#TEXT_EMBEDDING}. They define the structure and behavior of embeddings.</li> |
| 40 | + * </ul> |
| 41 | + * |
| 42 | + * @param taskType the type of task the inference model performs. |
| 43 | + * @param dimensions the number of dimensions for the embeddings, applicable only for {@link TaskType#TEXT_EMBEDDING} (nullable). |
| 44 | + * @param similarity the similarity measure used for embeddings, applicable only for {@link TaskType#TEXT_EMBEDDING} (nullable). |
| 45 | + * @param elementType the type of elements in the embeddings, applicable only for {@link TaskType#TEXT_EMBEDDING} (nullable). |
| 46 | + */ |
| 47 | +public record MinimalServiceSettings( |
| 48 | + TaskType taskType, |
| 49 | + @Nullable Integer dimensions, |
| 50 | + @Nullable SimilarityMeasure similarity, |
| 51 | + @Nullable ElementType elementType |
| 52 | +) implements ToXContentObject { |
| 53 | + |
| 54 | + public static final String TASK_TYPE_FIELD = "task_type"; |
| 55 | + static final String DIMENSIONS_FIELD = "dimensions"; |
| 56 | + static final String SIMILARITY_FIELD = "similarity"; |
| 57 | + static final String ELEMENT_TYPE_FIELD = "element_type"; |
| 58 | + |
| 59 | + private static final ConstructingObjectParser<MinimalServiceSettings, Void> PARSER = new ConstructingObjectParser<>( |
| 60 | + "model_settings", |
| 61 | + true, |
| 62 | + args -> { |
| 63 | + TaskType taskType = TaskType.fromString((String) args[0]); |
| 64 | + Integer dimensions = (Integer) args[1]; |
| 65 | + SimilarityMeasure similarity = args[2] == null ? null : SimilarityMeasure.fromString((String) args[2]); |
| 66 | + DenseVectorFieldMapper.ElementType elementType = args[3] == null |
| 67 | + ? null |
| 68 | + : DenseVectorFieldMapper.ElementType.fromString((String) args[3]); |
| 69 | + return new MinimalServiceSettings(taskType, dimensions, similarity, elementType); |
| 70 | + } |
| 71 | + ); |
| 72 | + |
| 73 | + static { |
| 74 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField(TASK_TYPE_FIELD)); |
| 75 | + PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), new ParseField(DIMENSIONS_FIELD)); |
| 76 | + PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField(SIMILARITY_FIELD)); |
| 77 | + PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField(ELEMENT_TYPE_FIELD)); |
| 78 | + } |
| 79 | + |
| 80 | + public static MinimalServiceSettings parse(XContentParser parser) throws IOException { |
| 81 | + return PARSER.parse(parser, null); |
| 82 | + } |
| 83 | + |
| 84 | + public static MinimalServiceSettings textEmbedding(int dimensions, SimilarityMeasure similarity, ElementType elementType) { |
| 85 | + return new MinimalServiceSettings(TEXT_EMBEDDING, dimensions, similarity, elementType); |
| 86 | + } |
| 87 | + |
| 88 | + public static MinimalServiceSettings sparseEmbedding() { |
| 89 | + return new MinimalServiceSettings(SPARSE_EMBEDDING, null, null, null); |
| 90 | + } |
| 91 | + |
| 92 | + public static MinimalServiceSettings rerank() { |
| 93 | + return new MinimalServiceSettings(RERANK, null, null, null); |
| 94 | + } |
| 95 | + |
| 96 | + public static MinimalServiceSettings completion() { |
| 97 | + return new MinimalServiceSettings(COMPLETION, null, null, null); |
| 98 | + } |
| 99 | + |
| 100 | + public MinimalServiceSettings(Model model) { |
| 101 | + this( |
| 102 | + model.getTaskType(), |
| 103 | + model.getServiceSettings().dimensions(), |
| 104 | + model.getServiceSettings().similarity(), |
| 105 | + model.getServiceSettings().elementType() |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + public MinimalServiceSettings( |
| 110 | + TaskType taskType, |
| 111 | + @Nullable Integer dimensions, |
| 112 | + @Nullable SimilarityMeasure similarity, |
| 113 | + @Nullable ElementType elementType |
| 114 | + ) { |
| 115 | + this.taskType = Objects.requireNonNull(taskType, "task type must not be null"); |
| 116 | + this.dimensions = dimensions; |
| 117 | + this.similarity = similarity; |
| 118 | + this.elementType = elementType; |
| 119 | + validate(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 124 | + builder.startObject(); |
| 125 | + builder.field(TASK_TYPE_FIELD, taskType.toString()); |
| 126 | + if (dimensions != null) { |
| 127 | + builder.field(DIMENSIONS_FIELD, dimensions); |
| 128 | + } |
| 129 | + if (similarity != null) { |
| 130 | + builder.field(SIMILARITY_FIELD, similarity); |
| 131 | + } |
| 132 | + if (elementType != null) { |
| 133 | + builder.field(ELEMENT_TYPE_FIELD, elementType); |
| 134 | + } |
| 135 | + return builder.endObject(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String toString() { |
| 140 | + final StringBuilder sb = new StringBuilder(); |
| 141 | + sb.append("task_type=").append(taskType); |
| 142 | + if (dimensions != null) { |
| 143 | + sb.append(", dimensions=").append(dimensions); |
| 144 | + } |
| 145 | + if (similarity != null) { |
| 146 | + sb.append(", similarity=").append(similarity); |
| 147 | + } |
| 148 | + if (elementType != null) { |
| 149 | + sb.append(", element_type=").append(elementType); |
| 150 | + } |
| 151 | + return sb.toString(); |
| 152 | + } |
| 153 | + |
| 154 | + private void validate() { |
| 155 | + switch (taskType) { |
| 156 | + case TEXT_EMBEDDING: |
| 157 | + validateFieldPresent(DIMENSIONS_FIELD, dimensions); |
| 158 | + validateFieldPresent(SIMILARITY_FIELD, similarity); |
| 159 | + validateFieldPresent(ELEMENT_TYPE_FIELD, elementType); |
| 160 | + break; |
| 161 | + |
| 162 | + default: |
| 163 | + validateFieldNotPresent(DIMENSIONS_FIELD, dimensions); |
| 164 | + validateFieldNotPresent(SIMILARITY_FIELD, similarity); |
| 165 | + validateFieldNotPresent(ELEMENT_TYPE_FIELD, elementType); |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private void validateFieldPresent(String field, Object fieldValue) { |
| 171 | + if (fieldValue == null) { |
| 172 | + throw new IllegalArgumentException("required [" + field + "] field is missing for task_type [" + taskType.name() + "]"); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private void validateFieldNotPresent(String field, Object fieldValue) { |
| 177 | + if (fieldValue != null) { |
| 178 | + throw new IllegalArgumentException("[" + field + "] is not allowed for task_type [" + taskType.name() + "]"); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments