2024-08-09 21:03:29 +00:00
|
|
|
namespace BotSharp.Plugin.KnowledgeBase.Services;
|
|
|
|
|
|
|
|
|
|
public partial class KnowledgeService
|
|
|
|
|
{
|
2024-08-15 23:19:10 +00:00
|
|
|
public async Task FeedVectorKnowledge(string collectionName, KnowledgeCreationModel knowledge)
|
2024-08-09 21:03:29 +00:00
|
|
|
{
|
|
|
|
|
var index = 0;
|
|
|
|
|
var lines = _textChopper.Chop(knowledge.Content, new ChunkOption
|
|
|
|
|
{
|
|
|
|
|
Size = 1024,
|
|
|
|
|
Conjunction = 32,
|
|
|
|
|
SplitByWord = true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var db = GetVectorDb();
|
2024-08-22 20:23:22 +00:00
|
|
|
var textEmbedding = GetTextEmbedding(collectionName);
|
2024-08-09 21:03:29 +00:00
|
|
|
|
2024-08-22 20:23:22 +00:00
|
|
|
await db.CreateCollection(collectionName, textEmbedding.GetDimension());
|
2024-08-09 21:03:29 +00:00
|
|
|
foreach (var line in lines)
|
|
|
|
|
{
|
|
|
|
|
var vec = await textEmbedding.GetVectorAsync(line);
|
2024-08-20 18:32:22 +00:00
|
|
|
await db.Upsert(collectionName, Guid.NewGuid(), vec, line);
|
2024-08-09 21:03:29 +00:00
|
|
|
index++;
|
|
|
|
|
Console.WriteLine($"Saved vector {index}/{lines.Count}: {line}\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-23 16:25:44 +00:00
|
|
|
|
|
|
|
|
public async Task<bool> CreateVectorCollectionData(string collectionName, VectorCreateModel create)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(collectionName) || string.IsNullOrWhiteSpace(create.Text))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var textEmbedding = GetTextEmbedding(collectionName);
|
|
|
|
|
var vector = await textEmbedding.GetVectorAsync(create.Text);
|
|
|
|
|
|
|
|
|
|
var db = GetVectorDb();
|
|
|
|
|
var guid = Guid.NewGuid();
|
|
|
|
|
return await db.Upsert(collectionName, guid, vector, create.Text, create.Payload);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Error when creating vector collection data. {ex.Message}\r\n{ex.InnerException}");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-09 21:03:29 +00:00
|
|
|
}
|