using System.Net.Http.Json; using VectorSearchApp.Configuration; using VectorSearchApp.Models; namespace VectorSearchApp.Services; public interface IEmbeddingService { Task GenerateEmbeddingAsync(string text, CancellationToken cancellationToken = default); } public class EmbeddingService : IEmbeddingService { private readonly HttpClient _httpClient; private readonly string _modelName; private readonly int _dimension; public EmbeddingService(EmbeddingConfiguration config) { _modelName = config.ModelName; _dimension = config.Dimension; _httpClient = new HttpClient { BaseAddress = new Uri("https://api-inference.huggingface.co/models/") }; _httpClient.DefaultRequestHeaders.Add("User-Agent", "VectorSearchApp"); } public async Task GenerateEmbeddingAsync(string text, CancellationToken cancellationToken = default) { var request = new { inputs = text }; var response = await _httpClient.PostAsJsonAsync(_modelName, request, cancellationToken); if (!response.IsSuccessStatusCode) { throw new InvalidOperationException($"Failed to generate embedding: {response.StatusCode}"); } var result = await response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken); if (result?.Length > 0 && result[0].Length > 0) { return result[0]; } throw new InvalidOperationException("Failed to generate embedding"); } }