BotSharp/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Get.cs

52 lines
1.9 KiB
C#
Raw Normal View History

namespace BotSharp.Plugin.KnowledgeBase.Services;
public partial class KnowledgeService
{
2024-08-13 18:26:57 +00:00
public async Task<IEnumerable<string>> GetKnowledgeCollections()
{
try
{
var db = GetVectorDb();
return await db.GetCollections();
}
catch (Exception ex)
{
_logger.LogWarning($"Error when getting knowledge collections. {ex.Message}\r\n{ex.InnerException}");
return Enumerable.Empty<string>();
}
}
public async Task<StringIdPagedItems<KnowledgeCollectionData>> GetKnowledgeCollectionData(string collectionName, KnowledgeFilter filter)
{
try
{
var db = GetVectorDb();
return await db.GetCollectionData(collectionName, filter);
}
catch (Exception ex)
{
_logger.LogWarning($"Error when getting knowledge collection data ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
return new StringIdPagedItems<KnowledgeCollectionData>();
}
}
2024-08-09 22:39:54 +00:00
public async Task<IEnumerable<KnowledgeRetrievalResult>> SearchKnowledge(string collectionName, KnowledgeRetrievalOptions options)
{
var textEmbedding = GetTextEmbedding();
2024-08-09 22:39:54 +00:00
var vector = await textEmbedding.GetVectorAsync(options.Text);
// Vector search
var db = GetVectorDb();
2024-08-09 22:39:54 +00:00
var fields = !options.Fields.IsNullOrEmpty() ? options.Fields : new List<string> { KnowledgePayloadName.Text, KnowledgePayloadName.Answer };
var found = await db.Search(collectionName, vector, fields, limit: options.Limit ?? 5, confidence: options.Confidence ?? 0.5f, withVector: options.WithVector);
var results = found.Select(x => new KnowledgeRetrievalResult
{
Data = x.Data,
Score = x.Score,
Vector = x.Vector
}).ToList();
return results;
}
}