2024-01-23 23:14:57 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
|
|
|
|
using BotSharp.Abstraction.Functions.Models;
|
|
|
|
|
using BotSharp.Abstraction.Planning;
|
|
|
|
|
using BotSharp.Abstraction.Routing;
|
|
|
|
|
using BotSharp.Abstraction.Routing.Models;
|
|
|
|
|
using BotSharp.Abstraction.Templating;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Planning;
|
|
|
|
|
|
|
|
|
|
public class SequentialPlanner : IPlaner
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public SequentialPlanner(IServiceProvider services, ILogger<NaivePlanner> logger)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId)
|
|
|
|
|
{
|
|
|
|
|
var next = GetNextStepPrompt(router);
|
|
|
|
|
|
|
|
|
|
var inst = new FunctionCallFromLlm();
|
|
|
|
|
|
|
|
|
|
// text completion
|
|
|
|
|
/*var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var instruction = agentService.RenderedInstruction(router);
|
|
|
|
|
var content = $"{instruction}\r\n###\r\n{next}";
|
|
|
|
|
content = content + "\r\nResponse: ";
|
|
|
|
|
var completion = CompletionProvider.GetTextCompletion(_services);*/
|
|
|
|
|
|
|
|
|
|
// chat completion
|
|
|
|
|
var completion = CompletionProvider.GetChatCompletion(_services,
|
|
|
|
|
provider: router?.LlmConfig?.Provider,
|
|
|
|
|
model: router?.LlmConfig?.Model);
|
|
|
|
|
|
|
|
|
|
int retryCount = 0;
|
|
|
|
|
while (retryCount < 3)
|
|
|
|
|
{
|
|
|
|
|
string text = string.Empty;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// text completion
|
|
|
|
|
// text = await completion.GetCompletion(content, router.Id, messageId);
|
|
|
|
|
var dialogs = new List<RoleDialogModel>
|
|
|
|
|
{
|
|
|
|
|
new RoleDialogModel(AgentRole.User, next)
|
|
|
|
|
{
|
2024-01-24 23:47:57 +00:00
|
|
|
FunctionName = nameof(NaivePlanner),
|
2024-01-23 23:14:57 +00:00
|
|
|
MessageId = messageId
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var response = await completion.GetChatCompletions(router, dialogs);
|
|
|
|
|
|
|
|
|
|
inst = response.Content.JsonContent<FunctionCallFromLlm>();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError($"{ex.Message}: {text}");
|
|
|
|
|
inst.Function = "response_to_user";
|
|
|
|
|
inst.Response = ex.Message;
|
|
|
|
|
inst.AgentName = "Router";
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
retryCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
// Set user content as Planner's question
|
|
|
|
|
message.FunctionName = inst.Function;
|
|
|
|
|
message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
var context = _services.GetRequiredService<RoutingContext>();
|
|
|
|
|
|
|
|
|
|
if (message.StopCompletion)
|
|
|
|
|
{
|
|
|
|
|
context.Empty();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handover to Router;
|
|
|
|
|
context.Pop();
|
|
|
|
|
|
|
|
|
|
var routing = _services.GetRequiredService<IRoutingService>();
|
|
|
|
|
routing.ResetRecursiveCounter();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetNextStepPrompt(Agent router)
|
|
|
|
|
{
|
|
|
|
|
var template = router.Templates.First(x => x.Name == "planner_prompt.sequential").Content;
|
|
|
|
|
|
|
|
|
|
var render = _services.GetRequiredService<ITemplateRender>();
|
|
|
|
|
return render.Render(template, new Dictionary<string, object>
|
|
|
|
|
{
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|