-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[ES|QL] Text embedding inference operator #135062
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
afoucret
merged 8 commits into
elastic:main
from
afoucret:esql_text_embedding_inference_operator
Sep 24, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e42624c
Move CompletionOperatorRequestIterator.PromptReader to InputTextReade…
9d1672a
Implementing the TextEmbeddingInferenceOperator.
4fc060f
Merge branch 'main' into esql_text_embedding_inference_operator
afoucret 27eedf5
Inference operator request iterator and output builder are package pr…
34941b0
Fix comment
6a3e8c7
[CI] Update transport version definitions
111530f
Lint fixes
33e114c
Merge branch 'main' into esql_text_embedding_inference_operator
afoucret 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
76 changes: 76 additions & 0 deletions
76
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/inference/InputTextReader.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,76 @@ | ||
| /* | ||
| * 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.esql.inference; | ||
|
|
||
| import org.apache.lucene.util.BytesRef; | ||
| import org.elasticsearch.compute.data.BytesRefBlock; | ||
| import org.elasticsearch.core.Releasable; | ||
| import org.elasticsearch.core.Releasables; | ||
|
|
||
| /** | ||
| * Helper class that reads text strings from a {@link BytesRefBlock}. | ||
| * This class is used by inference operators to extract text content from block data. | ||
| */ | ||
| public class InputTextReader implements Releasable { | ||
| private final BytesRefBlock textBlock; | ||
| private final StringBuilder strBuilder = new StringBuilder(); | ||
| private BytesRef readBuffer = new BytesRef(); | ||
|
|
||
| public InputTextReader(BytesRefBlock textBlock) { | ||
| this.textBlock = textBlock; | ||
| } | ||
|
|
||
| /** | ||
| * Reads the text string at the given position. | ||
| * Multiple values at the position are concatenated with newlines. | ||
| * | ||
| * @param pos the position index in the block | ||
| * @return the text string at the position, or null if the position contains a null value | ||
| */ | ||
| public String readText(int pos) { | ||
| return readText(pos, Integer.MAX_VALUE); | ||
| } | ||
|
|
||
| /** | ||
| * Reads the text string at the given position. | ||
| * | ||
| * @param pos the position index in the block | ||
| * @param limit the maximum number of value to read from the position | ||
| * @return the text string at the position, or null if the position contains a null value | ||
| */ | ||
| public String readText(int pos, int limit) { | ||
| if (textBlock.isNull(pos)) { | ||
| return null; | ||
| } | ||
|
|
||
| strBuilder.setLength(0); | ||
| int maxPos = Math.min(limit, textBlock.getValueCount(pos)); | ||
| for (int valueIndex = 0; valueIndex < maxPos; valueIndex++) { | ||
| readBuffer = textBlock.getBytesRef(textBlock.getFirstValueIndex(pos) + valueIndex, readBuffer); | ||
| strBuilder.append(readBuffer.utf8ToString()); | ||
| if (valueIndex != maxPos - 1) { | ||
| strBuilder.append("\n"); | ||
| } | ||
| } | ||
|
|
||
| return strBuilder.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the total number of positions (text entries) in the block. | ||
| */ | ||
| public int estimatedSize() { | ||
| return textBlock.getPositionCount(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| textBlock.allowPassingToDifferentDriver(); | ||
| Releasables.close(textBlock); | ||
| } | ||
| } | ||
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
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
96 changes: 96 additions & 0 deletions
96
...main/java/org/elasticsearch/xpack/esql/inference/textembedding/TextEmbeddingOperator.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,96 @@ | ||
| /* | ||
| * 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.esql.inference.textembedding; | ||
|
|
||
| import org.elasticsearch.compute.data.BytesRefBlock; | ||
| import org.elasticsearch.compute.data.FloatBlock; | ||
| import org.elasticsearch.compute.data.Page; | ||
| import org.elasticsearch.compute.operator.DriverContext; | ||
| import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator; | ||
| import org.elasticsearch.compute.operator.Operator; | ||
| import org.elasticsearch.core.Releasables; | ||
| import org.elasticsearch.xpack.esql.inference.InferenceOperator; | ||
| import org.elasticsearch.xpack.esql.inference.InferenceService; | ||
| import org.elasticsearch.xpack.esql.inference.bulk.BulkInferenceRequestIterator; | ||
| import org.elasticsearch.xpack.esql.inference.bulk.BulkInferenceRunner; | ||
| import org.elasticsearch.xpack.esql.inference.bulk.BulkInferenceRunnerConfig; | ||
|
|
||
| /** | ||
| * {@link TextEmbeddingOperator} is an {@link InferenceOperator} that performs text embedding inference. | ||
| * It evaluates a text expression for each input row, constructs text embedding inference requests, | ||
| * and emits the dense vector embeddings as output. | ||
| */ | ||
| public class TextEmbeddingOperator extends InferenceOperator { | ||
|
|
||
| private final ExpressionEvaluator textEvaluator; | ||
|
|
||
afoucret marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public TextEmbeddingOperator( | ||
| DriverContext driverContext, | ||
| BulkInferenceRunner bulkInferenceRunner, | ||
| String inferenceId, | ||
| ExpressionEvaluator textEvaluator, | ||
| int maxOutstandingPages | ||
| ) { | ||
| super(driverContext, bulkInferenceRunner, inferenceId, maxOutstandingPages); | ||
| this.textEvaluator = textEvaluator; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doClose() { | ||
| Releasables.close(textEvaluator); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "TextEmbeddingOperator[inference_id=[" + inferenceId() + "]]"; | ||
| } | ||
|
|
||
| /** | ||
| * Constructs the text embedding inference requests iterator for the given input page by evaluating the text expression. | ||
| * | ||
| * @param inputPage The input data page. | ||
| */ | ||
| @Override | ||
| protected BulkInferenceRequestIterator requests(Page inputPage) { | ||
| return new TextEmbeddingOperatorRequestIterator((BytesRefBlock) textEvaluator.eval(inputPage), inferenceId()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link TextEmbeddingOperatorOutputBuilder} to collect and emit the text embedding results. | ||
| * | ||
| * @param input The input page for which results will be constructed. | ||
| */ | ||
| @Override | ||
| protected TextEmbeddingOperatorOutputBuilder outputBuilder(Page input) { | ||
| FloatBlock.Builder outputBlockBuilder = blockFactory().newFloatBlockBuilder(input.getPositionCount()); | ||
| return new TextEmbeddingOperatorOutputBuilder(outputBlockBuilder, input); | ||
| } | ||
|
|
||
| /** | ||
| * Factory for creating {@link TextEmbeddingOperator} instances. | ||
| */ | ||
| public record Factory(InferenceService inferenceService, String inferenceId, ExpressionEvaluator.Factory textEvaluatorFactory) | ||
| implements | ||
| OperatorFactory { | ||
| @Override | ||
| public String describe() { | ||
| return "TextEmbeddingOperator[inference_id=[" + inferenceId + "]]"; | ||
| } | ||
|
|
||
| @Override | ||
| public Operator get(DriverContext driverContext) { | ||
| return new TextEmbeddingOperator( | ||
| driverContext, | ||
| inferenceService.bulkInferenceRunner(), | ||
| inferenceId, | ||
| textEvaluatorFactory.get(driverContext), | ||
| BulkInferenceRunnerConfig.DEFAULT.maxOutstandingBulkRequests() | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.