From e1a8d8b5319ab228fa068e0e6168d74814f18558 Mon Sep 17 00:00:00 2001 From: Kevin Pilch Date: Fri, 24 May 2024 14:34:21 -0700 Subject: [PATCH 1/8] Support records with no key, fix issue with Relevance --- .../AzureCosmosDBNoSQLMemoryStore.cs | 9 ++- .../AzureCosmosDBNoSQLMemoryStoreTests.cs | 64 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs index 70d6210fc355..c7adaffc1a9e 100644 --- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs @@ -164,6 +164,12 @@ public async Task UpsertAsync( MemoryRecord record, CancellationToken cancellationToken = default) { + // In some cases we're expected to generate the key to use. Do so if one isn't provided. + if (string.IsNullOrEmpty(record.Key)) + { + record.Key = Guid.NewGuid().ToString(); + } + var result = await this._cosmosClient .GetDatabase(this._databaseName) .GetContainer(collectionName) @@ -193,6 +199,7 @@ public async IAsyncEnumerable UpsertBatchAsync( bool withEmbedding = false, CancellationToken cancellationToken = default) { + // TODO: Consider using a query when `withEmbedding` is false to avoid passing it over the wire. var result = await this._cosmosClient .GetDatabase(this._databaseName) .GetContainer(collectionName) @@ -332,7 +339,7 @@ ORDER BY VectorDistance(x.embedding, @embedding) { if (memoryRecord.SimilarityScore >= minRelevanceScore) { - yield return (memoryRecord, memoryRecord.SimilarityScore); + yield return (memoryRecord, 1 - memoryRecord.SimilarityScore); } } } diff --git a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs index 0e8aee320856..1e810aa62a92 100644 --- a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs +++ b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs @@ -1,9 +1,14 @@ // Copyright (c) Microsoft. All rights reserved. using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; +using System.Threading; using System.Threading.Tasks; +using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Connectors.AzureCosmosDBNoSQL; +using Microsoft.SemanticKernel.Embeddings; using Microsoft.SemanticKernel.Memory; using MongoDB.Driver; using Xunit; @@ -117,6 +122,52 @@ public async Task ItCanGetNearestMatchesAsync(int limit, bool withEmbeddings) await memoryStore.DeleteCollectionAsync(collectionName); } + [Theory(Skip = SkipReason)] + [InlineData(true)] + [InlineData(false)] + public async Task ItCanSaveReferenceGetTextAndSearchTextAsync(bool withEmbedding) + { + var collectionName = this._fixture.CollectionName; + var memoryStore = this._fixture.MemoryStore; + var textMemory = new SemanticTextMemory(memoryStore, new MockTextEmbeddingGenerationService()); + var textToStore = "SampleText"; + var id = "MyExternalId"; + var source = "MyExternalSource"; + var refId = await textMemory.SaveReferenceAsync(collectionName, textToStore, id, source); + Assert.NotNull(refId); + + var expectedQueryResult = new MemoryQueryResult( + new MemoryRecordMetadata(isReference: true, id, text: "", description: "", source, additionalMetadata: ""), + 1.0, + withEmbedding ? MockTextEmbeddingGenerationService.MatchingEmbedding : null); + + var queryResult = await textMemory.GetAsync(collectionName, refId, withEmbedding); + AssertQueryResultEqual(expectedQueryResult, queryResult, withEmbedding); + + var searchResults = await textMemory.SearchAsync(collectionName, textToStore, withEmbeddings: withEmbedding).ToListAsync(); + Assert.Equal(1, searchResults?.Count); + AssertQueryResultEqual(expectedQueryResult, searchResults?[0], compareEmbeddings: true); + } + + private static void AssertQueryResultEqual(MemoryQueryResult expected, MemoryQueryResult? actual, bool compareEmbeddings) + { + Assert.NotNull(actual); + Assert.Equal(expected.Relevance, actual.Relevance); + Assert.Equal(expected.Metadata.Id, actual.Metadata.Id); + Assert.Equal(expected.Metadata.Text, actual.Metadata.Text); + Assert.Equal(expected.Metadata.Description, actual.Metadata.Description); + Assert.Equal(expected.Metadata.ExternalSourceName, actual.Metadata.ExternalSourceName); + Assert.Equal(expected.Metadata.AdditionalMetadata, actual.Metadata.AdditionalMetadata); + Assert.Equal(expected.Metadata.IsReference, actual.Metadata.IsReference); + + if (compareEmbeddings) + { + Assert.NotNull(expected.Embedding); + Assert.NotNull(actual.Embedding); + Assert.Equal(expected.Embedding.Value.Span, actual.Embedding.Value.Span); + } + } + private static void AssertMemoryRecordEqual( MemoryRecord expectedRecord, MemoryRecord actualRecord, @@ -147,4 +198,17 @@ private static void AssertMemoryRecordEqual( Assert.True(actualRecord.Embedding.Span.IsEmpty); } } + + private class MockTextEmbeddingGenerationService : ITextEmbeddingGenerationService + { + public static ReadOnlyMemory MatchingEmbedding = new[] { 0.0f, 0.0f, 0.0f }; + + public IReadOnlyDictionary Attributes { get; } = ReadOnlyDictionary.Empty; + + public Task>> GenerateEmbeddingsAsync(IList data, Kernel? kernel = null, CancellationToken cancellationToken = default) + { + IList> result = new List> { MatchingEmbedding }; + return Task.FromResult(result); + } + } } From 98926e20735bb37fae00a962e7deb2edac2e91bd Mon Sep 17 00:00:00 2001 From: Kevin Pilch Date: Fri, 24 May 2024 15:30:06 -0700 Subject: [PATCH 2/8] --ammend --- .../AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs index 1e810aa62a92..715a91039f8c 100644 --- a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs +++ b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs @@ -199,7 +199,7 @@ private static void AssertMemoryRecordEqual( } } - private class MockTextEmbeddingGenerationService : ITextEmbeddingGenerationService + private sealed class MockTextEmbeddingGenerationService : ITextEmbeddingGenerationService { public static ReadOnlyMemory MatchingEmbedding = new[] { 0.0f, 0.0f, 0.0f }; From af4248e6c680525f959bba2f40bc05fdfdb7f3b3 Mon Sep 17 00:00:00 2001 From: Kevin Pilch Date: Mon, 10 Jun 2024 15:15:10 -0700 Subject: [PATCH 3/8] Restrict to Cosine distance function --- .../AzureCosmosDBNoSQLMemoryStore.cs | 15 ++++++++++++--- .../AzureCosmosDBNoSQLMemoryStoreTests.cs | 8 ++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs index c7adaffc1a9e..c96f9784619a 100644 --- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs @@ -71,7 +71,8 @@ public AzureCosmosDBNoSQLMemoryStore( VectorEmbeddingPolicy vectorEmbeddingPolicy, IndexingPolicy indexingPolicy) { - if (!vectorEmbeddingPolicy.Embeddings.Any(e => e.Path == "/embedding")) + var embedding = vectorEmbeddingPolicy.Embeddings.FirstOrDefault(e => e.Path == "/embedding"); + if (embedding is null) { throw new InvalidOperationException($""" In order for {nameof(GetNearestMatchAsync)} to function, {nameof(vectorEmbeddingPolicy)} should @@ -79,6 +80,13 @@ contain an embedding path at /embedding. It's also recommended to include a that {nameof(indexingPolicy)} to improve performance and reduce cost for searches. """); } + else if (embedding.DistanceFunction != DistanceFunction.Cosine) + { + throw new InvalidOperationException($""" + In order for {nameof(GetNearestMatchAsync)} to reliably return relevance information, the {nameof(DistanceFunction)} should + be specified as {nameof(DistanceFunction)}.{nameof(DistanceFunction.Cosine)}. + """); + } this._cosmosClient = cosmosClient; this._databaseName = databaseName; this._vectorEmbeddingPolicy = vectorEmbeddingPolicy; @@ -337,9 +345,10 @@ ORDER BY VectorDistance(x.embedding, @embedding) { foreach (var memoryRecord in await feedIterator.ReadNextAsync(cancellationToken).ConfigureAwait(false)) { - if (memoryRecord.SimilarityScore >= minRelevanceScore) + var relevanceScore = (memoryRecord.SimilarityScore + 1) / 2; + if (relevanceScore >= minRelevanceScore) { - yield return (memoryRecord, 1 - memoryRecord.SimilarityScore); + yield return (memoryRecord, relevanceScore); } } } diff --git a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs index 715a91039f8c..e75116e34893 100644 --- a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs +++ b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTests.cs @@ -139,7 +139,7 @@ public async Task ItCanSaveReferenceGetTextAndSearchTextAsync(bool withEmbedding var expectedQueryResult = new MemoryQueryResult( new MemoryRecordMetadata(isReference: true, id, text: "", description: "", source, additionalMetadata: ""), 1.0, - withEmbedding ? MockTextEmbeddingGenerationService.MatchingEmbedding : null); + withEmbedding ? DataHelper.VectorSearchTestEmbedding : null); var queryResult = await textMemory.GetAsync(collectionName, refId, withEmbedding); AssertQueryResultEqual(expectedQueryResult, queryResult, withEmbedding); @@ -147,6 +147,8 @@ public async Task ItCanSaveReferenceGetTextAndSearchTextAsync(bool withEmbedding var searchResults = await textMemory.SearchAsync(collectionName, textToStore, withEmbeddings: withEmbedding).ToListAsync(); Assert.Equal(1, searchResults?.Count); AssertQueryResultEqual(expectedQueryResult, searchResults?[0], compareEmbeddings: true); + + await textMemory.RemoveAsync(collectionName, refId); } private static void AssertQueryResultEqual(MemoryQueryResult expected, MemoryQueryResult? actual, bool compareEmbeddings) @@ -201,13 +203,11 @@ private static void AssertMemoryRecordEqual( private sealed class MockTextEmbeddingGenerationService : ITextEmbeddingGenerationService { - public static ReadOnlyMemory MatchingEmbedding = new[] { 0.0f, 0.0f, 0.0f }; - public IReadOnlyDictionary Attributes { get; } = ReadOnlyDictionary.Empty; public Task>> GenerateEmbeddingsAsync(IList data, Kernel? kernel = null, CancellationToken cancellationToken = default) { - IList> result = new List> { MatchingEmbedding }; + IList> result = new List> { DataHelper.VectorSearchTestEmbedding }; return Task.FromResult(result); } } From 588315ba2c935ca8a80f8c1ac37cabd34a694f5e Mon Sep 17 00:00:00 2001 From: Kevin Pilch Date: Mon, 10 Jun 2024 16:46:18 -0700 Subject: [PATCH 4/8] Update dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com> --- .../AzureCosmosDBNoSQLMemoryStore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs index c96f9784619a..f69f7933d71d 100644 --- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs @@ -76,7 +76,7 @@ public AzureCosmosDBNoSQLMemoryStore( { throw new InvalidOperationException($""" In order for {nameof(GetNearestMatchAsync)} to function, {nameof(vectorEmbeddingPolicy)} should - contain an embedding path at /embedding. It's also recommended to include a that path in the + contain an embedding path at /embedding. It's also recommended to include that path in the {nameof(indexingPolicy)} to improve performance and reduce cost for searches. """); } From a52ac4226b8ed541e4a3272bc7bb05f2069cd9a7 Mon Sep 17 00:00:00 2001 From: Kevin Pilch Date: Tue, 11 Jun 2024 09:47:11 -0700 Subject: [PATCH 5/8] Add simpler constructor with just dimensions and datatype --- .../AzureCosmosDBNoSQLMemoryStore.cs | 54 ++++++++++++++++++- ...ureCosmosDBNoSQLMemoryStoreTestsFixture.cs | 25 +-------- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs index f69f7933d71d..5bf4311971c6 100644 --- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; @@ -22,11 +23,60 @@ namespace Microsoft.SemanticKernel.Connectors.AzureCosmosDBNoSQL; /// public class AzureCosmosDBNoSQLMemoryStore : IMemoryStore, IDisposable { + private const string EMBEDDING_PATH = "/embedding"; + private readonly CosmosClient _cosmosClient; private readonly VectorEmbeddingPolicy _vectorEmbeddingPolicy; private readonly IndexingPolicy _indexingPolicy; private readonly string _databaseName; + /// + /// Initiates a AzureCosmosDBNoSQLMemoryStore instance using a Azure Cosmos DB connection string + /// and other properties required for vector search. + /// + /// Connection string required to connect to Azure Cosmos DB. + /// The database name to connect to. + /// The number of dimensions the embedding vectors to be stored. + /// The data type of the embedding vectors to be stored. + /// The application name to use in requests. + public AzureCosmosDBNoSQLMemoryStore( + string connectionString, + string databaseName, + ulong dimensions, + VectorDataType vectorDataType, + string? applicationName = null) + : this( + new CosmosClient( + connectionString, + new CosmosClientOptions + { + ApplicationName = applicationName ?? HttpHeaderConstant.Values.UserAgent, + Serializer = new CosmosSystemTextJsonSerializer(JsonSerializerOptions.Default), + }), + databaseName, + new VectorEmbeddingPolicy( + [ + new Embedding + { + DataType = vectorDataType, + Dimensions = dimensions, + DistanceFunction = DistanceFunction.Cosine, + Path = EMBEDDING_PATH, + } + ]), + new IndexingPolicy + { + VectorIndexes = new Collection { + new() + { + Path = EMBEDDING_PATH, + Type = VectorIndexType.Flat, + }, + }, + }) + { + } + /// /// Initiates a AzureCosmosDBNoSQLMemoryStore instance using a Azure Cosmos DB connection string /// and other properties required for vector search. @@ -71,12 +121,12 @@ public AzureCosmosDBNoSQLMemoryStore( VectorEmbeddingPolicy vectorEmbeddingPolicy, IndexingPolicy indexingPolicy) { - var embedding = vectorEmbeddingPolicy.Embeddings.FirstOrDefault(e => e.Path == "/embedding"); + var embedding = vectorEmbeddingPolicy.Embeddings.FirstOrDefault(e => e.Path == EMBEDDING_PATH); if (embedding is null) { throw new InvalidOperationException($""" In order for {nameof(GetNearestMatchAsync)} to function, {nameof(vectorEmbeddingPolicy)} should - contain an embedding path at /embedding. It's also recommended to include that path in the + contain an embedding path at {EMBEDDING_PATH}. It's also recommended to include that path in the {nameof(indexingPolicy)} to improve performance and reduce cost for searches. """); } diff --git a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs index 93cbea170f40..7e54ce4cea34 100644 --- a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs +++ b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. using System; -using System.Collections.ObjectModel; using System.Threading.Tasks; using Microsoft.Azure.Cosmos; using Microsoft.Extensions.Configuration; @@ -35,28 +34,8 @@ public AzureCosmosDBNoSQLMemoryStoreTestsFixture() this.MemoryStore = new AzureCosmosDBNoSQLMemoryStore( connectionString, this.DatabaseName, - new VectorEmbeddingPolicy( - new Collection - { - new() - { - DataType = VectorDataType.Float32, - Dimensions = 3, - DistanceFunction = DistanceFunction.Cosine, - Path = "/embedding" - } - }), - new() - { - VectorIndexes = new Collection { - new() - { - Path = "/embedding", - Type = VectorIndexType.Flat, - }, - }, - } - ); + dimensions: 3, + vectorDataType: VectorDataType.Float32); } public Task InitializeAsync() From ff1a94fb2964cfb4dd8597b09c5192577bf1e0ec Mon Sep 17 00:00:00 2001 From: Kevin Pilch Date: Tue, 11 Jun 2024 09:56:43 -0700 Subject: [PATCH 6/8] Parameterize index type as well --- .../AzureCosmosDBNoSQLMemoryStore.cs | 4 +++- .../AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs index 5bf4311971c6..c173274a7292 100644 --- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs @@ -38,12 +38,14 @@ public class AzureCosmosDBNoSQLMemoryStore : IMemoryStore, IDisposable /// The database name to connect to. /// The number of dimensions the embedding vectors to be stored. /// The data type of the embedding vectors to be stored. + /// The type of index to use for the embedding vectors to be stored. /// The application name to use in requests. public AzureCosmosDBNoSQLMemoryStore( string connectionString, string databaseName, ulong dimensions, VectorDataType vectorDataType, + VectorIndexType vectorIndexType, string? applicationName = null) : this( new CosmosClient( @@ -70,7 +72,7 @@ public AzureCosmosDBNoSQLMemoryStore( new() { Path = EMBEDDING_PATH, - Type = VectorIndexType.Flat, + Type = vectorIndexType, }, }, }) diff --git a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs index 7e54ce4cea34..1df46166e63f 100644 --- a/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs +++ b/dotnet/src/IntegrationTests/Connectors/Memory/AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStoreTestsFixture.cs @@ -35,7 +35,8 @@ public AzureCosmosDBNoSQLMemoryStoreTestsFixture() connectionString, this.DatabaseName, dimensions: 3, - vectorDataType: VectorDataType.Float32); + VectorDataType.Float32, + VectorIndexType.Flat); } public Task InitializeAsync() From ca026ebfd4345571e3232c4e2247a4539439386c Mon Sep 17 00:00:00 2001 From: Kevin Pilch Date: Wed, 12 Jun 2024 09:24:24 -0700 Subject: [PATCH 7/8] Only allow float16 and float32 embedding types --- .../AzureCosmosDBNoSQLMemoryStore.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs index c173274a7292..b6d7ede23d9d 100644 --- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs @@ -139,6 +139,13 @@ contain an embedding path at {EMBEDDING_PATH}. It's also recommended to include be specified as {nameof(DistanceFunction)}.{nameof(DistanceFunction.Cosine)}. """); } + else if (embedding.DataType != VectorDataType.Float16 && embedding.DataType != VectorDataType.Float32) + { + throw new NotSupportedException($""" + Only {nameof(VectorDataType)}.{nameof(VectorDataType.Float16)} and {nameof(VectorDataType)}.{nameof(VectorDataType.Float32)} + are supported. + """); + } this._cosmosClient = cosmosClient; this._databaseName = databaseName; this._vectorEmbeddingPolicy = vectorEmbeddingPolicy; From bf4611010fc4accea71ca485f99fb85ddb6c1773 Mon Sep 17 00:00:00 2001 From: Kevin Pilch Date: Wed, 12 Jun 2024 13:06:54 -0700 Subject: [PATCH 8/8] Rename constant to PascalCase --- .../AzureCosmosDBNoSQLMemoryStore.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs index b6d7ede23d9d..d9d5b67ee4af 100644 --- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLMemoryStore.cs @@ -23,7 +23,7 @@ namespace Microsoft.SemanticKernel.Connectors.AzureCosmosDBNoSQL; /// public class AzureCosmosDBNoSQLMemoryStore : IMemoryStore, IDisposable { - private const string EMBEDDING_PATH = "/embedding"; + private const string EmbeddingPath = "/embedding"; private readonly CosmosClient _cosmosClient; private readonly VectorEmbeddingPolicy _vectorEmbeddingPolicy; @@ -63,7 +63,7 @@ public AzureCosmosDBNoSQLMemoryStore( DataType = vectorDataType, Dimensions = dimensions, DistanceFunction = DistanceFunction.Cosine, - Path = EMBEDDING_PATH, + Path = EmbeddingPath, } ]), new IndexingPolicy @@ -71,7 +71,7 @@ public AzureCosmosDBNoSQLMemoryStore( VectorIndexes = new Collection { new() { - Path = EMBEDDING_PATH, + Path = EmbeddingPath, Type = vectorIndexType, }, }, @@ -123,12 +123,12 @@ public AzureCosmosDBNoSQLMemoryStore( VectorEmbeddingPolicy vectorEmbeddingPolicy, IndexingPolicy indexingPolicy) { - var embedding = vectorEmbeddingPolicy.Embeddings.FirstOrDefault(e => e.Path == EMBEDDING_PATH); + var embedding = vectorEmbeddingPolicy.Embeddings.FirstOrDefault(e => e.Path == EmbeddingPath); if (embedding is null) { throw new InvalidOperationException($""" In order for {nameof(GetNearestMatchAsync)} to function, {nameof(vectorEmbeddingPolicy)} should - contain an embedding path at {EMBEDDING_PATH}. It's also recommended to include that path in the + contain an embedding path at {EmbeddingPath}. It's also recommended to include that path in the {nameof(indexingPolicy)} to improve performance and reduce cost for searches. """); }