|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.inference.embedding; |
| 9 | + |
| 10 | +import org.elasticsearch.compute.data.Block; |
| 11 | +import org.elasticsearch.compute.data.FloatBlock; |
| 12 | +import org.elasticsearch.compute.data.Page; |
| 13 | +import org.elasticsearch.core.Releasables; |
| 14 | +import org.elasticsearch.xpack.core.inference.action.InferenceAction; |
| 15 | +import org.elasticsearch.xpack.core.inference.results.EmbeddingResults; |
| 16 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingByteResults; |
| 17 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingFloatResults; |
| 18 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingResults; |
| 19 | +import org.elasticsearch.xpack.esql.inference.InferenceOperator; |
| 20 | + |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.stream.IntStream; |
| 23 | + |
| 24 | +/** |
| 25 | + * Builds the output page for the {@link DenseEmbeddingOperator}. |
| 26 | + */ |
| 27 | +public class DenseEmbeddingOperatorOutputBuilder implements InferenceOperator.OutputBuilder { |
| 28 | + |
| 29 | + |
| 30 | + private final FloatBlock.Builder outputBlockBuilder; |
| 31 | + private final Page inputPage; |
| 32 | + private final int dimensions; |
| 33 | + |
| 34 | + public DenseEmbeddingOperatorOutputBuilder(FloatBlock.Builder outputBlockBuilder, Page inputPage, int dimensions) { |
| 35 | + this.outputBlockBuilder = outputBlockBuilder; |
| 36 | + this.inputPage = inputPage; |
| 37 | + this.dimensions = dimensions; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void close() { |
| 42 | + Releasables.close(outputBlockBuilder); |
| 43 | + releasePageOnAnyThread(inputPage); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructs a new output {@link Page} with dense embedding in the last column. |
| 48 | + */ |
| 49 | + @Override |
| 50 | + public Page buildOutput() { |
| 51 | + Block outputBlock = outputBlockBuilder.build(); |
| 52 | + assert outputBlock.getPositionCount() == inputPage.getPositionCount(); |
| 53 | + return inputPage.shallowCopy().appendBlock(outputBlock); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Extracts the embedding results from the inference response and append them to the output block builder. |
| 58 | + * <p> |
| 59 | + * If the response is not of type {@link TextEmbeddingResults} an {@link IllegalStateException} is thrown. |
| 60 | + * </p> |
| 61 | + * <p> |
| 62 | + * The responses must be added in the same order as the corresponding inference requests were generated. |
| 63 | + * Failing to preserve order may lead to incorrect or misaligned output rows. |
| 64 | + * </p> |
| 65 | + */ |
| 66 | + @Override |
| 67 | + public void addInferenceResponse(InferenceAction.Response inferenceResponse) { |
| 68 | + EmbeddingValueReader embeddingValueReader = EmbeddingValueReader.of(inferenceResponse, dimensions); |
| 69 | + while (embeddingValueReader.hasNext()) { |
| 70 | + writeEmbeddings(embeddingValueReader.next()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void writeEmbeddings(float[] values) { |
| 75 | + outputBlockBuilder.beginPositionEntry(); |
| 76 | + for (float value : values) { |
| 77 | + outputBlockBuilder.appendFloat(value); |
| 78 | + } |
| 79 | + outputBlockBuilder.endPositionEntry(); |
| 80 | + } |
| 81 | + |
| 82 | + private static class EmbeddingValueReader implements Iterator<float[]> { |
| 83 | + private final int dimensions; |
| 84 | + |
| 85 | + private final Iterator<? extends EmbeddingResults.Embedding<?>> embeddingsIterator; |
| 86 | + |
| 87 | + private EmbeddingValueReader(Iterator<? extends EmbeddingResults.Embedding<?>> embeddingsIterator, int dimensions) { |
| 88 | + this.dimensions = dimensions; |
| 89 | + this.embeddingsIterator = embeddingsIterator; |
| 90 | + } |
| 91 | + |
| 92 | + public boolean hasNext() { |
| 93 | + return embeddingsIterator.hasNext(); |
| 94 | + } |
| 95 | + |
| 96 | + public float[] next() { |
| 97 | + EmbeddingResults.Embedding<?> embedding = embeddingsIterator.next(); |
| 98 | + float[] values = switch(embedding) { |
| 99 | + case TextEmbeddingFloatResults.Embedding textEmbeddingFloat -> textEmbeddingFloat.values(); |
| 100 | + case TextEmbeddingByteResults.Embedding textEmbeddingBytes -> toFloatArray(textEmbeddingBytes.values()); |
| 101 | + default -> throw new IllegalStateException("Unsupported embedding type [" + embedding.getClass() + "]"); |
| 102 | + }; |
| 103 | + |
| 104 | + assert values.length == dimensions : "Unexpected vector size: " + values.length ; |
| 105 | + |
| 106 | + return values; |
| 107 | + } |
| 108 | + |
| 109 | + private static float[] toFloatArray(byte[] bytes) { |
| 110 | + float[] floatValues = new float[bytes.length]; |
| 111 | + IntStream.range(0, floatValues.length).forEach(i -> floatValues[i] = ((Byte) bytes[i]).floatValue()); |
| 112 | + return floatValues; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + public static EmbeddingValueReader of(InferenceAction.Response inferenceResponse, int dimensions) { |
| 117 | + TextEmbeddingResults<?> inferenceResults = InferenceOperator.OutputBuilder.inferenceResults(inferenceResponse, TextEmbeddingResults.class); |
| 118 | + return new EmbeddingValueReader(inferenceResults.embeddings().iterator(), dimensions); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments