-
Notifications
You must be signed in to change notification settings - Fork 524
[Internal] ContainerProperties: Adds Vector Embedding and Indexing Policy #4379
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
ealsur
merged 20 commits into
master
from
users/kundadebdatta/4364_vector_index_add_collection_properties
Apr 5, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8fb29cc
Initial code changes to add vector embedding policy and index type in…
kundadebdatta 5e8011a
Merge branch 'master' into users/kundadebdatta/4364_vector_index_add_…
kundadebdatta 8e3a5c8
Code changes to add container builder for vector index type.
kundadebdatta 287b2cd
Merge branch 'master' into users/kundadebdatta/4364_vector_index_add_…
kundadebdatta 2a1556a
Code changes to add and fix tests.
kundadebdatta b63fdcd
Revert back client create and initialize test.
kundadebdatta 1c46250
Skipping some of the V2 tests for vector indexes.
kundadebdatta 4c69d61
Merge branch 'master' into users/kundadebdatta/4364_vector_index_add_…
kundadebdatta de2278c
Code changes to fix GA and preview contracts.
kundadebdatta 5a80ec0
Code changes to address review comments part 1.
kundadebdatta c9e72b4
Removed unnecessary JSON argument for vector index.
kundadebdatta 0643222
Code changes to update contract changes.
kundadebdatta 7fc1dde
Code changes to update preview contract changes in tests.
kundadebdatta ad6cec8
Code changes to address few review comments.
kundadebdatta 02e01bb
Code changes to add few more tests to validate serialization and dese…
kundadebdatta add9e94
Merge branch 'master' into users/kundadebdatta/4364_vector_index_add_…
kundadebdatta 66f3a87
Merge branch 'master' into users/kundadebdatta/4364_vector_index_add_…
kundadebdatta 55e3735
Code changes to address some review comments for best practices.
kundadebdatta 99ea8c0
Code changes to fix test failures.
kundadebdatta 6e225bf
Merge branch 'master' into users/kundadebdatta/4364_vector_index_add_…
kundadebdatta 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
42 changes: 42 additions & 0 deletions
42
Microsoft.Azure.Cosmos/src/Fluent/Settings/VectorEmbeddingPolicyDefinition.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,42 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos.Fluent | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| /// <summary> | ||
| /// <see cref="VectorEmbeddingPolicy"/> fluent definition. | ||
| /// </summary> | ||
| internal class VectorEmbeddingPolicyDefinition | ||
| { | ||
| private readonly ContainerBuilder parent; | ||
| private readonly Action<VectorEmbeddingPolicy> attachCallback; | ||
| private readonly Collection<Embedding> vectorEmbeddings; | ||
|
|
||
| internal VectorEmbeddingPolicyDefinition( | ||
| ContainerBuilder parent, | ||
| Collection<Embedding> embeddings, | ||
| Action<VectorEmbeddingPolicy> attachCallback) | ||
| { | ||
| this.parent = parent ?? throw new ArgumentNullException(nameof(parent)); | ||
| this.attachCallback = attachCallback ?? throw new ArgumentNullException(nameof(attachCallback)); | ||
| this.vectorEmbeddings = embeddings; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies the current definition to the parent. | ||
| /// </summary> | ||
| /// <returns>An instance of the parent.</returns> | ||
| public ContainerBuilder Attach() | ||
| { | ||
| VectorEmbeddingPolicy embeddingPolicy = new (this.vectorEmbeddings); | ||
|
|
||
| this.attachCallback(embeddingPolicy); | ||
| return this.parent; | ||
| } | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
Microsoft.Azure.Cosmos/src/Fluent/Settings/VectorIndexDefinition.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,58 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos.Fluent | ||
| { | ||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Vector index fluent definition. | ||
| /// </summary> | ||
| /// <seealso cref="VectorIndexPath"/> | ||
| internal class VectorIndexDefinition<T> | ||
| { | ||
| private readonly VectorIndexPath vectorIndexPath = new VectorIndexPath(); | ||
| private readonly T parent; | ||
| private readonly Action<VectorIndexPath> attachCallback; | ||
|
|
||
| internal VectorIndexDefinition( | ||
| T parent, | ||
| Action<VectorIndexPath> attachCallback) | ||
| { | ||
| this.parent = parent; | ||
| this.attachCallback = attachCallback; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Add a path to the current <see cref="VectorIndexPath"/> definition with a particular set of <see cref="VectorIndexType"/>s. | ||
| /// </summary> | ||
| /// <param name="path">Property path for the current definition. Example: /property</param> | ||
| /// <param name="indexType">Set of <see cref="VectorIndexType"/> to apply to the path.</param> | ||
| /// <returns>An instance of the current <see cref="VectorIndexDefinition{T}"/>.</returns> | ||
| public VectorIndexDefinition<T> Path( | ||
| string path, | ||
| VectorIndexType indexType) | ||
| { | ||
| if (string.IsNullOrEmpty(path)) | ||
| { | ||
| throw new ArgumentNullException(nameof(path)); | ||
| } | ||
|
|
||
| this.vectorIndexPath.Path = path; | ||
| this.vectorIndexPath.Type = indexType; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies the current definition to the parent. | ||
| /// </summary> | ||
| /// <returns>An instance of the parent.</returns> | ||
| public T Attach() | ||
| { | ||
| this.attachCallback(this.vectorIndexPath); | ||
| return this.parent; | ||
| } | ||
| } | ||
| } |
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
32 changes: 32 additions & 0 deletions
32
Microsoft.Azure.Cosmos/src/Resource/Settings/DistanceFunction.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,32 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
| namespace Microsoft.Azure.Cosmos | ||
| { | ||
| using System.Runtime.Serialization; | ||
|
|
||
| /// <summary> | ||
| /// Defines the distance function for a vector index specification in the Azure Cosmos DB service. | ||
kirankumarkolli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// <seealso cref="Embedding"/> for usage. | ||
| internal enum DistanceFunction | ||
| { | ||
| /// <summary> | ||
| /// Represents the euclidean distance function. | ||
| /// </summary> | ||
| [EnumMember(Value = "euclidean")] | ||
| Euclidean, | ||
|
|
||
| /// <summary> | ||
| /// Represents the cosine distance function. | ||
| /// </summary> | ||
| [EnumMember(Value = "cosine")] | ||
| Cosine, | ||
|
|
||
| /// <summary> | ||
| /// Represents the dot product distance function. | ||
| /// </summary> | ||
| [EnumMember(Value = "dotproduct")] | ||
| DotProduct | ||
| } | ||
| } | ||
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,77 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.Azure.Documents; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Converters; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| /// <summary> | ||
| /// Represents the embedding settings for the vector index. | ||
| /// </summary> | ||
| internal class Embedding : IEquatable<Embedding> | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets a string containing the path of the vector index. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = Constants.Properties.Path)] | ||
| public string Path { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the <see cref="Cosmos.VectorDataType"/> representing the corresponding vector data type. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "dataType")] | ||
| [JsonConverter(typeof(StringEnumConverter))] | ||
| public VectorDataType DataType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a long integer representing the dimensions of a vector. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "dimensions")] | ||
| public ulong Dimensions { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the <see cref="Cosmos.DistanceFunction"/> which is used to calculate the respective distance between the vectors. | ||
| /// </summary> | ||
| [JsonProperty(PropertyName = "distanceFunction")] | ||
| [JsonConverter(typeof(StringEnumConverter))] | ||
| public DistanceFunction DistanceFunction { get; set; } | ||
kirankumarkolli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// This contains additional values for scenarios where the SDK is not aware of new fields. | ||
| /// This ensures that if resource is read and updated none of the fields will be lost in the process. | ||
| /// </summary> | ||
| [JsonExtensionData] | ||
| internal IDictionary<string, JToken> AdditionalProperties { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Ensures that the paths specified in the vector embedding policy are valid. | ||
| /// </summary> | ||
| public void ValidateEmbeddingPath() | ||
| { | ||
| if (string.IsNullOrEmpty(this.Path)) | ||
| { | ||
| throw new ArgumentException("Argument {0} can't be null or empty.", nameof(this.Path)); | ||
| } | ||
|
|
||
| if (this.Path[0] != '/') | ||
| { | ||
| throw new ArgumentException("The argument {0} is not a valid path.", this.Path); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Equals(Embedding that) | ||
| { | ||
| return this.Path.Equals(that.Path) | ||
| && this.DataType.Equals(that.DataType) | ||
| && this.Dimensions == that.Dimensions | ||
| && this.Dimensions.Equals(that.Dimensions); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.