Skip to content

Commit 1a0e5be

Browse files
author
Haiping Chen
committed
SQL Driver
1 parent 98cada4 commit 1a0e5be

39 files changed

+548
-157
lines changed

src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionCallFromLlm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class FunctionCallFromLlm : RoutingArgs
1616
public bool ExecutingDirectly { get; set; }
1717

1818
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
19-
public bool HideDialogContext { get; set; }
19+
public bool HandleDialogsByPlanner { get; set; }
2020

2121
/// <summary>
2222
/// Router routed to a wrong agent.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using BotSharp.Abstraction.Knowledges.Models;
2+
3+
namespace BotSharp.Abstraction.Knowledges;
4+
5+
public interface IKnowledgeHook
6+
{
7+
Task<List<KnowledgeChunk>> CollectChunkedKnowledge();
8+
}

src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ namespace BotSharp.Abstraction.Knowledges;
44

55
public interface IKnowledgeService
66
{
7+
Task<List<KnowledgeChunk>> CollectChunkedKnowledge();
8+
Task EmbedKnowledge(List<KnowledgeChunk> chunks);
9+
710
Task Feed(KnowledgeFeedModel knowledge);
811
Task EmbedKnowledge(KnowledgeCreationModel knowledge);
912
Task<string> GetKnowledges(KnowledgeRetrievalModel retrievalModel);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.Knowledges.Models;
2+
3+
public class KnowledgeChunk
4+
{
5+
public string Id { get; set; }
6+
public string Name { get; set; }
7+
public string Content { get; set; }
8+
public string SourceAgentId { get; set; }
9+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Abstraction.Functions.Models;
2-
using BotSharp.Abstraction.Routing.Models;
32

43
namespace BotSharp.Abstraction.Routing.Planning;
54

@@ -10,7 +9,11 @@ namespace BotSharp.Abstraction.Routing.Planning;
109
public interface IPlaner
1110
{
1211
Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs);
13-
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message);
14-
Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message);
12+
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
13+
Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
14+
List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
15+
=> dialogs;
16+
bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
17+
=> true;
1518
int MaxLoopCount => 5;
1619
}

src/Infrastructure/BotSharp.Core/Routing/Planning/HFPlanner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
6767
return inst;
6868
}
6969

70-
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
70+
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
7171
{
7272
if (!string.IsNullOrEmpty(inst.AgentName))
7373
{
@@ -82,7 +82,7 @@ public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, R
8282
return true;
8383
}
8484

85-
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
85+
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
8686
{
8787
var context = _services.GetRequiredService<RoutingContext>();
8888
context.Empty();

src/Infrastructure/BotSharp.Core/Routing/Planning/NaivePlanner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
7676
return inst;
7777
}
7878

79-
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
79+
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
8080
{
8181
// Set user content as Planner's question
8282
message.FunctionName = inst.Function;
@@ -85,7 +85,7 @@ public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, R
8585
return true;
8686
}
8787

88-
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
88+
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
8989
{
9090
var context = _services.GetRequiredService<RoutingContext>();
9191
if (inst.UnmatchedAgent)

src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Amazon.SecurityToken.Model.Internal.MarshallTransformations;
12
using BotSharp.Abstraction.Agents.Models;
23
using BotSharp.Abstraction.Functions.Models;
34
using BotSharp.Abstraction.MLTasks;
@@ -102,14 +103,33 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
102103
{
103104
inst.Response = decomposation.Description;
104105
inst.Reason = $"{decomposation.TotalRemainingSteps} steps left.";
105-
inst.HideDialogContext = true;
106+
inst.HandleDialogsByPlanner = true;
106107
}
107108

108109
_lastInst = inst;
109110
return inst;
110111
}
111112

112-
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
113+
public List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
114+
{
115+
var taskAgentDialogs = new List<RoleDialogModel>
116+
{
117+
new RoleDialogModel(AgentRole.User, inst.Response)
118+
{
119+
MessageId = message.MessageId,
120+
}
121+
};
122+
123+
return taskAgentDialogs;
124+
}
125+
126+
public bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
127+
{
128+
dialogs.AddRange(taskAgentDialogs.Skip(1));
129+
return true;
130+
}
131+
132+
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
113133
{
114134
// Set user content as Planner's question
115135
message.FunctionName = inst.Function;
@@ -118,7 +138,7 @@ public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, R
118138
return true;
119139
}
120140

121-
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
141+
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
122142
{
123143
var context = _services.GetRequiredService<RoutingContext>();
124144

src/Infrastructure/BotSharp.Core/Routing/RoutingPlugin.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Routing;
22
using BotSharp.Abstraction.Routing.Models;
3+
using BotSharp.Abstraction.Routing.Planning;
34
using BotSharp.Abstraction.Routing.Settings;
45
using BotSharp.Abstraction.Settings;
56
using BotSharp.Core.Routing.Hooks;
@@ -34,8 +35,8 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
3435
services.AddScoped<IRoutingService, RoutingService>();
3536
services.AddScoped<IAgentHook, RoutingAgentHook>();
3637

37-
services.AddScoped<NaivePlanner>();
38-
services.AddScoped<HFPlanner>();
39-
services.AddScoped<SequentialPlanner>();
38+
services.AddScoped<IPlaner, NaivePlanner>();
39+
services.AddScoped<IPlaner, HFPlanner>();
40+
services.AddScoped<IPlaner, SequentialPlanner>();
4041
}
4142
}

src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetPlanner.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ public partial class RoutingService
99
{
1010
public IPlaner GetPlanner(Agent router)
1111
{
12-
var planner = router.RoutingRules.FirstOrDefault(x => x.Type == RuleType.Planner);
12+
var rule = router.RoutingRules.FirstOrDefault(x => x.Type == RuleType.Planner);
1313

14-
if (planner?.Field == nameof(HFPlanner))
15-
return _services.GetRequiredService<HFPlanner>();
16-
else if (planner?.Field == nameof(SequentialPlanner))
17-
return _services.GetRequiredService<SequentialPlanner>();
18-
else
14+
var planner = _services.GetServices<IPlaner>().
15+
FirstOrDefault(x => x.GetType().Name.EndsWith(rule.Field));
16+
17+
if (planner == null)
18+
{
19+
_logger.LogError($"Can't find specific planner named {rule.Field}");
1920
return _services.GetRequiredService<NaivePlanner>();
21+
}
22+
23+
return planner;
2024
}
2125
}

0 commit comments

Comments
 (0)