-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Internal] ContainerProperties: Adds Vector Embedding and Indexing Po…
…licy (#4379) * Initial code changes to add vector embedding policy and index type in container. * Code changes to add container builder for vector index type. * Code changes to add and fix tests. * Revert back client create and initialize test. * Skipping some of the V2 tests for vector indexes. * Code changes to fix GA and preview contracts. * Code changes to address review comments part 1. * Removed unnecessary JSON argument for vector index. * Code changes to update contract changes. * Code changes to update preview contract changes in tests. * Code changes to address few review comments. * Code changes to add few more tests to validate serialization and deserialization. * Code changes to address some review comments for best practices. * Code changes to fix test failures.
- Loading branch information
1 parent
b882e26
commit d1ff001
Showing
15 changed files
with
719 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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. | ||
/// </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 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; } | ||
|
||
/// <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 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.