Skip to content

Commit b385d82

Browse files
authored
Merge pull request #659 from Joannall/master
Update dictionary look up
2 parents d3142b5 + 68f3021 commit b385d82

23 files changed

+224
-53
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\functions\plan_secondary_stage.json" />
1717
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\functions\plan_summary.json" />
1818
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\instructions\instruction.liquid" />
19+
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\database.dictionary.sql.liquid" />
1920
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\database.summarize.MySql.liquid" />
2021
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\database.summarize.SqlServer.liquid" />
2122
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.2nd.plan.liquid" />
@@ -49,6 +50,9 @@
4950
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\database.summarize.sqlserver.liquid">
5051
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5152
</Content>
53+
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\database.dictionary.sql.liquid">
54+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
55+
</Content>
5256
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.next.liquid">
5357
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5458
</Content>

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,26 @@ public async Task<bool> Execute(RoleDialogModel message)
2121
var agentService = _services.GetRequiredService<IAgentService>();
2222
var knowledgeService = _services.GetRequiredService<IKnowledgeService>();
2323
var knowledgeSettings = _services.GetRequiredService<KnowledgeBaseSettings>();
24-
24+
var states = _services.GetRequiredService<IConversationStateService>();
25+
2526
var msgSecondary = RoleDialogModel.From(message);
26-
var taskPrimary = JsonSerializer.Deserialize<PrimaryRequirementRequest>(message.FunctionArgs);
2727
var collectionName = knowledgeSettings.Default.CollectionName ?? KnowledgeCollectionName.BotSharp;
28-
29-
msgSecondary.FunctionArgs = JsonSerializer.Serialize(new SecondaryBreakdownTask
30-
{
31-
TaskDescription = taskPrimary.Requirements
32-
});
28+
var planPrimary = states.GetState("planning_result");
29+
var taskPrimary = states.GetState("requirement_detail");
3330

3431
var taskSecondary = JsonSerializer.Deserialize<SecondaryBreakdownTask>(msgSecondary.FunctionArgs);
35-
var items = msgSecondary.Content.JsonArrayContent<FirstStagePlan>();
36-
32+
3733
// Search knowledgebase
38-
foreach (var item in items)
34+
var knowledges = await knowledgeService.SearchVectorKnowledge(taskSecondary.SolutionQuestion, collectionName, new VectorSearchOptions
3935
{
40-
if (!item.NeedAdditionalInformation) continue;
41-
42-
var knowledges = await knowledgeService.SearchVectorKnowledge(item.Task, collectionName, new VectorSearchOptions
43-
{
44-
Confidence = 0.6f
45-
});
46-
message.Content += string.Join("\r\n\r\n=====\r\n", knowledges.Select(x => x.ToQuestionAnswer()));
47-
}
36+
Confidence = 0.6f
37+
});
38+
var knowledgeResults = "";
39+
knowledgeResults = string.Join("\r\n\r\n=====\r\n", knowledges.Select(x => x.ToQuestionAnswer()));
4840

4941
// Get second stage planning prompt
5042
var currentAgent = await agentService.LoadAgent(message.CurrentAgentId);
51-
var secondPlanningPrompt = await GetSecondStagePlanPrompt(taskSecondary.TaskDescription, message);
43+
var secondPlanningPrompt = await GetSecondStagePlanPrompt(taskSecondary.TaskDescription, planPrimary, knowledgeResults, message);
5244
_logger.LogInformation(secondPlanningPrompt);
5345

5446
var plannerAgent = new Agent
@@ -64,12 +56,11 @@ public async Task<bool> Execute(RoleDialogModel message)
6456
message.Content = response.Content;
6557
_logger.LogInformation(response.Content);
6658

67-
var states = _services.GetRequiredService<IConversationStateService>();
6859
states.SetState("planning_result", response.Content);
6960
return true;
7061
}
7162

72-
private async Task<string> GetSecondStagePlanPrompt(string taskDescription, RoleDialogModel message)
63+
private async Task<string> GetSecondStagePlanPrompt(string taskDescription, string planPrimary, string knowledgeResults, RoleDialogModel message)
7364
{
7465
var agentService = _services.GetRequiredService<IAgentService>();
7566
var render = _services.GetRequiredService<ITemplateRender>();
@@ -85,7 +76,8 @@ private async Task<string> GetSecondStagePlanPrompt(string taskDescription, Role
8576
return render.Render(template, new Dictionary<string, object>
8677
{
8778
{ "task_description", taskDescription },
88-
{ "primary_plan", new[]{ message.Content } },
79+
{ "primary_plan", planPrimary },
80+
{ "additional_knowledge", knowledgeResults },
8981
{ "response_format", responseFormat }
9082
});
9183
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task<bool> Execute(RoleDialogModel message)
3535
var allTables = new List<string>();
3636
var ddlStatements = "";
3737
var relevantKnowledge = states.GetState("planning_result");
38-
relevantKnowledge += states.GetState("dictionary_items");
38+
var dictionaryItems = states.GetState("dictionary_items");
3939

4040
foreach (var step in steps)
4141
{
@@ -49,12 +49,12 @@ public async Task<bool> Execute(RoleDialogModel message)
4949
{
5050
table = table,
5151
});
52-
await fn.InvokeFunction("get_table_definition", msgCopy);
52+
await fn.InvokeFunction("sql_table_definition", msgCopy);
5353
ddlStatements += "\r\n" + msgCopy.Content;
5454
}
5555

5656
// Summarize and generate query
57-
var summaryPlanPrompt = await GetSummaryPlanPrompt(taskRequirement, relevantKnowledge, ddlStatements);
57+
var summaryPlanPrompt = await GetSummaryPlanPrompt(taskRequirement, relevantKnowledge, dictionaryItems, ddlStatements);
5858
_logger.LogInformation($"Summary plan prompt:\r\n{summaryPlanPrompt}");
5959

6060
var plannerAgent = new Agent
@@ -74,7 +74,7 @@ await HookEmitter.Emit<IPlanningHook>(_services, x =>
7474
return true;
7575
}
7676

77-
private async Task<string> GetSummaryPlanPrompt(string taskDescription, string relevantKnowledge, string ddlStatement)
77+
private async Task<string> GetSummaryPlanPrompt(string taskDescription, string relevantKnowledge, string dictionaryItems, string ddlStatement)
7878
{
7979
var agentService = _services.GetRequiredService<IAgentService>();
8080
var render = _services.GetRequiredService<ITemplateRender>();
@@ -94,6 +94,7 @@ await HookEmitter.Emit<IPlanningHook>(_services, async x =>
9494
{ "task_description", taskDescription },
9595
{ "summary_requirements", string.Join("\r\n",additionalRequirements) },
9696
{ "relevant_knowledges", relevantKnowledge },
97+
{ "dictionary_items", dictionaryItems },
9798
{ "table_structure", ddlStatement },
9899
});
99100
}

src/Plugins/BotSharp.Plugin.Planner/data/agents/282a7128-69a1-44b0-878c-a9159b88f3b9/functions/plan_primary_stage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"description": "User requirements in detail, don't miss any information especially for those line items, values and numbers.",
1414
"items": {
1515
"type": "string",
16-
"description": "Question converted from requirement in different ways to search in the knowledge base, be short"
16+
"description": "Question converted from requirement in different ways to search in the knowledge base, be short and you can refer to the global knowledge."
1717
}
1818
}
1919
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
You are a sql statement corrector. You will need to refer to the table structure and rewrite the original sql statement so it's using the correct information, e.g. column name.
2+
Output the sql statement only without comments, in JSON format: {{ response_format }}
3+
4+
=====
5+
Original Sql:
6+
{{ original_sql }}
7+
8+
=====
9+
Table Structure:
10+
{{ table_structure }}

src/Plugins/BotSharp.Plugin.Planner/data/agents/282a7128-69a1-44b0-878c-a9159b88f3b9/templates/database.summarize.mysql.liquid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ For example, you should use SET @id = select max(id) from table;
1818
*** the generated sql query MUST be basedd on the provided table structure. ***
1919
*** All queries return a maximum of 20 records. ***
2020
*** Only select user friendly columns. ***
21+
*** Try to use id instead of string in where clause if you have the dictionary. ***

src/Plugins/BotSharp.Plugin.Planner/data/agents/282a7128-69a1-44b0-878c-a9159b88f3b9/templates/two_stage.1st.plan.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Thinking process:
88
- If there is extra knowledge or relationship needed between steps, set the need_additional_information to true for both steps.
99
- If the solution mentioned "related solutions" is needed, set the need_additional_information to true.
1010
- You should find the relationships between data structure based on the task knowledge strictly. If lack of information, set the need_additional_information to true.
11-
- If you need to verify or get the enum/term/dictionary value, set the need_additional_information to true.
11+
- If you need to lookup the dictionary to verify or get the enum/term/dictionary value, set the need_additional_information to true.
1212
3. Input argument must reference to corresponding variable name that retrieved by previous steps, variable name must start with '@';
1313
4. Output all the subtasks as much detail as possible in JSON: [{{ response_format }}]
1414
5. You can NOT generate the final query before calling function plan_summary.

src/Plugins/BotSharp.Plugin.Planner/data/agents/282a7128-69a1-44b0-878c-a9159b88f3b9/templates/two_stage.2nd.plan.liquid

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ Additional Requirements:
1111
* "output_results" is variable name that needed to be used in the next step.
1212

1313
=====
14-
TASK: {{ task_description }}
14+
Sub Task Description:
15+
{{ task_description }}
1516

1617
=====
1718
Primary Planning:
18-
{{ primary_plan }}
19+
{{ primary_plan }}
20+
21+
=====
22+
Additional Knowledge:
23+
{{ additional_knowledge }}

src/Plugins/BotSharp.Plugin.Planner/data/agents/282a7128-69a1-44b0-878c-a9159b88f3b9/templates/two_stage.summarize.liquid

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Task description:
1111
Relevant Knowledges:
1212
{{ relevant_knowledges }}
1313

14+
=====
15+
Dictionary Items:
16+
{{ dictionary_items }}
17+
1418
=====
1519
Table Structure:
1620
{{ table_structure }}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,34 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\get_table_definition.json" />
2120
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\sql_dictionary_lookup.json" />
2221
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\sql_select.json" />
23-
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\get_table_definition.fn.liquid" />
22+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\sql_table_definition.json" />
2423
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_dictionary_lookup.fn.liquid" />
2524
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_executor.fn.liquid" />
25+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_table_definition.fn.liquid" />
2626
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\agent.json" />
2727
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\execute_sql.json" />
2828
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\lookup_dictionary.json" />
2929
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_insert.json" />
3030
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_select.json" />
3131
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\instructions\instruction.liquid" />
32-
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\sql_dictionary_lookup.liquid" />
3332
</ItemGroup>
3433

3534
<ItemGroup>
35+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\sql_table_definition.json">
36+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
37+
</Content>
3638
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\sql_dictionary_lookup.json">
3739
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3840
</Content>
3941
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_dictionary_lookup.fn.liquid">
4042
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4143
</Content>
42-
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\get_table_definition.json">
44+
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_table_definition.json">
4345
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4446
</Content>
45-
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\get_table_definition.fn.liquid">
47+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_table_definition.fn.liquid">
4648
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4749
</Content>
4850
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\agent.json">
@@ -54,9 +56,6 @@
5456
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\instructions\instruction.liquid">
5557
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5658
</Content>
57-
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\sql_dictionary_lookup.liquid">
58-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
59-
</Content>
6059
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_insert.json">
6160
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6261
</Content>
@@ -81,4 +80,8 @@
8180
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
8281
</ItemGroup>
8382

83+
<ItemGroup>
84+
<Folder Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\" />
85+
</ItemGroup>
86+
8487
</Project>

0 commit comments

Comments
 (0)