|
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.IO; |
6 | 6 | using System.Linq; |
| 7 | +using System.Net; |
7 | 8 | using System.Net.Http; |
8 | 9 | using System.Text.Json; |
| 10 | +using System.Threading; |
9 | 11 | using System.Threading.Tasks; |
| 12 | +using Microsoft.Extensions.AI; |
10 | 13 | using Microsoft.SemanticKernel.Connectors.Google; |
11 | 14 | using Microsoft.SemanticKernel.Connectors.Google.Core; |
12 | 15 | using Microsoft.SemanticKernel.Http; |
@@ -206,76 +209,113 @@ public void ItAcceptsValidHostnameSegments(string validLocation) |
206 | 209 | Assert.Null(exception); |
207 | 210 | } |
208 | 211 |
|
209 | | - [Fact] |
210 | | - public async Task ShouldUseBatchEmbedContentsEndpointForGeminiEmbeddingModelAsync() |
| 212 | + [Theory] |
| 213 | + [InlineData("gemini-embedding-2")] |
| 214 | + [InlineData("gemini-embedding-2-preview")] |
| 215 | + public async Task ShouldUseVertexEmbedContentContractAsync(string modelId) |
211 | 216 | { |
212 | | - // Arrange |
213 | | - string modelId = "gemini-embedding-2"; |
214 | | - var client = this.CreateEmbeddingsClient(modelId: modelId); |
| 217 | + var client = this.CreateEmbeddingsClient(modelId: modelId, location: "global"); |
215 | 218 | this._messageHandlerStub.ResponseToReturn.Content = new StringContent( |
216 | 219 | File.ReadAllText("./TestData/vertex_embed_content_response.json")); |
217 | | - IList<string> data = ["sample data"]; |
218 | | - |
219 | | - // Act |
220 | | - await client.GenerateEmbeddingsAsync(data); |
221 | 220 |
|
222 | | - // Assert |
223 | | - Assert.NotNull(this._messageHandlerStub.RequestUri); |
224 | | - Assert.EndsWith(":batchEmbedContents", this._messageHandlerStub.RequestUri.ToString(), StringComparison.Ordinal); |
225 | | - Assert.NotNull(this._messageHandlerStub.RequestContent); |
226 | | - string requestBody = System.Text.Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent); |
227 | | - using var requestJson = JsonDocument.Parse(requestBody); |
228 | | - Assert.Equal(JsonValueKind.Array, requestJson.RootElement.GetProperty("requests").ValueKind); |
229 | | - var firstRequest = requestJson.RootElement.GetProperty("requests")[0]; |
230 | | - Assert.Equal($"projects/fake-project-id/locations/us-central1/publishers/google/models/{modelId}", firstRequest.GetProperty("model").GetString()); |
231 | | - Assert.Equal("sample data", firstRequest.GetProperty("content").GetProperty("parts")[0].GetProperty("text").GetString()); |
232 | | - Assert.False(requestJson.RootElement.TryGetProperty("instances", out _)); |
| 221 | + var embeddings = await client.GenerateEmbeddingsAsync(["sample data"]); |
| 222 | + |
| 223 | + Assert.Equal($"https://aiplatform.googleapis.com/v1/projects/fake-project-id/locations/global/publishers/google/models/{modelId}:embedContent", |
| 224 | + this._messageHandlerStub.RequestUri!.ToString()); |
| 225 | + using var body = JsonDocument.Parse(this._messageHandlerStub.RequestContent!); |
| 226 | + Assert.Equal("sample data", body.RootElement.GetProperty("content").GetProperty("parts")[0].GetProperty("text").GetString()); |
| 227 | + Assert.False(body.RootElement.TryGetProperty("requests", out _)); |
| 228 | + Assert.False(body.RootElement.TryGetProperty("instances", out _)); |
| 229 | + Assert.False(body.RootElement.TryGetProperty("model", out _)); |
| 230 | + Assert.Single(embeddings); |
| 231 | + Assert.Equal(new float[] { 0.1f, 0.2f, 0.3f }, embeddings[0].ToArray()); |
233 | 232 | } |
234 | 233 |
|
235 | | - [Fact] |
236 | | - public async Task ShouldUsePredictEndpointForLegacyEmbeddingModelAsync() |
| 234 | + [Theory] |
| 235 | + [InlineData("text-embedding-004")] |
| 236 | + [InlineData("gemini-embedding-001")] |
| 237 | + public async Task ShouldUsePredictEndpointForLegacyEmbeddingModelAsync(string modelId) |
237 | 238 | { |
238 | | - // Arrange |
239 | | - string modelId = "text-embedding-004"; |
240 | 239 | var client = this.CreateEmbeddingsClient(modelId: modelId); |
241 | | - IList<string> data = ["sample data"]; |
242 | 240 |
|
243 | | - // Act |
244 | | - await client.GenerateEmbeddingsAsync(data); |
| 241 | + await client.GenerateEmbeddingsAsync(["sample data"]); |
245 | 242 |
|
246 | | - // Assert |
247 | | - Assert.NotNull(this._messageHandlerStub.RequestUri); |
248 | | - Assert.EndsWith(":predict", this._messageHandlerStub.RequestUri.ToString(), StringComparison.Ordinal); |
249 | | - Assert.NotNull(this._messageHandlerStub.RequestContent); |
250 | | - string requestBody = System.Text.Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent); |
251 | | - using var requestJson = JsonDocument.Parse(requestBody); |
252 | | - Assert.Equal(JsonValueKind.Array, requestJson.RootElement.GetProperty("instances").ValueKind); |
| 243 | + Assert.EndsWith(":predict", this._messageHandlerStub.RequestUri!.ToString(), StringComparison.Ordinal); |
| 244 | + using var body = JsonDocument.Parse(this._messageHandlerStub.RequestContent!); |
| 245 | + Assert.Equal(JsonValueKind.Array, body.RootElement.GetProperty("instances").ValueKind); |
253 | 246 | } |
254 | 247 |
|
255 | 248 | [Fact] |
256 | | - public async Task ShouldReturnValidEmbeddingsResponseForGeminiEmbeddingModelAsync() |
| 249 | + public async Task ShouldEmbedEachInputSeparatelyAndPreserveOrderAsync() |
257 | 250 | { |
258 | | - // Arrange |
259 | | - string modelId = "gemini-embedding-2"; |
260 | | - var client = this.CreateEmbeddingsClient(modelId: modelId); |
| 251 | + using var handler = new MultipleHttpMessageHandlerStub(); |
| 252 | + handler.AddJsonResponse("{\"embedding\":{\"values\":[0.1,0.2]}}"); |
| 253 | + handler.AddJsonResponse("{\"embedding\":{\"values\":[0.3,0.4]}}"); |
| 254 | + using var httpClient = handler.CreateHttpClient(); |
| 255 | + var client = this.CreateEmbeddingsClient(modelId: "gemini-embedding-2", httpClient: httpClient); |
| 256 | + |
| 257 | + var embeddings = await client.GenerateEmbeddingsAsync(["first input", "second input"]); |
| 258 | + |
| 259 | + Assert.Equal(2, handler.RequestContents.Count); |
| 260 | + for (int i = 0; i < 2; i++) |
| 261 | + { |
| 262 | + using var body = JsonDocument.Parse(handler.RequestContents[i]!); |
| 263 | + Assert.Equal(i == 0 ? "first input" : "second input", body.RootElement.GetProperty("content").GetProperty("parts")[0].GetProperty("text").GetString()); |
| 264 | + } |
| 265 | + |
| 266 | + Assert.Collection(embeddings, |
| 267 | + values => Assert.Equal(new float[] { 0.1f, 0.2f }, values.ToArray()), |
| 268 | + values => Assert.Equal(new float[] { 0.3f, 0.4f }, values.ToArray())); |
| 269 | + } |
| 270 | + |
| 271 | + [Theory] |
| 272 | + [InlineData(null, null, null)] |
| 273 | + [InlineData(1024, null, 1024)] |
| 274 | + [InlineData(null, 768, 768)] |
| 275 | + [InlineData(1024, 768, 768)] |
| 276 | + public async Task ShouldRespectEmbedContentDimensionsAsync(int? defaultDimensions, int? requestDimensions, int? expectedDimensions) |
| 277 | + { |
| 278 | + var client = this.CreateEmbeddingsClient(modelId: "gemini-embedding-2", dimensions: defaultDimensions); |
261 | 279 | this._messageHandlerStub.ResponseToReturn.Content = new StringContent( |
262 | 280 | File.ReadAllText("./TestData/vertex_embed_content_response.json")); |
263 | | - var dataToEmbed = new List<string>() |
| 281 | + |
| 282 | + await client.GenerateEmbeddingsAsync(["sample data"], new EmbeddingGenerationOptions { Dimensions = requestDimensions }); |
| 283 | + |
| 284 | + using var body = JsonDocument.Parse(this._messageHandlerStub.RequestContent!); |
| 285 | + if (expectedDimensions.HasValue) |
264 | 286 | { |
265 | | - "Write a story about a magic backpack.", |
266 | | - "Print color of backpack." |
267 | | - }; |
| 287 | + Assert.Equal(expectedDimensions.Value, body.RootElement.GetProperty("outputDimensionality").GetInt32()); |
| 288 | + } |
| 289 | + else |
| 290 | + { |
| 291 | + Assert.False(body.RootElement.TryGetProperty("outputDimensionality", out _)); |
| 292 | + } |
| 293 | + } |
268 | 294 |
|
269 | | - // Act |
270 | | - var embeddings = await client.GenerateEmbeddingsAsync(dataToEmbed); |
| 295 | + [Fact] |
| 296 | + public async Task ShouldCancelBeforeSendingEmbedContentAsync() |
| 297 | + { |
| 298 | + var client = this.CreateEmbeddingsClient(modelId: "gemini-embedding-2"); |
| 299 | + using var cancellation = new CancellationTokenSource(); |
| 300 | + cancellation.Cancel(); |
271 | 301 |
|
272 | | - // Assert |
273 | | - VertexAIEmbedContentResponse testDataResponse = JsonSerializer.Deserialize<VertexAIEmbedContentResponse>( |
274 | | - await File.ReadAllTextAsync("./TestData/vertex_embed_content_response.json"))!; |
275 | | - Assert.NotNull(embeddings); |
276 | | - Assert.Collection(embeddings, |
277 | | - values => Assert.Equal(testDataResponse.Embeddings[0].Values, values), |
278 | | - values => Assert.Equal(testDataResponse.Embeddings[1].Values, values)); |
| 302 | + await Assert.ThrowsAnyAsync<OperationCanceledException>(() => client.GenerateEmbeddingsAsync(["sample data"], cancellationToken: cancellation.Token)); |
| 303 | + |
| 304 | + Assert.Null(this._messageHandlerStub.RequestUri); |
| 305 | + } |
| 306 | + |
| 307 | + [Fact] |
| 308 | + public async Task ShouldStopEmbedContentBatchAfterHttpFailureAsync() |
| 309 | + { |
| 310 | + using var handler = new MultipleHttpMessageHandlerStub(); |
| 311 | + handler.AddJsonResponse("{\"embedding\":{\"values\":[0.1,0.2]}}"); |
| 312 | + handler.ResponsesToReturn.Add(new HttpResponseMessage(HttpStatusCode.BadRequest)); |
| 313 | + using var httpClient = handler.CreateHttpClient(); |
| 314 | + var client = this.CreateEmbeddingsClient(modelId: "gemini-embedding-2", httpClient: httpClient); |
| 315 | + |
| 316 | + await Assert.ThrowsAsync<Microsoft.SemanticKernel.HttpOperationException>(() => client.GenerateEmbeddingsAsync(["first", "second", "third"])); |
| 317 | + |
| 318 | + Assert.Equal(2, handler.RequestContents.Count); |
279 | 319 | } |
280 | 320 |
|
281 | 321 | [Fact] |
@@ -326,15 +366,19 @@ public async Task ShouldUseRegionalEndpointWhenLocationIsRegionalAsync() |
326 | 366 |
|
327 | 367 | private VertexAIEmbeddingClient CreateEmbeddingsClient( |
328 | 368 | string modelId = "fake-model", |
329 | | - string? bearerKey = "fake-key") |
| 369 | + string? bearerKey = "fake-key", |
| 370 | + HttpClient? httpClient = null, |
| 371 | + int? dimensions = null, |
| 372 | + string location = "us-central1") |
330 | 373 | { |
331 | 374 | var client = new VertexAIEmbeddingClient( |
332 | | - httpClient: this._httpClient, |
| 375 | + httpClient: httpClient ?? this._httpClient, |
333 | 376 | modelId: modelId, |
334 | 377 | bearerTokenProvider: () => ValueTask.FromResult(bearerKey ?? "fake-key"), |
335 | 378 | apiVersion: VertexAIVersion.V1, |
336 | | - location: "us-central1", |
337 | | - projectId: "fake-project-id"); |
| 379 | + location: location, |
| 380 | + projectId: "fake-project-id", |
| 381 | + dimensions: dimensions); |
338 | 382 | return client; |
339 | 383 | } |
340 | 384 |
|
|
0 commit comments