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");
|
||||
}
|
||||
}
|
||||
69
VectorSearchApp/Services/QdrantService.cs
Normal file
69
VectorSearchApp/Services/QdrantService.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Qdrant.Client;
|
||||
using Qdrant.Client.Grpc;
|
||||
using VectorSearchApp.Configuration;
|
||||
using VectorSearchApp.Models;
|
||||
|
||||
namespace VectorSearchApp.Services;
|
||||
|
||||
public interface IQdrantService
|
||||
{
|
||||
Task InitializeCollectionAsync(CancellationToken cancellationToken = default);
|
||||
Task StoreAddressAsync(Address address, float[] embedding, CancellationToken cancellationToken = default);
|
||||
Task<List<AddressEmbedding>> SearchSimilarAddressesAsync(float[] queryEmbedding, int limit = 5, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public class QdrantService : IQdrantService
|
||||
{
|
||||
private readonly QdrantClient _client;
|
||||
private readonly string _collectionName;
|
||||
private readonly int _vectorDimension;
|
||||
|
||||
public QdrantService(QdrantConfiguration config, int vectorDimension)
|
||||
{
|
||||
_client = new QdrantClient(config.Host, config.GrpcPort);
|
||||
_collectionName = config.CollectionName;
|
||||
_vectorDimension = vectorDimension;
|
||||
}
|
||||
|
||||
public async Task InitializeCollectionAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var collections = await _client.ListCollectionsAsync(cancellationToken: cancellationToken);
|
||||
|
||||
if (!collections.Contains(_collectionName))
|
||||
{
|
||||
await _client.CreateCollectionAsync(_collectionName, new VectorParams
|
||||
{
|
||||
Size = (ulong)_vectorDimension,
|
||||
Distance = Distance.Cosine
|
||||
}, cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StoreAddressAsync(Address address, float[] embedding, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var point = new PointStruct
|
||||
{
|
||||
Id = new PointId { Uuid = address.Id.ToString() },
|
||||
Vectors = embedding,
|
||||
Payload =
|
||||
{
|
||||
["address"] = address.FullAddress,
|
||||
["created_at"] = address.CreatedAt.ToString("O")
|
||||
}
|
||||
};
|
||||
|
||||
await _client.UpsertAsync(_collectionName, new[] { point }, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<List<AddressEmbedding>> SearchSimilarAddressesAsync(float[] queryEmbedding, int limit = 5, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var results = await _client.SearchAsync(_collectionName, queryEmbedding, limit: (ulong)limit, cancellationToken: cancellationToken);
|
||||
|
||||
return results.Select(r => new AddressEmbedding
|
||||
{
|
||||
Id = Guid.Parse(r.Id.Uuid),
|
||||
FullAddress = r.Payload["address"].StringValue,
|
||||
Vector = Array.Empty<float>()
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user