BotSharp/src/Infrastructure/BotSharp.Core/Planning/HFPlanner.cs

100 lines
3.1 KiB
C#
Raw Normal View History

2023-10-28 20:59:26 +00:00
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Planning;
2023-10-29 01:54:10 +00:00
using BotSharp.Abstraction.Repositories;
2023-11-27 22:20:49 +00:00
using BotSharp.Abstraction.Repositories.Filters;
2023-10-29 01:54:10 +00:00
using BotSharp.Abstraction.Routing.Models;
2023-10-28 20:59:26 +00:00
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Planning;
2023-10-30 16:48:18 +00:00
/// <summary>
/// Human feedback based planner
/// </summary>
public class HFPlanner : IPlaner
2023-10-28 20:59:26 +00:00
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
2023-10-30 16:48:18 +00:00
public HFPlanner(IServiceProvider services, ILogger<HFPlanner> logger)
2023-10-28 20:59:26 +00:00
{
_services = services;
_logger = logger;
}
2023-10-30 16:48:18 +00:00
public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId)
2023-10-28 20:59:26 +00:00
{
var next = GetNextStepPrompt(router);
RoleDialogModel response = default;
var inst = new FunctionCallFromLlm();
var completion = CompletionProvider.GetChatCompletion(_services,
2023-12-13 18:12:25 +00:00
provider: router?.LlmConfig?.Provider,
model: router?.LlmConfig?.Model);
2023-10-28 20:59:26 +00:00
int retryCount = 0;
while (retryCount < 3)
{
try
{
2023-11-01 01:48:12 +00:00
var dialogs = new List<RoleDialogModel>
2023-10-28 20:59:26 +00:00
{
2023-10-29 01:54:10 +00:00
new RoleDialogModel(AgentRole.User, next)
2023-10-30 16:48:18 +00:00
{
2024-01-24 23:47:57 +00:00
FunctionName = nameof(NaivePlanner),
2023-10-30 16:48:18 +00:00
MessageId = messageId
}
2023-11-01 01:48:12 +00:00
};
2024-01-14 04:48:26 +00:00
response = await completion.GetChatCompletions(router, dialogs);
2023-10-28 20:59:26 +00:00
inst = response.Content.JsonContent<FunctionCallFromLlm>();
break;
}
catch (Exception ex)
{
_logger.LogError($"{ex.Message}: {response.Content}");
inst.Function = "response_to_user";
inst.Response = ex.Message;
inst.AgentName = "Router";
}
finally
{
retryCount++;
}
}
return inst;
}
2024-01-06 22:24:22 +00:00
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
2023-10-29 01:54:10 +00:00
{
2023-10-30 16:48:18 +00:00
if (!string.IsNullOrEmpty(inst.AgentName))
{
var db = _services.GetRequiredService<IBotSharpRepository>();
2023-11-27 22:20:49 +00:00
var filter = new AgentFilter { AgentName = inst.AgentName };
var agent = db.GetAgents(filter).FirstOrDefault();
2023-10-29 01:54:10 +00:00
2023-10-30 16:48:18 +00:00
var context = _services.GetRequiredService<RoutingContext>();
context.Push(agent.Id);
}
2023-10-29 01:54:10 +00:00
return true;
}
2024-01-06 22:24:22 +00:00
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
2023-10-28 20:59:26 +00:00
{
2023-10-29 01:54:10 +00:00
var context = _services.GetRequiredService<RoutingContext>();
2023-11-01 01:48:12 +00:00
context.Empty();
2023-10-28 20:59:26 +00:00
return true;
}
private string GetNextStepPrompt(Agent router)
{
2024-01-23 23:14:57 +00:00
var template = router.Templates.First(x => x.Name == "planner_prompt.hf").Content;
2023-10-28 20:59:26 +00:00
var render = _services.GetRequiredService<ITemplateRender>();
2023-11-01 01:48:12 +00:00
var prompt = render.Render(template, router.TemplateDict);
return prompt.Trim();
2023-10-28 20:59:26 +00:00
}
}