< Summary

Information
Class: GistBackend.Handlers.AIHandler.EmbeddingClientHandler
Assembly: GistBackend
File(s): /home/runner/work/the-gist-of-it-sec/the-gist-of-it-sec/backend/GistBackend/Handlers/AIHandler/EmbeddingClientHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 13
Coverable lines: 13
Total lines: 36
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Model()100%210%
.ctor(...)0%2040%
GenerateEmbeddingAsync()100%210%

File(s)

/home/runner/work/the-gist-of-it-sec/the-gist-of-it-sec/backend/GistBackend/Handlers/AIHandler/EmbeddingClientHandler.cs

#LineLine coverage
 1using System.ClientModel;
 2using Microsoft.Extensions.Options;
 3using OpenAI;
 4using OpenAI.Embeddings;
 5
 6namespace GistBackend.Handlers.AIHandler;
 7
 8public interface IEmbeddingClientHandler
 9{
 10    public Task<float[]> GenerateEmbeddingAsync(string input, CancellationToken ct);
 11    public string Model { get; }
 12}
 13
 14public class EmbeddingClientHandler : IEmbeddingClientHandler
 15{
 16    private readonly EmbeddingClient _client;
 017    public string Model { get; }
 18
 019    public EmbeddingClientHandler(IOptions<EmbeddingClientHandlerOptions> options)
 20    {
 021        if (string.IsNullOrWhiteSpace(options.Value.ApiKey))
 022            throw new ArgumentException("API key is not set in the options.");
 023        Model = options.Value.Model;
 024        _client = options.Value.ProjectId is not null
 025            ? new EmbeddingClient(Model, new ApiKeyCredential(options.Value.ApiKey),
 026                new OpenAIClientOptions { ProjectId = options.Value.ProjectId })
 027            : new EmbeddingClient(Model, new ApiKeyCredential(options.Value.ApiKey));
 028    }
 29
 30    public async Task<float[]> GenerateEmbeddingAsync(string input, CancellationToken ct)
 31    {
 032        var result = await _client.GenerateEmbeddingAsync(input, cancellationToken: ct);
 033        return result.Value.ToFloats().ToArray();
 034    }
 35}
 36