| | | 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.AzureOpenAI; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Azure OpenAI embedding provider. On 429 after retries throws <see cref="AzureOpenAIRateLimitException"/>. |
| | | 11 | | /// Built-in retry for 429 (up to 5 attempts, respects Retry-After). |
| | | 12 | | /// </summary> |
| | 1 | 13 | | public sealed class AzureOpenAIEmbeddingProvider(AzureOpenAIOptions options, HttpClient httpClient) |
| | | 14 | | : IIntentEmbeddingProvider |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public IntentEmbedding Embed(string behaviorKey) |
| | 1 | 18 | | => EmbedAsync(behaviorKey, CancellationToken.None).GetAwaiter().GetResult(); |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public async Task<IntentEmbedding> EmbedAsync(string behaviorKey, CancellationToken cancellationToken = default) |
| | 1 | 22 | | { |
| | 1 | 23 | | options.Validate(); |
| | | 24 | | |
| | 1 | 25 | | var request = new AzureEmbeddingRequest(behaviorKey); |
| | 1 | 26 | | var url = $"openai/deployments/{options.EmbeddingDeployment}/embeddings?api-version={options.ApiVersion}"; |
| | | 27 | | |
| | 1 | 28 | | var response = await EmbeddingHttpRetryHandler.SendWithRetryAsync( |
| | 1 | 29 | | ct => httpClient.PostAsJsonAsync(url, request, ct), |
| | 0 | 30 | | (retryAfterSec, body) => throw new AzureOpenAIRateLimitException(retryAfterSec, body), |
| | 1 | 31 | | cancellationToken: cancellationToken); |
| | | 32 | | |
| | 1 | 33 | | var payload = await response.Content |
| | 1 | 34 | | .ReadFromJsonAsync<AzureEmbeddingResponse>(cancellationToken); |
| | | 35 | | |
| | 1 | 36 | | var values = payload?.Data.FirstOrDefault()?.Embedding ?? []; |
| | 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 AzureEmbeddingRequest( |
| | 1 | 44 | | [property: JsonPropertyName("input")] string Input); |
| | | 45 | | |
| | 1 | 46 | | private sealed record AzureEmbeddingResponse( |
| | 2 | 47 | | [property: JsonPropertyName("data")] List<AzureEmbeddingData> Data); |
| | | 48 | | |
| | 1 | 49 | | private sealed record AzureEmbeddingData( |
| | 2 | 50 | | [property: JsonPropertyName("embedding")] List<double> Embedding); |
| | | 51 | | } |