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

91 lines
3.2 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.Agents.Models;
2023-10-24 21:38:34 +00:00
using BotSharp.Abstraction.MLTasks.Settings;
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Routing;
public partial class RoutingService
{
const int MAXIMUM_RECURSION_DEPTH = 3;
private int _currentRecursionDepth = 0;
2023-10-30 16:48:18 +00:00
public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs)
{
_currentRecursionDepth++;
if (_currentRecursionDepth > MAXIMUM_RECURSION_DEPTH)
{
_logger.LogWarning($"Current recursive call depth greater than {MAXIMUM_RECURSION_DEPTH}, which will cause unexpected result.");
return false;
}
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(agentId);
2023-10-24 21:38:34 +00:00
var settings = _services.GetRequiredService<ChatCompletionSetting>();
var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider: settings.Provider, model: settings.Model);
2023-10-30 16:48:18 +00:00
2023-11-01 21:30:39 +00:00
var message = dialogs.Last();
var response = chatCompletion.GetChatCompletions(agent, dialogs);
2023-09-27 20:49:44 +00:00
if (response.Role == AgentRole.Function)
{
2023-11-01 21:30:39 +00:00
message = RoleDialogModel.From(message,
role: AgentRole.Function);
message.FunctionName = response.FunctionName;
message.FunctionArgs = response.FunctionArgs;
await InvokeFunction(agent, message, dialogs);
}
else
{
2023-11-01 21:30:39 +00:00
dialogs.Add(RoleDialogModel.From(message,
role: AgentRole.Assistant,
content: response.Content));
}
return true;
}
2023-10-30 16:48:18 +00:00
private async Task<bool> InvokeFunction(Agent agent, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
// execute function
// Save states
2023-11-01 01:48:12 +00:00
var states = _services.GetRequiredService<IConversationStateService>();
states.SaveStateByArgs(message.FunctionArgs?.JsonContent<JsonDocument>());
var conversationService = _services.GetRequiredService<IConversationService>();
// Call functions
await conversationService.CallFunctions(message);
// Pass execution result to LLM to get response
if (!message.StopCompletion)
{
// Find response template
var templateService = _services.GetRequiredService<IResponseTemplateService>();
var responseTemplate = await templateService.RenderFunctionResponse(agent.Id, message);
if (!string.IsNullOrEmpty(responseTemplate))
2023-09-27 20:49:44 +00:00
{
2023-10-30 20:06:45 +00:00
dialogs.Add(RoleDialogModel.From(message,
role: AgentRole.Assistant,
content: responseTemplate));
2023-09-27 20:49:44 +00:00
}
else
{
2023-10-30 16:48:18 +00:00
// Save to memory dialogs
2023-10-30 20:06:45 +00:00
dialogs.Add(RoleDialogModel.From(message,
role: AgentRole.Function,
content: message.Content));
2023-10-30 16:48:18 +00:00
// Send to LLM
await InvokeAgent(agent.Id, dialogs);
2023-09-27 20:49:44 +00:00
}
}
2023-10-30 19:27:12 +00:00
else
{
2023-10-30 20:06:45 +00:00
dialogs.Add(RoleDialogModel.From(message,
role: AgentRole.Assistant,
content: message.Content));
2023-10-30 19:27:12 +00:00
}
2023-10-30 16:48:18 +00:00
return true;
}
}