< Summary

Information
Class: Intentum.AI.AzureOpenAI.AzureOpenAIEmbeddingProvider
Assembly: Intentum.AI.AzureOpenAI
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.AI.AzureOpenAI/AzureOpenAIEmbeddingProvider.cs
Line coverage
95%
Covered lines: 21
Uncovered lines: 1
Coverable lines: 22
Total lines: 51
Line coverage: 95.4%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
Embed(...)100%11100%
EmbedAsync()50%66100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.AI.AzureOpenAI/AzureOpenAIEmbeddingProvider.cs

#LineLine coverage
 1using System.Net.Http.Json;
 2using System.Text.Json.Serialization;
 3using Intentum.AI.Embeddings;
 4using Intentum.AI.Http;
 5using JetBrains.Annotations;
 6
 7namespace 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>
 113public sealed class AzureOpenAIEmbeddingProvider(AzureOpenAIOptions options, HttpClient httpClient)
 14    : IIntentEmbeddingProvider
 15{
 16    /// <inheritdoc />
 17    public IntentEmbedding Embed(string behaviorKey)
 118        => EmbedAsync(behaviorKey, CancellationToken.None).GetAwaiter().GetResult();
 19
 20    /// <inheritdoc />
 21    public async Task<IntentEmbedding> EmbedAsync(string behaviorKey, CancellationToken cancellationToken = default)
 122    {
 123        options.Validate();
 24
 125        var request = new AzureEmbeddingRequest(behaviorKey);
 126        var url = $"openai/deployments/{options.EmbeddingDeployment}/embeddings?api-version={options.ApiVersion}";
 27
 128        var response = await EmbeddingHttpRetryHandler.SendWithRetryAsync(
 129            ct => httpClient.PostAsJsonAsync(url, request, ct),
 030            (retryAfterSec, body) => throw new AzureOpenAIRateLimitException(retryAfterSec, body),
 131            cancellationToken: cancellationToken);
 32
 133        var payload = await response.Content
 134            .ReadFromJsonAsync<AzureEmbeddingResponse>(cancellationToken);
 35
 136        var values = payload?.Data.FirstOrDefault()?.Embedding ?? [];
 137        var score = EmbeddingScore.Normalize(values);
 38
 139        return new IntentEmbedding(Source: behaviorKey, Score: score);
 140    }
 41
 42    [UsedImplicitly]
 143    private sealed record AzureEmbeddingRequest(
 144        [property: JsonPropertyName("input")] string Input);
 45
 146    private sealed record AzureEmbeddingResponse(
 247        [property: JsonPropertyName("data")] List<AzureEmbeddingData> Data);
 48
 149    private sealed record AzureEmbeddingData(
 250        [property: JsonPropertyName("embedding")] List<double> Embedding);
 51}