2024-09-13 22:23:05 +00:00
|
|
|
using BotSharp.Abstraction.Knowledges.Models;
|
2024-09-10 19:02:25 +00:00
|
|
|
using BotSharp.Abstraction.VectorStorage.Models;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.MongoStorage.Repository;
|
|
|
|
|
|
|
|
|
|
public partial class MongoRepository
|
|
|
|
|
{
|
2024-09-13 22:23:05 +00:00
|
|
|
#region Configs
|
2024-09-10 19:02:25 +00:00
|
|
|
public bool AddKnowledgeCollectionConfigs(List<VectorCollectionConfig> configs, bool reset = false)
|
|
|
|
|
{
|
|
|
|
|
var filter = Builders<KnowledgeCollectionConfigDocument>.Filter.Empty;
|
|
|
|
|
var docs = configs?.Where(x => !string.IsNullOrWhiteSpace(x.Name))
|
|
|
|
|
.Select(x => new KnowledgeCollectionConfigDocument
|
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid().ToString(),
|
|
|
|
|
Name = x.Name,
|
|
|
|
|
Type = x.Type,
|
2024-09-12 16:51:23 +00:00
|
|
|
VectorStore = KnowledgeVectorStoreConfigMongoModel.ToMongoModel(x.VectorStore),
|
2024-09-10 19:02:25 +00:00
|
|
|
TextEmbedding = KnowledgeEmbeddingConfigMongoModel.ToMongoModel(x.TextEmbedding)
|
|
|
|
|
})?.ToList() ?? new List<KnowledgeCollectionConfigDocument>();
|
|
|
|
|
|
|
|
|
|
if (reset)
|
|
|
|
|
{
|
|
|
|
|
_dc.KnowledgeCollectionConfigs.DeleteMany(filter);
|
|
|
|
|
_dc.KnowledgeCollectionConfigs.InsertMany(docs);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update if collection already exists, otherwise insert.
|
|
|
|
|
var insertDocs = new List<KnowledgeCollectionConfigDocument>();
|
|
|
|
|
var updateDocs = new List<KnowledgeCollectionConfigDocument>();
|
|
|
|
|
|
|
|
|
|
var names = docs.Select(x => x.Name).ToList();
|
|
|
|
|
filter = Builders<KnowledgeCollectionConfigDocument>.Filter.In(x => x.Name, names);
|
|
|
|
|
var savedConfigs = _dc.KnowledgeCollectionConfigs.Find(filter).ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var doc in docs)
|
|
|
|
|
{
|
|
|
|
|
var found = savedConfigs.FirstOrDefault(x => x.Name == doc.Name);
|
|
|
|
|
if (found != null)
|
|
|
|
|
{
|
|
|
|
|
found.Type = doc.Type;
|
2024-09-12 16:51:23 +00:00
|
|
|
found.VectorStore = doc.VectorStore;
|
2024-09-10 19:02:25 +00:00
|
|
|
found.TextEmbedding = doc.TextEmbedding;
|
|
|
|
|
updateDocs.Add(found);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
insertDocs.Add(doc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!insertDocs.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
_dc.KnowledgeCollectionConfigs.InsertMany(docs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!updateDocs.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
foreach (var doc in updateDocs)
|
|
|
|
|
{
|
|
|
|
|
filter = Builders<KnowledgeCollectionConfigDocument>.Filter.Eq(x => x.Id, doc.Id);
|
|
|
|
|
_dc.KnowledgeCollectionConfigs.ReplaceOne(filter, doc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeleteKnowledgeCollectionConfig(string collectionName)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(collectionName)) return false;
|
|
|
|
|
|
|
|
|
|
var filter = Builders<KnowledgeCollectionConfigDocument>.Filter.Eq(x => x.Name, collectionName);
|
|
|
|
|
var deleted = _dc.KnowledgeCollectionConfigs.DeleteMany(filter);
|
|
|
|
|
return deleted.DeletedCount > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<VectorCollectionConfig> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter)
|
|
|
|
|
{
|
|
|
|
|
if (filter == null)
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Empty<VectorCollectionConfig>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var builder = Builders<KnowledgeCollectionConfigDocument>.Filter;
|
|
|
|
|
var filters = new List<FilterDefinition<KnowledgeCollectionConfigDocument>> { builder.Empty };
|
|
|
|
|
|
|
|
|
|
// Apply filters
|
|
|
|
|
if (!filter.CollectionNames.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
filters.Add(builder.In(x => x.Name, filter.CollectionNames));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!filter.CollectionTypes.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
filters.Add(builder.In(x => x.Type, filter.CollectionTypes));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!filter.VectorStroageProviders.IsNullOrEmpty())
|
|
|
|
|
{
|
2024-09-12 16:51:23 +00:00
|
|
|
filters.Add(builder.In(x => x.VectorStore.Provider, filter.VectorStroageProviders));
|
2024-09-10 19:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get data
|
|
|
|
|
var configs = _dc.KnowledgeCollectionConfigs.Find(Builders<KnowledgeCollectionConfigDocument>.Filter.And(filters)).ToList();
|
|
|
|
|
|
|
|
|
|
return configs.Select(x => new VectorCollectionConfig
|
|
|
|
|
{
|
|
|
|
|
Name = x.Name,
|
|
|
|
|
Type = x.Type,
|
2024-09-12 16:51:23 +00:00
|
|
|
VectorStore = KnowledgeVectorStoreConfigMongoModel.ToDomainModel(x.VectorStore),
|
2024-09-10 19:02:25 +00:00
|
|
|
TextEmbedding = KnowledgeEmbeddingConfigMongoModel.ToDomainModel(x.TextEmbedding)
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-09-13 22:23:05 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Documents
|
|
|
|
|
public bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData)
|
|
|
|
|
{
|
|
|
|
|
if (metaData == null
|
|
|
|
|
|| string.IsNullOrWhiteSpace(metaData.Collection)
|
2024-09-17 17:50:39 +00:00
|
|
|
|| string.IsNullOrWhiteSpace(metaData.VectorStoreProvider))
|
2024-09-13 22:23:05 +00:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 17:50:39 +00:00
|
|
|
var doc = new KnowledgeCollectionFileMetaDocument
|
2024-09-13 22:23:05 +00:00
|
|
|
{
|
2024-09-14 00:11:41 +00:00
|
|
|
Id = Guid.NewGuid().ToString(),
|
2024-09-13 22:23:05 +00:00
|
|
|
Collection = metaData.Collection,
|
|
|
|
|
FileId = metaData.FileId,
|
|
|
|
|
FileName = metaData.FileName,
|
|
|
|
|
FileSource = metaData.FileSource,
|
|
|
|
|
ContentType = metaData.ContentType,
|
|
|
|
|
VectorStoreProvider = metaData.VectorStoreProvider,
|
|
|
|
|
VectorDataIds = metaData.VectorDataIds,
|
2024-09-18 15:35:28 +00:00
|
|
|
RefData = KnowledgeFileMetaRefMongoModel.ToMongoModel(metaData.RefData),
|
2024-09-13 22:23:05 +00:00
|
|
|
CreateDate = metaData.CreateDate,
|
|
|
|
|
CreateUserId = metaData.CreateUserId
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-17 17:50:39 +00:00
|
|
|
_dc.KnowledgeCollectionFileMeta.InsertOne(doc);
|
2024-09-13 22:23:05 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 17:50:39 +00:00
|
|
|
public bool DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStoreProvider, Guid? fileId = null)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(collectionName)
|
|
|
|
|
|| string.IsNullOrWhiteSpace(vectorStoreProvider))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var builder = Builders<KnowledgeCollectionFileMetaDocument>.Filter;
|
|
|
|
|
var filters = new List<FilterDefinition<KnowledgeCollectionFileMetaDocument>>()
|
|
|
|
|
{
|
|
|
|
|
builder.Eq(x => x.Collection, collectionName),
|
|
|
|
|
builder.Eq(x => x.VectorStoreProvider, vectorStoreProvider)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (fileId != null)
|
|
|
|
|
{
|
|
|
|
|
filters.Add(builder.Eq(x => x.FileId, fileId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var res = _dc.KnowledgeCollectionFileMeta.DeleteMany(builder.And(filters));
|
|
|
|
|
return res.DeletedCount > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 22:23:05 +00:00
|
|
|
public PagedItems<KnowledgeDocMetaData> GetKnowledgeBaseFileMeta(string collectionName, string vectorStoreProvider, KnowledgeFileFilter filter)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(collectionName)
|
|
|
|
|
|| string.IsNullOrWhiteSpace(vectorStoreProvider))
|
|
|
|
|
{
|
|
|
|
|
return new PagedItems<KnowledgeDocMetaData>();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 17:50:39 +00:00
|
|
|
var builder = Builders<KnowledgeCollectionFileMetaDocument>.Filter;
|
|
|
|
|
var docFilters = new List<FilterDefinition<KnowledgeCollectionFileMetaDocument>>()
|
2024-09-13 22:23:05 +00:00
|
|
|
{
|
|
|
|
|
builder.Eq(x => x.Collection, collectionName),
|
|
|
|
|
builder.Eq(x => x.VectorStoreProvider, vectorStoreProvider)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Apply filters
|
2024-09-19 16:43:49 +00:00
|
|
|
if (filter != null)
|
2024-09-13 22:23:05 +00:00
|
|
|
{
|
2024-09-19 16:43:49 +00:00
|
|
|
if (!filter.FileIds.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
docFilters.Add(builder.In(x => x.FileId, filter.FileIds));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!filter.FileSources.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
docFilters.Add(builder.In(x => x.FileSource, filter.FileSources));
|
|
|
|
|
}
|
2024-09-13 22:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var filterDef = builder.And(docFilters);
|
2024-09-17 17:50:39 +00:00
|
|
|
var sortDef = Builders<KnowledgeCollectionFileMetaDocument>.Sort.Descending(x => x.CreateDate);
|
|
|
|
|
var docs = _dc.KnowledgeCollectionFileMeta.Find(filterDef).Sort(sortDef).Skip(filter.Offset).Limit(filter.Size).ToList();
|
|
|
|
|
var count = _dc.KnowledgeCollectionFileMeta.CountDocuments(filterDef);
|
2024-09-13 22:23:05 +00:00
|
|
|
|
|
|
|
|
var files = docs?.Select(x => new KnowledgeDocMetaData
|
|
|
|
|
{
|
|
|
|
|
Collection = x.Collection,
|
|
|
|
|
FileId = x.FileId,
|
|
|
|
|
FileName = x.FileName,
|
|
|
|
|
FileSource = x.FileSource,
|
|
|
|
|
ContentType = x.ContentType,
|
|
|
|
|
VectorStoreProvider = x.VectorStoreProvider,
|
|
|
|
|
VectorDataIds = x.VectorDataIds,
|
2024-09-18 15:35:28 +00:00
|
|
|
RefData = KnowledgeFileMetaRefMongoModel.ToDomainModel(x.RefData),
|
2024-09-13 22:23:05 +00:00
|
|
|
CreateDate = x.CreateDate,
|
|
|
|
|
CreateUserId = x.CreateUserId
|
2024-09-18 15:35:28 +00:00
|
|
|
})?.ToList() ?? new();
|
2024-09-13 22:23:05 +00:00
|
|
|
|
|
|
|
|
return new PagedItems<KnowledgeDocMetaData>
|
|
|
|
|
{
|
|
|
|
|
Items = files,
|
|
|
|
|
Count = (int)count
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2024-09-10 19:02:25 +00:00
|
|
|
}
|