Lots of groundwork and the app runs but not currently working to generate embeddings.
This commit is contained in:
52
VectorSearchApp/Services/EmbeddingService.cs
Normal file
52
VectorSearchApp/Services/EmbeddingService.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user