-
Notifications
You must be signed in to change notification settings - Fork 25.3k
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
Draft
dan-rubinstein
wants to merge
4
commits into
elastic:main
Choose a base branch
from
dan-rubinstein:rerank-request-chunker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add RerankRequestChunker #130485
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
97 changes: 97 additions & 0 deletions
97
...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,97 @@ | ||
/* | ||
* 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 com.ibm.icu.text.BreakIterator; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.inference.ChunkingSettings; | ||
import org.elasticsearch.inference.InferenceServiceResults; | ||
import org.elasticsearch.xpack.core.inference.results.RankedDocsResults; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class RerankRequestChunker { | ||
private final List<String> inputs; | ||
private final List<RerankChunks> rerankChunks; | ||
|
||
public RerankRequestChunker(String query, List<String> inputs) { | ||
this.inputs = inputs; | ||
this.rerankChunks = chunk(inputs, buildChunkingSettingsForElasticRerank(query)); | ||
} | ||
|
||
private List<RerankChunks> chunk(List<String> inputs, ChunkingSettings chunkingSettings) { | ||
var chunker = ChunkerBuilder.fromChunkingStrategy(chunkingSettings.getChunkingStrategy()); | ||
var chunks = new ArrayList<RerankChunks>(); | ||
for (int i = 0; i < inputs.size(); i++) { | ||
var chunksForInput = chunker.chunk(inputs.get(i), chunkingSettings); | ||
for (var chunk : chunksForInput) { | ||
chunks.add(new RerankChunks(i, inputs.get(i).substring(chunk.start(), chunk.end()))); | ||
} | ||
} | ||
return chunks; | ||
} | ||
|
||
public List<String> getChunkedInputs() { | ||
List<String> chunkedInputs = new ArrayList<>(); | ||
for (RerankChunks chunk : rerankChunks) { | ||
chunkedInputs.add(chunk.chunkString()); | ||
} | ||
|
||
// TODO: Score the inputs here and only return the top N chunks for each document | ||
return chunkedInputs; | ||
} | ||
|
||
public ActionListener<InferenceServiceResults> parseChunkedRerankResultsListener(ActionListener<InferenceServiceResults> listener) { | ||
return ActionListener.wrap(results -> { | ||
if (results instanceof RankedDocsResults rankedDocsResults) { | ||
listener.onResponse(parseRankedDocResultsForChunks(rankedDocsResults)); | ||
|
||
} else { | ||
listener.onFailure(new IllegalArgumentException("Expected RankedDocsResults but got: " + results.getClass())); | ||
} | ||
|
||
}, listener::onFailure); | ||
} | ||
|
||
// TODO: Can we assume the rankeddocsresults are always sorted by relevance score? | ||
// TODO: Should we short circuit if no chunking was done? | ||
private RankedDocsResults parseRankedDocResultsForChunks(RankedDocsResults rankedDocsResults) { | ||
List<RankedDocsResults.RankedDoc> updatedRankedDocs = new ArrayList<>(); | ||
Set<Integer> docIndicesSeen = new HashSet<>(); | ||
for (RankedDocsResults.RankedDoc rankedDoc : rankedDocsResults.getRankedDocs()) { | ||
int chunkIndex = rankedDoc.index(); | ||
int docIndex = rerankChunks.get(chunkIndex).docIndex(); | ||
|
||
if (docIndicesSeen.contains(docIndex) == false) { | ||
// Create a ranked doc with the full input string and the index for the document instead of the chunk | ||
RankedDocsResults.RankedDoc updatedRankedDoc = new RankedDocsResults.RankedDoc( | ||
docIndex, | ||
rankedDoc.relevanceScore(), | ||
inputs.get(docIndex) | ||
); | ||
updatedRankedDocs.add(updatedRankedDoc); | ||
docIndicesSeen.add(docIndex); | ||
} | ||
} | ||
|
||
return new RankedDocsResults(updatedRankedDocs); | ||
} | ||
|
||
public record RerankChunks(int docIndex, String chunkString) {}; | ||
|
||
private ChunkingSettings buildChunkingSettingsForElasticRerank(String query) { | ||
var wordIterator = BreakIterator.getWordInstance(); | ||
wordIterator.setText(query); | ||
var queryWordCount = ChunkerUtils.countWords(0, query.length(), wordIterator); | ||
return ChunkingSettingsBuilder.buildChunkingSettingsForElasticRerank(queryWordCount); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -156,6 +156,7 @@ protected RankFeatureDoc[] preprocess(RankFeatureDoc[] originalDocs, boolean rer | |
} | ||
|
||
protected InferenceAction.Request generateRequest(List<String> docFeatures) { | ||
// TODO: Try running the RerankRequestChunker here. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed as we're calling this in the service now. |
||
return new InferenceAction.Request( | ||
TaskType.RERANK, | ||
inferenceId, | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed.