| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Intentum.AI.Embeddings; |
| | | 4 | | using Intentum.AI.Http; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Intentum.AI.Gemini; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Gemini embedding provider. On 429 after retries throws <see cref="GeminiRateLimitException"/>. |
| | | 11 | | /// Built-in retry for 429 (up to 5 attempts, respects Retry-After). |
| | | 12 | | /// </summary> |
| | 1 | 13 | | public sealed class GeminiEmbeddingProvider(GeminiOptions options, HttpClient httpClient) : IIntentEmbeddingProvider |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public IntentEmbedding Embed(string behaviorKey) |
| | 1 | 17 | | => EmbedAsync(behaviorKey, CancellationToken.None).GetAwaiter().GetResult(); |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public async Task<IntentEmbedding> EmbedAsync(string behaviorKey, CancellationToken cancellationToken = default) |
| | 1 | 21 | | { |
| | 1 | 22 | | options.Validate(); |
| | | 23 | | |
| | 1 | 24 | | var request = new GeminiEmbedRequest( |
| | 1 | 25 | | new GeminiContent([new GeminiPart(behaviorKey)])); |
| | 1 | 26 | | var url = $"models/{options.EmbeddingModel}:embedContent?key={options.ApiKey}"; |
| | | 27 | | |
| | 1 | 28 | | var response = await EmbeddingHttpRetryHandler.SendWithRetryAsync( |
| | 1 | 29 | | ct => httpClient.PostAsJsonAsync(url, request, ct), |
| | 0 | 30 | | (retryAfterSec, body) => throw new GeminiRateLimitException(retryAfterSec, body), |
| | 1 | 31 | | cancellationToken: cancellationToken); |
| | | 32 | | |
| | 1 | 33 | | var payload = await response.Content |
| | 1 | 34 | | .ReadFromJsonAsync<GeminiEmbedResponse>(cancellationToken); |
| | | 35 | | |
| | 1 | 36 | | var values = payload?.Embedding.Values ?? []; |
| | 1 | 37 | | var score = EmbeddingScore.Normalize(values); |
| | | 38 | | |
| | 1 | 39 | | return new IntentEmbedding(Source: behaviorKey, Score: score); |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | [UsedImplicitly] |
| | 1 | 43 | | private sealed record GeminiEmbedRequest( |
| | 1 | 44 | | [property: JsonPropertyName("content")] GeminiContent Content); |
| | | 45 | | |
| | | 46 | | [UsedImplicitly] |
| | 1 | 47 | | private sealed record GeminiContent( |
| | 1 | 48 | | [property: JsonPropertyName("parts")] IReadOnlyList<GeminiPart> Parts); |
| | | 49 | | |
| | | 50 | | [UsedImplicitly] |
| | 1 | 51 | | private sealed record GeminiPart( |
| | 1 | 52 | | [property: JsonPropertyName("text")] string Text); |
| | | 53 | | |
| | 1 | 54 | | private sealed record GeminiEmbedResponse( |
| | 2 | 55 | | [property: JsonPropertyName("embedding")] GeminiEmbedding Embedding); |
| | | 56 | | |
| | 1 | 57 | | private sealed record GeminiEmbedding( |
| | 2 | 58 | | [property: JsonPropertyName("values")] List<double> Values); |
| | | 59 | | } |