Lots of groundwork and the app runs but not currently working to generate embeddings.

This commit is contained in:
2026-01-13 21:48:42 -05:00
parent 5ce3a30588
commit 98ea050bd8
11 changed files with 431 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
using System.Net.Http.Json;
using VectorSearchApp.Configuration;
using VectorSearchApp.Models;
namespace VectorSearchApp.Services;
public interface IEmbeddingService
{
Task<float[]> 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<float[]> 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<float[][]>(cancellationToken: cancellationToken);
if (result?.Length > 0 && result[0].Length > 0)
{
return result[0];
}
throw new InvalidOperationException("Failed to generate embedding");
}
}