2023-09-29 18:08:42 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2023-10-24 21:38:34 +00:00
|
|
|
using BotSharp.Abstraction.MLTasks.Settings;
|
2023-09-27 12:51:15 +00:00
|
|
|
using BotSharp.Abstraction.Templating;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Routing;
|
|
|
|
|
|
|
|
|
|
public partial class RoutingService
|
|
|
|
|
{
|
2023-10-02 20:16:35 +00:00
|
|
|
const int MAXIMUM_RECURSION_DEPTH = 3;
|
2023-10-20 23:57:45 +00:00
|
|
|
private int _currentRecursionDepth = 0;
|
2023-10-30 16:48:18 +00:00
|
|
|
public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs)
|
2023-09-27 12:51:15 +00:00
|
|
|
{
|
2023-10-20 23:57:45 +00:00
|
|
|
_currentRecursionDepth++;
|
|
|
|
|
if (_currentRecursionDepth > MAXIMUM_RECURSION_DEPTH)
|
2023-09-27 12:51:15 +00:00
|
|
|
{
|
2023-10-20 23:57:45 +00:00
|
|
|
_logger.LogWarning($"Current recursive call depth greater than {MAXIMUM_RECURSION_DEPTH}, which will cause unexpected result.");
|
2023-10-27 20:36:26 +00:00
|
|
|
return false;
|
2023-09-27 12:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
RoleDialogModel response = chatCompletion.GetChatCompletions(agent, dialogs);
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-09-27 20:49:44 +00:00
|
|
|
if (response.Role == AgentRole.Function)
|
|
|
|
|
{
|
2023-10-30 16:48:18 +00:00
|
|
|
await InvokeFunction(agent, response, dialogs);
|
2023-10-20 23:57:45 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-30 16:48:18 +00:00
|
|
|
dialogs.Add(response);
|
2023-09-29 18:08:42 +00:00
|
|
|
}
|
2023-10-27 20:36:26 +00:00
|
|
|
|
|
|
|
|
return true;
|
2023-09-29 18:08:42 +00:00
|
|
|
}
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
private async Task<bool> InvokeFunction(Agent agent, RoleDialogModel message, List<RoleDialogModel> dialogs)
|
2023-09-29 18:08:42 +00:00
|
|
|
{
|
|
|
|
|
// execute function
|
|
|
|
|
// Save states
|
2023-10-27 20:36:26 +00:00
|
|
|
SaveStateByArgs(JsonSerializer.Deserialize<JsonDocument>(message.FunctionArgs));
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-09-29 18:08:42 +00:00
|
|
|
var conversationService = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
// Call functions
|
2023-10-27 20:36:26 +00:00
|
|
|
await conversationService.CallFunctions(message);
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-10-20 23:57:45 +00:00
|
|
|
// Pass execution result to LLM to get response
|
2023-10-27 20:36:26 +00:00
|
|
|
if (!message.StopCompletion)
|
2023-09-29 18:08:42 +00:00
|
|
|
{
|
|
|
|
|
// Find response template
|
|
|
|
|
var templateService = _services.GetRequiredService<IResponseTemplateService>();
|
2023-10-27 20:36:26 +00:00
|
|
|
var responseTemplate = await templateService.RenderFunctionResponse(agent.Id, message);
|
2023-09-29 18:08:42 +00:00
|
|
|
if (!string.IsNullOrEmpty(responseTemplate))
|
2023-09-27 20:49:44 +00:00
|
|
|
{
|
2023-10-30 19:27:12 +00:00
|
|
|
dialogs.Add(new RoleDialogModel(AgentRole.Assistant, responseTemplate)
|
|
|
|
|
{
|
|
|
|
|
CurrentAgentId = agent.Id,
|
|
|
|
|
MessageId = message.MessageId,
|
|
|
|
|
FunctionArgs = message.FunctionArgs,
|
|
|
|
|
FunctionName = message.FunctionName
|
|
|
|
|
});
|
2023-09-27 20:49:44 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-30 16:48:18 +00:00
|
|
|
// Save to memory dialogs
|
|
|
|
|
dialogs.Add(new RoleDialogModel(AgentRole.Function, message.Content)
|
|
|
|
|
{
|
2023-10-30 19:27:12 +00:00
|
|
|
CurrentAgentId = agent.Id,
|
|
|
|
|
MessageId = message.MessageId,
|
2023-10-30 16:48:18 +00:00
|
|
|
FunctionArgs = message.FunctionArgs,
|
|
|
|
|
FunctionName = message.FunctionName
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
{
|
|
|
|
|
dialogs.Add(new RoleDialogModel(AgentRole.Assistant, message.Content)
|
|
|
|
|
{
|
|
|
|
|
CurrentAgentId = agent.Id,
|
|
|
|
|
MessageId = message.MessageId,
|
|
|
|
|
FunctionArgs = message.FunctionArgs,
|
|
|
|
|
FunctionName = message.FunctionName,
|
|
|
|
|
StopCompletion = true
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-10-20 23:57:45 +00:00
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
return true;
|
2023-09-27 12:51:15 +00:00
|
|
|
}
|
|
|
|
|
}
|