Skip to content

Improve Primary Planning #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/Plugins/BotSharp.Plugin.Planner/Functions/PrimaryStagePlanFn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Knowledges;
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.Routing;
using BotSharp.Plugin.Planner.TwoStaging.Models;

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

state.SetState("max_tokens", "4096");
var task = JsonSerializer.Deserialize<PrimaryRequirementRequest>(message.FunctionArgs);
// Get knowledge from vectordb
var collectionName = knowledgeSettings.Default.CollectionName ?? KnowledgeCollectionName.BotSharp; ;
var knowledges = await knowledgeService.SearchVectorKnowledge(task.Requirements, collectionName, new VectorSearchOptions

//get knowledge from vectordb
var knowledges = new List<string>();
foreach (var question in task.Questions)
{
Confidence = 0.1f
});
message.Content = string.Join("\r\n\r\n=====\r\n", knowledges.Select(x => x.ToQuestionAnswer()));
var retrievalMessage = new RoleDialogModel(AgentRole.User, question)
{
FunctionArgs = JsonSerializer.Serialize(new ExtractedKnowledge
{
Question = question
}),
Content = ""
};
await fn.InvokeFunction("knowledge_retrieval", retrievalMessage);
knowledges.Add(retrievalMessage.Content);
}

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

private async Task<string> GetFirstStagePlanPrompt(PrimaryRequirementRequest task, RoleDialogModel message)
private async Task<string> GetFirstStagePlanPrompt(PrimaryRequirementRequest task, List<string> relevantKnowledges)
{
var agentService = _services.GetRequiredService<IAgentService>();
var render = _services.GetRequiredService<ITemplateRender>();
Expand All @@ -78,7 +90,7 @@ private async Task<string> GetFirstStagePlanPrompt(PrimaryRequirementRequest tas
{
{ "task_description", task.Requirements },
{ "global_knowledges", globalKnowledges },
{ "relevant_knowledges", new[]{ message.Content } },
{ "relevant_knowledges", relevantKnowledges },
{ "response_format", responseFormat }
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ public class PrimaryRequirementRequest
[JsonPropertyName("requirement_detail")]
public string Requirements { get; set; } = null!;

[JsonPropertyName("has_knowledge_reference")]
public bool HasKnowledgeReference { get; set; }

[JsonPropertyName("question")]
public string Question { get; set; } = null!;
[JsonPropertyName("questions")]
public string[] Questions { get; set; } = [];
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Use the TwoStagePlanner approach to plan the overall implementation steps, call plan_primary_stage.
If need_additional_information is true, call plan_secondary_stage for the specific primary stage.
You must Call plan_summary as the last step to summarize the final planning steps.
You must Call plan_summary as the last step to summarize the final planning steps.

{% if global_knowledges != empty -%}
=====
Global Knowledge:
{% for k in global_knowledges %}
{{ k }}
{% endfor %}
{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"name": "plan_primary_stage",
"description": "Extract user's original requirements with detail specifications to make the primary plan, you have to include every important informations.",
"description": "Plan the high level steps to finish the task",
"parameters": {
"type": "object",
"properties": {
"requirement_detail": {
"type": "string",
"description": "User original requirements in detail, don't miss any information especially for those line items, values and numbers."
},
"question": {
"type": "string",
"description": "Question convert from requirement and reference tables for knowledge search. The question should contain all the detailed information in the requirement"
"questions": {
"type": "array",
"description": "User requirements in detail, don't miss any information especially for those line items, values and numbers.",
"items": {
"type": "string",
"description": "Question converted from requirement in different ways to search in the knowledge base, be short"
}
}
},
"required": [ "requirement_detail", "question" ]
"required": [ "requirement_detail", "questions" ]
}
}
}