2024-01-23 23:14:57 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
|
|
|
|
using BotSharp.Abstraction.Functions.Models;
|
2024-02-02 22:36:05 +00:00
|
|
|
using BotSharp.Abstraction.MLTasks;
|
2024-01-23 23:14:57 +00:00
|
|
|
using BotSharp.Abstraction.Routing;
|
|
|
|
|
using BotSharp.Abstraction.Routing.Models;
|
2024-01-29 18:08:49 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Planning;
|
2024-01-23 23:14:57 +00:00
|
|
|
using BotSharp.Abstraction.Templating;
|
2024-02-01 18:32:16 +00:00
|
|
|
using System.Drawing;
|
2024-01-23 23:14:57 +00:00
|
|
|
|
2024-01-29 18:08:49 +00:00
|
|
|
namespace BotSharp.Core.Routing.Planning;
|
2024-01-23 23:14:57 +00:00
|
|
|
|
|
|
|
|
public class SequentialPlanner : IPlaner
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2024-02-01 18:32:16 +00:00
|
|
|
public bool HideDialogContext => true;
|
2024-02-02 04:16:57 +00:00
|
|
|
public int MaxLoopCount => 100;
|
|
|
|
|
private FunctionCallFromLlm _lastInst;
|
2024-02-01 18:32:16 +00:00
|
|
|
|
2024-01-23 23:14:57 +00:00
|
|
|
public SequentialPlanner(IServiceProvider services, ILogger<NaivePlanner> logger)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 04:16:57 +00:00
|
|
|
public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs)
|
2024-01-23 23:14:57 +00:00
|
|
|
{
|
2024-02-02 04:16:57 +00:00
|
|
|
var decomposation = await GetDecomposedStepAsync(router, messageId, dialogs);
|
|
|
|
|
if (decomposation.TotalRemainingSteps > 0 && _lastInst != null)
|
|
|
|
|
{
|
|
|
|
|
_lastInst.Response = decomposation.Description;
|
2024-02-08 21:39:56 +00:00
|
|
|
_lastInst.Reason = $"Having {decomposation.TotalRemainingSteps} steps left.";
|
2024-02-02 04:16:57 +00:00
|
|
|
return _lastInst;
|
|
|
|
|
}
|
2024-02-06 05:58:38 +00:00
|
|
|
else if (decomposation.TotalRemainingSteps == 0 || decomposation.ShouldStop)
|
2024-02-05 04:22:43 +00:00
|
|
|
{
|
2024-02-08 21:39:56 +00:00
|
|
|
if (!string.IsNullOrEmpty(decomposation.StopReason))
|
2024-02-05 04:22:43 +00:00
|
|
|
{
|
2024-02-08 21:39:56 +00:00
|
|
|
// Tell router all steps are done
|
|
|
|
|
dialogs.Add(new RoleDialogModel(AgentRole.Assistant, decomposation.StopReason)
|
|
|
|
|
{
|
|
|
|
|
CurrentAgentId = router.Id,
|
|
|
|
|
MessageId = messageId
|
|
|
|
|
});
|
|
|
|
|
router.TemplateDict["conversation"] = router.TemplateDict["conversation"].ToString().TrimEnd() +
|
|
|
|
|
$"\r\n{router.Name}: {decomposation.StopReason}";
|
|
|
|
|
}
|
2024-02-05 04:22:43 +00:00
|
|
|
}
|
2024-02-02 04:16:57 +00:00
|
|
|
|
2024-01-23 23:14:57 +00:00
|
|
|
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);
|
2024-02-02 04:16:57 +00:00
|
|
|
dialogs = new List<RoleDialogModel>
|
2024-01-23 23:14:57 +00:00
|
|
|
{
|
|
|
|
|
new RoleDialogModel(AgentRole.User, next)
|
|
|
|
|
{
|
2024-02-01 18:32:16 +00:00
|
|
|
FunctionName = nameof(SequentialPlanner),
|
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++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 04:16:57 +00:00
|
|
|
if (decomposation.TotalRemainingSteps > 0)
|
|
|
|
|
{
|
|
|
|
|
inst.Response = decomposation.Description;
|
|
|
|
|
inst.Reason = $"{decomposation.TotalRemainingSteps} steps left.";
|
|
|
|
|
inst.HideDialogContext = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_lastInst = inst;
|
2024-01-23 23:14:57 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2024-01-29 18:08:49 +00:00
|
|
|
|
2024-01-23 23:14:57 +00:00
|
|
|
// 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>
|
|
|
|
|
{
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-01 18:32:16 +00:00
|
|
|
|
|
|
|
|
public async Task<DecomposedStep> GetDecomposedStepAsync(Agent router, string messageId, List<RoleDialogModel> dialogs)
|
|
|
|
|
{
|
|
|
|
|
var systemPrompt = GetDecomposeTaskPrompt(router);
|
|
|
|
|
|
|
|
|
|
var inst = new DecomposedStep();
|
|
|
|
|
|
2024-02-02 22:36:05 +00:00
|
|
|
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
|
|
|
|
|
var model = llmProviderService.GetProviderModel("azure-openai", "gpt-4");
|
|
|
|
|
|
2024-02-01 18:32:16 +00:00
|
|
|
// chat completion
|
|
|
|
|
var completion = CompletionProvider.GetChatCompletion(_services,
|
2024-02-02 22:36:05 +00:00
|
|
|
provider: "azure-openai",
|
|
|
|
|
model: model.Name);
|
2024-02-01 18:32:16 +00:00
|
|
|
|
|
|
|
|
int retryCount = 0;
|
|
|
|
|
while (retryCount < 2)
|
|
|
|
|
{
|
|
|
|
|
string text = string.Empty;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var response = await completion.GetChatCompletions(new Agent
|
|
|
|
|
{
|
|
|
|
|
Id = router.Id,
|
|
|
|
|
Name = nameof(SequentialPlanner),
|
|
|
|
|
Instruction = systemPrompt
|
|
|
|
|
}, dialogs);
|
|
|
|
|
|
|
|
|
|
text = response.Content;
|
|
|
|
|
inst = response.Content.JsonContent<DecomposedStep>();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError($"{ex.Message}: {text}");
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
retryCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetDecomposeTaskPrompt(Agent router)
|
|
|
|
|
{
|
|
|
|
|
var template = router.Templates.First(x => x.Name == "planner_prompt.sequential.get_remaining_task").Content;
|
|
|
|
|
|
|
|
|
|
var render = _services.GetRequiredService<ITemplateRender>();
|
|
|
|
|
return render.Render(template, new Dictionary<string, object>
|
|
|
|
|
{
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-02 22:36:05 +00:00
|
|
|
|
|
|
|
|
public Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2024-01-23 23:14:57 +00:00
|
|
|
}
|