-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[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 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e42624c
Move CompletionOperatorRequestIterator.PromptReader to InputTextReade…
afoucret 9d1672a
Implementing the TextEmbeddingInferenceOperator.
afoucret 4fc060f
Merge branch 'main' into esql_text_embedding_inference_operator
afoucret 27eedf5
Inference operator request iterator and output builder are package pr…
afoucret 34941b0
Fix comment
afoucret 6a3e8c7
[CI] Update transport version definitions
111530f
Lint fixes
afoucret 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
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() | ||
| ); | ||
| } | ||
| } | ||
| } | ||
111 changes: 111 additions & 0 deletions
111
.../elasticsearch/xpack/esql/inference/textembedding/TextEmbeddingOperatorOutputBuilder.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,111 @@ | ||
| /* | ||
| * 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.Block; | ||
| import org.elasticsearch.compute.data.FloatBlock; | ||
| import org.elasticsearch.compute.data.Page; | ||
| import org.elasticsearch.core.Releasables; | ||
| import org.elasticsearch.xpack.core.inference.action.InferenceAction; | ||
| import org.elasticsearch.xpack.core.inference.results.TextEmbeddingByteResults; | ||
| import org.elasticsearch.xpack.core.inference.results.TextEmbeddingFloatResults; | ||
| import org.elasticsearch.xpack.core.inference.results.TextEmbeddingResults; | ||
| import org.elasticsearch.xpack.esql.inference.InferenceOperator; | ||
|
|
||
| /** | ||
| * {@link TextEmbeddingOperatorOutputBuilder} builds the output page for text embedding by converting | ||
| * {@link TextEmbeddingResults} into a {@link FloatBlock} containing dense vector embeddings. | ||
| */ | ||
| public class TextEmbeddingOperatorOutputBuilder implements InferenceOperator.OutputBuilder { | ||
| private final Page inputPage; | ||
| private final FloatBlock.Builder outputBlockBuilder; | ||
|
|
||
| public TextEmbeddingOperatorOutputBuilder(FloatBlock.Builder outputBlockBuilder, Page inputPage) { | ||
| this.inputPage = inputPage; | ||
| this.outputBlockBuilder = outputBlockBuilder; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| Releasables.close(outputBlockBuilder); | ||
| } | ||
|
|
||
| /** | ||
| * Adds an inference response to the output builder. | ||
| * | ||
| * <p> | ||
| * If the response is null or not of type {@link TextEmbeddingResults} an {@link IllegalStateException} is thrown. | ||
| * Else, the embedding vector is added to the output block as a multi-value position. | ||
| * </p> | ||
| * | ||
| * <p> | ||
| * The responses must be added in the same order as the corresponding inference requests were generated. | ||
| * Failing to preserve order may lead to incorrect or misaligned output rows. | ||
| * </p> | ||
| */ | ||
| @Override | ||
| public void addInferenceResponse(InferenceAction.Response inferenceResponse) { | ||
| if (inferenceResponse == null) { | ||
| outputBlockBuilder.appendNull(); | ||
| return; | ||
| } | ||
|
|
||
| TextEmbeddingResults<?> embeddingResults = inferenceResults(inferenceResponse); | ||
|
|
||
| var embeddings = embeddingResults.embeddings(); | ||
| if (embeddings.isEmpty()) { | ||
| outputBlockBuilder.appendNull(); | ||
| return; | ||
| } | ||
|
|
||
| float[] embeddingArray = getEmbeddingAsFloatArray(embeddingResults); | ||
|
|
||
| outputBlockBuilder.beginPositionEntry(); | ||
| for (float component : embeddingArray) { | ||
| outputBlockBuilder.appendFloat(component); | ||
| } | ||
| outputBlockBuilder.endPositionEntry(); | ||
| } | ||
|
|
||
| /** | ||
| * Builds the final output page by appending the embedding output block to the input page. | ||
| */ | ||
| @Override | ||
| public Page buildOutput() { | ||
| Block outputBlock = outputBlockBuilder.build(); | ||
| assert outputBlock.getPositionCount() == inputPage.getPositionCount(); | ||
| return inputPage.appendBlock(outputBlock); | ||
| } | ||
|
|
||
| private TextEmbeddingResults<?> inferenceResults(InferenceAction.Response inferenceResponse) { | ||
| return InferenceOperator.OutputBuilder.inferenceResults(inferenceResponse, TextEmbeddingResults.class); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the embedding as a float array from the embedding result. | ||
| */ | ||
| private float[] getEmbeddingAsFloatArray(TextEmbeddingResults<?> embedding) { | ||
carlosdelest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return switch (embedding.embeddings().get(0)) { | ||
| case TextEmbeddingFloatResults.Embedding floatEmbedding -> floatEmbedding.values(); | ||
| case TextEmbeddingByteResults.Embedding byteEmbedding -> toFloatArray(byteEmbedding.values()); | ||
| default -> throw new IllegalArgumentException( | ||
| "Unsupported embedding type: " | ||
| + embedding.embeddings().get(0).getClass().getName() | ||
| + ". Expected TextEmbeddingFloatResults.Embedding or TextEmbeddingByteResults.Embedding." | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| private float[] toFloatArray(byte[] values) { | ||
carlosdelest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| float[] floatArray = new float[values.length]; | ||
| for (int i = 0; i < values.length; i++) { | ||
| floatArray[i] = ((Byte) values[i]).floatValue(); | ||
| } | ||
| return floatArray; | ||
| } | ||
| } | ||
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.