-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[Inference API] Add "rerank" task type to "elastic" provider #126022
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
Merged
timgrein
merged 25 commits into
elastic:main
from
timgrein:elastic-provider-rerank-integration
Jun 10, 2025
Merged
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
33b8110
Add "rerank" task type to "elastic" provider
timgrein efee615
Resolve merge conflicts
timgrein b4b1f05
Fix checkstyle violation
timgrein 2d68ed7
Merge branch 'main' into elastic-provider-rerank-integration
timgrein 8911f0a
Merge branch 'main' into elastic-provider-rerank-integration
timgrein 595859f
Remove uri field and use getter
timgrein 603b91b
Merge remote-tracking branch 'origin/elastic-provider-rerank-integrat…
timgrein 504ed93
[CI] Auto commit changes from spotless
2b89009
Remove ElasticInferenceServiceRerankRequestManager
timgrein 8389205
Merge remote-tracking branch 'origin/elastic-provider-rerank-integrat…
timgrein d5c2744
Spotless apply
timgrein f43417b
Use Strings.format(...)
timgrein 3a447f1
Remove ElasticInferenceServiceRerankTaskSettings and override validat…
timgrein 249d98a
Merge branch 'main' into elastic-provider-rerank-integration
timgrein 2639a12
Merge branch 'main' into elastic-provider-rerank-integration
timgrein c0d8a30
Use Strings.format(...) for rerank action, too
timgrein d954f87
Merge remote-tracking branch 'origin/elastic-provider-rerank-integrat…
timgrein 9e73b10
Add backport transport version
timgrein 9fb2aa8
[CI] Auto commit changes from spotless
46fcaa9
Merge branch 'main' into elastic-provider-rerank-integration
timgrein a6a5d0a
Merge branch 'main' into elastic-provider-rerank-integration
timgrein 2ea8cc5
Use Strings.format in ElasticInferenceServiceActionCreator
timgrein 5271bf0
Merge remote-tracking branch 'origin/elastic-provider-rerank-integrat…
timgrein 5d464ae
Merge branch 'main' into elastic-provider-rerank-integration
timgrein 67b1c70
Merge branch 'main' into elastic-provider-rerank-integration
timgrein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...xpack/inference/external/request/elastic/rerank/ElasticInferenceServiceRerankRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.inference.external.request.elastic.rerank; | ||
|
|
||
| import org.apache.http.HttpHeaders; | ||
| import org.apache.http.client.methods.HttpPost; | ||
| import org.apache.http.client.methods.HttpRequestBase; | ||
| import org.apache.http.entity.ByteArrayEntity; | ||
| import org.apache.http.message.BasicHeader; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
| import org.elasticsearch.xpack.inference.external.request.Request; | ||
| import org.elasticsearch.xpack.inference.services.elastic.request.ElasticInferenceServiceRequest; | ||
| import org.elasticsearch.xpack.inference.services.elastic.request.ElasticInferenceServiceRequestMetadata; | ||
| import org.elasticsearch.xpack.inference.services.elastic.rerank.ElasticInferenceServiceRerankModel; | ||
| import org.elasticsearch.xpack.inference.telemetry.TraceContext; | ||
| import org.elasticsearch.xpack.inference.telemetry.TraceContextHandler; | ||
|
|
||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class ElasticInferenceServiceRerankRequest extends ElasticInferenceServiceRequest { | ||
|
|
||
| private final String query; | ||
| private final List<String> documents; | ||
| private final Integer topN; | ||
| private final TraceContextHandler traceContextHandler; | ||
| private final ElasticInferenceServiceRerankModel model; | ||
|
|
||
| public ElasticInferenceServiceRerankRequest( | ||
| String query, | ||
| List<String> documents, | ||
| Integer topN, | ||
| ElasticInferenceServiceRerankModel model, | ||
| TraceContext traceContext, | ||
| ElasticInferenceServiceRequestMetadata metadata | ||
| ) { | ||
| super(metadata); | ||
| this.query = query; | ||
| this.documents = documents; | ||
| this.topN = topN; | ||
| this.model = Objects.requireNonNull(model); | ||
| this.traceContextHandler = new TraceContextHandler(traceContext); | ||
| } | ||
|
|
||
| @Override | ||
| public HttpRequestBase createHttpRequestBase() { | ||
| var httpPost = new HttpPost(getURI()); | ||
| var requestEntity = Strings.toString( | ||
| new ElasticInferenceServiceRerankRequestEntity(query, documents, model.getServiceSettings().modelId(), topN) | ||
| ); | ||
|
|
||
| ByteArrayEntity byteEntity = new ByteArrayEntity(requestEntity.getBytes(StandardCharsets.UTF_8)); | ||
| httpPost.setEntity(byteEntity); | ||
|
|
||
| traceContextHandler.propagateTraceContext(httpPost); | ||
| httpPost.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, XContentType.JSON.mediaType())); | ||
|
|
||
| return httpPost; | ||
| } | ||
|
|
||
| public TraceContext getTraceContext() { | ||
| return traceContextHandler.traceContext(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getInferenceEntityId() { | ||
| return model.getInferenceEntityId(); | ||
| } | ||
|
|
||
| @Override | ||
| public URI getURI() { | ||
| return model.uri(); | ||
| } | ||
|
|
||
| @Override | ||
| public Request truncate() { | ||
| // no truncation | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean[] getTruncationInfo() { | ||
| // no truncation | ||
| return null; | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
...inference/external/request/elastic/rerank/ElasticInferenceServiceRerankRequestEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.inference.external.request.elastic.rerank; | ||
|
|
||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.xcontent.ToXContentObject; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public record ElasticInferenceServiceRerankRequestEntity( | ||
| String query, | ||
| List<String> documents, | ||
| String modelId, | ||
| @Nullable Integer topNDocumentsOnly | ||
| ) implements ToXContentObject { | ||
|
|
||
| private static final String QUERY_FIELD = "query"; | ||
| private static final String MODEL_FIELD = "model"; | ||
| private static final String TOP_N_DOCUMENTS_ONLY_FIELD = "top_n"; | ||
| private static final String DOCUMENTS_FIELD = "documents"; | ||
|
|
||
| public ElasticInferenceServiceRerankRequestEntity { | ||
| Objects.requireNonNull(query); | ||
| Objects.requireNonNull(documents); | ||
| Objects.requireNonNull(modelId); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.startObject(); | ||
|
|
||
| builder.field(QUERY_FIELD, query); | ||
|
|
||
| builder.field(MODEL_FIELD, modelId); | ||
|
|
||
| if (Objects.nonNull(topNDocumentsOnly)) { | ||
| builder.field(TOP_N_DOCUMENTS_ONLY_FIELD, topNDocumentsOnly); | ||
| } | ||
|
|
||
| builder.startArray(DOCUMENTS_FIELD); | ||
| for (String document : documents) { | ||
| builder.value(document); | ||
| } | ||
|
|
||
| builder.endArray(); | ||
|
|
||
| builder.endObject(); | ||
|
|
||
| return builder; | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
...pack/inference/external/response/elastic/ElasticInferenceServiceRerankResponseEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.inference.external.response.elastic; | ||
|
|
||
| import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; | ||
| import org.elasticsearch.inference.InferenceServiceResults; | ||
| import org.elasticsearch.xcontent.ConstructingObjectParser; | ||
| import org.elasticsearch.xcontent.ParseField; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
| import org.elasticsearch.xpack.core.inference.results.RankedDocsResults; | ||
| import org.elasticsearch.xpack.inference.external.http.HttpResult; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; | ||
|
|
||
| public class ElasticInferenceServiceRerankResponseEntity { | ||
|
|
||
| record RerankResult(List<RerankResultEntry> entries) { | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static final ConstructingObjectParser<RerankResult, Void> PARSER = new ConstructingObjectParser<>( | ||
| RerankResult.class.getSimpleName(), | ||
| true, | ||
| args -> new RerankResult((List<RerankResultEntry>) args[0]) | ||
| ); | ||
|
|
||
| static { | ||
| PARSER.declareObjectArray(constructorArg(), RerankResultEntry.PARSER::apply, new ParseField("results")); | ||
| } | ||
|
|
||
| record RerankResultEntry(Integer index, Float relevanceScore) { | ||
|
|
||
| public static final ConstructingObjectParser<RerankResultEntry, Void> PARSER = new ConstructingObjectParser<>( | ||
| RerankResultEntry.class.getSimpleName(), | ||
| args -> new RerankResultEntry((Integer) args[0], (Float) args[1]) | ||
| ); | ||
|
|
||
| static { | ||
| PARSER.declareInt(constructorArg(), new ParseField("index")); | ||
| PARSER.declareFloat(constructorArg(), new ParseField("relevance_score")); | ||
| } | ||
|
|
||
| public RankedDocsResults.RankedDoc toRankedDoc() { | ||
| return new RankedDocsResults.RankedDoc(index, relevanceScore, null); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static InferenceServiceResults fromResponse(HttpResult response) throws IOException { | ||
| var parserConfig = XContentParserConfiguration.EMPTY.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE); | ||
|
|
||
| try (XContentParser jsonParser = XContentFactory.xContent(XContentType.JSON).createParser(parserConfig, response.body())) { | ||
| var rerankResult = RerankResult.PARSER.apply(jsonParser, null); | ||
|
|
||
| return new RankedDocsResults(rerankResult.entries.stream().map(RerankResult.RerankResultEntry::toRankedDoc).toList()); | ||
| } | ||
| } | ||
|
|
||
| private ElasticInferenceServiceRerankResponseEntity() {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're also targeting 8.19, we need another transport version place holder for the 8.19 branch (but in this PR).
For example I recently added one
public static final TransportVersion INFERENCE_CUSTOM_SERVICE_ADDED_8_19 = def(8_841_0_39);in themainbranch even though it isn't used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add backport transport version