Skip to content
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

.Net: GoogleVertexAI - memory builder #5416

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public async Task RunAsync()
.WithMemoryStore(new VolatileMemoryStore())
.Build();

// Uncomment the following line to use GoogleAI embeddings
// var memoryWithCustomDb = new MemoryBuilder()
// .WithGoogleAITextEmbeddingGeneration(TestConfiguration.GoogleAI.EmbeddingModelId, TestConfiguration.GoogleAI.ApiKey)
// .WithMemoryStore(new VolatileMemoryStore())
// .Build();

await RunExampleAsync(memoryWithCustomDb);
}

Expand Down Expand Up @@ -167,7 +173,5 @@ private static Dictionary<string, string> SampleData()
};
}

public Example14_SemanticMemory(ITestOutputHelper output) : base(output)
{
}
public Example14_SemanticMemory(ITestOutputHelper output) : base(output) { }
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Unit tests for <see cref="GoogleAIMemoryBuilderExtensions"/> class.
/// </summary>
public sealed class GoogleAIMemoryBuilderExtensionsTests
{
private readonly Mock<IMemoryStore> _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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Unit tests for <see cref="VertexAIMemoryBuilderExtensions"/> class.
/// </summary>
public sealed class VertexAIMemoryBuilderExtensionsTests
{
private readonly Mock<IMemoryStore> _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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Provides extension methods for the <see cref="MemoryBuilder"/> class to configure GoogleAI connector.
/// </summary>
public static class GoogleAIMemoryBuilderExtensions
{
/// <summary>
/// Add GoogleAI embeddings generation service to the memory builder.
/// </summary>
/// <param name="builder">The <see cref="MemoryBuilder"/> instance</param>
/// <param name="modelId">The model for text generation.</param>
/// <param name="apiKey">The API key for authentication Gemini API.</param>
/// <param name="httpClient">The optional custom HttpClient.</param>
/// <returns>The updated memory builder.</returns>
public static MemoryBuilder WithGoogleAITextEmbeddingGeneration(
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));
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Provides extension methods for the <see cref="MemoryBuilder"/> class to configure VertexAI connector.
/// </summary>
public static class VertexAIMemoryBuilderExtensions
{
/// <summary>
/// Add VertexAI embeddings generation service to the memory builder.
/// </summary>
/// <param name="builder">The <see cref="MemoryBuilder"/> instance</param>
/// <param name="modelId">The model for text generation.</param>
/// <param name="bearerKey">The Bearer Key for authentication.</param>
/// <param name="location">The location to process the request</param>
/// <param name="projectId">Your project ID</param>
/// <param name="httpClient">The optional custom HttpClient.</param>
/// <returns>The updated memory builder.</returns>
public static MemoryBuilder WithVertexAITextEmbeddingGeneration(
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));
}
}
Loading