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

89 lines
3 KiB
C#
Raw Normal View History

2023-06-27 23:36:50 +00:00
using BotSharp.Abstraction.Agents;
2023-06-26 23:08:24 +00:00
using BotSharp.Abstraction.VectorStorage;
2023-06-27 23:36:50 +00:00
using Microsoft.Extensions.DependencyInjection;
2023-06-18 02:56:22 +00:00
using QdrantCSharp;
using QdrantCSharp.Enums;
using QdrantCSharp.Models;
using System;
using System.Collections.Generic;
2023-06-27 23:36:50 +00:00
using System.IO;
2023-06-18 02:56:22 +00:00
using System.Linq;
using System.Threading.Tasks;
namespace BotSharp.Plugin.Qdrant;
public class QdrantDb : IVectorDb
{
private readonly QdrantHttpClient _client;
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;
2023-06-18 02:56:22 +00:00
_client = new QdrantHttpClient
(
url: _setting.Url,
apiKey: _setting.ApiKey
);
}
public async Task<List<string>> GetCollections()
{
// List all the collections
var collections = await _client.GetCollections();
return collections.Result.Collections.Select(x => x.Name).ToList();
}
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
2023-06-18 18:15:00 +00:00
await _client.CreateCollection(collectionName, new VectorParams(size: dim, distance: Distance.COSINE));
2023-06-27 23:36:50 +00:00
var agentService = _services.GetRequiredService<IAgentService>();
var agentDataDir = agentService.GetAgentDataDir(collectionName);
var knowledgePath = Path.Combine(agentDataDir, "knowledge.txt");
File.WriteAllLines(knowledgePath, new string[0]);
2023-06-18 02:56:22 +00:00
}
// Get collection info
var collectionInfo = await _client.GetCollection(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.");
}
}
2023-06-27 23:36:50 +00:00
public async Task Upsert(string collectionName, int id, float[] vector, string text)
2023-06-18 02:56:22 +00:00
{
// Insert vectors
await _client.Upsert(collectionName, points: new List<PointStruct>
{
new PointStruct(id: id, vector: vector)
});
2023-06-27 23:36:50 +00:00
// Store chunks in local file system
var agentService = _services.GetRequiredService<IAgentService>();
var agentDataDir = agentService.GetAgentDataDir(collectionName);
var knowledgePath = Path.Combine(agentDataDir, "knowledge.txt");
File.WriteAllLines(knowledgePath, new string[] { text });
2023-06-18 02:56:22 +00:00
}
2023-06-27 23:36:50 +00:00
public async Task<List<string>> Search(string collectionName, float[] vector, int limit = 10)
2023-06-18 02:56:22 +00:00
{
var result = await _client.Search(collectionName, vector, limit);
2023-06-27 23:36:50 +00:00
var agentService = _services.GetRequiredService<IAgentService>();
var agentDataDir = agentService.GetAgentDataDir(collectionName);
var knowledgePath = Path.Combine(agentDataDir, "knowledge.txt");
var texts = File.ReadAllLines(knowledgePath);
return result.Result.Select(x => texts[x.Id]).ToList();
2023-06-18 02:56:22 +00:00
}
}