BotSharp/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Knowledge.cs

61 lines
2.1 KiB
C#
Raw Normal View History

2024-09-09 16:32:31 +00:00
using BotSharp.Abstraction.VectorStorage.Models;
namespace BotSharp.Plugin.MongoStorage.Repository;
public partial class MongoRepository
{
2024-09-09 19:16:09 +00:00
public bool AddKnowledgeCollectionConfigs(List<VectorCollectionConfig> configs, bool reset = false)
2024-09-09 17:05:11 +00:00
{
var docs = configs?.Select(x => new KnowledgeCollectionConfigDocument
{
Id = Guid.NewGuid().ToString(),
Name = x.Name,
Type = x.Type,
TextEmbedding = KnowledgeEmbeddingConfigMongoModel.ToMongoModel(x.TextEmbedding),
CreateDate = x.CreateDate,
CreateUserId = x.CreateUserId,
})?.ToList() ?? new List<KnowledgeCollectionConfigDocument>();
2024-09-09 16:32:31 +00:00
2024-09-09 19:16:09 +00:00
if (reset)
{
var filter = Builders<KnowledgeCollectionConfigDocument>.Filter.Empty;
_dc.KnowledgeCollectionConfigs.DeleteMany(filter);
}
2024-09-09 17:05:11 +00:00
2024-09-09 19:16:09 +00:00
_dc.KnowledgeCollectionConfigs.InsertMany(docs);
2024-09-09 17:05:11 +00:00
return true;
}
2024-09-09 19:16:09 +00:00
public bool DeleteKnowledgeCollectionConfig(string collectionName)
2024-09-09 17:05:11 +00:00
{
2024-09-09 19:16:09 +00:00
if (string.IsNullOrWhiteSpace(collectionName)) return false;
2024-09-09 17:05:11 +00:00
var filter = Builders<KnowledgeCollectionConfigDocument>.Filter.Eq(x => x.Name, collectionName);
2024-09-09 19:16:09 +00:00
var deleted = _dc.KnowledgeCollectionConfigs.DeleteMany(filter);
return deleted.DeletedCount > 0;
}
2024-09-09 17:05:11 +00:00
2024-09-09 19:16:09 +00:00
public IEnumerable<VectorCollectionConfig> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter)
{
if (filter == null)
2024-09-09 17:05:11 +00:00
{
2024-09-09 19:16:09 +00:00
return Enumerable.Empty<VectorCollectionConfig>();
}
var builder = Builders<KnowledgeCollectionConfigDocument>.Filter;
var filters = new List<FilterDefinition<KnowledgeCollectionConfigDocument>> { builder.Empty };
var configs = _dc.KnowledgeCollectionConfigs.Find(Builders<KnowledgeCollectionConfigDocument>.Filter.And(filters)).ToList();
return configs.Select(x => new VectorCollectionConfig
{
Name = x.Name,
Type = x.Type,
TextEmbedding = KnowledgeEmbeddingConfigMongoModel.ToDomainModel(x.TextEmbedding),
CreateDate = x.CreateDate,
CreateUserId= x.CreateUserId
});
2024-09-09 17:05:11 +00:00
}
2024-09-09 16:32:31 +00:00
}