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

106 lines
3.7 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.Agents.Models;
2023-11-14 20:56:51 +00:00
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Routing;
public partial class RoutingService
{
private int _currentRecursionDepth = 0;
2023-10-30 16:48:18 +00:00
public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs)
{
2024-01-26 04:42:57 +00:00
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(agentId);
_currentRecursionDepth++;
2024-01-26 04:42:57 +00:00
if (_currentRecursionDepth > agent.LlmConfig.MaxRecursionDepth)
{
2024-01-26 04:42:57 +00:00
_logger.LogWarning($"Current recursive call depth greater than {agent.LlmConfig.MaxRecursionDepth}, which will cause unexpected result.");
return false;
}
2023-12-13 18:12:25 +00:00
var chatCompletion = CompletionProvider.GetChatCompletion(_services,
2023-12-15 17:54:44 +00:00
agentConfig: agent.LlmConfig);
2023-10-30 16:48:18 +00:00
2023-11-01 21:30:39 +00:00
var message = dialogs.Last();
2024-01-14 04:48:26 +00:00
var response = await 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;
2023-11-03 20:45:58 +00:00
message.CurrentAgentId = agent.Id;
2023-11-14 20:56:51 +00:00
await InvokeFunction(message, dialogs);
}
else
{
2023-11-03 21:32:27 +00:00
message = RoleDialogModel.From(message,
2023-11-01 21:30:39 +00:00
role: AgentRole.Assistant,
2023-11-03 21:32:27 +00:00
content: response.Content);
message.CurrentAgentId = agent.Id;
dialogs.Add(message);
}
return true;
}
2023-11-14 20:56:51 +00:00
private async Task<bool> InvokeFunction(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);
2024-01-06 22:24:22 +00:00
// Router selected the wrong agent, handle this excluding the agent
if (message.UnmatchedAgent)
{
// Save to memory dialogs
var msg = RoleDialogModel.From(message,
role: AgentRole.Function,
content: message.Content);
msg.UnmatchedAgent = true;
dialogs.Add(msg);
}
// Pass execution result to LLM to get response
2024-01-06 22:24:22 +00:00
else if (!message.StopCompletion)
{
2023-11-14 20:56:51 +00:00
var routing = _services.GetRequiredService<RoutingContext>();
// Find response template
var templateService = _services.GetRequiredService<IResponseTemplateService>();
var responseTemplate = await templateService.RenderFunctionResponse(message.CurrentAgentId, 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
dialogs.Add(RoleDialogModel.From(message,
role: AgentRole.Function,
2023-10-30 20:06:45 +00:00
content: message.Content));
2023-10-30 16:48:18 +00:00
// Send to Next LLM
var agentId = routing.GetCurrentAgentId();
2023-11-14 20:56:51 +00:00
await InvokeAgent(agentId, dialogs);
2023-09-27 20:49:44 +00:00
}
}
2023-10-30 19:27:12 +00:00
else
{
dialogs.Add(RoleDialogModel.From(message,
role: AgentRole.Assistant,
2023-10-30 20:06:45 +00:00
content: message.Content));
2023-10-30 19:27:12 +00:00
}
2023-10-30 16:48:18 +00:00
return true;
}
}