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
}

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public InterruptTaskExecutionRoutingHandler(IServiceProvider services, ILogger<I
2424
{
2525
}
2626

27-
public async Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
27+
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
2828
{
2929
var result = new RoleDialogModel(AgentRole.User, inst.Reason)
3030
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ResponseToUserRoutingHandler(IServiceProvider services, ILogger<ResponseT
2424
{
2525
}
2626

27-
public async Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
27+
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
2828
{
2929
var result = new RoleDialogModel(AgentRole.Assistant, inst.Answer)
3030
{

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ public RetrieveDataFromAgentRoutingHandler(IServiceProvider services, ILogger<Re
2727
{
2828
}
2929

30-
public async Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
30+
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
3131
{
32-
if (string.IsNullOrEmpty(inst.AgentName))
33-
{
34-
inst = await GetNextInstructionFromReasoner($"What's the next step? your response must have agent name.");
35-
}
36-
3732
// Retrieve information from specific agent
3833
var db = _services.GetRequiredService<IBotSharpRepository>();
3934
var record = db.Agents.First(x => x.Name.ToLower() == inst.AgentName.ToLower());
40-
var response = await InvokeAgent(record.Id);
35+
var response = await routing.InvokeAgent(record.Id);
4136

4237
inst.Answer = response.Content;
4338

@@ -60,7 +55,7 @@ public async Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
6055
_router.Instruction += $"\r\n{AgentRole.Function}: {response.Content}";
6156

6257
// Got the response from agent, then send to reasoner again to make the decision
63-
inst = await GetNextInstructionFromReasoner($"What's the next step based on user's original goal and function result?");
58+
// inst = await GetNextInstructionFromReasoner($"What's the next step based on user's original goal and function result?");
6459

6560
return null;
6661
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public RouteToAgentRoutingHandler(IServiceProvider services, ILogger<RouteToAgen
2727
{
2828
}
2929

30-
public async Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
30+
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
3131
{
3232
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == inst.Function);
3333
var message = new RoleDialogModel(AgentRole.Function, inst.Question)
@@ -41,7 +41,7 @@ public async Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
4141

4242
var ret = await function.Execute(message);
4343

44-
var result = await InvokeAgent(message.CurrentAgentId);
44+
var result = await routing.InvokeAgent(message.CurrentAgentId);
4545
result.ExecutionData = result.ExecutionData ?? message.ExecutionData;
4646

4747
return result;

0 commit comments

Comments
 (0)