Repository navigation
Support findAndRerank single leg retrieval #2678
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
Changes from all commits
23b7dbb
5291641
576fcd7
ac3a6e2
7e73946
9978cd4
b2a8a91
f22880a
82a8ad7
019784d
298eff3
1960bd4
a90f088
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,14 +6,16 @@ | |
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * The reranking query to use for a reranking operation. Either from the $vectorize sort clause or | ||
| * manually specified by the user. | ||
| * The reranking query to use for a reranking operation. Either from the $vectorize or $lexical sort | ||
| * clause, or manually specified by the user. | ||
| */ | ||
| public class RerankingQuery implements Recordable { | ||
|
|
||
| public enum Source { | ||
| /** Query came from the $vectorize sort in the user query. This is the default. */ | ||
| VECTORIZE, | ||
| /** Query came from the $lexical sort in the user query, when the sort is only $lexical. */ | ||
| LEXICAL, | ||
| /** Query came from the options.rerankQuery property in the user query. */ | ||
| OPTIONS, | ||
| } | ||
|
|
@@ -45,15 +47,34 @@ public Source source() { | |
| * vectorize. | ||
| * @return Constructed RerankingQuery, with the source indicating where the query came from. | ||
| */ | ||
| public static RerankingQuery create(String rerankQuery, String vectorizeText) { | ||
| public static RerankingQuery forVectorizeSort(String rerankQuery, String vectorizeText) { | ||
| return create(rerankQuery, vectorizeText, Source.VECTORIZE); | ||
|
Contributor
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. nit: we dont need createFor* we can just have for* when we have a factory method like thes. |
||
| } | ||
|
|
||
| /** | ||
| * Creates a new RerankingQuery for a $lexical sort: the <code>rerankQuery</code> option when it | ||
| * is not blank, otherwise the text of the $lexical sort. | ||
| * | ||
| * <p>Throws {@link RequestException.Code#MISSING_RERANK_QUERY_TEXT} if both are null or blank. | ||
| * | ||
| * @param rerankQuery The <code>rerankQuery</code> option, may be <code>null</code>. | ||
| * @param lexicalText The text of the $lexical sort. | ||
| * @return Constructed RerankingQuery, with the source indicating where the query came from. | ||
| */ | ||
| public static RerankingQuery forLexicalSort(String rerankQuery, String lexicalText) { | ||
| return create(rerankQuery, lexicalText, Source.LEXICAL); | ||
| } | ||
|
|
||
| /** The rerankQuery option when it is not blank, otherwise the sort text and its source. */ | ||
| private static RerankingQuery create(String rerankQuery, String sortText, Source sortSource) { | ||
|
|
||
| if (rerankQuery != null && !rerankQuery.isBlank()) { | ||
| return new RerankingQuery(rerankQuery, Source.OPTIONS); | ||
| } | ||
|
|
||
| // will never be blank, but double-checking for safety | ||
| if (vectorizeText != null && !vectorizeText.isBlank()) { | ||
| return new RerankingQuery(vectorizeText, Source.VECTORIZE); | ||
| if (sortText != null && !sortText.isBlank()) { | ||
| return new RerankingQuery(sortText, sortSource); | ||
| } | ||
|
|
||
| throw RequestException.Code.MISSING_RERANK_QUERY_TEXT.get(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,12 @@ | |
| import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.*; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.clause.filter.SortDefinition; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.clause.sort.FindAndRerankSort; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.clause.sort.SortClause; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.clause.sort.SortExpression; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.impl.FindAndRerankCommand; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.impl.FindCommand; | ||
| import io.stargate.sgv2.jsonapi.metrics.CommandFeatures; | ||
| import io.stargate.sgv2.jsonapi.service.embedding.operation.EmbeddingProvider; | ||
| import io.stargate.sgv2.jsonapi.service.operation.Operation; | ||
| import io.stargate.sgv2.jsonapi.service.operation.embeddings.EmbeddingDeferredAction; | ||
|
|
@@ -189,11 +191,20 @@ private TaskGroupAndDeferrables<IntermediateCollectionReadTask, CollectionSchema | |
| // Hack: See https://github.com/stargate/data-api/issues/1961 | ||
| // copying the hybrid limits on the command context so the find command resolver can pick it up | ||
| // when the command runs later, so we can set the page size to be the same as the limit | ||
| // A sort without $hybrid has no hybridLimits, so we copy the read limit of its one leg | ||
| commandContext.setHybridLimits( | ||
| getOrDefault( | ||
| command.options(), | ||
| FindAndRerankCommand.Options::hybridLimits, | ||
| FindAndRerankCommand.HybridLimits.DEFAULT)); | ||
| switch (command.sortClause()) { | ||
| case FindAndRerankSort.MultiLeg ignored -> | ||
| getOrDefault( | ||
| command.options(), | ||
| FindAndRerankCommand.Options::hybridLimits, | ||
| FindAndRerankCommand.HybridLimits.DEFAULT); | ||
| case FindAndRerankSort.SingleLeg ignored -> { | ||
| var readLimit = plan.legs().getFirst().readLimit(); | ||
| yield new FindAndRerankCommand.HybridLimits( | ||
| readLimit, readLimit, CommandFeatures.EMPTY); | ||
| } | ||
| }); | ||
|
Comment on lines
+196
to
+207
Contributor
Author
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. Just reuse the hack to keep things moving, will have another PR to handle this properly. Test: IT
Contributor
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. +1 |
||
|
|
||
| var deferrables = new ArrayList<Deferrable>(); | ||
|
|
||
|
|
||
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.
For a
$lexicalsort:$lexicaltext.$lexical.rerankQueryandrerankOnoptions replace these defaults. This is the same rule as for a$vectorizesort and a$hybridsort.Example 1, no options:
{"findAndRerank": { "filter": {"country": "nz"}, "sort": {"$lexical": "cheese balls"}, "options": {} }}Lexical read:
"I like cheese!". Rerank query:"cheese balls". Rerank field:$lexical.Example 2, with options:
{"findAndRerank": { "filter": {}, "sort": {"$lexical": "I like cheese!"}, "options": { "rerankQuery": "who loves cheese the most?", "rerankOn": "food_preferences", "rerankCandidateLimit": 100, "limit": 10 } }}The lexical read uses
"I like cheese!"and reads up to 100 documents. The reranker compares"who loves cheese the most?"tofood_preferencesand returns the top 10.LMK if we don't want to do this
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.
that looks correct - the $lexical is used for the lexical sort the option.rerankQuery is sent to the rerank model