Skip to content

Commit 9935a9f

Browse files
committed
fix(google): use Vertex embedContent contract for Gemini models
1 parent 999ac56 commit 9935a9f

5 files changed

Lines changed: 142 additions & 129 deletions

File tree

‎dotnet/src/Connectors/Connectors.Google.UnitTests/Core/VertexAI/VertexAIClientEmbeddingsGenerationTests.cs‎

Lines changed: 99 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Net;
78
using System.Net.Http;
89
using System.Text.Json;
10+
using System.Threading;
911
using System.Threading.Tasks;
12+
using Microsoft.Extensions.AI;
1013
using Microsoft.SemanticKernel.Connectors.Google;
1114
using Microsoft.SemanticKernel.Connectors.Google.Core;
1215
using Microsoft.SemanticKernel.Http;
@@ -206,76 +209,113 @@ public void ItAcceptsValidHostnameSegments(string validLocation)
206209
Assert.Null(exception);
207210
}
208211

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)
211216
{
212-
// Arrange
213-
string modelId = "gemini-embedding-2";
214-
var client = this.CreateEmbeddingsClient(modelId: modelId);
217+
var client = this.CreateEmbeddingsClient(modelId: modelId, location: "global");
215218
this._messageHandlerStub.ResponseToReturn.Content = new StringContent(
216219
File.ReadAllText("./TestData/vertex_embed_content_response.json"));
217-
IList<string> data = ["sample data"];
218-
219-
// Act
220-
await client.GenerateEmbeddingsAsync(data);
221220

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());
233232
}
234233

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)
237238
{
238-
// Arrange
239-
string modelId = "text-embedding-004";
240239
var client = this.CreateEmbeddingsClient(modelId: modelId);
241-
IList<string> data = ["sample data"];
242240

243-
// Act
244-
await client.GenerateEmbeddingsAsync(data);
241+
await client.GenerateEmbeddingsAsync(["sample data"]);
245242

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);
253246
}
254247

255248
[Fact]
256-
public async Task ShouldReturnValidEmbeddingsResponseForGeminiEmbeddingModelAsync()
249+
public async Task ShouldEmbedEachInputSeparatelyAndPreserveOrderAsync()
257250
{
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);
261279
this._messageHandlerStub.ResponseToReturn.Content = new StringContent(
262280
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)
264286
{
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+
}
268294

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();
271301

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);
279319
}
280320

281321
[Fact]
@@ -326,15 +366,19 @@ public async Task ShouldUseRegionalEndpointWhenLocationIsRegionalAsync()
326366

327367
private VertexAIEmbeddingClient CreateEmbeddingsClient(
328368
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")
330373
{
331374
var client = new VertexAIEmbeddingClient(
332-
httpClient: this._httpClient,
375+
httpClient: httpClient ?? this._httpClient,
333376
modelId: modelId,
334377
bearerTokenProvider: () => ValueTask.FromResult(bearerKey ?? "fake-key"),
335378
apiVersion: VertexAIVersion.V1,
336-
location: "us-central1",
337-
projectId: "fake-project-id");
379+
location: location,
380+
projectId: "fake-project-id",
381+
dimensions: dimensions);
338382
return client;
339383
}
340384

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
{
2-
"embeddings": [
3-
{
4-
"values": [
5-
0.1,
6-
0.2,
7-
0.3
8-
]
9-
},
10-
{
11-
"values": [
12-
0.4,
13-
0.5,
14-
0.6
15-
]
16-
}
17-
]
18-
}
2+
"embedding": {
3+
"values": [0.1, 0.2, 0.3]
4+
}
5+
}

‎dotnet/src/Connectors/Connectors.Google/Core/VertexAI/VertexAIEmbedContentRequest.cs‎

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,28 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System.Collections.Generic;
4-
using System.Linq;
54
using System.Text.Json.Serialization;
65

76
namespace Microsoft.SemanticKernel.Connectors.Google.Core;
87

98
internal sealed class VertexAIEmbedContentRequest
109
{
11-
[JsonPropertyName("requests")]
12-
public IList<EmbedContentRequestItem> Requests { get; set; } = null!;
10+
[JsonPropertyName("content")]
11+
public RequestContent Content { get; set; } = null!;
1312

14-
public static VertexAIEmbedContentRequest FromData(IEnumerable<string> data, string model, int? dimensions = null) => new()
13+
[JsonPropertyName("outputDimensionality")]
14+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
15+
public int? OutputDimensionality { get; set; }
16+
17+
public static VertexAIEmbedContentRequest FromText(string text, int? dimensions = null) => new()
1518
{
16-
Requests = data.Select(text => new EmbedContentRequestItem
19+
Content = new RequestContent
1720
{
18-
Model = model,
19-
Content = new RequestContent
20-
{
21-
Parts =
22-
[
23-
new RequestPart
24-
{
25-
Text = text
26-
}
27-
]
28-
},
29-
OutputDimensionality = dimensions
30-
}).ToList()
21+
Parts = [new RequestPart { Text = text }]
22+
},
23+
OutputDimensionality = dimensions
3124
};
3225

33-
internal sealed class EmbedContentRequestItem
34-
{
35-
[JsonPropertyName("model")]
36-
public string Model { get; set; } = null!;
37-
38-
[JsonPropertyName("content")]
39-
public RequestContent Content { get; set; } = null!;
40-
41-
[JsonPropertyName("outputDimensionality")]
42-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
43-
public int? OutputDimensionality { get; set; }
44-
}
45-
4626
internal sealed class RequestContent
4727
{
4828
[JsonPropertyName("parts")]

‎dotnet/src/Connectors/Connectors.Google/Core/VertexAI/VertexAIEmbedContentResponse.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System;
4-
using System.Collections.Generic;
54
using System.Text.Json.Serialization;
65

76
namespace Microsoft.SemanticKernel.Connectors.Google.Core;
87

98
internal sealed class VertexAIEmbedContentResponse
109
{
11-
[JsonPropertyName("embeddings")]
10+
[JsonPropertyName("embedding")]
1211
[JsonRequired]
13-
public IList<ResponseEmbedding> Embeddings { get; set; } = null!;
12+
public ResponseEmbedding Embedding { get; set; } = null!;
1413

1514
internal sealed class ResponseEmbedding
1615
{

‎dotnet/src/Connectors/Connectors.Google/Core/VertexAI/VertexAIEmbeddingClient.cs‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ internal sealed class VertexAIEmbeddingClient : ClientBase
2020
private readonly Uri _embeddingEndpoint;
2121
private readonly int? _dimensions;
2222
private readonly bool _useEmbedContentMethod;
23-
private readonly string _fullModelResourceName;
2423

2524
/// <summary>
2625
/// Represents a client for interacting with the embeddings models by Vertex AI.
@@ -57,14 +56,15 @@ public VertexAIEmbeddingClient(
5756

5857
this._embeddingModelId = modelId;
5958
this._useEmbedContentMethod = UsesEmbedContentMethod(modelId);
60-
string embeddingMethod = this._useEmbedContentMethod ? "batchEmbedContents" : "predict";
61-
this._fullModelResourceName = $"projects/{projectId}/locations/{location}/publishers/google/models/{this._embeddingModelId}";
62-
this._embeddingEndpoint = new Uri($"{baseUri}/{versionSubLink}/{this._fullModelResourceName}:{embeddingMethod}");
59+
string embeddingMethod = this._useEmbedContentMethod ? "embedContent" : "predict";
60+
string fullModelResourceName = $"projects/{projectId}/locations/{location}/publishers/google/models/{this._embeddingModelId}";
61+
this._embeddingEndpoint = new Uri($"{baseUri}/{versionSubLink}/{fullModelResourceName}:{embeddingMethod}");
6362
this._dimensions = dimensions;
6463
}
6564

6665
private static bool UsesEmbedContentMethod(string modelId)
67-
=> modelId.StartsWith("gemini-embedding", StringComparison.Ordinal);
66+
=> modelId.StartsWith("gemini-embedding-", StringComparison.Ordinal) &&
67+
!string.Equals(modelId, "gemini-embedding-001", StringComparison.Ordinal);
6868

6969
/// <summary>
7070
/// Generates embeddings for the given data asynchronously.
@@ -80,26 +80,29 @@ public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(
8080
{
8181
Verify.NotNullOrEmpty(data);
8282

83-
object request = this._useEmbedContentMethod
84-
? VertexAIEmbedContentRequest.FromData(data, this._fullModelResourceName, options?.Dimensions ?? this._dimensions)
85-
: this.GetEmbeddingRequest(data, options);
86-
87-
using var httpRequestMessage = await this.CreateHttpRequestAsync(request, this._embeddingEndpoint).ConfigureAwait(false);
88-
89-
string body = await this.SendRequestAndGetStringBodyAsync(httpRequestMessage, cancellationToken)
90-
.ConfigureAwait(false);
91-
92-
return this._useEmbedContentMethod
93-
? ProcessEmbedContentResponse(body)
94-
: DeserializeAndProcessEmbeddingsResponse(body);
83+
if (this._useEmbedContentMethod)
84+
{
85+
// Vertex embedContent accepts one content per call. Preserve input order
86+
// and return one embedding for each string in the public batch interface.
87+
var embeddings = new List<ReadOnlyMemory<float>>(data.Count);
88+
foreach (string text in data)
89+
{
90+
cancellationToken.ThrowIfCancellationRequested();
91+
var request = VertexAIEmbedContentRequest.FromText(text, options?.Dimensions ?? this._dimensions);
92+
using var message = await this.CreateHttpRequestAsync(request, this._embeddingEndpoint).ConfigureAwait(false);
93+
string response = await this.SendRequestAndGetStringBodyAsync(message, cancellationToken).ConfigureAwait(false);
94+
embeddings.Add(DeserializeResponse<VertexAIEmbedContentResponse>(response).Embedding.Values);
95+
}
96+
97+
return embeddings;
98+
}
99+
100+
var predictRequest = VertexAIEmbeddingRequest.FromData(data, options?.Dimensions ?? this._dimensions);
101+
using var httpRequestMessage = await this.CreateHttpRequestAsync(predictRequest, this._embeddingEndpoint).ConfigureAwait(false);
102+
string body = await this.SendRequestAndGetStringBodyAsync(httpRequestMessage, cancellationToken).ConfigureAwait(false);
103+
return DeserializeAndProcessEmbeddingsResponse(body);
95104
}
96105

97-
private VertexAIEmbeddingRequest GetEmbeddingRequest(IEnumerable<string> data, EmbeddingGenerationOptions? options = null)
98-
=> VertexAIEmbeddingRequest.FromData(data, options?.Dimensions ?? this._dimensions);
99-
100-
private static List<ReadOnlyMemory<float>> ProcessEmbedContentResponse(string body)
101-
=> DeserializeResponse<VertexAIEmbedContentResponse>(body).Embeddings.Select(embedding => embedding.Values).ToList();
102-
103106
private static List<ReadOnlyMemory<float>> DeserializeAndProcessEmbeddingsResponse(string body)
104107
=> ProcessEmbeddingsResponse(DeserializeResponse<VertexAIEmbeddingResponse>(body));
105108

0 commit comments

Comments
 (0)