Skip to content

Commit 35cbee5

Browse files
authored
Merge pull request #622 from Joannall/master
Improve Primary Planning
2 parents 1a42d91 + 348888a commit 35cbee5

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using BotSharp.Abstraction.Conversations.Models;
2+
using BotSharp.Abstraction.Functions;
13
using BotSharp.Abstraction.Knowledges;
4+
using BotSharp.Abstraction.Knowledges.Models;
5+
using BotSharp.Abstraction.Routing;
26
using BotSharp.Plugin.Planner.TwoStaging.Models;
37

48
namespace BotSharp.Plugin.Planner.Functions;
@@ -27,18 +31,26 @@ public async Task<bool> Execute(RoleDialogModel message)
2731

2832
state.SetState("max_tokens", "4096");
2933
var task = JsonSerializer.Deserialize<PrimaryRequirementRequest>(message.FunctionArgs);
30-
31-
// Get knowledge from vectordb
32-
var collectionName = knowledgeSettings.Default.CollectionName ?? KnowledgeCollectionName.BotSharp; ;
33-
var knowledges = await knowledgeService.SearchVectorKnowledge(task.Requirements, collectionName, new VectorSearchOptions
34+
35+
//get knowledge from vectordb
36+
var knowledges = new List<string>();
37+
foreach (var question in task.Questions)
3438
{
35-
Confidence = 0.1f
36-
});
37-
message.Content = string.Join("\r\n\r\n=====\r\n", knowledges.Select(x => x.ToQuestionAnswer()));
39+
var retrievalMessage = new RoleDialogModel(AgentRole.User, question)
40+
{
41+
FunctionArgs = JsonSerializer.Serialize(new ExtractedKnowledge
42+
{
43+
Question = question
44+
}),
45+
Content = ""
46+
};
47+
await fn.InvokeFunction("knowledge_retrieval", retrievalMessage);
48+
knowledges.Add(retrievalMessage.Content);
49+
}
3850

3951
// Send knowledge to AI to refine and summarize the primary planning
4052
var currentAgent = await agentService.LoadAgent(message.CurrentAgentId);
41-
var firstPlanningPrompt = await GetFirstStagePlanPrompt(task, message);
53+
var firstPlanningPrompt = await GetFirstStagePlanPrompt(task, knowledges);
4254
var plannerAgent = new Agent
4355
{
4456
Id = BuiltInAgentId.Planner,
@@ -53,7 +65,7 @@ public async Task<bool> Execute(RoleDialogModel message)
5365
return true;
5466
}
5567

56-
private async Task<string> GetFirstStagePlanPrompt(PrimaryRequirementRequest task, RoleDialogModel message)
68+
private async Task<string> GetFirstStagePlanPrompt(PrimaryRequirementRequest task, List<string> relevantKnowledges)
5769
{
5870
var agentService = _services.GetRequiredService<IAgentService>();
5971
var render = _services.GetRequiredService<ITemplateRender>();
@@ -78,7 +90,7 @@ private async Task<string> GetFirstStagePlanPrompt(PrimaryRequirementRequest tas
7890
{
7991
{ "task_description", task.Requirements },
8092
{ "global_knowledges", globalKnowledges },
81-
{ "relevant_knowledges", new[]{ message.Content } },
93+
{ "relevant_knowledges", relevantKnowledges },
8294
{ "response_format", responseFormat }
8395
});
8496
}

src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Models/PrimaryRequirementRequest.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ public class PrimaryRequirementRequest
55
[JsonPropertyName("requirement_detail")]
66
public string Requirements { get; set; } = null!;
77

8-
[JsonPropertyName("has_knowledge_reference")]
9-
public bool HasKnowledgeReference { get; set; }
10-
11-
[JsonPropertyName("question")]
12-
public string Question { get; set; } = null!;
8+
[JsonPropertyName("questions")]
9+
public string[] Questions { get; set; } = [];
1310
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
Use the TwoStagePlanner approach to plan the overall implementation steps, call plan_primary_stage.
22
If need_additional_information is true, call plan_secondary_stage for the specific primary stage.
3-
You must Call plan_summary as the last step to summarize the final planning steps.
3+
You must Call plan_summary as the last step to summarize the final planning steps.
4+
5+
{% if global_knowledges != empty -%}
6+
=====
7+
Global Knowledge:
8+
{% for k in global_knowledges %}
9+
{{ k }}
10+
{% endfor %}
11+
{%- endif %}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
{
22
"name": "plan_primary_stage",
3-
"description": "Extract user's original requirements with detail specifications to make the primary plan, you have to include every important informations.",
3+
"description": "Plan the high level steps to finish the task",
44
"parameters": {
55
"type": "object",
66
"properties": {
77
"requirement_detail": {
88
"type": "string",
99
"description": "User original requirements in detail, don't miss any information especially for those line items, values and numbers."
1010
},
11-
"question": {
12-
"type": "string",
13-
"description": "Question convert from requirement and reference tables for knowledge search. The question should contain all the detailed information in the requirement"
11+
"questions": {
12+
"type": "array",
13+
"description": "User requirements in detail, don't miss any information especially for those line items, values and numbers.",
14+
"items": {
15+
"type": "string",
16+
"description": "Question converted from requirement in different ways to search in the knowledge base, be short"
17+
}
1418
}
1519
},
16-
"required": [ "requirement_detail", "question" ]
20+
"required": [ "requirement_detail", "questions" ]
1721
}
18-
}
22+
}

0 commit comments

Comments
 (0)