2024-07-17 21:03:46 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
|
|
|
|
using BotSharp.Core.Infrastructures;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.KnowledgeBase.Functions;
|
|
|
|
|
|
|
|
|
|
public class KnowledgeRetrievalFn : IFunctionCallback
|
|
|
|
|
{
|
|
|
|
|
public string Name => "knowledge_retrieval";
|
|
|
|
|
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly KnowledgeBaseSettings _settings;
|
|
|
|
|
|
|
|
|
|
public KnowledgeRetrievalFn(IServiceProvider services, KnowledgeBaseSettings settings)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_settings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
var args = JsonSerializer.Deserialize<ExtractedKnowledge>(message.FunctionArgs ?? "{}");
|
|
|
|
|
|
|
|
|
|
var embedding = _services.GetServices<ITextEmbedding>()
|
|
|
|
|
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.TextEmbedding));
|
|
|
|
|
|
|
|
|
|
var vector = await embedding.GetVectorsAsync(new List<string>
|
|
|
|
|
{
|
|
|
|
|
args.Question
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var vectorDb = _services.GetRequiredService<IVectorDb>();
|
|
|
|
|
|
|
|
|
|
var id = Utilities.HashTextMd5(args.Question);
|
|
|
|
|
var knowledges = await vectorDb.Search("lessen", vector[0], "answer");
|
|
|
|
|
|
2024-07-25 22:11:51 +00:00
|
|
|
if (knowledges.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
message.Content = string.Join("\r\n\r\n=====\r\n", knowledges);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message.Content = $"I didn't find any useful knowledge related to [{args.Question}]. \r\nCan you tell me the instruction and I'll memorize it.";
|
|
|
|
|
message.StopCompletion = true;
|
|
|
|
|
}
|
2024-07-17 21:03:46 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|