-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5828: Add Rerank stage builder #1936
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
ddf3780
abee37f
44cb787
6c0d2c3
83f9a3b
9b6a730
a0c2e39
ca2b247
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 |
|---|---|---|
|
|
@@ -1081,6 +1081,79 @@ public static PipelineDefinition<TInput, TOutput> RankFusion<TInput, TIntermedia | |
| return pipeline.AppendStage(PipelineStageDefinitionBuilder.RankFusion(pipelinesWithWeights, options)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends a $rerank stage to the pipeline. | ||
| /// </summary> | ||
| /// <typeparam name="TInput">The type of the input documents.</typeparam> | ||
| /// <typeparam name="TField">The type of the field.</typeparam> | ||
| /// <typeparam name="TOutput">The type of the output documents.</typeparam> | ||
| /// <param name="pipeline">The pipeline.</param> | ||
| /// <param name="query">The rerank query.</param> | ||
| /// <param name="path">The field to send to the reranker.</param> | ||
| /// <param name="numDocsToRerank">The maximum number of documents to rerank.</param> | ||
| /// <param name="model">The reranking model name.</param> | ||
| /// <returns>A new pipeline with an additional stage.</returns> | ||
| public static PipelineDefinition<TInput, TOutput> Rerank<TInput, TField, TOutput>( | ||
| this PipelineDefinition<TInput, TOutput> pipeline, | ||
| RerankQuery query, | ||
| Expression<Func<TOutput, TField>> path, | ||
|
sanych-sun marked this conversation as resolved.
|
||
| int numDocsToRerank, | ||
| string model) | ||
| { | ||
| Ensure.IsNotNull(pipeline, nameof(pipeline)); | ||
| return pipeline.AppendStage( | ||
| PipelineStageDefinitionBuilder.Rerank(query, path, numDocsToRerank, model), | ||
| pipeline.OutputSerializer); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends a $rerank stage to the pipeline. | ||
| /// </summary> | ||
| /// <typeparam name="TInput">The type of the input documents.</typeparam> | ||
| /// <typeparam name="TOutput">The type of the output documents.</typeparam> | ||
| /// <param name="pipeline">The pipeline.</param> | ||
| /// <param name="query">The rerank query.</param> | ||
| /// <param name="path">The field to send to the reranker.</param> | ||
| /// <param name="numDocsToRerank">The maximum number of documents to rerank.</param> | ||
| /// <param name="model">The reranking model name.</param> | ||
| /// <returns>A new pipeline with an additional stage.</returns> | ||
| public static PipelineDefinition<TInput, TOutput> Rerank<TInput, TOutput>( | ||
| this PipelineDefinition<TInput, TOutput> pipeline, | ||
| RerankQuery query, | ||
| FieldDefinition<TOutput> path, | ||
| int numDocsToRerank, | ||
| string model) | ||
| { | ||
| Ensure.IsNotNull(pipeline, nameof(pipeline)); | ||
| return pipeline.AppendStage( | ||
| PipelineStageDefinitionBuilder.Rerank(query, path, numDocsToRerank, model), | ||
| pipeline.OutputSerializer); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends a $rerank stage to the pipeline. | ||
| /// </summary> | ||
| /// <typeparam name="TInput">The type of the input documents.</typeparam> | ||
| /// <typeparam name="TOutput">The type of the output documents.</typeparam> | ||
| /// <param name="pipeline">The pipeline.</param> | ||
| /// <param name="query">The rerank query.</param> | ||
| /// <param name="paths">The fields to send to the reranker.</param> | ||
| /// <param name="numDocsToRerank">The maximum number of documents to rerank.</param> | ||
| /// <param name="model">The reranking model name.</param> | ||
| /// <returns>A new pipeline with an additional stage.</returns> | ||
| public static PipelineDefinition<TInput, TOutput> Rerank<TInput, TOutput>( | ||
|
sanych-sun marked this conversation as resolved.
|
||
| this PipelineDefinition<TInput, TOutput> pipeline, | ||
| RerankQuery query, | ||
| IEnumerable<FieldDefinition<TOutput>> paths, | ||
|
Member
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. Let's either use IEnumerable parameter or params in both cases: here and on the line 1126.
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. The two multi-path overloads serve different use cases: the
Member
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. I see this as a different way to provide the same parameters. Having one as an array and another as a params looks confusing a little. About different use cases: both expressions and FieldDefinitions could be used as fixed list of values or dynamic collections I believe. I think I would prefer consistency between overloads if it's possible. @BorisDog @papafe @ajcvickers WDYT?
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. Agree with @adelinowona.
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. Changing my mind, as I already commented. I don't agree with, "Having one as an array and another as a params looks confusing a little. " but I do like keeping the parameter order the same between the overloads. |
||
| int numDocsToRerank, | ||
| string model) | ||
| { | ||
| Ensure.IsNotNull(pipeline, nameof(pipeline)); | ||
| return pipeline.AppendStage( | ||
| PipelineStageDefinitionBuilder.Rerank(query, paths, numDocsToRerank, model), | ||
| pipeline.OutputSerializer); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends a $replaceRoot stage to the pipeline. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using MongoDB.Bson; | ||
| using MongoDB.Driver.Core.Misc; | ||
|
|
||
| namespace MongoDB.Driver; | ||
|
|
||
| /// <summary> | ||
| /// Represents a query for the $rerank aggregation stage. | ||
| /// </summary> | ||
| /// <seealso cref="PipelineStageDefinitionBuilder.Rerank{TInput}(RerankQuery, FieldDefinition{TInput}, int, string)"/> | ||
| public sealed class RerankQuery | ||
| { | ||
| private readonly string _text; | ||
|
|
||
| private RerankQuery(string text) | ||
| { | ||
| _text = text; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a text-based rerank query. | ||
| /// </summary> | ||
| /// <param name="text">The text to rerank against.</param> | ||
| /// <returns>A text rerank query.</returns> | ||
| public static RerankQuery Text(string text) => | ||
| new RerankQuery(Ensure.IsNotNullOrEmpty(text, nameof(text))); | ||
|
|
||
| internal BsonDocument Render() => new BsonDocument("text", _text); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.