Skip to content

Commit 5c9135c

Browse files
authored
Merge pull request #309 from hchen2020/master
SQL Driver
2 parents 98cada4 + 1a0e5be commit 5c9135c

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
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,21 @@ public async Task<RoleDialogModel> InstructLoop(RoleDialogModel message)
106106
#else
107107
_logger.LogInformation($"*** Next Instruction *** {inst}");
108108
#endif
109-
await planner.AgentExecuting(_router, inst, message);
109+
await planner.AgentExecuting(_router, inst, message, dialogs);
110110

111111
// Handover to Task Agent
112-
if (inst.HideDialogContext)
112+
if (inst.HandleDialogsByPlanner)
113113
{
114-
var dialogWithoutContext = new List<RoleDialogModel>
115-
{
116-
new RoleDialogModel(AgentRole.User, inst.Response)
117-
};
114+
var dialogWithoutContext = planner.BeforeHandleContext(inst, message, dialogs);
118115
response = await executor.Execute(this, inst, message, dialogWithoutContext);
119-
dialogs.AddRange(dialogWithoutContext.Skip(1));
116+
planner.AfterHandleContext(dialogs, dialogWithoutContext);
120117
}
121118
else
122119
{
123120
response = await executor.Execute(this, inst, message, dialogs);
124121
}
125122

126-
await planner.AgentExecuted(_router, inst, response);
123+
await planner.AgentExecuted(_router, inst, response, dialogs);
127124
}
128125

129126
return response;

src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BotSharp.Abstraction.Knowledges.Models;
22
using BotSharp.Abstraction.Knowledges.Settings;
3-
using Microsoft.AspNetCore.Http;
43

54
namespace BotSharp.OpenAPI.Controllers;
65

@@ -17,6 +16,13 @@ public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvi
1716
_services = services;
1817
}
1918

19+
[HttpPost("/knowledge-base/embed")]
20+
public async Task EmbedKnowledge()
21+
{
22+
var chunks = await _knowledgeService.CollectChunkedKnowledge();
23+
await _knowledgeService.EmbedKnowledge(chunks);
24+
}
25+
2026
[HttpGet("/knowledge/{agentId}")]
2127
public async Task<List<RetrievedResult>> RetrieveKnowledge([FromRoute] string agentId, [FromQuery(Name = "q")] string question)
2228
{

src/Plugins/BotSharp.Plugin.KnowledgeBase/BotSharp.Plugin.KnowledgeBase.csproj

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<None Remove="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\agent.json" />
21-
<None Remove="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\functions.json" />
22-
<None Remove="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\instruction.liquid" />
23-
</ItemGroup>
24-
25-
<ItemGroup>
26-
<Content Include="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\agent.json">
27-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
28-
</Content>
29-
<Content Include="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\functions.json">
30-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
31-
</Content>
32-
<Content Include="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\instruction.liquid">
33-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
34-
</Content>
35-
</ItemGroup>
36-
37-
<ItemGroup>
38-
<PackageReference Include="PdfPig" Version="0.1.9-alpha-20240121-04fc8" />
20+
<PackageReference Include="PdfPig" Version="0.1.9-alpha-20240208-19734" />
3921
<PackageReference Include="TensorFlow.Keras" Version="0.15.0" />
4022
</ItemGroup>
4123

src/Plugins/BotSharp.Plugin.KnowledgeBase/Functions/SearchKnowledgesFn.cs

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

src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class KnowledgeBasePlugin : IBotSharpPlugin
1010
public string Name => "Knowledge Base";
1111
public string Description => "Embedding private data and feed them into LLM in the conversation.";
1212
public string IconUrl => "https://cdn-icons-png.flaticon.com/512/9592/9592995.png";
13-
public string[] AgentIds => new[] { "f5679799-ba89-4fef-936a-bcc311e5f14d" };
1413

1514
public void RegisterDI(IServiceCollection services, IConfiguration config)
1615
{

src/Plugins/BotSharp.Plugin.KnowledgeBase/LlmContexts/KnowledgeContextIn.cs

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

src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace BotSharp.Plugin.KnowledgeBase.Services;
22

3-
public class KnowledgeService : IKnowledgeService
3+
public partial class KnowledgeService : IKnowledgeService
44
{
55
private readonly IServiceProvider _services;
66
private readonly KnowledgeBaseSettings _settings;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace BotSharp.Plugin.KnowledgeBase.Services;
2+
3+
public partial class KnowledgeService
4+
{
5+
public async Task<List<KnowledgeChunk>> CollectChunkedKnowledge()
6+
{
7+
throw new NotImplementedException();
8+
}
9+
10+
public async Task EmbedKnowledge(List<KnowledgeChunk> chunks)
11+
{
12+
throw new NotImplementedException();
13+
}
14+
}

src/Plugins/BotSharp.Plugin.KnowledgeBase/data/agents/f5679799-ba89-4fef-936a-bcc311e5f14d/agent.json

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

src/Plugins/BotSharp.Plugin.KnowledgeBase/data/agents/f5679799-ba89-4fef-936a-bcc311e5f14d/functions.json

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

src/Plugins/BotSharp.Plugin.KnowledgeBase/data/agents/f5679799-ba89-4fef-936a-bcc311e5f14d/instruction.liquid

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535

3636
<ItemGroup>
3737
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
38+
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
39+
</ItemGroup>
40+
41+
<ItemGroup>
42+
<Folder Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\" />
3843
</ItemGroup>
3944

4045
</Project>

0 commit comments

Comments
 (0)