Skip to content

Commit 714535e

Browse files
author
Max Hniebergall
committed
Add endpointVersion exceptin in index mappings
1 parent 50d8912 commit 714535e

File tree

113 files changed

+2429
-695
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+2429
-695
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ static TransportVersion def(int id) {
234234
public static final TransportVersion RRF_QUERY_REWRITE = def(8_758_00_0);
235235
public static final TransportVersion SEARCH_FAILURE_STATS = def(8_759_00_0);
236236
public static final TransportVersion INGEST_GEO_DATABASE_PROVIDERS = def(8_760_00_0);
237+
public static final TransportVersion INFERENCE_API_PARAMATERS_INTRODUCED = def(8_761_00_0);
237238

238239
/*
239240
* STOP! READ THIS FIRST! No, really,

server/src/main/java/org/elasticsearch/inference/InferenceService.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,55 @@ default void init(Client client) {}
3636
* If the map contains unrecognized configuration option an
3737
* {@code ElasticsearchStatusException} is thrown.
3838
*
39-
* @param modelId Model Id
40-
* @param taskType The model task type
41-
* @param config Configuration options including the secrets
42-
* @param parsedModelListener A listener which will handle the resulting model or failure
39+
* @param modelId Model Id
40+
* @param taskType The model task type
41+
* @param config Configuration options including the secrets
42+
* @param endpointVersion
43+
* @param parsedModelListener A listener which will handle the resulting model or failure
4344
*/
44-
void parseRequestConfig(String modelId, TaskType taskType, Map<String, Object> config, ActionListener<Model> parsedModelListener);
45+
void parseRequestConfig(
46+
String modelId,
47+
TaskType taskType,
48+
Map<String, Object> config,
49+
String endpointVersion,
50+
ActionListener<Model> parsedModelListener
51+
);
4552

4653
/**
4754
* Parse model configuration from {@code config map} from persisted storage and return the parsed {@link Model}. This requires that
4855
* secrets and service settings be in two separate maps.
4956
* This function modifies {@code config map}, fields are removed from the map as they are read.
50-
*
57+
* <p>
5158
* If the map contains unrecognized configuration options, no error is thrown.
5259
*
53-
* @param modelId Model Id
54-
* @param taskType The model task type
55-
* @param config Configuration options
56-
* @param secrets Sensitive configuration options (e.g. api key)
60+
* @param modelId Model Id
61+
* @param taskType The model task type
62+
* @param config Configuration options
63+
* @param secrets Sensitive configuration options (e.g. api key)
64+
* @param endpointVersion
5765
* @return The parsed {@link Model}
5866
*/
59-
Model parsePersistedConfigWithSecrets(String modelId, TaskType taskType, Map<String, Object> config, Map<String, Object> secrets);
67+
Model parsePersistedConfigWithSecrets(
68+
String modelId,
69+
TaskType taskType,
70+
Map<String, Object> config,
71+
Map<String, Object> secrets,
72+
String endpointVersion
73+
);
6074

6175
/**
6276
* Parse model configuration from {@code config map} from persisted storage and return the parsed {@link Model}.
6377
* This function modifies {@code config map}, fields are removed from the map as they are read.
64-
*
78+
* <p>
6579
* If the map contains unrecognized configuration options, no error is thrown.
6680
*
67-
* @param modelId Model Id
68-
* @param taskType The model task type
69-
* @param config Configuration options
81+
* @param modelId Model Id
82+
* @param taskType The model task type
83+
* @param config Configuration options
84+
* @param endpointVersion
7085
* @return The parsed {@link Model}
7186
*/
72-
Model parsePersistedConfig(String modelId, TaskType taskType, Map<String, Object> config);
87+
Model parsePersistedConfig(String modelId, TaskType taskType, Map<String, Object> config, String endpointVersion);
7388

7489
/**
7590
* Perform inference on the model.

server/src/main/java/org/elasticsearch/inference/ModelConfigurations.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class ModelConfigurations implements ToFilteredXContentObject, VersionedN
3232
public static final String PARAMETERS = "parameters";
3333
public static final String CHUNKING_SETTINGS = "chunking_settings";
3434
public static final String INCLUDE_PARAMETERS = "include_parameters";
35+
public static final String ENDPOINT_VERSION_FIELD_NAME = "endpoint_version";
36+
public static final String FIRST_ENDPOINT_VERSION = "2023-09-29";
37+
public static final String PARAMETERS_INTRODUCED_ENDPOINT_VERSION = "2024-10-17";
3538
private static final String NAME = "inference_model";
3639

3740
public static ModelConfigurations of(Model model, TaskSettings taskSettings) {
@@ -44,7 +47,8 @@ public static ModelConfigurations of(Model model, TaskSettings taskSettings) {
4447
model.getConfigurations().getService(),
4548
model.getServiceSettings(),
4649
taskSettings,
47-
model.getConfigurations().getChunkingSettings()
50+
model.getConfigurations().getChunkingSettings(),
51+
model.getConfigurations().getEndpointVersion()
4852
);
4953
}
5054

@@ -58,7 +62,8 @@ public static ModelConfigurations of(Model model, ServiceSettings serviceSetting
5862
model.getConfigurations().getService(),
5963
serviceSettings,
6064
model.getTaskSettings(),
61-
model.getConfigurations().getChunkingSettings()
65+
model.getConfigurations().getChunkingSettings(),
66+
model.getConfigurations().getEndpointVersion()
6267
);
6368
}
6469

@@ -68,36 +73,46 @@ public static ModelConfigurations of(Model model, ServiceSettings serviceSetting
6873
private final ServiceSettings serviceSettings;
6974
private final TaskSettings taskSettings;
7075
private final ChunkingSettings chunkingSettings;
76+
private final String endpointVersion;
7177

7278
/**
7379
* Allows no task settings to be defined. This will default to the {@link EmptyTaskSettings} object.
7480
*/
75-
public ModelConfigurations(String inferenceEntityId, TaskType taskType, String service, ServiceSettings serviceSettings) {
76-
this(inferenceEntityId, taskType, service, serviceSettings, EmptyTaskSettings.INSTANCE);
81+
public ModelConfigurations(
82+
String inferenceEntityId,
83+
TaskType taskType,
84+
String service,
85+
ServiceSettings serviceSettings,
86+
String endpointVersion
87+
) {
88+
this(inferenceEntityId, taskType, service, serviceSettings, EmptyTaskSettings.INSTANCE, endpointVersion);
7789
}
7890

7991
public ModelConfigurations(
8092
String inferenceEntityId,
8193
TaskType taskType,
8294
String service,
8395
ServiceSettings serviceSettings,
84-
ChunkingSettings chunkingSettings
96+
ChunkingSettings chunkingSettings,
97+
String endpointVersion
8598
) {
86-
this(inferenceEntityId, taskType, service, serviceSettings, EmptyTaskSettings.INSTANCE, chunkingSettings);
99+
this(inferenceEntityId, taskType, service, serviceSettings, EmptyTaskSettings.INSTANCE, chunkingSettings, endpointVersion);
87100
}
88101

89102
public ModelConfigurations(
90103
String inferenceEntityId,
91104
TaskType taskType,
92105
String service,
93106
ServiceSettings serviceSettings,
94-
TaskSettings taskSettings
107+
TaskSettings taskSettings,
108+
String endpointVersion
95109
) {
96110
this.inferenceEntityId = Objects.requireNonNull(inferenceEntityId);
97111
this.taskType = Objects.requireNonNull(taskType);
98112
this.service = Objects.requireNonNull(service);
99113
this.serviceSettings = Objects.requireNonNull(serviceSettings);
100114
this.taskSettings = Objects.requireNonNull(taskSettings);
115+
this.endpointVersion = endpointVersion;
101116
this.chunkingSettings = null;
102117
}
103118

@@ -107,14 +122,16 @@ public ModelConfigurations(
107122
String service,
108123
ServiceSettings serviceSettings,
109124
TaskSettings taskSettings,
110-
ChunkingSettings chunkingSettings
125+
ChunkingSettings chunkingSettings,
126+
String endpointVersion
111127
) {
112128
this.inferenceEntityId = Objects.requireNonNull(inferenceEntityId);
113129
this.taskType = Objects.requireNonNull(taskType);
114130
this.service = Objects.requireNonNull(service);
115131
this.serviceSettings = Objects.requireNonNull(serviceSettings);
116132
this.taskSettings = Objects.requireNonNull(taskSettings);
117133
this.chunkingSettings = chunkingSettings;
134+
this.endpointVersion = endpointVersion;
118135
}
119136

120137
public ModelConfigurations(StreamInput in) throws IOException {
@@ -126,6 +143,9 @@ public ModelConfigurations(StreamInput in) throws IOException {
126143
this.chunkingSettings = in.getTransportVersion().onOrAfter(TransportVersions.ML_INFERENCE_CHUNKING_SETTINGS)
127144
? in.readOptionalNamedWriteable(ChunkingSettings.class)
128145
: null;
146+
this.endpointVersion = in.getTransportVersion().onOrAfter(TransportVersions.INFERENCE_API_PARAMATERS_INTRODUCED)
147+
? Objects.requireNonNullElse(in.readOptionalString(), FIRST_ENDPOINT_VERSION)
148+
: FIRST_ENDPOINT_VERSION;
129149
}
130150

131151
@Override
@@ -138,6 +158,9 @@ public void writeTo(StreamOutput out) throws IOException {
138158
if (out.getTransportVersion().onOrAfter(TransportVersions.ML_INFERENCE_CHUNKING_SETTINGS)) {
139159
out.writeOptionalNamedWriteable(chunkingSettings);
140160
}
161+
if (out.getTransportVersion().onOrAfter(TransportVersions.INFERENCE_API_PARAMATERS_INTRODUCED)) {
162+
out.writeOptionalString(endpointVersion); // not nullable after 9.0
163+
}
141164
}
142165

143166
public String getInferenceEntityId() {
@@ -164,6 +187,10 @@ public ChunkingSettings getChunkingSettings() {
164187
return chunkingSettings;
165188
}
166189

190+
public String getEndpointVersion() {
191+
return endpointVersion;
192+
}
193+
167194
@Override
168195
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
169196
builder.startObject();
@@ -182,6 +209,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
182209
if (params.paramAsBoolean(INCLUDE_PARAMETERS, true)) { // default true so that REST requests get parameters
183210
builder.field(PARAMETERS, taskSettings);
184211
}
212+
builder.field(ENDPOINT_VERSION_FIELD_NAME, endpointVersion);
185213
builder.endObject();
186214
return builder;
187215
}
@@ -204,6 +232,7 @@ public XContentBuilder toFilteredXContent(XContentBuilder builder, Params params
204232
if (params.paramAsBoolean(INCLUDE_PARAMETERS, true)) { // default true so that REST requests get parameters
205233
builder.field(PARAMETERS, taskSettings);
206234
}
235+
builder.field(ENDPOINT_VERSION_FIELD_NAME, endpointVersion);
207236
builder.endObject();
208237
return builder;
209238
}
@@ -227,11 +256,12 @@ public boolean equals(Object o) {
227256
&& taskType == model.taskType
228257
&& Objects.equals(service, model.service)
229258
&& Objects.equals(serviceSettings, model.serviceSettings)
230-
&& Objects.equals(taskSettings, model.taskSettings);
259+
&& Objects.equals(taskSettings, model.taskSettings)
260+
&& Objects.equals(endpointVersion, model.endpointVersion);
231261
}
232262

233263
@Override
234264
public int hashCode() {
235-
return Objects.hash(inferenceEntityId, taskType, service, serviceSettings, taskSettings);
265+
return Objects.hash(inferenceEntityId, taskType, service, serviceSettings, taskSettings, endpointVersion);
236266
}
237267
}

server/src/main/java/org/elasticsearch/inference/UnparsedModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public record UnparsedModel(
2020
TaskType taskType,
2121
String service,
2222
Map<String, Object> settings,
23-
Map<String, Object> secrets
23+
Map<String, Object> secrets,
24+
String endpointVersion
2425
) {}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/inference/action/PutInferenceModelAction.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.elasticsearch.xpack.core.inference.action;
99

10+
import org.elasticsearch.ElasticsearchStatusException;
1011
import org.elasticsearch.action.ActionRequestValidationException;
1112
import org.elasticsearch.action.ActionResponse;
1213
import org.elasticsearch.action.ActionType;
@@ -17,15 +18,24 @@
1718
import org.elasticsearch.common.xcontent.XContentHelper;
1819
import org.elasticsearch.inference.ModelConfigurations;
1920
import org.elasticsearch.inference.TaskType;
21+
import org.elasticsearch.rest.RestStatus;
2022
import org.elasticsearch.xcontent.ToXContentObject;
2123
import org.elasticsearch.xcontent.XContentBuilder;
24+
import org.elasticsearch.xcontent.XContentFactory;
2225
import org.elasticsearch.xcontent.XContentType;
2326
import org.elasticsearch.xpack.core.ml.job.messages.Messages;
2427
import org.elasticsearch.xpack.core.ml.utils.MlStrings;
2528

2629
import java.io.IOException;
30+
import java.util.Map;
2731
import java.util.Objects;
2832

33+
import static org.elasticsearch.inference.ModelConfigurations.ENDPOINT_VERSION_FIELD_NAME;
34+
import static org.elasticsearch.inference.ModelConfigurations.FIRST_ENDPOINT_VERSION;
35+
import static org.elasticsearch.inference.ModelConfigurations.OLD_TASK_SETTINGS;
36+
import static org.elasticsearch.inference.ModelConfigurations.PARAMETERS;
37+
import static org.elasticsearch.inference.ModelConfigurations.PARAMETERS_INTRODUCED_ENDPOINT_VERSION;
38+
2939
public class PutInferenceModelAction extends ActionType<PutInferenceModelAction.Response> {
3040

3141
public static final PutInferenceModelAction INSTANCE = new PutInferenceModelAction();
@@ -40,6 +50,7 @@ public static class Request extends AcknowledgedRequest<Request> {
4050
private final TaskType taskType;
4151
private final String inferenceEntityId;
4252
private final BytesReference content;
53+
private BytesReference rewrittenContent;
4354
private final XContentType contentType;
4455

4556
public Request(TaskType taskType, String inferenceEntityId, BytesReference content, XContentType contentType) {
@@ -70,6 +81,33 @@ public BytesReference getContent() {
7081
return content;
7182
}
7283

84+
public BytesReference getRewrittenContent() {
85+
if (rewrittenContent == null) { // rewrittenContent is deterministic on content, so we only need to calculate it once
86+
Map<String, Object> newContent = XContentHelper.convertToMap(content, false, contentType).v2();
87+
if (newContent.containsKey(PARAMETERS) && newContent.containsKey(OLD_TASK_SETTINGS)) {
88+
throw new ElasticsearchStatusException(
89+
"Request cannot contain both [task_settings] and [parameters], use only [parameters]",
90+
RestStatus.BAD_REQUEST
91+
);
92+
} else if (newContent.containsKey(PARAMETERS)) {
93+
newContent.put(OLD_TASK_SETTINGS, newContent.get(PARAMETERS));
94+
newContent.put(ENDPOINT_VERSION_FIELD_NAME, PARAMETERS_INTRODUCED_ENDPOINT_VERSION);
95+
newContent.remove(PARAMETERS);
96+
} else if (newContent.containsKey(OLD_TASK_SETTINGS)) {
97+
newContent.put(ENDPOINT_VERSION_FIELD_NAME, FIRST_ENDPOINT_VERSION);
98+
} else {
99+
newContent.put(ENDPOINT_VERSION_FIELD_NAME, FIRST_ENDPOINT_VERSION);
100+
}
101+
try (XContentBuilder builder = XContentFactory.contentBuilder(this.contentType)) {
102+
builder.map(newContent);
103+
this.rewrittenContent = BytesReference.bytes(builder);
104+
} catch (IOException e) {
105+
throw new ElasticsearchStatusException("Failed to parse rewritten request", RestStatus.INTERNAL_SERVER_ERROR, e);
106+
}
107+
}
108+
return rewrittenContent;
109+
}
110+
73111
public XContentType getContentType() {
74112
return contentType;
75113
}

0 commit comments

Comments
 (0)