-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5763: Auto-embedding for vector search #1842
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07b4a96
CSHARP-5763: Auto-embedding for vector search
ajcvickers 653a923
Fixes for Copilot feedback.
ajcvickers 92a1f02
Updates based on community server testing.
ajcvickers d82aa66
- Removing compression options, since they are not included in this r…
ajcvickers e2e42d8
- Cleanup and testing of negative cases
ajcvickers baf4731
Create subclasses for different kinds of vector index.
ajcvickers 343199a
Review updates.
ajcvickers df5e499
Updated ordering in test baseline.
ajcvickers 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
97 changes: 97 additions & 0 deletions
97
src/MongoDB.Driver/CreateAutoEmbeddingVectorSearchIndexModel.cs
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 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 System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Linq.Expressions; | ||
| using MongoDB.Bson; | ||
|
|
||
| namespace MongoDB.Driver; | ||
|
|
||
| /// <summary> | ||
| /// Defines a vector index model for an auto-embedding vector index using strongly-typed C# APIs. | ||
| /// </summary> | ||
| public sealed class CreateAutoEmbeddingVectorSearchIndexModel<TDocument> : CreateVectorSearchIndexModelBase<TDocument> | ||
| { | ||
| /// <summary> | ||
| /// The name of the embedding model to use, such as "voyage-4", "voyage-4-large", etc. | ||
| /// </summary> | ||
| public string AutoEmbeddingModelName { get; } | ||
|
|
||
| /// <summary> | ||
| /// Indicates the type of data that will be embedded for an auto-embedding index. | ||
| /// </summary> | ||
| public VectorEmbeddingModality Modality { get; init; } = VectorEmbeddingModality.Text; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CreateVectorSearchIndexModel{TDocument}"/> for a vector index | ||
| /// that will automatically create embeddings from a given field in the document. The embedding model to use must | ||
| /// be passed to this constructor. | ||
| /// </summary> | ||
| /// <param name="name">The index name.</param> | ||
| /// <param name="field">The field containing the vectors to index.</param> | ||
| /// <param name="embeddingModelName">The name of the embedding model to use, such as "voyage-4", "voyage-4-large", etc.</param> | ||
| /// <param name="filterFields">Fields that may be used as filters in the vector query.</param> | ||
| public CreateAutoEmbeddingVectorSearchIndexModel( | ||
| FieldDefinition<TDocument> field, | ||
| string name, | ||
| string embeddingModelName, | ||
| params FieldDefinition<TDocument>[] filterFields) | ||
| : base(field, name, filterFields) | ||
| { | ||
| AutoEmbeddingModelName = embeddingModelName; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CreateVectorSearchIndexModel{TDocument}"/> for a vector index | ||
| /// that will automatically create embeddings from a given field in the document. The embedding model to use must | ||
| /// be passed to this constructor. | ||
| /// </summary> | ||
| /// <param name="name">The index name.</param> | ||
| /// <param name="field">An expression pointing to the field containing the vectors to index.</param> | ||
| /// <param name="embeddingModelName">The name of the embedding model to use, such as "voyage-4", "voyage-4-large", etc.</param> | ||
| /// <param name="filterFields">Expressions pointing to fields that may be used as filters in the vector query.</param> | ||
| public CreateAutoEmbeddingVectorSearchIndexModel( | ||
| Expression<Func<TDocument, object>> field, | ||
| string name, | ||
| string embeddingModelName, | ||
| params Expression<Func<TDocument, object>>[] filterFields) | ||
| : this( | ||
| new ExpressionFieldDefinition<TDocument>(field), | ||
| name, | ||
| embeddingModelName, | ||
| filterFields? | ||
| .Select(f => (FieldDefinition<TDocument>)new ExpressionFieldDefinition<TDocument>(f)) | ||
| .ToArray()) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| internal override BsonDocument Render(RenderArgs<TDocument> renderArgs) | ||
| { | ||
| var vectorField = new BsonDocument | ||
| { | ||
| { "type", "autoEmbed" }, | ||
| { "path", Field.Render(renderArgs).FieldName }, | ||
| { "modality", Modality.ToString().ToLowerInvariant() }, | ||
| { "model", AutoEmbeddingModelName }, | ||
| }; | ||
|
|
||
| var fieldDocuments = new List<BsonDocument> { vectorField }; | ||
| RenderFilterFields(renderArgs, fieldDocuments); | ||
| return new BsonDocument { { "fields", new BsonArray(fieldDocuments) } }; | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* 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 System.Collections.Generic; | ||
| using System.Linq; | ||
| using MongoDB.Bson; | ||
|
|
||
| namespace MongoDB.Driver; | ||
|
|
||
| /// <summary> | ||
| /// Defines common parts of a vector index model using strongly-typed C# APIs. | ||
| /// </summary> | ||
| public abstract class CreateVectorSearchIndexModelBase<TDocument> : CreateSearchIndexModel | ||
| { | ||
| /// <summary> | ||
| /// The field containing the vectors to index. | ||
| /// </summary> | ||
| public FieldDefinition<TDocument> Field { get; } | ||
|
|
||
| /// <summary> | ||
| /// Fields that may be used as filters in the vector query. | ||
| /// </summary> | ||
| public IReadOnlyList<FieldDefinition<TDocument>> FilterFields { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CreateVectorSearchIndexModel{TDocument}"/> class for a vector | ||
| /// index where the vector embeddings are created manually. The required options for <see cref="VectorSimilarity"/> | ||
| /// and the number of vector dimensions are passed to the constructor. | ||
| /// </summary> | ||
| /// <param name="name">The index name.</param> | ||
| /// <param name="field">The field containing the vectors to index.</param> | ||
| /// <param name="filterFields">Fields that may be used as filters in the vector query.</param> | ||
| protected CreateVectorSearchIndexModelBase( | ||
| FieldDefinition<TDocument> field, | ||
| string name, | ||
| params FieldDefinition<TDocument>[] filterFields) | ||
| : base(name, SearchIndexType.VectorSearch) | ||
| { | ||
| Field = field; | ||
| FilterFields = filterFields?.ToList() ?? []; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Renders the index model to a <see cref="BsonDocument"/>. | ||
| /// </summary> | ||
| /// <param name="renderArgs">The render arguments.</param> | ||
| /// <returns>A <see cref="BsonDocument" />.</returns> | ||
| internal abstract BsonDocument Render(RenderArgs<TDocument> renderArgs); | ||
|
|
||
| /// <summary> | ||
| /// Called by subclasses to render the filters for the index fields section. | ||
| /// </summary> | ||
| /// <param name="renderArgs">The render args.</param> | ||
| /// <param name="fields">The list into which fields should be added.</param> | ||
| private protected void RenderFilterFields(RenderArgs<TDocument> renderArgs, List<BsonDocument> fields) | ||
| { | ||
| if (FilterFields != null) | ||
| { | ||
| foreach (var filterPath in FilterFields) | ||
| { | ||
| var fieldDocument = new BsonDocument | ||
| { | ||
| { "type", "filter" }, { "path", filterPath.Render(renderArgs).FieldName } | ||
| }; | ||
|
|
||
| fields.Add(fieldDocument); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
Suggestion for
CreateSearchIndexModel:Should we introduce
BsonDocument Render(RenderArgs<TDocument> renderArgs) => Definitionfor more uniform handling in
CreateCreateIndexesOperation?Also now that
Renderis internal, the exception inCreateSearchIndexModel.Definitionis not accurate. Is it possible to override that property and throw there? I think making a property is acceptable as breaking change?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.
Can you refer back to the comments that Robert had on the previous PR, and see if you are okay with the breaking changes that were rejected there?
#1769
#1795
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.
From: #1795 (review)
Uh oh!
There was an error while loading. Please reload this page.
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.
Likewise, anything that breaks what the property currently does--like throwing and expecting people (almost certainly just us) to call Render instead for current cases that work with the old type.
My preference historically would be to take small binary breaks to things like this that are primarily called by the driver, in order to keep internal quality high, and to create a coherent design and experience. But I was under the impression that we didn't want to take binary breaks because of the impact of third-party libraries that have not been rebuilt against latest.
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.
property virtual is a breaking changeI think this is something we can tolerate, I don't see how this can be breaking in practice.
Also my other suggestion was having
Rendermethod inCreateSearchIndexModelBsonDocument Render(RenderArgs<TDocument> renderArgs) => Definition, for consistency.Both suggestions for your consideration.