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

107 lines
4 KiB
C#
Raw Normal View History

2024-08-15 23:19:10 +00:00
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.VectorStorage.Models;
namespace BotSharp.Plugin.KnowledgeBase.Services;
public partial class KnowledgeService
{
2024-08-15 23:19:10 +00:00
public async Task<IEnumerable<string>> GetVectorCollections()
2024-08-13 18:26:57 +00:00
{
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>();
}
}
2024-08-15 23:19:10 +00:00
public async Task<StringIdPagedItems<VectorSearchResult>> GetVectorCollectionData(string collectionName, VectorFilter filter)
{
try
{
var db = GetVectorDb();
2024-08-15 03:46:01 +00:00
var pagedResult = await db.GetCollectionData(collectionName, filter);
2024-08-15 23:19:10 +00:00
return new StringIdPagedItems<VectorSearchResult>
2024-08-15 03:46:01 +00:00
{
Count = pagedResult.Count,
2024-08-15 23:19:10 +00:00
Items = pagedResult.Items.Select(x => VectorSearchResult.CopyFrom(x)),
2024-08-15 03:46:01 +00:00
NextId = pagedResult.NextId,
};
}
catch (Exception ex)
{
_logger.LogWarning($"Error when getting knowledge collection data ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
2024-08-15 23:19:10 +00:00
return new StringIdPagedItems<VectorSearchResult>();
}
}
2024-08-15 23:19:10 +00:00
public async Task<IEnumerable<VectorSearchResult>> SearchVectorKnowledge(string query, string collectionName, VectorSearchOptions options)
{
2024-08-13 18:30:06 +00:00
try
{
var textEmbedding = GetTextEmbedding();
2024-08-15 23:19:10 +00:00
var vector = await textEmbedding.GetVectorAsync(query);
2024-08-13 18:30:06 +00:00
// Vector search
var db = GetVectorDb();
2024-08-15 03:46:01 +00:00
var found = await db.Search(collectionName, vector, options.Fields, limit: options.Limit ?? 5, confidence: options.Confidence ?? 0.5f, withVector: options.WithVector);
2024-08-15 23:19:10 +00:00
var results = found.Select(x => VectorSearchResult.CopyFrom(x)).ToList();
2024-08-13 18:30:06 +00:00
return results;
}
catch (Exception ex)
{
2024-08-13 18:30:06 +00:00
_logger.LogWarning($"Error when searching knowledge ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
2024-08-15 23:19:10 +00:00
return new List<VectorSearchResult>();
}
}
public async Task<GraphSearchResult> SearchGraphKnowledge(string query, GraphSearchOptions options)
{
try
{
var db = GetGraphDb();
var found = await db.Search(query, options);
return new GraphSearchResult
{
Result = found.Result
};
}
catch (Exception ex)
{
_logger.LogWarning($"Error when searching graph {query}. {ex.Message}\r\n{ex.InnerException}");
return new GraphSearchResult();
}
}
public async Task<KnowledgeSearchResult> SearchKnowledge(string query, string collectionName, VectorSearchOptions vectorOptions, GraphSearchOptions graphOptions)
{
try
{
var textEmbedding = GetTextEmbedding();
var vector = await textEmbedding.GetVectorAsync(query);
var vectorDb = GetVectorDb();
var vectorRes = await vectorDb.Search(collectionName, vector, vectorOptions.Fields, limit: vectorOptions.Limit ?? 5,
confidence: vectorOptions.Confidence ?? 0.5f, withVector: vectorOptions.WithVector);
var graphDb = GetGraphDb();
var graphRes = await graphDb.Search(query, graphOptions);
return new KnowledgeSearchResult
{
VectorResult = vectorRes.Select(x => VectorSearchResult.CopyFrom(x)),
GraphResult = new GraphSearchResult { Result = graphRes.Result }
};
}
catch (Exception ex)
{
_logger.LogWarning($"Error when searching knowledge (vector collection: {collectionName}) {query}. {ex.Message}\r\n{ex.InnerException}");
return new KnowledgeSearchResult();
2024-08-13 18:30:06 +00:00
}
}
}