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

99 lines
3 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;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Settings;
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 routerSetting = _services.GetRequiredService<RoutingSettings>();
var completion = CompletionProvider.GetChatCompletion(_services,
provider: routerSetting.Provider,
model: routerSetting.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
{
MessageId = messageId
}
2023-11-01 01:48:12 +00:00
};
response = 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;
}
2023-10-29 01:54:10 +00:00
public async Task<bool> AgentExecuting(FunctionCallFromLlm inst, RoleDialogModel message)
{
2023-10-30 16:48:18 +00:00
if (!string.IsNullOrEmpty(inst.AgentName))
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgents(inst.AgentName).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;
}
2023-10-28 20:59:26 +00:00
public async Task<bool> AgentExecuted(FunctionCallFromLlm inst, RoleDialogModel message)
{
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)
{
2023-11-03 14:16:16 +00:00
var template = router.Templates.First(x => x.Name == "next_step_prompt.hf_planner").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
}
}