2023-09-29 18:08:42 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
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-09-27 12:51:15 +00:00
|
|
|
int CurrentRecursionDepth = 0;
|
|
|
|
|
public async Task<RoleDialogModel> InvokeAgent(string agentId)
|
|
|
|
|
{
|
|
|
|
|
CurrentRecursionDepth++;
|
|
|
|
|
if (CurrentRecursionDepth > MAXIMUM_RECURSION_DEPTH)
|
|
|
|
|
{
|
|
|
|
|
return Dialogs.Last();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var agent = await agentService.LoadAgent(agentId);
|
|
|
|
|
|
|
|
|
|
var chatCompletion = CompletionProvider.GetChatCompletion(_services);
|
2023-09-27 20:49:44 +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-09-29 18:08:42 +00:00
|
|
|
await InvokeFunction(agent, response);
|
|
|
|
|
}
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-09-29 18:08:42 +00:00
|
|
|
return response;
|
|
|
|
|
}
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-09-29 18:08:42 +00:00
|
|
|
private async Task InvokeFunction(Agent agent, RoleDialogModel response)
|
|
|
|
|
{
|
|
|
|
|
// execute function
|
|
|
|
|
// Save states
|
|
|
|
|
SaveStateByArgs(JsonSerializer.Deserialize<JsonDocument>(response.FunctionArgs));
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-09-29 18:08:42 +00:00
|
|
|
var conversationService = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
// Call functions
|
|
|
|
|
await conversationService.CallFunctions(response);
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-09-29 18:08:42 +00:00
|
|
|
if (string.IsNullOrEmpty(response.Content))
|
|
|
|
|
{
|
|
|
|
|
response.Content = response.ExecutionResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dialogs.Add(response);
|
|
|
|
|
|
|
|
|
|
if (!response.StopCompletion)
|
|
|
|
|
{
|
|
|
|
|
// Find response template
|
|
|
|
|
var templateService = _services.GetRequiredService<IResponseTemplateService>();
|
|
|
|
|
var responseTemplate = await templateService.RenderFunctionResponse(agent.Id, response);
|
|
|
|
|
if (!string.IsNullOrEmpty(responseTemplate))
|
2023-09-27 20:49:44 +00:00
|
|
|
{
|
2023-09-29 18:08:42 +00:00
|
|
|
response.Role = AgentRole.Assistant;
|
|
|
|
|
response.Content = responseTemplate;
|
2023-09-27 20:49:44 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-09-29 18:08:42 +00:00
|
|
|
var recursiveResponse = await InvokeAgent(response.CurrentAgentId);
|
|
|
|
|
response.Role = recursiveResponse.Role;
|
|
|
|
|
response.Content = recursiveResponse.Content;
|
|
|
|
|
response.ExecutionResult = recursiveResponse.ExecutionResult;
|
2023-10-10 21:53:04 +00:00
|
|
|
response.ExecutionData = recursiveResponse.ExecutionData ?? response.ExecutionData;
|
2023-09-29 18:08:42 +00:00
|
|
|
response.StopCompletion = recursiveResponse.StopCompletion;
|
2023-09-27 20:49:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-27 12:51:15 +00:00
|
|
|
}
|
|
|
|
|
}
|