38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using System.Net.Http.Json;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace GymLogAI.AI;
|
|
|
|
public sealed class OpenRouterEmbeddingClient(
|
|
HttpClient httpClient,
|
|
IOptions<OpenRouterOptions> options,
|
|
ILogger<OpenRouterEmbeddingClient> logger)
|
|
{
|
|
private readonly OpenRouterOptions _options = options.Value;
|
|
|
|
public async Task<float[]?> GenerateAsync(string input, CancellationToken cancellationToken)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_options.ApiKey))
|
|
{
|
|
logger.LogInformation("OpenRouter API key is not configured. Returning stubbed embedding.");
|
|
return null;
|
|
}
|
|
|
|
using var request = new HttpRequestMessage(HttpMethod.Post, "embeddings")
|
|
{
|
|
Content = JsonContent.Create(new
|
|
{
|
|
model = _options.EmbeddingModel,
|
|
input
|
|
})
|
|
};
|
|
|
|
request.Headers.Authorization = new("Bearer", _options.ApiKey);
|
|
var response = await httpClient.SendAsync(request, cancellationToken);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
return null;
|
|
}
|
|
}
|