From dcc5961a930063f9cd02e642103c246b62f28dd3 Mon Sep 17 00:00:00 2001 From: Krzysztof Kasprowicz Date: Mon, 11 Mar 2024 13:53:20 +0100 Subject: [PATCH 1/4] Add GoogleAI and VertexAI connector extensions Added two extension methods to the MemoryBuilder class for configuring connectors to the GoogleAI and VertexAI services. These extensions allow for the incorporation of text embedding generation services from GoogleAI and VertexAI, providing users with additional options for text generation services. --- .../GoogleAIMemoryBuilderExtensions.cs | 40 ++++++++++++++++ .../VertexAIMemoryBuilderExtensions.cs | 48 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/GoogleAIMemoryBuilderExtensions.cs create mode 100644 dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/VertexAIMemoryBuilderExtensions.cs diff --git a/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/GoogleAIMemoryBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/GoogleAIMemoryBuilderExtensions.cs new file mode 100644 index 000000000000..fcd012421c08 --- /dev/null +++ b/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/GoogleAIMemoryBuilderExtensions.cs @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Net.Http; +using Microsoft.SemanticKernel.Connectors.GoogleVertexAI; +using Microsoft.SemanticKernel.Http; +using Microsoft.SemanticKernel.Memory; + +namespace Microsoft.SemanticKernel; + +/// +/// Provides extension methods for the class to configure GoogleAI connector. +/// +public static class GoogleAIMemoryBuilderExtensions +{ + /// + /// Add GoogleAI embeddings generation service to the memory builder. + /// + /// The instance + /// The model for text generation. + /// The API key for authentication Gemini API. + /// The optional custom HttpClient. + /// The updated memory builder. + public static MemoryBuilder WithGoogleAIEmbeddingGeneration( + this MemoryBuilder builder, + string modelId, + string apiKey, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(modelId); + Verify.NotNull(apiKey); + + return builder.WithTextEmbeddingGeneration((loggerFactory, builderHttpClient) => + new GoogleAITextEmbeddingGenerationService( + model: modelId, + apiKey: apiKey, + httpClient: HttpClientProvider.GetHttpClient(httpClient ?? builderHttpClient), + loggerFactory: loggerFactory)); + } +} diff --git a/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/VertexAIMemoryBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/VertexAIMemoryBuilderExtensions.cs new file mode 100644 index 000000000000..d76fb1597f43 --- /dev/null +++ b/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/VertexAIMemoryBuilderExtensions.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Net.Http; +using Microsoft.SemanticKernel.Connectors.GoogleVertexAI; +using Microsoft.SemanticKernel.Http; +using Microsoft.SemanticKernel.Memory; + +namespace Microsoft.SemanticKernel; + +/// +/// Provides extension methods for the class to configure VertexAI connector. +/// +public static class VertexAIMemoryBuilderExtensions +{ + /// + /// Add VertexAI embeddings generation service to the memory builder. + /// + /// The instance + /// The model for text generation. + /// The Bearer Key for authentication. + /// The location to process the request + /// Your project ID + /// The optional custom HttpClient. + /// The updated memory builder. + public static MemoryBuilder WithVertexAIEmbeddingGeneration( + this MemoryBuilder builder, + string modelId, + string bearerKey, + string location, + string projectId, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(modelId); + Verify.NotNull(bearerKey); + Verify.NotNull(location); + Verify.NotNull(projectId); + + return builder.WithTextEmbeddingGeneration((loggerFactory, builderHttpClient) => + new VertexAITextEmbeddingGenerationService( + model: modelId, + bearerKey: bearerKey, + location: location, + projectId: projectId, + httpClient: HttpClientProvider.GetHttpClient(httpClient ?? builderHttpClient), + loggerFactory: loggerFactory)); + } +} From 264f74d65d82ae91c2e0f77907409b382ba940a7 Mon Sep 17 00:00:00 2001 From: Krzysztof Kasprowicz Date: Mon, 11 Mar 2024 13:59:39 +0100 Subject: [PATCH 2/4] Add GoogleAI embeddings option in SemanticMemory Example. --- .../KernelSyntaxExamples/Example14_SemanticMemory.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs b/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs index dc5b52d1eee7..1008493c7e59 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs @@ -61,6 +61,12 @@ public async Task RunAsync() .WithMemoryStore(new VolatileMemoryStore()) .Build(); + // Uncomment the following line to use GoogleAI embeddings + // var memoryWithCustomDb = new MemoryBuilder() + // .WithGoogleAIEmbeddingGeneration(TestConfiguration.GoogleAI.EmbeddingModelId, TestConfiguration.GoogleAI.ApiKey) + // .WithMemoryStore(new VolatileMemoryStore()) + // .Build(); + await RunExampleAsync(memoryWithCustomDb); } @@ -167,7 +173,5 @@ private static Dictionary SampleData() }; } - public Example14_SemanticMemory(ITestOutputHelper output) : base(output) - { - } + public Example14_SemanticMemory(ITestOutputHelper output) : base(output) { } } From 2b3d7d129414b5fe3583fcc8cf8c0ff7761e77a0 Mon Sep 17 00:00:00 2001 From: Krzysztof Kasprowicz Date: Mon, 11 Mar 2024 14:01:19 +0100 Subject: [PATCH 3/4] minor --- dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs | 2 +- .../Extensions/GoogleAIMemoryBuilderExtensions.cs | 2 +- .../Extensions/VertexAIMemoryBuilderExtensions.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs b/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs index 1008493c7e59..9dc878ea3455 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs @@ -63,7 +63,7 @@ public async Task RunAsync() // Uncomment the following line to use GoogleAI embeddings // var memoryWithCustomDb = new MemoryBuilder() - // .WithGoogleAIEmbeddingGeneration(TestConfiguration.GoogleAI.EmbeddingModelId, TestConfiguration.GoogleAI.ApiKey) + // .WithGoogleAITextEmbeddingGeneration(TestConfiguration.GoogleAI.EmbeddingModelId, TestConfiguration.GoogleAI.ApiKey) // .WithMemoryStore(new VolatileMemoryStore()) // .Build(); diff --git a/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/GoogleAIMemoryBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/GoogleAIMemoryBuilderExtensions.cs index fcd012421c08..f67a9a452dc1 100644 --- a/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/GoogleAIMemoryBuilderExtensions.cs +++ b/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/GoogleAIMemoryBuilderExtensions.cs @@ -20,7 +20,7 @@ public static class GoogleAIMemoryBuilderExtensions /// The API key for authentication Gemini API. /// The optional custom HttpClient. /// The updated memory builder. - public static MemoryBuilder WithGoogleAIEmbeddingGeneration( + public static MemoryBuilder WithGoogleAITextEmbeddingGeneration( this MemoryBuilder builder, string modelId, string apiKey, diff --git a/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/VertexAIMemoryBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/VertexAIMemoryBuilderExtensions.cs index d76fb1597f43..430b7d4071f1 100644 --- a/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/VertexAIMemoryBuilderExtensions.cs +++ b/dotnet/src/Connectors/Connectors.GoogleVertexAI/Extensions/VertexAIMemoryBuilderExtensions.cs @@ -22,7 +22,7 @@ public static class VertexAIMemoryBuilderExtensions /// Your project ID /// The optional custom HttpClient. /// The updated memory builder. - public static MemoryBuilder WithVertexAIEmbeddingGeneration( + public static MemoryBuilder WithVertexAITextEmbeddingGeneration( this MemoryBuilder builder, string modelId, string bearerKey, From 373614137421400d616fdcfc20885fa6242ec738 Mon Sep 17 00:00:00 2001 From: Krzysztof Kasprowicz Date: Mon, 11 Mar 2024 14:19:13 +0100 Subject: [PATCH 4/4] Added tests --- .../GoogleAIMemoryBuilderExtensionsTests.cs | 32 +++++++++++++++++++ .../VertexAIMemoryBuilderExtensionsTests.cs | 32 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 dotnet/src/Connectors/Connectors.GoogleVertexAI.UnitTests/Extensions/GoogleAIMemoryBuilderExtensionsTests.cs create mode 100644 dotnet/src/Connectors/Connectors.GoogleVertexAI.UnitTests/Extensions/VertexAIMemoryBuilderExtensionsTests.cs diff --git a/dotnet/src/Connectors/Connectors.GoogleVertexAI.UnitTests/Extensions/GoogleAIMemoryBuilderExtensionsTests.cs b/dotnet/src/Connectors/Connectors.GoogleVertexAI.UnitTests/Extensions/GoogleAIMemoryBuilderExtensionsTests.cs new file mode 100644 index 000000000000..c5738ae0902b --- /dev/null +++ b/dotnet/src/Connectors/Connectors.GoogleVertexAI.UnitTests/Extensions/GoogleAIMemoryBuilderExtensionsTests.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.SemanticKernel; +using Microsoft.SemanticKernel.Memory; +using Moq; +using Xunit; + +namespace SemanticKernel.Connectors.GoogleVertexAI.UnitTests.Extensions; + +/// +/// Unit tests for class. +/// +public sealed class GoogleAIMemoryBuilderExtensionsTests +{ + private readonly Mock _mockMemoryStore = new(); + + [Fact] + public void ShouldBuildMemoryWithGoogleAIEmbeddingGenerator() + { + // Arrange + var builder = new MemoryBuilder(); + + // Act + var memory = builder + .WithGoogleAITextEmbeddingGeneration("fake-model", "fake-apikey") + .WithMemoryStore(this._mockMemoryStore.Object) + .Build(); + + // Assert + Assert.NotNull(memory); + } +} diff --git a/dotnet/src/Connectors/Connectors.GoogleVertexAI.UnitTests/Extensions/VertexAIMemoryBuilderExtensionsTests.cs b/dotnet/src/Connectors/Connectors.GoogleVertexAI.UnitTests/Extensions/VertexAIMemoryBuilderExtensionsTests.cs new file mode 100644 index 000000000000..b61ee8b3d2a3 --- /dev/null +++ b/dotnet/src/Connectors/Connectors.GoogleVertexAI.UnitTests/Extensions/VertexAIMemoryBuilderExtensionsTests.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.SemanticKernel; +using Microsoft.SemanticKernel.Memory; +using Moq; +using Xunit; + +namespace SemanticKernel.Connectors.GoogleVertexAI.UnitTests.Extensions; + +/// +/// Unit tests for class. +/// +public sealed class VertexAIMemoryBuilderExtensionsTests +{ + private readonly Mock _mockMemoryStore = new(); + + [Fact] + public void ShouldBuildMemoryWithVertexAIEmbeddingGenerator() + { + // Arrange + var builder = new MemoryBuilder(); + + // Act + var memory = builder + .WithVertexAITextEmbeddingGeneration("fake-model", "fake-bearer-key", "fake-location", "fake-project") + .WithMemoryStore(this._mockMemoryStore.Object) + .Build(); + + // Assert + Assert.NotNull(memory); + } +}