-
Notifications
You must be signed in to change notification settings - Fork 4.5k
.Net: feat: Add support for dimensions in Vertex AI embedding services #13612
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
Merged
westey-m
merged 15 commits into
microsoft:main
from
abbottdev:feature/vertex-dimensions
Mar 20, 2026
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
34dedf9
feat: Add support for dimensions in Vertex AI embedding services and …
abbottdev-kroll 42b98c2
Merge branch 'main' into feature/vertex-dimensions
abbottdev-kroll 30fae55
Merge branch 'main' into feature/vertex-dimensions
abbottdev-kroll 99df050
Merge branch 'main' into feature/vertex-dimensions
abbottdev-kroll b918726
Merge branch 'main' into feature/vertex-dimensions
abbottdev-kroll bafdb29
fix: Improve JSON assertions for Vertex AI tests.
abbottdev 0fa0efd
Initial plan
Copilot 637ff9f
Add missing namespace and precedence tests for Vertex AI dimensions
Copilot b3636c5
Fix VertexAI EmbeddingGenerator options propagation and add missing p…
abbottdev 751ceee
Merge branch 'main' into feature/vertex-dimensions
abbottdev 0969e6c
Initial plan
Copilot a6132bc
Apply review comments: delegate Dispose to underlying service, fix XM…
Copilot f2956b8
fix: property implement idisposable and update xml docs for vertex em…
abbottdev 8e7e3d4
Merge branch 'main' into feature/vertex-dimensions
westey-m 5b9c39e
format: fix namespace ordering format
abbottdev-kroll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 140 additions & 7 deletions
147
...et/src/Connectors/Connectors.Google.UnitTests/Services/VertexAIEmbeddingGeneratorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,166 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.SemanticKernel.Connectors.Google; | ||
| using Xunit; | ||
|
|
||
| namespace SemanticKernel.Connectors.Google.UnitTests.Services; | ||
|
|
||
| public sealed class VertexAIEmbeddingGeneratorTests | ||
| public sealed class VertexAIEmbeddingGeneratorTests : IDisposable | ||
| { | ||
| private const string Model = "fake-model"; | ||
| private const string BearerKey = "fake-key"; | ||
| private const int Dimensions = 512; | ||
| private readonly HttpMessageHandlerStub _messageHandlerStub; | ||
| private readonly HttpClient _httpClient; | ||
|
|
||
| public VertexAIEmbeddingGeneratorTests() | ||
| { | ||
| this._messageHandlerStub = new HttpMessageHandlerStub | ||
| { | ||
| ResponseToReturn = new HttpResponseMessage(System.Net.HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent( | ||
| """ | ||
| { | ||
| "predictions": [ | ||
| { | ||
| "embeddings": { | ||
| "values": [0.1, 0.2, 0.3, 0.4, 0.5] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| """, | ||
| Encoding.UTF8, | ||
| "application/json" | ||
| ) | ||
| } | ||
| }; | ||
|
|
||
| this._httpClient = new HttpClient(this._messageHandlerStub, disposeHandler: false); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AttributesShouldContainModelIdBearerAsString() | ||
| { | ||
| // Arrange & Act | ||
| string model = "fake-model"; | ||
| using var service = new VertexAIEmbeddingGenerator(model, "key", "location", "project"); | ||
| using var service = new VertexAIEmbeddingGenerator(Model, BearerKey, "location", "project"); | ||
|
|
||
| // Assert | ||
| Assert.Equal(model, service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelId); | ||
| Assert.Equal(Model, service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AttributesShouldContainModelIdBearerAsFunc() | ||
| { | ||
| // Arrange & Act | ||
| string model = "fake-model"; | ||
| using var service = new VertexAIEmbeddingGenerator(model, () => ValueTask.FromResult("key"), "location", "project"); | ||
| using var service = new VertexAIEmbeddingGenerator(Model, () => ValueTask.FromResult(BearerKey), "location", "project"); | ||
|
|
||
| // Assert | ||
| Assert.Equal(model, service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelId); | ||
| Assert.Equal(Model, service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AttributesShouldNotContainDimensionsWhenNotProvided() | ||
| { | ||
| // Arrange & Act | ||
| using var service = new VertexAIEmbeddingGenerator(Model, BearerKey, "location", "project"); | ||
|
|
||
| // Assert | ||
| Assert.Null(service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelDimensions); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AttributesShouldContainDimensionsWhenProvided() | ||
| { | ||
| // Arrange & Act | ||
| using var service = new VertexAIEmbeddingGenerator(Model, BearerKey, "location", "project", dimensions: Dimensions); | ||
|
|
||
| // Assert | ||
| Assert.Equal(Dimensions, service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelDimensions); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetDimensionsReturnsCorrectValue() | ||
| { | ||
| // Arrange | ||
| using var service = new VertexAIEmbeddingGenerator(Model, BearerKey, "location", "project", dimensions: Dimensions); | ||
|
|
||
| // Act | ||
| var result = service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelDimensions; | ||
|
|
||
| // Assert | ||
| Assert.Equal(Dimensions, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetDimensionsReturnsNullWhenNotProvided() | ||
| { | ||
| // Arrange | ||
| using var service = new VertexAIEmbeddingGenerator(Model, BearerKey, "location", "project"); | ||
|
|
||
| // Act | ||
| var result = service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelDimensions; | ||
|
|
||
| // Assert | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldNotIncludeDimensionsInRequestWhenNotProvidedAsync() | ||
| { | ||
| // Arrange | ||
| using var service = new VertexAIEmbeddingGenerator( | ||
| modelId: Model, | ||
| bearerKey: BearerKey, | ||
| location: "location", | ||
| projectId: "project", | ||
| dimensions: null, | ||
| httpClient: this._httpClient); | ||
| var dataToEmbed = new List<string> { "Text to embed" }; | ||
|
|
||
| // Act | ||
| await service.GenerateAsync(dataToEmbed); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(this._messageHandlerStub.RequestContent); | ||
| var requestBody = Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent); | ||
| Assert.DoesNotContain("outputDimensionality", requestBody); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(Dimensions)] | ||
| [InlineData(Dimensions * 2)] | ||
| public async Task ShouldIncludeDimensionsInRequestWhenProvidedAsync(int? dimensions) | ||
| { | ||
| // Arrange | ||
| using var service = new VertexAIEmbeddingGenerator( | ||
| modelId: Model, | ||
| bearerKey: BearerKey, | ||
| location: "location", | ||
| projectId: "project", | ||
| dimensions: dimensions, | ||
| httpClient: this._httpClient); | ||
| var dataToEmbed = new List<string> { "Text to embed" }; | ||
|
|
||
| // Act | ||
| await service.GenerateAsync(dataToEmbed); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(this._messageHandlerStub.RequestContent); | ||
| var requestBody = Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent); | ||
| Assert.Contains($"\"outputDimensionality\":{dimensions}", requestBody); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this._messageHandlerStub.Dispose(); | ||
| this._httpClient.Dispose(); | ||
| } | ||
| } |
147 changes: 140 additions & 7 deletions
147
...ctors/Connectors.Google.UnitTests/Services/VertexAITextEmbeddingGenerationServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,168 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.SemanticKernel.Connectors.Google; | ||
| using Microsoft.SemanticKernel.Embeddings; | ||
| using Microsoft.SemanticKernel.Services; | ||
| using Xunit; | ||
|
|
||
| namespace SemanticKernel.Connectors.Google.UnitTests.Services; | ||
|
|
||
| [Obsolete("Temporary test for Obsolete ITextEmbeddingGenerationService")] | ||
| public sealed class VertexAITextEmbeddingGenerationServiceTests | ||
| public sealed class VertexAITextEmbeddingGenerationServiceTests : IDisposable | ||
| { | ||
| private const string Model = "fake-model"; | ||
| private const string BearerKey = "fake-key"; | ||
| private const int Dimensions = 512; | ||
| private readonly HttpMessageHandlerStub _messageHandlerStub; | ||
| private readonly HttpClient _httpClient; | ||
|
|
||
| public VertexAITextEmbeddingGenerationServiceTests() | ||
| { | ||
| this._messageHandlerStub = new HttpMessageHandlerStub | ||
| { | ||
| ResponseToReturn = new HttpResponseMessage(System.Net.HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent( | ||
| """ | ||
| { | ||
| "predictions": [ | ||
| { | ||
| "embeddings": { | ||
| "values": [0.1, 0.2, 0.3, 0.4, 0.5] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| """, | ||
| Encoding.UTF8, | ||
| "application/json" | ||
| ) | ||
| } | ||
| }; | ||
|
|
||
| this._httpClient = new HttpClient(this._messageHandlerStub, disposeHandler: false); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AttributesShouldContainModelIdBearerAsString() | ||
| { | ||
| // Arrange & Act | ||
| string model = "fake-model"; | ||
| var service = new VertexAITextEmbeddingGenerationService(model, "key", "location", "project"); | ||
| var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project"); | ||
|
|
||
| // Assert | ||
| Assert.Equal(model, service.Attributes[AIServiceExtensions.ModelIdKey]); | ||
| Assert.Equal(Model, service.Attributes[AIServiceExtensions.ModelIdKey]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AttributesShouldContainModelIdBearerAsFunc() | ||
| { | ||
| // Arrange & Act | ||
| string model = "fake-model"; | ||
| var service = new VertexAITextEmbeddingGenerationService(model, () => ValueTask.FromResult("key"), "location", "project"); | ||
| var service = new VertexAITextEmbeddingGenerationService(Model, () => ValueTask.FromResult(BearerKey), "location", "project"); | ||
|
|
||
| // Assert | ||
| Assert.Equal(model, service.Attributes[AIServiceExtensions.ModelIdKey]); | ||
| Assert.Equal(Model, service.Attributes[AIServiceExtensions.ModelIdKey]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AttributesShouldNotContainDimensionsWhenNotProvided() | ||
| { | ||
| // Arrange & Act | ||
| var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project"); | ||
|
|
||
| // Assert | ||
| Assert.False(service.Attributes.ContainsKey(EmbeddingGenerationExtensions.DimensionsKey)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AttributesShouldContainDimensionsWhenProvided() | ||
| { | ||
| // Arrange & Act | ||
| var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project", dimensions: Dimensions); | ||
|
|
||
| // Assert | ||
| Assert.Equal(Dimensions, service.Attributes[EmbeddingGenerationExtensions.DimensionsKey]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetDimensionsReturnsCorrectValue() | ||
| { | ||
| // Arrange | ||
| var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project", dimensions: Dimensions); | ||
|
|
||
| // Act | ||
| var result = service.GetDimensions(); | ||
|
|
||
| // Assert | ||
| Assert.Equal(Dimensions, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetDimensionsReturnsNullWhenNotProvided() | ||
| { | ||
| // Arrange | ||
| var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project"); | ||
|
|
||
| // Act | ||
| var result = service.GetDimensions(); | ||
|
|
||
| // Assert | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldNotIncludeDimensionsInRequestWhenNotProvidedAsync() | ||
| { | ||
| // Arrange | ||
| var service = new VertexAITextEmbeddingGenerationService( | ||
| modelId: Model, | ||
| bearerKey: BearerKey, | ||
| location: "location", | ||
| projectId: "project", | ||
| dimensions: null, | ||
| httpClient: this._httpClient); | ||
| var dataToEmbed = new List<string> { "Text to embed" }; | ||
|
|
||
| // Act | ||
| await service.GenerateEmbeddingsAsync(dataToEmbed); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(this._messageHandlerStub.RequestContent); | ||
| var requestBody = Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent); | ||
| Assert.DoesNotContain("outputDimensionality", requestBody); | ||
abbottdev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(Dimensions)] | ||
| [InlineData(Dimensions * 2)] | ||
| public async Task ShouldIncludeDimensionsInRequestWhenProvidedAsync(int? dimensions) | ||
| { | ||
| // Arrange | ||
| var service = new VertexAITextEmbeddingGenerationService( | ||
| modelId: Model, | ||
| bearerKey: BearerKey, | ||
| location: "location", | ||
| projectId: "project", | ||
| dimensions: dimensions, | ||
| httpClient: this._httpClient); | ||
| var dataToEmbed = new List<string> { "Text to embed" }; | ||
|
|
||
| // Act | ||
| await service.GenerateEmbeddingsAsync(dataToEmbed); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(this._messageHandlerStub.RequestContent); | ||
| var requestBody = Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent); | ||
| Assert.Contains($"\"outputDimensionality\":{dimensions}", requestBody); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this._messageHandlerStub.Dispose(); | ||
| this._httpClient.Dispose(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.