BotSharp/src/Infrastructure/BotSharp.Core/Routing/Handlers/RetrieveDataFromAgentRoutingHandler.cs

63 lines
2.1 KiB
C#
Raw Normal View History

2023-09-22 20:38:58 +00:00
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing;
2023-10-29 01:54:10 +00:00
using BotSharp.Abstraction.Routing.Models;
2023-09-22 20:38:58 +00:00
using BotSharp.Abstraction.Routing.Settings;
2023-10-29 01:54:10 +00:00
using BotSharp.Core.Planning;
2023-09-22 20:38:58 +00:00
namespace BotSharp.Core.Routing.Handlers;
2023-10-29 01:54:10 +00:00
/// <summary>
/// Retrieve information from specific agent
/// </summary>
2023-09-22 20:38:58 +00:00
public class RetrieveDataFromAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
{
public string Name => "retrieve_data_from_agent";
public string Description => "Retrieve data from appropriate agent.";
2023-10-28 20:59:26 +00:00
public List<ParameterPropertyDef> Parameters => new List<ParameterPropertyDef>
2023-09-22 20:38:58 +00:00
{
2023-10-28 20:59:26 +00:00
new ParameterPropertyDef("reason", "why retrieve data"),
2023-10-29 01:54:10 +00:00
new ParameterPropertyDef("question", "the question you will ask the agent to get the necessary data"),
new ParameterPropertyDef("next_action_agent", "agent that can handle the question"),
2023-10-30 16:48:18 +00:00
2023-10-28 20:59:26 +00:00
new ParameterPropertyDef("args", "required parameters extracted from question and hand over to the next agent")
{
Type = "object"
}
2023-09-22 20:38:58 +00:00
};
2023-10-28 20:59:26 +00:00
public List<string> Planers => new List<string>
{
2023-10-30 16:48:18 +00:00
nameof(HFPlanner)
2023-10-28 20:59:26 +00:00
};
2023-09-22 20:38:58 +00:00
public RetrieveDataFromAgentRoutingHandler(IServiceProvider services, ILogger<RetrieveDataFromAgentRoutingHandler> logger, RoutingSettings settings)
: base(services, logger, settings)
{
}
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
2023-09-22 20:38:58 +00:00
{
2023-10-29 01:54:10 +00:00
var context = _services.GetRequiredService<RoutingContext>();
2023-10-30 16:48:18 +00:00
var agentId = context.GetCurrentAgentId();
var dialogs = new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, inst.Question)
{
CurrentAgentId = agentId,
MessageId = message.MessageId
}
};
var ret = await routing.InvokeAgent(agentId, dialogs);
var response = dialogs.Last();
inst.Response = response.Content;
// Add final response to parent dialog
_dialogs.Add(response);
2023-09-22 20:38:58 +00:00
2023-10-29 01:54:10 +00:00
return ret;
2023-09-22 20:38:58 +00:00
}
}