BotSharp/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetNextInstruction.cs

151 lines
5 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.Functions.Models;
2023-10-16 20:07:07 +00:00
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
namespace BotSharp.Core.Routing;
public partial class RoutingService
{
public async Task<FunctionCallFromLlm> GetNextInstruction()
{
var content = GetNextStepPrompt();
2023-10-09 22:28:17 +00:00
RoleDialogModel response = default;
var args = new FunctionCallFromLlm();
2023-10-09 22:28:17 +00:00
if (_settings.UseTextCompletion)
{
var completion = CompletionProvider.GetTextCompletion(_services,
provider: _settings.Provider,
model: _settings.Model);
content = _routerInstance.Router.Instruction + "\r\n\r\n" + content + "\r\nResponse: ";
2023-10-12 00:59:09 +00:00
int retryCount = 0;
while (retryCount < 3)
{
try
{
var text = await completion.GetCompletion(content);
response = new RoleDialogModel(AgentRole.Assistant, text);
var pattern = @"\{(?:[^{}]|(?<open>\{)|(?<-open>\}))+(?(open)(?!))\}";
response.Content = Regex.Match(response.Content, pattern).Value;
args = JsonSerializer.Deserialize<FunctionCallFromLlm>(response.Content);
break;
}
catch (Exception ex)
{
_logger.LogError($"{ex.Message}: {response.Content}");
args.Function = "response_to_user";
args.Response = ex.Message;
2023-10-12 11:30:13 +00:00
args.AgentName = "Router";
2023-10-12 00:59:09 +00:00
content += "\r\nPlease response in JSON format.";
}
finally
{
retryCount++;
}
}
2023-10-09 22:28:17 +00:00
}
else
{
var completion = CompletionProvider.GetChatCompletion(_services,
provider: _settings.Provider,
model: _settings.Model);
int retryCount = 0;
while (retryCount < 3)
{
try
{
response = completion.GetChatCompletions(_routerInstance.Router, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, content)
});
var pattern = @"\{(?:[^{}]|(?<open>\{)|(?<-open>\}))+(?(open)(?!))\}";
response.Content = Regex.Match(response.Content, pattern).Value;
args = JsonSerializer.Deserialize<FunctionCallFromLlm>(response.Content);
break;
}
catch (Exception ex)
{
_logger.LogError($"{ex.Message}: {response.Content}");
args.Function = "response_to_user";
args.Response = ex.Message;
2023-10-12 11:30:13 +00:00
args.AgentName = "Router";
content += "\r\nPlease response in JSON format.";
}
finally
{
retryCount++;
}
}
2023-10-09 22:28:17 +00:00
}
#if DEBUG
Console.WriteLine(response.Content, Color.Gray);
#else
_logger.LogInformation(response.Content);
#endif
// Sometimes it populate malformed Function in Agent name
if (!string.IsNullOrEmpty(args.Function) && args.Function == args.AgentName)
{
args.Function = "route_to_agent";
_logger.LogWarning($"Captured LLM malformed response");
}
// Another case of malformed response
var agentService = _services.GetRequiredService<IAgentService>();
var agents = await agentService.GetAgents();
if (string.IsNullOrEmpty(args.AgentName) && agents.Select(x => x.Name).Contains(args.Function))
{
args.AgentName = args.Function;
args.Function = "route_to_agent";
_logger.LogWarning($"Captured LLM malformed response");
}
if (args.Arguments != null)
{
SaveStateByArgs(args.Arguments);
}
args.Function = args.Function.Split('.').Last();
#if DEBUG
Console.WriteLine($"*** Next Instruction *** {args}", Color.Green);
#else
_logger.LogInformation($"*** Next Instruction *** {args}");
#endif
return args;
}
#if !DEBUG
[MemoryCache(60 * 60)]
#endif
private string GetNextStepPrompt()
{
2023-10-16 20:07:07 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
// _routerInstance.Router.Templates.First(x => x.Name == "next_step_prompt").Content;
var template = db.GetAgentTemplate(_routerInstance.AgentId, "next_step_prompt");
// If enabled reasoning
// JsonSerializer.Serialize(new FunctionCallFromLlm());
var render = _services.GetRequiredService<ITemplateRender>();
return render.Render(template, new Dictionary<string, object>
{
2023-10-16 20:07:07 +00:00
{ "enabled_reasoning", _settings.EnableReasoning }
});
}
}