|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using Google.Apis.Auth.OAuth2; |
| 4 | +using Microsoft.SemanticKernel; |
| 5 | +using Microsoft.SemanticKernel.Embeddings; |
| 6 | +using xRetry; |
| 7 | + |
| 8 | +namespace Memory; |
| 9 | + |
| 10 | +// The following example shows how to use Semantic Kernel with Google AI and Google's Vertex AI for embedding generation, |
| 11 | +// including the ability to specify custom dimensions. |
| 12 | +public class Google_EmbeddingGeneration(ITestOutputHelper output) : BaseTest(output) |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// This test demonstrates how to use the Google Vertex AI embedding generation service with default dimensions. |
| 16 | + /// </summary> |
| 17 | + /// <remarks> |
| 18 | + /// Currently custom dimensions are not supported for Vertex AI. |
| 19 | + /// </remarks> |
| 20 | + [RetryFact(typeof(HttpOperationException))] |
| 21 | + public async Task GenerateEmbeddingWithDefaultDimensionsUsingVertexAI() |
| 22 | + { |
| 23 | + string? bearerToken = null; |
| 24 | + |
| 25 | + Assert.NotNull(TestConfiguration.VertexAI.EmbeddingModelId); |
| 26 | + Assert.NotNull(TestConfiguration.VertexAI.ClientId); |
| 27 | + Assert.NotNull(TestConfiguration.VertexAI.ClientSecret); |
| 28 | + Assert.NotNull(TestConfiguration.VertexAI.Location); |
| 29 | + Assert.NotNull(TestConfiguration.VertexAI.ProjectId); |
| 30 | + |
| 31 | + IKernelBuilder kernelBuilder = Kernel.CreateBuilder(); |
| 32 | + kernelBuilder.AddVertexAIEmbeddingGeneration( |
| 33 | + modelId: TestConfiguration.VertexAI.EmbeddingModelId!, |
| 34 | + bearerTokenProvider: GetBearerToken, |
| 35 | + location: TestConfiguration.VertexAI.Location, |
| 36 | + projectId: TestConfiguration.VertexAI.ProjectId); |
| 37 | + Kernel kernel = kernelBuilder.Build(); |
| 38 | + |
| 39 | + var embeddingGenerator = kernel.GetRequiredService<ITextEmbeddingGenerationService>(); |
| 40 | + |
| 41 | + // Generate embeddings with the default dimensions for the model |
| 42 | + var embeddings = await embeddingGenerator.GenerateEmbeddingsAsync( |
| 43 | + ["Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your codebase."]); |
| 44 | + |
| 45 | + Console.WriteLine($"Generated '{embeddings.Count}' embedding(s) with '{embeddings[0].Length}' dimensions (default) for the provided text"); |
| 46 | + |
| 47 | + // Uses Google.Apis.Auth.OAuth2 to get the bearer token |
| 48 | + async ValueTask<string> GetBearerToken() |
| 49 | + { |
| 50 | + if (!string.IsNullOrEmpty(bearerToken)) |
| 51 | + { |
| 52 | + return bearerToken; |
| 53 | + } |
| 54 | + |
| 55 | + var credential = GoogleWebAuthorizationBroker.AuthorizeAsync( |
| 56 | + new ClientSecrets |
| 57 | + { |
| 58 | + ClientId = TestConfiguration.VertexAI.ClientId, |
| 59 | + ClientSecret = TestConfiguration.VertexAI.ClientSecret |
| 60 | + }, |
| 61 | + ["https://www.googleapis.com/auth/cloud-platform"], |
| 62 | + "user", |
| 63 | + CancellationToken.None); |
| 64 | + |
| 65 | + var userCredential = await credential.WaitAsync(CancellationToken.None); |
| 66 | + bearerToken = userCredential.Token.AccessToken; |
| 67 | + |
| 68 | + return bearerToken; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [RetryFact(typeof(HttpOperationException))] |
| 73 | + public async Task GenerateEmbeddingWithDefaultDimensionsUsingGoogleAI() |
| 74 | + { |
| 75 | + Assert.NotNull(TestConfiguration.GoogleAI.EmbeddingModelId); |
| 76 | + Assert.NotNull(TestConfiguration.GoogleAI.ApiKey); |
| 77 | + |
| 78 | + IKernelBuilder kernelBuilder = Kernel.CreateBuilder(); |
| 79 | + kernelBuilder.AddGoogleAIEmbeddingGeneration( |
| 80 | + modelId: TestConfiguration.GoogleAI.EmbeddingModelId!, |
| 81 | + apiKey: TestConfiguration.GoogleAI.ApiKey); |
| 82 | + Kernel kernel = kernelBuilder.Build(); |
| 83 | + |
| 84 | + var embeddingGenerator = kernel.GetRequiredService<ITextEmbeddingGenerationService>(); |
| 85 | + |
| 86 | + // Generate embeddings with the default dimensions for the model |
| 87 | + var embeddings = await embeddingGenerator.GenerateEmbeddingsAsync( |
| 88 | + ["Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your codebase."]); |
| 89 | + |
| 90 | + Console.WriteLine($"Generated '{embeddings.Count}' embedding(s) with '{embeddings[0].Length}' dimensions (default) for the provided text"); |
| 91 | + } |
| 92 | + |
| 93 | + [RetryFact(typeof(HttpOperationException))] |
| 94 | + public async Task GenerateEmbeddingWithCustomDimensionsUsingGoogleAI() |
| 95 | + { |
| 96 | + Assert.NotNull(TestConfiguration.GoogleAI.EmbeddingModelId); |
| 97 | + Assert.NotNull(TestConfiguration.GoogleAI.ApiKey); |
| 98 | + |
| 99 | + // Specify custom dimensions for the embeddings |
| 100 | + const int CustomDimensions = 512; |
| 101 | + |
| 102 | + IKernelBuilder kernelBuilder = Kernel.CreateBuilder(); |
| 103 | + kernelBuilder.AddGoogleAIEmbeddingGeneration( |
| 104 | + modelId: TestConfiguration.GoogleAI.EmbeddingModelId!, |
| 105 | + apiKey: TestConfiguration.GoogleAI.ApiKey, |
| 106 | + dimensions: CustomDimensions); |
| 107 | + Kernel kernel = kernelBuilder.Build(); |
| 108 | + |
| 109 | + var embeddingGenerator = kernel.GetRequiredService<ITextEmbeddingGenerationService>(); |
| 110 | + |
| 111 | + // Generate embeddings with the specified custom dimensions |
| 112 | + var embeddings = await embeddingGenerator.GenerateEmbeddingsAsync( |
| 113 | + ["Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your codebase."]); |
| 114 | + |
| 115 | + Console.WriteLine($"Generated '{embeddings.Count}' embedding(s) with '{embeddings[0].Length}' dimensions (custom: '{CustomDimensions}') for the provided text"); |
| 116 | + |
| 117 | + // Verify that we received embeddings with our requested dimensions |
| 118 | + Assert.Equal(CustomDimensions, embeddings[0].Length); |
| 119 | + } |
| 120 | +} |
0 commit comments