Skip to content

Commit 2109a68

Browse files
Merge pull request #157 from Deep-Blue-2013/master
Add GetChatCompletions and clean routing code.
2 parents 38b84f7 + 3740a8c commit 2109a68

28 files changed

+569
-473
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentRouting.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public interface IChatCompletion
1313
/// <param name="model"></param>
1414
void SetModelName(string model);
1515

16+
RoleDialogModel GetChatCompletions(Agent agent,
17+
List<RoleDialogModel> conversations);
18+
1619
Task<bool> GetChatCompletionsAsync(Agent agent,
1720
List<RoleDialogModel> conversations,
1821
Func<RoleDialogModel, Task> onMessageReceived,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using BotSharp.Abstraction.Routing.Models;
2+
3+
namespace BotSharp.Abstraction.Routing;
4+
5+
public interface IRouterInstance
6+
{
7+
string AgentId { get; }
8+
Agent Router { get; }
9+
List<RoutingHandlerDef> GetHandlers();
10+
IRouterInstance Load();
11+
IRouterInstance WithDialogs(List<RoleDialogModel> dialogs);
12+
RoutingRule[] GetRulesByName(string name);
13+
}

src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHandler.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ public interface IRoutingHandler
1313

1414
void SetRouter(Agent router) { }
1515

16-
void SetDialogs(List<RoleDialogModel> dialogs) { }
16+
void SetDialogs(List<RoleDialogModel> dialogs) { }
1717

18-
Task<FunctionCallFromLlm> GetNextInstructionFromReasoner(string prompt)
19-
=> throw new NotImplementedException("");
20-
21-
Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
22-
=> throw new NotImplementedException("");
18+
Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst);
2319
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
using BotSharp.Abstraction.Functions.Models;
2+
13
namespace BotSharp.Abstraction.Routing;
24

35
public interface IRoutingService
46
{
5-
Agent LoadRouter();
67
List<RoleDialogModel> Dialogs { get; }
8+
Task<FunctionCallFromLlm> GetNextInstruction(string prompt);
9+
Task<RoleDialogModel> InvokeAgent(string agentId);
710
Task<RoleDialogModel> InstructLoop();
811
Task<RoleDialogModel> ExecuteOnce(Agent agent);
912
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using BotSharp.Abstraction.Routing.Settings;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace BotSharp.Abstraction.Routing;
5+
6+
public abstract class RoutingHandlerBase
7+
{
8+
protected Agent _router;
9+
protected readonly IServiceProvider _services;
10+
protected readonly ILogger _logger;
11+
protected RoutingSettings _settings;
12+
protected List<RoleDialogModel> _dialogs;
13+
14+
public RoutingHandlerBase(IServiceProvider services,
15+
ILogger logger,
16+
RoutingSettings settings)
17+
{
18+
_services = services;
19+
_logger = logger;
20+
_settings = settings;
21+
}
22+
23+
public void SetRouter(Agent router)
24+
{
25+
_router = router;
26+
}
27+
28+
public void SetDialogs(List<RoleDialogModel> dialogs)
29+
{
30+
_dialogs = dialogs;
31+
}
32+
}

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ join u in _db.Users on ua.UserId equals u.Id
2323
public async Task<Agent> GetAgent(string id)
2424
{
2525
var settings = _services.GetRequiredService<RoutingSettings>();
26-
var routingService = _services.GetRequiredService<IRoutingService>();
26+
var routerInstance = _services.GetRequiredService<IRouterInstance>();
2727
if (settings.RouterId == id)
2828
{
29-
return routingService.LoadRouter();
29+
return routerInstance.Load().Router;
3030
}
3131

3232
var profile = _db.GetAgent(id);

src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
5252
config.Bind("Router", routingSettings);
5353
services.AddSingleton((IServiceProvider x) => routingSettings);
5454

55-
services.AddScoped<IAgentRouting, Router>();
55+
services.AddScoped<IRouterInstance, RouterInstance>();
5656
services.AddScoped<IRoutingService, RoutingService>();
5757

5858
if (myDatabaseSettings.Default == "FileRepository")

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ public ContinueExecuteTaskRoutingHandler(IServiceProvider services, ILogger<Cont
2626
{
2727
}
2828

29-
public async Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
29+
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
3030
{
31-
var routing = _services.GetRequiredService<IAgentRouting>();
3231
var db = _services.GetRequiredService<IBotSharpRepository>();
3332
var record = db.Agents.First(x => x.Name.ToLower() == inst.AgentName.ToLower());
3433

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ConversationEndRoutingHandler(IServiceProvider services, ILogger<Conversa
2323
{
2424
}
2525

26-
public Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
26+
public Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
2727
{
2828
throw new NotImplementedException();
2929
}

0 commit comments

Comments
 (0)