BotSharp/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs

194 lines
5.8 KiB
C#
Raw Normal View History

2024-08-06 22:43:11 +00:00
using BotSharp.Abstraction.Utilities;
2024-08-15 23:19:10 +00:00
using BotSharp.Abstraction.VectorStorage.Models;
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,
2023-06-27 23:36:50 +00:00
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
}
public string Name => "Qdrant";
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
}
2024-08-08 22:15:51 +00:00
public async Task<IEnumerable<string>> GetCollections()
2023-06-18 02:56:22 +00:00
{
// 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
}
2024-08-15 23:19:10 +00:00
public async Task<StringIdPagedItems<VectorCollectionData>> GetCollectionData(string collectionName, VectorFilter filter)
2024-08-06 22:43:11 +00:00
{
var client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
if (!exist)
2024-08-06 22:43:11 +00:00
{
2024-08-15 23:19:10 +00:00
return new StringIdPagedItems<VectorCollectionData>();
2024-08-06 22:43:11 +00:00
}
2024-08-07 16:50:23 +00:00
var totalPointCount = await client.CountAsync(collectionName);
var response = await client.ScrollAsync(collectionName, limit: (uint)filter.Size,
2024-08-06 22:43:11 +00:00
offset: !string.IsNullOrWhiteSpace(filter.StartId) ? new PointId { Uuid = filter.StartId } : 0,
vectorsSelector: filter.WithVector);
2024-08-15 23:19:10 +00:00
var points = response?.Result?.Select(x => new VectorCollectionData
2024-08-06 22:43:11 +00:00
{
Id = x.Id?.Uuid ?? string.Empty,
2024-08-15 03:46:01 +00:00
Data = x.Payload.ToDictionary(x => x.Key, x => x.Value.StringValue),
2024-08-06 22:43:11 +00:00
Vector = filter.WithVector ? x.Vectors?.Vector?.Data?.ToArray() : null
2024-08-15 23:19:10 +00:00
})?.ToList() ?? new List<VectorCollectionData>();
2024-08-06 22:43:11 +00:00
2024-08-15 23:19:10 +00:00
return new StringIdPagedItems<VectorCollectionData>
2024-08-06 22:43:11 +00:00
{
Count = totalPointCount,
NextId = response?.NextPageOffset?.Uuid,
Items = points
};
}
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 client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
if (!exist)
2023-06-18 02:56:22 +00:00
{
// Create a new collection
await client.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
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.");
}
}
public async Task<bool> Upsert(string collectionName, Guid 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
{
Uuid = id.ToString()
2024-07-02 14:52:16 +00:00
},
Vectors = vector,
2024-08-06 22:43:11 +00:00
Payload =
2024-07-17 21:03:46 +00:00
{
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-08-15 23:19:10 +00:00
public async Task<IEnumerable<VectorCollectionData>> Search(string collectionName, float[] vector,
2024-08-15 03:46:01 +00:00
IEnumerable<string>? fields, int limit = 5, float confidence = 0.5f, bool withVector = false)
2023-06-18 02:56:22 +00:00
{
2024-08-15 23:19:10 +00:00
var results = new List<VectorCollectionData>();
2024-08-13 18:26:57 +00:00
2024-07-17 21:03:46 +00:00
var client = GetClient();
2024-08-13 18:26:57 +00:00
var exist = await DoesCollectionExist(client, collectionName);
if (!exist)
{
return results;
}
2023-06-27 23:36:50 +00:00
var points = await client.SearchAsync(collectionName,
vector,
limit: (ulong)limit,
scoreThreshold: confidence,
vectorsSelector: new WithVectorsSelector { Enable = withVector });
2024-08-15 03:46:01 +00:00
var pickFields = fields != null;
foreach (var point in points)
{
var data = new Dictionary<string, string>();
2024-08-15 03:46:01 +00:00
if (pickFields)
{
2024-08-15 03:46:01 +00:00
foreach (var field in fields)
{
2024-08-15 03:46:01 +00:00
if (point.Payload.ContainsKey(field))
{
data[field] = point.Payload[field].StringValue;
}
else
{
data[field] = "";
}
}
}
2024-08-15 03:46:01 +00:00
else
{
data = point.Payload.ToDictionary(k => k.Key, v => v.Value.StringValue);
}
2024-08-15 23:19:10 +00:00
results.Add(new VectorCollectionData
{
2024-08-15 03:46:01 +00:00
Id = point.Id.Uuid,
Data = data,
Score = point.Score,
Vector = withVector ? point.Vectors?.Vector?.Data?.ToArray() : null
});
}
return results;
2023-06-18 02:56:22 +00:00
}
2024-08-08 22:15:51 +00:00
public async Task<bool> DeleteCollectionData(string collectionName, Guid id)
2024-08-08 22:15:51 +00:00
{
var client = GetClient();
var result = await client.DeleteAsync(collectionName, id);
2024-08-08 22:15:51 +00:00
return result.Status == UpdateStatus.Completed;
}
private async Task<bool> DoesCollectionExist(QdrantClient client, string collectionName)
{
return await client.CollectionExistsAsync(collectionName);
}
2023-06-18 02:56:22 +00:00
}