|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 |
|
3 | 3 | using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text; |
4 | 7 | using System.Threading.Tasks; |
5 | 8 | using Microsoft.SemanticKernel.Connectors.Google; |
| 9 | +using Microsoft.SemanticKernel.Embeddings; |
6 | 10 | using Microsoft.SemanticKernel.Services; |
7 | 11 | using Xunit; |
8 | 12 |
|
9 | 13 | namespace SemanticKernel.Connectors.Google.UnitTests.Services; |
10 | 14 |
|
11 | 15 | [Obsolete("Temporary test for Obsolete ITextEmbeddingGenerationService")] |
12 | | -public sealed class VertexAITextEmbeddingGenerationServiceTests |
| 16 | +public sealed class VertexAITextEmbeddingGenerationServiceTests : IDisposable |
13 | 17 | { |
| 18 | + private const string Model = "fake-model"; |
| 19 | + private const string BearerKey = "fake-key"; |
| 20 | + private const int Dimensions = 512; |
| 21 | + private readonly HttpMessageHandlerStub _messageHandlerStub; |
| 22 | + private readonly HttpClient _httpClient; |
| 23 | + |
| 24 | + public VertexAITextEmbeddingGenerationServiceTests() |
| 25 | + { |
| 26 | + this._messageHandlerStub = new HttpMessageHandlerStub |
| 27 | + { |
| 28 | + ResponseToReturn = new HttpResponseMessage(System.Net.HttpStatusCode.OK) |
| 29 | + { |
| 30 | + Content = new StringContent( |
| 31 | + """ |
| 32 | + { |
| 33 | + "predictions": [ |
| 34 | + { |
| 35 | + "embeddings": { |
| 36 | + "values": [0.1, 0.2, 0.3, 0.4, 0.5] |
| 37 | + } |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + """, |
| 42 | + Encoding.UTF8, |
| 43 | + "application/json" |
| 44 | + ) |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + this._httpClient = new HttpClient(this._messageHandlerStub, disposeHandler: false); |
| 49 | + } |
| 50 | + |
14 | 51 | [Fact] |
15 | 52 | public void AttributesShouldContainModelIdBearerAsString() |
16 | 53 | { |
17 | 54 | // Arrange & Act |
18 | | - string model = "fake-model"; |
19 | | - var service = new VertexAITextEmbeddingGenerationService(model, "key", "location", "project"); |
| 55 | + var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project"); |
20 | 56 |
|
21 | 57 | // Assert |
22 | | - Assert.Equal(model, service.Attributes[AIServiceExtensions.ModelIdKey]); |
| 58 | + Assert.Equal(Model, service.Attributes[AIServiceExtensions.ModelIdKey]); |
23 | 59 | } |
24 | 60 |
|
25 | 61 | [Fact] |
26 | 62 | public void AttributesShouldContainModelIdBearerAsFunc() |
27 | 63 | { |
28 | 64 | // Arrange & Act |
29 | | - string model = "fake-model"; |
30 | | - var service = new VertexAITextEmbeddingGenerationService(model, () => ValueTask.FromResult("key"), "location", "project"); |
| 65 | + var service = new VertexAITextEmbeddingGenerationService(Model, () => ValueTask.FromResult(BearerKey), "location", "project"); |
31 | 66 |
|
32 | 67 | // Assert |
33 | | - Assert.Equal(model, service.Attributes[AIServiceExtensions.ModelIdKey]); |
| 68 | + Assert.Equal(Model, service.Attributes[AIServiceExtensions.ModelIdKey]); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void AttributesShouldNotContainDimensionsWhenNotProvided() |
| 73 | + { |
| 74 | + // Arrange & Act |
| 75 | + var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project"); |
| 76 | + |
| 77 | + // Assert |
| 78 | + Assert.False(service.Attributes.ContainsKey(EmbeddingGenerationExtensions.DimensionsKey)); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void AttributesShouldContainDimensionsWhenProvided() |
| 83 | + { |
| 84 | + // Arrange & Act |
| 85 | + var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project", dimensions: Dimensions); |
| 86 | + |
| 87 | + // Assert |
| 88 | + Assert.Equal(Dimensions, service.Attributes[EmbeddingGenerationExtensions.DimensionsKey]); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void GetDimensionsReturnsCorrectValue() |
| 93 | + { |
| 94 | + // Arrange |
| 95 | + var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project", dimensions: Dimensions); |
| 96 | + |
| 97 | + // Act |
| 98 | + var result = service.GetDimensions(); |
| 99 | + |
| 100 | + // Assert |
| 101 | + Assert.Equal(Dimensions, result); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void GetDimensionsReturnsNullWhenNotProvided() |
| 106 | + { |
| 107 | + // Arrange |
| 108 | + var service = new VertexAITextEmbeddingGenerationService(Model, BearerKey, "location", "project"); |
| 109 | + |
| 110 | + // Act |
| 111 | + var result = service.GetDimensions(); |
| 112 | + |
| 113 | + // Assert |
| 114 | + Assert.Null(result); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public async Task ShouldNotIncludeDimensionsInRequestWhenNotProvidedAsync() |
| 119 | + { |
| 120 | + // Arrange |
| 121 | + var service = new VertexAITextEmbeddingGenerationService( |
| 122 | + modelId: Model, |
| 123 | + bearerKey: BearerKey, |
| 124 | + location: "location", |
| 125 | + projectId: "project", |
| 126 | + dimensions: null, |
| 127 | + httpClient: this._httpClient); |
| 128 | + var dataToEmbed = new List<string> { "Text to embed" }; |
| 129 | + |
| 130 | + // Act |
| 131 | + await service.GenerateEmbeddingsAsync(dataToEmbed); |
| 132 | + |
| 133 | + // Assert |
| 134 | + Assert.NotNull(this._messageHandlerStub.RequestContent); |
| 135 | + var requestBody = Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent); |
| 136 | + Assert.DoesNotContain("outputDimensionality", requestBody); |
| 137 | + } |
| 138 | + |
| 139 | + [Theory] |
| 140 | + [InlineData(Dimensions)] |
| 141 | + [InlineData(Dimensions * 2)] |
| 142 | + public async Task ShouldIncludeDimensionsInRequestWhenProvidedAsync(int? dimensions) |
| 143 | + { |
| 144 | + // Arrange |
| 145 | + var service = new VertexAITextEmbeddingGenerationService( |
| 146 | + modelId: Model, |
| 147 | + bearerKey: BearerKey, |
| 148 | + location: "location", |
| 149 | + projectId: "project", |
| 150 | + dimensions: dimensions, |
| 151 | + httpClient: this._httpClient); |
| 152 | + var dataToEmbed = new List<string> { "Text to embed" }; |
| 153 | + |
| 154 | + // Act |
| 155 | + await service.GenerateEmbeddingsAsync(dataToEmbed); |
| 156 | + |
| 157 | + // Assert |
| 158 | + Assert.NotNull(this._messageHandlerStub.RequestContent); |
| 159 | + var requestBody = Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent); |
| 160 | + Assert.Contains($"\"outputDimensionality\":{dimensions}", requestBody); |
| 161 | + } |
| 162 | + |
| 163 | + public void Dispose() |
| 164 | + { |
| 165 | + this._messageHandlerStub.Dispose(); |
| 166 | + this._httpClient.Dispose(); |
34 | 167 | } |
35 | 168 | } |
0 commit comments