-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add RerankRequestChunker #130485
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
dan-rubinstein
merged 17 commits into
elastic:main
from
dan-rubinstein:rerank-request-chunker
Sep 29, 2025
Merged
Add RerankRequestChunker #130485
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
849147e
Add RerankRequestChunker
dan-rubinstein c41d54c
Merge branch 'main' into rerank-request-chunker
elasticmachine da4c939
Add chunking strategy generation
dan-rubinstein 004ca8f
Merge branch 'main' into rerank-request-chunker
davidkyle 5ec620a
Merge branch 'main' into rerank-request-chunker
elasticmachine 4ff8eb0
Adding unit tests and fixing token/word ratio
dan-rubinstein ec78b87
Merge branch 'main' into rerank-request-chunker
elasticmachine 9ef8917
Add configurable values for long document handling strategy and maxim…
dan-rubinstein 24497ae
Adding back sentence overlap for rerank chunking strategy
dan-rubinstein 1fea365
Merge branch 'main' into rerank-request-chunker
elasticmachine 8396214
Merge branch 'main' into rerank-request-chunker
elasticmachine 8b97711
Adding unit tests, transport version, and feature flag
dan-rubinstein 833ef02
Update docs/changelog/130485.yaml
dan-rubinstein 77701e1
Merge branch 'main' of github.com:elastic/elasticsearch into rerank-r…
dan-rubinstein 344e121
Adding unit tests and refactoring code with clearer naming conventions
dan-rubinstein 02c9d0a
Merge branch 'main' of github.com:elastic/elasticsearch into rerank-r…
dan-rubinstein d68bf09
Merge branch 'main' of github.com:elastic/elasticsearch into rerank-r…
dan-rubinstein 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
99 changes: 99 additions & 0 deletions
99
...erence/src/main/java/org/elasticsearch/xpack/inference/chunking/RerankRequestChunker.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,99 @@ | ||
| /* | ||
| * 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.chunking; | ||
|
|
||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.inference.ChunkingSettings; | ||
| import org.elasticsearch.xpack.core.inference.action.InferenceAction; | ||
| import org.elasticsearch.xpack.core.inference.results.RankedDocsResults; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class RerankRequestChunker { | ||
|
|
||
| private final ChunkingSettings chunkingSettings; | ||
| private final List<String> inputs; | ||
| private final Map<Integer, RerankChunks> rerankChunks; | ||
|
|
||
| public RerankRequestChunker(List<String> inputs) { | ||
| // TODO: Make chunking settings dependent on the model being used. | ||
| // There may be a way to do this dynamically knowing the max token size for the model/service and query size | ||
| // instead of hardcoding it ona model/service basis. | ||
| this.chunkingSettings = new WordBoundaryChunkingSettings(100, 10); | ||
| this.inputs = inputs; | ||
| this.rerankChunks = chunk(inputs, chunkingSettings); | ||
| } | ||
|
|
||
| private Map<Integer, RerankChunks> chunk(List<String> inputs, ChunkingSettings chunkingSettings) { | ||
| var chunker = ChunkerBuilder.fromChunkingStrategy(chunkingSettings.getChunkingStrategy()); | ||
| var chunks = new HashMap<Integer, RerankChunks>(); | ||
| var chunkIndex = 0; | ||
| for (int i = 0; i < inputs.size(); i++) { | ||
| var chunksForInput = chunker.chunk(inputs.get(i), chunkingSettings); | ||
| for (var chunk : chunksForInput) { | ||
| chunks.put(chunkIndex, new RerankChunks(i, inputs.get(i).substring(chunk.start(), chunk.end()))); | ||
| chunkIndex++; | ||
| } | ||
| } | ||
| return chunks; | ||
| } | ||
|
|
||
| public List<String> getChunkedInputs() { | ||
| List<String> chunkedInputs = new ArrayList<>(); | ||
| for (RerankChunks chunk : rerankChunks.values()) { | ||
| chunkedInputs.add(chunk.chunkString()); | ||
| } | ||
| // TODO: Score the inputs here and only return the top N chunks for each document | ||
| return chunkedInputs; | ||
| } | ||
|
|
||
| public ActionListener<InferenceAction.Response> parseChunkedRerankResultsListener(ActionListener<InferenceAction.Response> listener) { | ||
| return ActionListener.wrap(results -> { | ||
| if (results.getResults() instanceof RankedDocsResults rankedDocsResults) { | ||
| listener.onResponse(new InferenceAction.Response(parseRankedDocResultsForChunks(rankedDocsResults))); | ||
| // TODO: Figure out if the above correctly creates the response or if it loses any info | ||
|
|
||
| } else { | ||
| listener.onFailure(new IllegalArgumentException("Expected RankedDocsResults but got: " + results.getClass())); | ||
| } | ||
|
|
||
| }, listener::onFailure); | ||
| } | ||
|
|
||
| private RankedDocsResults parseRankedDocResultsForChunks(RankedDocsResults rankedDocsResults) { | ||
| Map<Integer, RankedDocsResults.RankedDoc> bestRankedDocResultPerDoc = new HashMap<>(); | ||
| for (var rankedDoc : rankedDocsResults.getRankedDocs()) { | ||
| int chunkIndex = rankedDoc.index(); | ||
| int docIndex = rerankChunks.get(chunkIndex).docIndex(); | ||
| if (bestRankedDocResultPerDoc.containsKey(docIndex)) { | ||
| RankedDocsResults.RankedDoc existingDoc = bestRankedDocResultPerDoc.get(docIndex); | ||
| if (rankedDoc.relevanceScore() > existingDoc.relevanceScore()) { | ||
| bestRankedDocResultPerDoc.put( | ||
| docIndex, | ||
| new RankedDocsResults.RankedDoc(docIndex, rankedDoc.relevanceScore(), inputs.get(docIndex)) | ||
| ); | ||
| } | ||
| } else { | ||
| bestRankedDocResultPerDoc.put( | ||
| docIndex, | ||
| new RankedDocsResults.RankedDoc(docIndex, rankedDoc.relevanceScore(), inputs.get(docIndex)) | ||
| ); | ||
| } | ||
| } | ||
| var bestRankedDocResultPerDocList = new ArrayList<>(bestRankedDocResultPerDoc.values()); | ||
| bestRankedDocResultPerDocList.sort( | ||
| (RankedDocsResults.RankedDoc d1, RankedDocsResults.RankedDoc d2) -> Float.compare(d2.relevanceScore(), d1.relevanceScore()) | ||
| ); | ||
| return new RankedDocsResults(bestRankedDocResultPerDocList); | ||
| } | ||
|
|
||
| public record RerankChunks(int docIndex, String chunkString) {}; | ||
| } |
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
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.