2023-11-14 01:25:25 +00:00
|
|
|
using Qdrant.Client;
|
|
|
|
|
using Qdrant.Client.Grpc;
|
2023-06-18 02:56:22 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.Qdrant;
|
|
|
|
|
|
|
|
|
|
public class QdrantDb : IVectorDb
|
|
|
|
|
{
|
2024-01-06 03:24:13 +00:00
|
|
|
private QdrantClient _client;
|
2023-06-18 02:56:22 +00:00
|
|
|
private readonly QdrantSetting _setting;
|
2023-06-27 23:36:50 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
|
|
|
|
|
public QdrantDb(QdrantSetting setting,
|
|
|
|
|
IServiceProvider services)
|
2023-06-18 02:56:22 +00:00
|
|
|
{
|
|
|
|
|
_setting = setting;
|
2023-06-27 23:36:50 +00:00
|
|
|
_services = services;
|
2024-01-06 03:24:13 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private QdrantClient GetClient()
|
|
|
|
|
{
|
|
|
|
|
if (_client == null)
|
|
|
|
|
{
|
|
|
|
|
_client = new QdrantClient
|
|
|
|
|
(
|
|
|
|
|
host: _setting.Url,
|
2024-07-02 14:52:16 +00:00
|
|
|
https: true,
|
2024-01-06 03:24:13 +00:00
|
|
|
apiKey: _setting.ApiKey
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return _client;
|
2023-06-18 02:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<string>> GetCollections()
|
|
|
|
|
{
|
|
|
|
|
// List all the collections
|
2024-07-02 14:52:16 +00:00
|
|
|
var collections = await GetClient().ListCollectionsAsync();
|
2023-11-14 01:25:25 +00:00
|
|
|
return collections.ToList();
|
2023-06-18 02:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-18 18:15:00 +00:00
|
|
|
public async Task CreateCollection(string collectionName, int dim)
|
2023-06-18 02:56:22 +00:00
|
|
|
{
|
|
|
|
|
var collections = await GetCollections();
|
|
|
|
|
if (!collections.Contains(collectionName))
|
|
|
|
|
{
|
|
|
|
|
// Create a new collection
|
2024-01-06 03:24:13 +00:00
|
|
|
await GetClient().CreateCollectionAsync(collectionName, new VectorParams()
|
2023-11-14 01:25:25 +00:00
|
|
|
{
|
|
|
|
|
Size = (ulong)dim,
|
|
|
|
|
Distance = Distance.Cosine
|
|
|
|
|
});
|
2023-06-18 02:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get collection info
|
2023-11-14 01:25:25 +00:00
|
|
|
var collectionInfo = await _client.GetCollectionInfoAsync(collectionName);
|
2023-06-18 18:15:00 +00:00
|
|
|
if (collectionInfo == null)
|
2023-06-18 02:56:22 +00:00
|
|
|
{
|
|
|
|
|
throw new Exception($"Create {collectionName} failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 21:03:46 +00:00
|
|
|
public async Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null)
|
2023-06-18 02:56:22 +00:00
|
|
|
{
|
|
|
|
|
// Insert vectors
|
2024-07-02 14:52:16 +00:00
|
|
|
var point = new PointStruct()
|
2023-06-18 02:56:22 +00:00
|
|
|
{
|
2024-07-02 14:52:16 +00:00
|
|
|
Id = new PointId()
|
2023-11-14 01:25:25 +00:00
|
|
|
{
|
2024-07-02 14:52:16 +00:00
|
|
|
Uuid = id
|
|
|
|
|
},
|
|
|
|
|
Vectors = vector,
|
2024-07-02 14:56:15 +00:00
|
|
|
|
2024-07-17 21:03:46 +00:00
|
|
|
Payload =
|
|
|
|
|
{
|
2024-08-06 19:01:26 +00:00
|
|
|
{ KnowledgePayloadName.Text, text }
|
2024-07-17 21:03:46 +00:00
|
|
|
}
|
2024-07-02 14:52:16 +00:00
|
|
|
};
|
2023-06-27 23:36:50 +00:00
|
|
|
|
2024-07-17 21:03:46 +00:00
|
|
|
if (payload != null)
|
2024-07-02 14:52:16 +00:00
|
|
|
{
|
2024-07-17 21:03:46 +00:00
|
|
|
foreach (var item in payload)
|
|
|
|
|
{
|
|
|
|
|
point.Payload.Add(item.Key, item.Value);
|
|
|
|
|
}
|
2024-07-02 14:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-17 21:03:46 +00:00
|
|
|
var client = GetClient();
|
|
|
|
|
|
|
|
|
|
var result = await client.UpsertAsync(collectionName, points: new List<PointStruct>
|
2024-07-02 14:52:16 +00:00
|
|
|
{
|
|
|
|
|
point
|
|
|
|
|
});
|
2024-07-17 21:03:46 +00:00
|
|
|
|
|
|
|
|
return result.Status == UpdateStatus.Completed;
|
2023-06-18 02:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-17 21:03:46 +00:00
|
|
|
public async Task<List<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 5, float confidence = 0.5f)
|
2023-06-18 02:56:22 +00:00
|
|
|
{
|
2024-07-17 21:03:46 +00:00
|
|
|
var client = GetClient();
|
|
|
|
|
var points = await client.SearchAsync(collectionName, vector,
|
|
|
|
|
limit: (ulong)limit,
|
|
|
|
|
scoreThreshold: confidence);
|
2023-06-27 23:36:50 +00:00
|
|
|
|
2024-07-17 21:03:46 +00:00
|
|
|
return points.Select(x => x.Payload[returnFieldName].StringValue).ToList();
|
2023-06-18 02:56:22 +00:00
|
|
|
}
|
|
|
|
|
}
|