Skip to content

Commit 04413d1

Browse files
dmytrostrukwestey-mmarkwallace-microsoft
authored
.Net: Added implementation of Azure CosmosDB for NoSQL connector for new memory design (#8065)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Related: #6522 In this PR: - Implemented `IVectorStore` - Implemented `IVectorStoreRecordCollection<TKey, TRecord>` - Azure CosmosDB for NoSQL default record mapper - `Options` classes - Extension methods for DI - Integration tests   - Unit tests ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: westey <[email protected]> Co-authored-by: Mark Wallace <[email protected]>
1 parent a82712c commit 04413d1

File tree

36 files changed

+2728
-15
lines changed

36 files changed

+2728
-15
lines changed

dotnet/SK-dotnet.sln

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Qdrant.UnitTests
320320
EndProject
321321
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StepwisePlannerMigration", "samples\Demos\StepwisePlannerMigration\StepwisePlannerMigration.csproj", "{38374C62-0263-4FE8-A18C-70FC8132912B}"
322322
EndProject
323-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AIModelRouter", "samples\Demos\AIModelRouter\AIModelRouter.csproj", "{E06818E3-00A5-41AC-97ED-9491070CDEA1}"
323+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AIModelRouter", "samples\Demos\AIModelRouter\AIModelRouter.csproj", "{E06818E3-00A5-41AC-97ED-9491070CDEA1}"
324+
EndProject
325+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Connectors.AzureCosmosDBNoSQL.UnitTests", "src\Connectors\Connectors.AzureCosmosDBNoSQL.UnitTests\Connectors.AzureCosmosDBNoSQL.UnitTests.csproj", "{385A8FE5-87E2-4458-AE09-35E10BD2E67F}"
324326
EndProject
325327
Global
326328
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -803,6 +805,12 @@ Global
803805
{E06818E3-00A5-41AC-97ED-9491070CDEA1}.Publish|Any CPU.Build.0 = Debug|Any CPU
804806
{E06818E3-00A5-41AC-97ED-9491070CDEA1}.Release|Any CPU.ActiveCfg = Release|Any CPU
805807
{E06818E3-00A5-41AC-97ED-9491070CDEA1}.Release|Any CPU.Build.0 = Release|Any CPU
808+
{385A8FE5-87E2-4458-AE09-35E10BD2E67F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
809+
{385A8FE5-87E2-4458-AE09-35E10BD2E67F}.Debug|Any CPU.Build.0 = Debug|Any CPU
810+
{385A8FE5-87E2-4458-AE09-35E10BD2E67F}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
811+
{385A8FE5-87E2-4458-AE09-35E10BD2E67F}.Publish|Any CPU.Build.0 = Debug|Any CPU
812+
{385A8FE5-87E2-4458-AE09-35E10BD2E67F}.Release|Any CPU.ActiveCfg = Release|Any CPU
813+
{385A8FE5-87E2-4458-AE09-35E10BD2E67F}.Release|Any CPU.Build.0 = Release|Any CPU
806814
EndGlobalSection
807815
GlobalSection(SolutionProperties) = preSolution
808816
HideSolutionNode = FALSE
@@ -913,6 +921,7 @@ Global
913921
{E92AE954-8F3A-4A6F-A4F9-DC12017E5AAF} = {0247C2C9-86C3-45BA-8873-28B0948EDC0C}
914922
{38374C62-0263-4FE8-A18C-70FC8132912B} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
915923
{E06818E3-00A5-41AC-97ED-9491070CDEA1} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
924+
{385A8FE5-87E2-4458-AE09-35E10BD2E67F} = {0247C2C9-86C3-45BA-8873-28B0948EDC0C}
916925
EndGlobalSection
917926
GlobalSection(ExtensibilityGlobals) = postSolution
918927
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Suppressing errors for Test projects under dotnet folder
2+
[*.cs]
3+
dotnet_diagnostic.CA2007.severity = none # Do not directly await a Task
4+
dotnet_diagnostic.VSTHRD111.severity = none # Use .ConfigureAwait(bool) is hidden by default, set to none to prevent IDE from changing on autosave
5+
dotnet_diagnostic.CS1591.severity = none # Missing XML comment for publicly visible type or member
6+
dotnet_diagnostic.IDE1006.severity = warning # Naming rule violations
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text.Json.Serialization;
6+
using Microsoft.SemanticKernel.Data;
7+
8+
namespace SemanticKernel.Connectors.AzureCosmosDBNoSQL.UnitTests;
9+
10+
public class AzureCosmosDBNoSQLHotel(string hotelId)
11+
{
12+
/// <summary>The key of the record.</summary>
13+
[VectorStoreRecordKey]
14+
public string HotelId { get; init; } = hotelId;
15+
16+
/// <summary>A string metadata field.</summary>
17+
[VectorStoreRecordData]
18+
public string? HotelName { get; set; }
19+
20+
/// <summary>An int metadata field.</summary>
21+
[VectorStoreRecordData]
22+
public int HotelCode { get; set; }
23+
24+
/// <summary>A float metadata field.</summary>
25+
[VectorStoreRecordData]
26+
public float? HotelRating { get; set; }
27+
28+
/// <summary>A bool metadata field.</summary>
29+
[VectorStoreRecordData(StoragePropertyName = "parking_is_included")]
30+
public bool ParkingIncluded { get; set; }
31+
32+
/// <summary>An array metadata field.</summary>
33+
[VectorStoreRecordData]
34+
public List<string> Tags { get; set; } = [];
35+
36+
/// <summary>A data field.</summary>
37+
[VectorStoreRecordData]
38+
public string? Description { get; set; }
39+
40+
/// <summary>A vector field.</summary>
41+
[JsonPropertyName("description_embedding")]
42+
[VectorStoreRecordVector(Dimensions: 4, IndexKind: IndexKind.Flat, DistanceFunction: DistanceFunction.CosineSimilarity)]
43+
public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.Azure.Cosmos;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.SemanticKernel;
6+
using Microsoft.SemanticKernel.Connectors.AzureCosmosDBNoSQL;
7+
using Microsoft.SemanticKernel.Data;
8+
using Moq;
9+
using Xunit;
10+
11+
namespace SemanticKernel.Connectors.AzureCosmosDBNoSQL.UnitTests;
12+
13+
/// <summary>
14+
/// Unit tests for <see cref="AzureCosmosDBNoSQLKernelBuilderExtensions"/> class.
15+
/// </summary>
16+
public sealed class AzureCosmosDBNoSQLKernelBuilderExtensionsTests
17+
{
18+
private readonly IKernelBuilder _kernelBuilder = Kernel.CreateBuilder();
19+
20+
[Fact]
21+
public void AddVectorStoreRegistersClass()
22+
{
23+
// Arrange
24+
this._kernelBuilder.Services.AddSingleton<Database>(Mock.Of<Database>());
25+
26+
// Act
27+
this._kernelBuilder.AddAzureCosmosDBNoSQLVectorStore();
28+
29+
var kernel = this._kernelBuilder.Build();
30+
var vectorStore = kernel.Services.GetRequiredService<IVectorStore>();
31+
32+
// Assert
33+
Assert.NotNull(vectorStore);
34+
Assert.IsType<AzureCosmosDBNoSQLVectorStore>(vectorStore);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.Azure.Cosmos;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.SemanticKernel;
6+
using Microsoft.SemanticKernel.Connectors.AzureCosmosDBNoSQL;
7+
using Microsoft.SemanticKernel.Data;
8+
using Moq;
9+
using Xunit;
10+
11+
namespace SemanticKernel.Connectors.AzureCosmosDBNoSQL.UnitTests;
12+
13+
/// <summary>
14+
/// Unit tests for <see cref="AzureCosmosDBNoSQLServiceCollectionExtensions"/> class.
15+
/// </summary>
16+
public sealed class AzureCosmosDBNoSQLServiceCollectionExtensionsTests
17+
{
18+
private readonly IServiceCollection _serviceCollection = new ServiceCollection();
19+
20+
[Fact]
21+
public void AddVectorStoreRegistersClass()
22+
{
23+
// Arrange
24+
this._serviceCollection.AddSingleton<Database>(Mock.Of<Database>());
25+
26+
// Act
27+
this._serviceCollection.AddAzureCosmosDBNoSQLVectorStore();
28+
29+
var serviceProvider = this._serviceCollection.BuildServiceProvider();
30+
var vectorStore = serviceProvider.GetRequiredService<IVectorStore>();
31+
32+
// Assert
33+
Assert.NotNull(vectorStore);
34+
Assert.IsType<AzureCosmosDBNoSQLVectorStore>(vectorStore);
35+
}
36+
}

0 commit comments

Comments
 (0)