Merge pull request #120 from hchen2020/master
Fix send message directly to agent without routing.
This commit is contained in:
commit
041e1fbf16
|
|
@ -2,16 +2,6 @@ namespace BotSharp.Abstraction.Agents.Settings;
|
|||
|
||||
public class AgentSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Router Agent Id
|
||||
/// </summary>
|
||||
public string RouterId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Reasoner Agent Id
|
||||
/// </summary>
|
||||
public string ReasonerId { get; set; }
|
||||
|
||||
public string DataDir { get; set; }
|
||||
public string TemplateFormat { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,5 +7,4 @@ public class ConversationSetting
|
|||
public bool EnableKnowledgeBase { get; set; }
|
||||
public bool ShowVerboseLog { get; set; }
|
||||
public int MaxRecursiveDepth { get; set; } = 3;
|
||||
public bool EnableReasoning { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
public class RoutingProfileRecord
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("agent_ids")]
|
||||
public string[] AgentIds { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
namespace BotSharp.Abstraction.Routing.Settings;
|
||||
|
||||
public class RoutingSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Router Agent Id
|
||||
/// </summary>
|
||||
public string RouterId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Reasoner Agent Id
|
||||
/// </summary>
|
||||
public string ReasonerId { get; set; }
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ using BotSharp.Core.Templating;
|
|||
using BotSharp.Core.Plugins.Knowledges.Services;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using BotSharp.Abstraction.Routing.Settings;
|
||||
|
||||
namespace BotSharp.Core;
|
||||
|
||||
|
|
@ -44,13 +45,14 @@ public static class BotSharpServiceCollectionExtensions
|
|||
services.AddSingleton<TemplateRender>();
|
||||
|
||||
// Register router
|
||||
var routingSettings = new RoutingSettings();
|
||||
config.Bind("Router", routingSettings);
|
||||
services.AddSingleton((IServiceProvider x) => routingSettings);
|
||||
|
||||
services.AddScoped<Router>();
|
||||
services.AddScoped<Reasoner>();
|
||||
services.AddScoped<IAgentRouting>(p =>
|
||||
{
|
||||
var setting = p.GetRequiredService<ConversationSetting>();
|
||||
return setting.EnableReasoning ? p.GetRequiredService<Reasoner>() : p.GetRequiredService<Router>();
|
||||
});
|
||||
services.AddScoped<IAgentRouting, Router>();
|
||||
services.AddScoped<Reasoner>();
|
||||
|
||||
// Register function callback
|
||||
services.AddScoped<IFunctionCallback, RouteToAgentFn>();
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ using BotSharp.Abstraction.Agents.Enums;
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Abstraction.Routing.Settings;
|
||||
using BotSharp.Core.Routing;
|
||||
|
||||
namespace BotSharp.Core.Conversations.Services;
|
||||
|
|
@ -33,8 +34,8 @@ public partial class ConversationService
|
|||
stateService.Load();
|
||||
stateService.SetState("channel", lastDialog.Channel);
|
||||
|
||||
var router = _services.GetRequiredService<IAgentRouting>();
|
||||
Agent agent = await router.LoadRouter();
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
Agent agent = await agentService.LoadAgent(agentId);
|
||||
|
||||
_logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
|
||||
|
||||
|
|
@ -69,7 +70,8 @@ public partial class ConversationService
|
|||
}
|
||||
|
||||
// reasoning
|
||||
if (_settings.EnableReasoning)
|
||||
var settings = _services.GetRequiredService<RoutingSettings>();
|
||||
if (settings.ReasonerId == agent.Id)
|
||||
{
|
||||
var simulator = _services.GetRequiredService<Simulator>();
|
||||
var reasonedContext = await simulator.Enter(agent, wholeDialogs);
|
||||
|
|
@ -96,7 +98,6 @@ public partial class ConversationService
|
|||
{
|
||||
if (reasonedContext.CurrentAgentId != agent.Id)
|
||||
{
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
agent = await agentService.LoadAgent(reasonedContext.CurrentAgentId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Routing.Settings;
|
||||
|
||||
namespace BotSharp.Core.Routing;
|
||||
|
||||
public class Reasoner : Router
|
||||
|
|
@ -6,7 +8,7 @@ public class Reasoner : Router
|
|||
|
||||
public Reasoner(IServiceProvider services,
|
||||
ILogger<Reasoner> logger,
|
||||
AgentSettings settings) : base(services, logger, settings)
|
||||
RoutingSettings settings) : base(services, logger, settings)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.Abstraction.Routing.Settings;
|
||||
using System.IO;
|
||||
using static Tensorflow.ApiDef.Types;
|
||||
|
||||
namespace BotSharp.Core.Routing;
|
||||
|
||||
|
|
@ -9,13 +9,13 @@ public class Router : IAgentRouting
|
|||
{
|
||||
protected readonly IServiceProvider _services;
|
||||
protected readonly ILogger _logger;
|
||||
protected readonly AgentSettings _settings;
|
||||
protected readonly RoutingSettings _settings;
|
||||
|
||||
public virtual string AgentId => _settings.RouterId;
|
||||
|
||||
public Router(IServiceProvider services,
|
||||
ILogger<Router> logger,
|
||||
AgentSettings settings)
|
||||
RoutingSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
|
|
@ -32,8 +32,24 @@ public class Router : IAgentRouting
|
|||
{
|
||||
var agentSettings = _services.GetRequiredService<AgentSettings>();
|
||||
var dbSettings = _services.GetRequiredService<MyDatabaseSettings>();
|
||||
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json");
|
||||
return JsonSerializer.Deserialize<RoutingRecord[]>(File.ReadAllText(filePath));
|
||||
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, _settings.RouterId, "route.json");
|
||||
var records = JsonSerializer.Deserialize<RoutingRecord[]>(File.ReadAllText(filePath));
|
||||
|
||||
// check if routing profile is specified
|
||||
filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var name = state.GetState("channel");
|
||||
var profiles = JsonSerializer.Deserialize<RoutingProfileRecord[]>(File.ReadAllText(filePath));
|
||||
var spcificedProfile = profiles.FirstOrDefault(x => x.Name == name);
|
||||
if (spcificedProfile != null)
|
||||
{
|
||||
records = records.Where(x => spcificedProfile.AgentIds.Contains(x.AgentId)).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
public RoutingRecord GetRecordByName(string name)
|
||||
|
|
|
|||
|
|
@ -40,7 +40,8 @@ public class ConversationController : ControllerBase, IApiAdapter
|
|||
[HttpPost("/conversation/{agentId}/{conversationId}")]
|
||||
public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
|
||||
[FromRoute] string conversationId,
|
||||
[FromBody] NewMessageModel input)
|
||||
[FromBody] NewMessageModel input,
|
||||
[FromQuery] string? channel = "openapi")
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
|
||||
|
|
@ -50,7 +51,7 @@ public class ConversationController : ControllerBase, IApiAdapter
|
|||
await conv.SendMessage(agentId, conversationId,
|
||||
new RoleDialogModel("user", input.Text)
|
||||
{
|
||||
Channel = "webapi"
|
||||
Channel = channel
|
||||
},
|
||||
async msg =>
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue