forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Include the chunk text offsets in chunked inference response (el…
- Loading branch information
Showing
56 changed files
with
610 additions
and
1,412 deletions.
There are no files selected for viewing
This file contains 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 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
45 changes: 45 additions & 0 deletions
45
...in/java/org/elasticsearch/xpack/core/inference/results/ChunkedInferenceEmbeddingByte.java
This file contains 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,45 @@ | ||
/* | ||
* 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.core.inference.results; | ||
|
||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.inference.ChunkedInference; | ||
import org.elasticsearch.xcontent.XContent; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public record ChunkedInferenceEmbeddingByte(List<ChunkedInferenceEmbeddingByte.ByteEmbeddingChunk> chunks) implements ChunkedInference { | ||
|
||
@Override | ||
public Iterator<Chunk> chunksAsMatchedTextAndByteReference(XContent xcontent) throws IOException { | ||
var asChunk = new ArrayList<Chunk>(); | ||
for (var chunk : chunks) { | ||
asChunk.add(new Chunk(chunk.matchedText(), chunk.offset(), toBytesReference(xcontent, chunk.embedding()))); | ||
} | ||
return asChunk.iterator(); | ||
} | ||
|
||
/** | ||
* Serialises the {@code value} array, according to the provided {@link XContent}, into a {@link BytesReference}. | ||
*/ | ||
private static BytesReference toBytesReference(XContent xContent, byte[] value) throws IOException { | ||
XContentBuilder builder = XContentBuilder.builder(xContent); | ||
builder.startArray(); | ||
for (byte v : value) { | ||
builder.value(v); | ||
} | ||
builder.endArray(); | ||
return BytesReference.bytes(builder); | ||
} | ||
|
||
public record ByteEmbeddingChunk(byte[] embedding, String matchedText, TextOffset offset) {} | ||
} |
45 changes: 45 additions & 0 deletions
45
...n/java/org/elasticsearch/xpack/core/inference/results/ChunkedInferenceEmbeddingFloat.java
This file contains 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,45 @@ | ||
/* | ||
* 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.core.inference.results; | ||
|
||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.inference.ChunkedInference; | ||
import org.elasticsearch.xcontent.XContent; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public record ChunkedInferenceEmbeddingFloat(List<FloatEmbeddingChunk> chunks) implements ChunkedInference { | ||
|
||
@Override | ||
public Iterator<Chunk> chunksAsMatchedTextAndByteReference(XContent xcontent) throws IOException { | ||
var asChunk = new ArrayList<Chunk>(); | ||
for (var chunk : chunks) { | ||
asChunk.add(new Chunk(chunk.matchedText(), chunk.offset(), toBytesReference(xcontent, chunk.embedding()))); | ||
} | ||
return asChunk.iterator(); | ||
} | ||
|
||
/** | ||
* Serialises the {@code value} array, according to the provided {@link XContent}, into a {@link BytesReference}. | ||
*/ | ||
private static BytesReference toBytesReference(XContent xContent, float[] value) throws IOException { | ||
XContentBuilder b = XContentBuilder.builder(xContent); | ||
b.startArray(); | ||
for (float v : value) { | ||
b.value(v); | ||
} | ||
b.endArray(); | ||
return BytesReference.bytes(b); | ||
} | ||
|
||
public record FloatEmbeddingChunk(float[] embedding, String matchedText, TextOffset offset) {} | ||
} |
67 changes: 67 additions & 0 deletions
67
.../java/org/elasticsearch/xpack/core/inference/results/ChunkedInferenceEmbeddingSparse.java
This file contains 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,67 @@ | ||
/* | ||
* 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.core.inference.results; | ||
|
||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.inference.ChunkedInference; | ||
import org.elasticsearch.xcontent.ToXContent; | ||
import org.elasticsearch.xcontent.XContent; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.core.ml.search.WeightedToken; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.xpack.core.inference.results.TextEmbeddingUtils.validateInputSizeAgainstEmbeddings; | ||
|
||
public record ChunkedInferenceEmbeddingSparse(List<SparseEmbeddingChunk> chunks) implements ChunkedInference { | ||
|
||
public static List<ChunkedInference> listOf(List<String> inputs, SparseEmbeddingResults sparseEmbeddingResults) { | ||
validateInputSizeAgainstEmbeddings(inputs, sparseEmbeddingResults.embeddings().size()); | ||
|
||
var results = new ArrayList<ChunkedInference>(inputs.size()); | ||
for (int i = 0; i < inputs.size(); i++) { | ||
results.add( | ||
new ChunkedInferenceEmbeddingSparse( | ||
List.of( | ||
new SparseEmbeddingChunk( | ||
sparseEmbeddingResults.embeddings().get(i).tokens(), | ||
inputs.get(i), | ||
new TextOffset(0, inputs.get(i).length()) | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
@Override | ||
public Iterator<Chunk> chunksAsMatchedTextAndByteReference(XContent xcontent) throws IOException { | ||
var asChunk = new ArrayList<Chunk>(); | ||
for (var chunk : chunks) { | ||
asChunk.add(new Chunk(chunk.matchedText(), chunk.offset(), toBytesReference(xcontent, chunk.weightedTokens()))); | ||
} | ||
return asChunk.iterator(); | ||
} | ||
|
||
private static BytesReference toBytesReference(XContent xContent, List<WeightedToken> tokens) throws IOException { | ||
XContentBuilder b = XContentBuilder.builder(xContent); | ||
b.startObject(); | ||
for (var weightedToken : tokens) { | ||
weightedToken.toXContent(b, ToXContent.EMPTY_PARAMS); | ||
} | ||
b.endObject(); | ||
return BytesReference.bytes(b); | ||
} | ||
|
||
public record SparseEmbeddingChunk(List<WeightedToken> weightedTokens, String matchedText, TextOffset offset) {} | ||
} |
23 changes: 23 additions & 0 deletions
23
...e/src/main/java/org/elasticsearch/xpack/core/inference/results/ChunkedInferenceError.java
This file contains 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,23 @@ | ||
/* | ||
* 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.core.inference.results; | ||
|
||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.inference.ChunkedInference; | ||
import org.elasticsearch.xcontent.XContent; | ||
|
||
import java.util.Iterator; | ||
import java.util.stream.Stream; | ||
|
||
public record ChunkedInferenceError(Exception exception) implements ChunkedInference { | ||
|
||
@Override | ||
public Iterator<Chunk> chunksAsMatchedTextAndByteReference(XContent xcontent) { | ||
return Stream.of(exception).map(e -> new Chunk(e.getMessage(), new TextOffset(0, 0), BytesArray.EMPTY)).iterator(); | ||
} | ||
} |
106 changes: 0 additions & 106 deletions
106
...ain/java/org/elasticsearch/xpack/core/inference/results/ErrorChunkedInferenceResults.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.