Skip to content

Commit 3ce6599

Browse files
authored
Merge pull request #612 from hchen2020/master
adjust two stage planner.
2 parents a37a80f + f0b6806 commit 3ce6599

File tree

25 files changed

+539
-108
lines changed

25 files changed

+539
-108
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentType.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ namespace BotSharp.Abstraction.Agents.Enums;
33
public class AgentType
44
{
55
/// <summary>
6-
/// Routing Agent
6+
/// Routing agent
77
/// </summary>
88
public const string Routing = "routing";
99

10-
public const string Evaluating = "evaluating";
10+
/// <summary>
11+
/// Planning agent
12+
/// </summary>
13+
public const string Planning = "plan";
14+
15+
public const string Evaluating = "evaluation";
1116

1217
/// <summary>
1318
/// Routable task agent with capability of interaction with external environment

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public class BuiltInAgentId
3131
/// Used by knowledgebase plugin to acquire domain knowledge
3232
/// </summary>
3333
public const string Learner = "01acc3e5-0af7-49e6-ad7a-a760bd12dc40";
34+
35+
/// <summary>
36+
/// Plan feasible implementation steps for complex problems
37+
/// </summary>
38+
public const string Planner = "282a7128-69a1-44b0-878c-a9159b88f3b9";
3439
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656

5757
<ItemGroup>
5858
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\database_knowledge.liquid" />
59-
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.summarize.liquid" />
6059
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json" />
6160
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instructions\instruction.liquid" />
6261
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json" />
@@ -75,9 +74,6 @@
7574
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.naive.liquid" />
7675
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.get_remaining_task.liquid" />
7776
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.liquid" />
78-
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.1st.plan.liquid" />
79-
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.plan.liquid" />
80-
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.task.liquid" />
8177
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\response_with_function.liquid" />
8278
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\translation_prompt.liquid" />
8379
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_file_prompt.liquid" />
@@ -119,15 +115,6 @@
119115
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\database_knowledge.liquid">
120116
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
121117
</Content>
122-
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.1st.plan.liquid">
123-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
124-
</Content>
125-
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.plan.liquid">
126-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
127-
</Content>
128-
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.task.liquid">
129-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
130-
</Content>
131118
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.get_remaining_task.liquid">
132119
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
133120
</Content>
@@ -140,9 +127,6 @@
140127
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.naive.liquid">
141128
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
142129
</Content>
143-
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.summarize.liquid">
144-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
145-
</Content>
146130
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\response_with_function.liquid">
147131
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
148132
</Content>

src/Infrastructure/BotSharp.Core/Routing/Planning/TwoStagePlanner/TwoStagePlanner.FirstStage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private async Task<FirstStagePlan[]> GetFirstStagePlanAsync(Agent router, string
4444

4545
private async Task<string> GetFirstStagePlanPrompt(Agent router)
4646
{
47-
var template = router.Templates.First(x => x.Name == "planner_prompt.two_stage.1st.plan").Content;
47+
var template = router.Templates.First(x => x.Name == "two_stage.1st.plan").Content;
4848
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
4949
{
5050
Parameters = new JsonDocument[]{ JsonDocument.Parse("{}") },
@@ -69,7 +69,7 @@ private async Task<string> GetFirstStagePlanPrompt(Agent router)
6969

7070
private string GetFirstStageNextPrompt(Agent router)
7171
{
72-
var template = router.Templates.First(x => x.Name == "planner_prompt.first_stage.next").Content;
72+
var template = router.Templates.First(x => x.Name == "first_stage.next").Content;
7373
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
7474
{
7575
});

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

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using BotSharp.Abstraction.Agents.Models;
2-
using BotSharp.Abstraction.Functions.Models;
3-
using BotSharp.Abstraction.Routing;
4-
using BotSharp.Abstraction.Routing.Models;
51
using BotSharp.Abstraction.Routing.Planning;
6-
using System.IO;
72

83
namespace BotSharp.Core.Routing.Planning;
94

@@ -28,25 +23,9 @@ public TwoStagePlanner(IServiceProvider services, ILogger<TwoStagePlanner> logge
2823

2924
public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs)
3025
{
31-
var tempDir = Path.Combine(Path.GetTempPath(), "botsharp", "cache");
3226
if (_plan1st.IsNullOrEmpty() && _plan2nd.IsNullOrEmpty())
3327
{
34-
Directory.CreateDirectory(tempDir);
35-
_md5 = Utilities.HashTextMd5($"{string.Join(".", dialogs.Where(x => x.Role == AgentRole.User))}{"botsharp"}");
36-
var filePath = Path.Combine(tempDir, $"{_md5}-1st.json");
37-
FirstStagePlan[] items = new FirstStagePlan[0];
38-
if (File.Exists(filePath))
39-
{
40-
var cache = File.ReadAllText(filePath);
41-
items = JsonSerializer.Deserialize<FirstStagePlan[]>(cache);
42-
}
43-
else
44-
{
45-
items = await GetFirstStagePlanAsync(router, messageId, dialogs);
46-
47-
var cache = JsonSerializer.Serialize(items);
48-
File.WriteAllText(filePath, cache);
49-
}
28+
FirstStagePlan[] items = await GetFirstStagePlanAsync(router, messageId, dialogs);
5029

5130
foreach (var item in items)
5231
{
@@ -61,20 +40,7 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
6140

6241
if (plan1.ContainMultipleSteps)
6342
{
64-
var filePath = Path.Combine(tempDir, $"{_md5}-2nd-{plan1.Step}.json");
65-
SecondStagePlan[] items = new SecondStagePlan[0];
66-
if (File.Exists(filePath))
67-
{
68-
var cache = File.ReadAllText(filePath);
69-
items = JsonSerializer.Deserialize<SecondStagePlan[]>(cache);
70-
}
71-
else
72-
{
73-
items = await GetSecondStagePlanAsync(router, messageId, plan1, dialogs);
74-
75-
var cache = JsonSerializer.Serialize(items);
76-
File.WriteAllText(filePath, cache);
77-
}
43+
SecondStagePlan[] items = await GetSecondStagePlanAsync(router, messageId, plan1, dialogs);
7844

7945
foreach (var item in items)
8046
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
3838
services.AddScoped<IRoutingPlaner, NaivePlanner>();
3939
services.AddScoped<IRoutingPlaner, HFPlanner>();
4040
services.AddScoped<IRoutingPlaner, SequentialPlanner>();
41-
services.AddScoped<IRoutingPlaner, TwoStagePlanner>();
4241
}
4342
}

src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.two_stage.2nd.task.liquid

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

src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planning.two_stage.primary_plan.liquid

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

src/Plugins/BotSharp.Plugin.Planner/BotSharp.Plugin.Planner.csproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,53 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\agent.json" />
15+
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\instructions\instruction.liquid" />
16+
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.2nd.plan.liquid" />
17+
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.summarize.liquid" />
1418
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_primary_stage.json" />
1519
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_secondary_stage.json" />
20+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_summary.json" />
21+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\planner_prompt.two_stage.1st.plan.liquid" />
1622
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_primary_stage.fn.liquid" />
1723
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_secondary_stage.fn.liquid" />
24+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_summary.fn.liquid" />
1825
</ItemGroup>
1926

2027
<ItemGroup>
28+
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\agent.json">
29+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
30+
</Content>
31+
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\instructions\instruction.liquid">
32+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
33+
</Content>
34+
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.2nd.plan.liquid">
35+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
36+
</Content>
37+
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.summarize.liquid">
38+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39+
</Content>
2140
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_secondary_stage.json">
2241
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2342
</Content>
2443
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_primary_stage.json">
2544
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2645
</Content>
46+
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.1st.plan.liquid">
47+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
48+
</Content>
49+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_summary.json">
50+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
51+
</Content>
2752
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_secondary_stage.fn.liquid">
2853
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2954
</Content>
3055
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_primary_stage.fn.liquid">
3156
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3257
</Content>
58+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_summary.fn.liquid">
59+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
60+
</Content>
3361
</ItemGroup>
3462

3563
<ItemGroup>

src/Plugins/BotSharp.Plugin.Planner/Functions/PrimaryStagePlanFn.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
using BotSharp.Abstraction.Templating;
44
using System.Threading.Tasks;
55
using BotSharp.Abstraction.Routing;
6-
using BotSharp.Abstraction.MLTasks;
76
using BotSharp.Core.Infrastructures;
87
using BotSharp.Plugin.Planner.TwoStaging.Models;
9-
using BotSharp.Abstraction.Knowledges;
108
using Microsoft.Extensions.Logging;
119
using BotSharp.Abstraction.Knowledges.Models;
1210

@@ -55,7 +53,7 @@ public async Task<bool> Execute(RoleDialogModel message)
5553
var firstPlanningPrompt = await GetFirstStagePlanPrompt(task, message);
5654
var plannerAgent = new Agent
5755
{
58-
Id = "",
56+
Id = BuiltInAgentId.Planner,
5957
Name = "planning_1st",
6058
Instruction = firstPlanningPrompt,
6159
TemplateDict = new Dictionary<string, object>(),
@@ -64,7 +62,7 @@ public async Task<bool> Execute(RoleDialogModel message)
6462
var response = await GetAIResponse(plannerAgent);
6563
message.Content = response.Content;
6664

67-
await fn.InvokeFunction("plan_secondary_stage", message);
65+
/*await fn.InvokeFunction("plan_secondary_stage", message);
6866
var items = message.Content.JsonArrayContent<SecondStagePlan>();
6967
7068
//get all the related tables
@@ -85,7 +83,7 @@ public async Task<bool> Execute(RoleDialogModel message)
8583
_logger.LogInformation(summaryPlanningPrompt);
8684
plannerAgent = new Agent
8785
{
88-
Id = "",
86+
Id = BuiltInAgentId.Planner,
8987
Name = "planner_summary",
9088
Instruction = summaryPlanningPrompt,
9189
TemplateDict = new Dictionary<string, object>(),
@@ -95,19 +93,19 @@ public async Task<bool> Execute(RoleDialogModel message)
9593
_logger.LogInformation(response_summary.Content);
9694
9795
message.Content = response_summary.Content;
98-
message.StopCompletion = true;
96+
message.StopCompletion = true;*/
9997
return true;
10098
}
10199
private async Task<string> GetFirstStagePlanPrompt(PrimaryRequirementRequest task, RoleDialogModel message)
102100
{
103101
var agentService = _services.GetRequiredService<IAgentService>();
104-
var aiAssistant = await agentService.GetAgent(BuiltInAgentId.AIAssistant);
102+
var aiAssistant = await agentService.GetAgent(BuiltInAgentId.Planner);
105103
var render = _services.GetRequiredService<ITemplateRender>();
106-
var template = aiAssistant.Templates.First(x => x.Name == "planner_prompt.two_stage.1st.plan").Content;
104+
var template = aiAssistant.Templates.First(x => x.Name == "two_stage.1st.plan").Content;
107105
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
108106
{
109-
Parameters = new JsonDocument[] { JsonDocument.Parse("{}") },
110-
Results = new string[] { "" }
107+
Parameters = [JsonDocument.Parse("{}")],
108+
Results = [""]
111109
});
112110

113111
return render.Render(template, new Dictionary<string, object>
@@ -126,8 +124,8 @@ private async Task<string> GetPlanSummaryPrompt(PrimaryRequirementRequest task,
126124
var template = aiAssistant.Templates.First(x => x.Name == "planner_prompt.two_stage.summarize").Content;
127125
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
128126
{
129-
Parameters = new JsonDocument[] { JsonDocument.Parse("{}") },
130-
Results = new string[] { "" }
127+
Parameters = [JsonDocument.Parse("{}")],
128+
Results = [""]
131129
});
132130

133131
return render.Render(template, new Dictionary<string, object>

0 commit comments

Comments
 (0)