This commit is contained in:
Haiping Chen 2024-08-30 10:23:52 -05:00
commit d0438f7a29
25 changed files with 534 additions and 108 deletions

View file

@ -3,11 +3,16 @@ namespace BotSharp.Abstraction.Agents.Enums;
public class AgentType
{
/// <summary>
/// Routing Agent
/// Routing agent
/// </summary>
public const string Routing = "routing";
public const string Evaluating = "evaluating";
/// <summary>
/// Planning agent
/// </summary>
public const string Planning = "plan";
public const string Evaluating = "evaluation";
/// <summary>
/// Routable task agent with capability of interaction with external environment

View file

@ -31,4 +31,9 @@ public class BuiltInAgentId
/// Used by knowledgebase plugin to acquire domain knowledge
/// </summary>
public const string Learner = "01acc3e5-0af7-49e6-ad7a-a760bd12dc40";
/// <summary>
/// Plan feasible implementation steps for complex problems
/// </summary>
public const string Planner = "282a7128-69a1-44b0-878c-a9159b88f3b9";
}

View file

@ -56,7 +56,6 @@
<ItemGroup>
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\database_knowledge.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.summarize.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instructions\instruction.liquid" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json" />
@ -75,9 +74,6 @@
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.naive.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.get_remaining_task.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.1st.plan.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.plan.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.task.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\response_with_function.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\translation_prompt.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_file_prompt.liquid" />
@ -119,15 +115,6 @@
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\database_knowledge.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.1st.plan.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.plan.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.task.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.get_remaining_task.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
@ -140,9 +127,6 @@
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.naive.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.summarize.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\response_with_function.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -44,7 +44,7 @@ public partial class TwoStagePlanner
private async Task<string> GetFirstStagePlanPrompt(Agent router)
{
var template = router.Templates.First(x => x.Name == "planner_prompt.two_stage.1st.plan").Content;
var template = router.Templates.First(x => x.Name == "two_stage.1st.plan").Content;
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
{
Parameters = new JsonDocument[]{ JsonDocument.Parse("{}") },
@ -69,7 +69,7 @@ public partial class TwoStagePlanner
private string GetFirstStageNextPrompt(Agent router)
{
var template = router.Templates.First(x => x.Name == "planner_prompt.first_stage.next").Content;
var template = router.Templates.First(x => x.Name == "first_stage.next").Content;
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
{
});

View file

@ -1,9 +1,4 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Planning;
using System.IO;
namespace BotSharp.Core.Routing.Planning;
@ -28,25 +23,9 @@ public partial class TwoStagePlanner : IRoutingPlaner
public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs)
{
var tempDir = Path.Combine(Path.GetTempPath(), "botsharp", "cache");
if (_plan1st.IsNullOrEmpty() && _plan2nd.IsNullOrEmpty())
{
Directory.CreateDirectory(tempDir);
_md5 = Utilities.HashTextMd5($"{string.Join(".", dialogs.Where(x => x.Role == AgentRole.User))}{"botsharp"}");
var filePath = Path.Combine(tempDir, $"{_md5}-1st.json");
FirstStagePlan[] items = new FirstStagePlan[0];
if (File.Exists(filePath))
{
var cache = File.ReadAllText(filePath);
items = JsonSerializer.Deserialize<FirstStagePlan[]>(cache);
}
else
{
items = await GetFirstStagePlanAsync(router, messageId, dialogs);
var cache = JsonSerializer.Serialize(items);
File.WriteAllText(filePath, cache);
}
FirstStagePlan[] items = await GetFirstStagePlanAsync(router, messageId, dialogs);
foreach (var item in items)
{
@ -61,20 +40,7 @@ public partial class TwoStagePlanner : IRoutingPlaner
if (plan1.ContainMultipleSteps)
{
var filePath = Path.Combine(tempDir, $"{_md5}-2nd-{plan1.Step}.json");
SecondStagePlan[] items = new SecondStagePlan[0];
if (File.Exists(filePath))
{
var cache = File.ReadAllText(filePath);
items = JsonSerializer.Deserialize<SecondStagePlan[]>(cache);
}
else
{
items = await GetSecondStagePlanAsync(router, messageId, plan1, dialogs);
var cache = JsonSerializer.Serialize(items);
File.WriteAllText(filePath, cache);
}
SecondStagePlan[] items = await GetSecondStagePlanAsync(router, messageId, plan1, dialogs);
foreach (var item in items)
{

View file

@ -38,6 +38,5 @@ public class RoutingPlugin : IBotSharpPlugin
services.AddScoped<IRoutingPlaner, NaivePlanner>();
services.AddScoped<IRoutingPlaner, HFPlanner>();
services.AddScoped<IRoutingPlaner, SequentialPlanner>();
services.AddScoped<IRoutingPlaner, TwoStagePlanner>();
}
}

View file

@ -1,8 +0,0 @@
{{ task_description }}
{% if related_tables != empty -%}
Relevant tables:
{% for t in related_tables -%}
- {{ t }}{{ "\r\n" }}
{%- endfor %}
{%- endif %}

View file

@ -11,25 +11,53 @@
</PropertyGroup>
<ItemGroup>
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\agent.json" />
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\instructions\instruction.liquid" />
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.2nd.plan.liquid" />
<None Remove="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.summarize.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_primary_stage.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_secondary_stage.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_summary.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\planner_prompt.two_stage.1st.plan.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_primary_stage.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_secondary_stage.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_summary.fn.liquid" />
</ItemGroup>
<ItemGroup>
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.2nd.plan.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.summarize.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_secondary_stage.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_primary_stage.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\282a7128-69a1-44b0-878c-a9159b88f3b9\templates\two_stage.1st.plan.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_summary.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_secondary_stage.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_primary_stage.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_summary.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>

View file

@ -3,10 +3,8 @@ using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Templating;
using System.Threading.Tasks;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.Planner.TwoStaging.Models;
using BotSharp.Abstraction.Knowledges;
using Microsoft.Extensions.Logging;
using BotSharp.Abstraction.Knowledges.Models;
@ -55,7 +53,7 @@ public class PrimaryStagePlanFn : IFunctionCallback
var firstPlanningPrompt = await GetFirstStagePlanPrompt(task, message);
var plannerAgent = new Agent
{
Id = "",
Id = BuiltInAgentId.Planner,
Name = "planning_1st",
Instruction = firstPlanningPrompt,
TemplateDict = new Dictionary<string, object>(),
@ -64,7 +62,7 @@ public class PrimaryStagePlanFn : IFunctionCallback
var response = await GetAIResponse(plannerAgent);
message.Content = response.Content;
await fn.InvokeFunction("plan_secondary_stage", message);
/*await fn.InvokeFunction("plan_secondary_stage", message);
var items = message.Content.JsonArrayContent<SecondStagePlan>();
//get all the related tables
@ -85,7 +83,7 @@ public class PrimaryStagePlanFn : IFunctionCallback
_logger.LogInformation(summaryPlanningPrompt);
plannerAgent = new Agent
{
Id = "",
Id = BuiltInAgentId.Planner,
Name = "planner_summary",
Instruction = summaryPlanningPrompt,
TemplateDict = new Dictionary<string, object>(),
@ -95,19 +93,19 @@ public class PrimaryStagePlanFn : IFunctionCallback
_logger.LogInformation(response_summary.Content);
message.Content = response_summary.Content;
message.StopCompletion = true;
message.StopCompletion = true;*/
return true;
}
private async Task<string> GetFirstStagePlanPrompt(PrimaryRequirementRequest task, RoleDialogModel message)
{
var agentService = _services.GetRequiredService<IAgentService>();
var aiAssistant = await agentService.GetAgent(BuiltInAgentId.AIAssistant);
var aiAssistant = await agentService.GetAgent(BuiltInAgentId.Planner);
var render = _services.GetRequiredService<ITemplateRender>();
var template = aiAssistant.Templates.First(x => x.Name == "planner_prompt.two_stage.1st.plan").Content;
var template = aiAssistant.Templates.First(x => x.Name == "two_stage.1st.plan").Content;
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
{
Parameters = new JsonDocument[] { JsonDocument.Parse("{}") },
Results = new string[] { "" }
Parameters = [JsonDocument.Parse("{}")],
Results = [""]
});
return render.Render(template, new Dictionary<string, object>
@ -126,8 +124,8 @@ public class PrimaryStagePlanFn : IFunctionCallback
var template = aiAssistant.Templates.First(x => x.Name == "planner_prompt.two_stage.summarize").Content;
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
{
Parameters = new JsonDocument[] { JsonDocument.Parse("{}") },
Results = new string[] { "" }
Parameters = [JsonDocument.Parse("{}")],
Results = [""]
});
return render.Render(template, new Dictionary<string, object>

View file

@ -24,6 +24,7 @@ public class SecondaryStagePlanFn : IFunctionCallback
_services = services;
_logger = logger;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var fn = _services.GetRequiredService<IRoutingService>();
@ -36,7 +37,7 @@ public class SecondaryStagePlanFn : IFunctionCallback
var task_secondary = JsonSerializer.Deserialize<SecondaryBreakdownTask>(msg_secondary.FunctionArgs);
var items = msg_secondary.Content.JsonArrayContent<FirstStagePlan>();
msg_secondary.KnowledgeConfidence = 0.5f;
msg_secondary.KnowledgeConfidence = 0.5f;
foreach (var item in items)
{
if (item.NeedAdditionalInformation)
@ -73,9 +74,9 @@ public class SecondaryStagePlanFn : IFunctionCallback
private async Task<string> GetSecondStagePlanPrompt(SecondaryBreakdownTask task, RoleDialogModel message)
{
var agentService = _services.GetRequiredService<IAgentService>();
var aiAssistant = await agentService.GetAgent(BuiltInAgentId.AIAssistant);
var planner = await agentService.GetAgent(message.CurrentAgentId);
var render = _services.GetRequiredService<ITemplateRender>();
var template = aiAssistant.Templates.First(x => x.Name == "planner_prompt.two_stage.2nd.plan").Content;
var template = planner.Templates.First(x => x.Name == "two_stage.2nd.plan").Content;
var responseFormat = JsonSerializer.Serialize(new SecondStagePlan
{
Tool = "tool name if task solution provided",

View file

@ -0,0 +1,94 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Templating;
using System.Threading.Tasks;
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.Planner.TwoStaging.Models;
using Microsoft.Extensions.Logging;
namespace BotSharp.Plugin.Planner.Functions;
public class SummaryPlanFn : IFunctionCallback
{
public string Name => "plan_summary";
private readonly IServiceProvider _services;
private readonly ILogger _logger;
private object aiAssistant;
public SummaryPlanFn(IServiceProvider services, ILogger<PrimaryStagePlanFn> logger)
{
_services = services;
_logger = logger;
}
public async Task<bool> Execute(RoleDialogModel message)
{
//debug
var state = _services.GetRequiredService<IConversationStateService>();
state.SetState("max_tokens", "4096");
var task = state.GetState("requirement_detail");
// summarize and generate query
var summaryPlanningPrompt = await GetPlanSummaryPrompt(task, message);
_logger.LogInformation(summaryPlanningPrompt);
var plannerAgent = new Agent
{
Id = BuiltInAgentId.Planner,
Name = "planner_summary",
Instruction = summaryPlanningPrompt,
TemplateDict = new Dictionary<string, object>()
};
var response_summary = await GetAIResponse(plannerAgent);
message.Content = response_summary.Content;
message.StopCompletion = true;
return true;
}
private async Task<string> GetPlanSummaryPrompt(string task, RoleDialogModel message)
{
// save to knowledge base
var agentService = _services.GetRequiredService<IAgentService>();
var aiAssistant = await agentService.GetAgent(message.CurrentAgentId);
var render = _services.GetRequiredService<ITemplateRender>();
var template = aiAssistant.Templates.First(x => x.Name == "two_stage.summarize").Content;
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
{
Parameters = [JsonDocument.Parse("{}")],
Results = [""]
});
return render.Render(template, new Dictionary<string, object>
{
{ "table_structure", message.SecondaryContent }, ////check
{ "task_description", task},
{ "relevant_knowledges", message.Content },
{ "response_format", responseFormat }
});
}
private async Task<RoleDialogModel> GetAIResponse(Agent plannerAgent)
{
var conv = _services.GetRequiredService<IConversationService>();
var wholeDialogs = conv.GetDialogHistory();
//add "test" to wholeDialogs' last element
if(plannerAgent.Name == "planner_summary")
{
//add "test" to wholeDialogs' last element in a new paragraph
wholeDialogs.Last().Content += "\n\nIf the table structure didn't mention auto incremental, the data field id needs to insert id manually and you need to use max(id) instead of LAST_INSERT_ID function.\nFor example, you should use SET @id = select max(id) from table;";
wholeDialogs.Last().Content += "\n\nTry if you can generate a single query to fulfill the needs";
}
if (plannerAgent.Name == "planning_1st")
{
//add "test" to wholeDialogs' last element in a new paragraph
wholeDialogs.Last().Content += "\n\nYou must analyze the table description to infer the table relations.";
}
var completion = CompletionProvider.GetChatCompletion(_services,
provider: plannerAgent.LlmConfig.Provider,
model: plannerAgent.LlmConfig.Model);
return await completion.GetChatCompletions(plannerAgent, wholeDialogs);
}
}

View file

@ -52,6 +52,24 @@ public class PlannerAgentHook : AgentHookBase
agent.Functions.Add(fn);
}
}
(prompt, fn) = GetPromptAndFunction("plan_summary");
if (fn != null)
{
if (!string.IsNullOrWhiteSpace(prompt))
{
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
}
if (agent.Functions == null)
{
agent.Functions = new List<FunctionDef> { fn };
}
else
{
agent.Functions.Add(fn);
}
}
}
base.OnAgentLoaded(agent);

View file

@ -1,14 +1,23 @@
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Plugin.Planner.TwoStaging;
namespace BotSharp.Plugin.Planner;
/// <summary>
/// Plugin for AI Planning.
/// </summary>
public class PlannerPlugin : IBotSharpPlugin
{
public string Id => "571f71fe-1583-46f2-b577-c8577a0a2903";
public string Name => "AI Planning Plugin";
public string Description => "Provide AI with different planning approaches to improve AI's ability to solve complex problems.";
public string IconUrl => "https://library.ucf.edu/wp-content/uploads/sites/5/2015/03/SC-Planning-Icon-300x290.png";
public string IconUrl => "https://e7.pngegg.com/pngimages/775/350/png-clipart-action-plan-computer-icons-plan-miscellaneous-text-thumbnail.png";
public string[] AgentIds => [ BuiltInAgentId.Planner ];
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<IRoutingPlaner, TwoStageTaskPlanner>();
services.AddScoped<IAgentHook, PlannerAgentHook>();
services.AddScoped<IAgentUtilityHook, PlannerUtilityHook>();
}

View file

@ -1,11 +1,312 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Knowledges;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Abstraction.Templating;
using BotSharp.Core.Infrastructures;
using BotSharp.Core.Routing.Planning;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace BotSharp.Plugin.Planner.TwoStaging;
public partial class TwoStageTaskPlanner : ITaskPlanner
public partial class TwoStageTaskPlanner : IRoutingPlaner
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
public int MaxLoopCount => 100;
private bool _isTaskCompleted;
public TwoStageTaskPlanner(IServiceProvider services)
private Queue<FirstStagePlan> _plan1st = new Queue<FirstStagePlan>();
private Queue<SecondStagePlan> _plan2nd = new Queue<SecondStagePlan>();
private List<string> _executionContext = new List<string>();
public TwoStageTaskPlanner(IServiceProvider services, ILogger<TwoStagePlanner> logger)
{
_services = services;
_logger = logger;
}
public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs)
{
// push agent to routing context
var routing = _services.GetRequiredService<IRoutingService>();
routing.Context.Push(BuiltInAgentId.Planner, "Make plan in TwoStage planner");
return new FunctionCallFromLlm
{
AgentName = "Planner",
UserGoal = "",
Response = "",
Function = "route_to_agent"
};
/*FirstStagePlan[] items = await GetFirstStagePlanAsync(router, messageId, dialogs);
foreach (var item in items)
{
_plan1st.Enqueue(item);
};
// Get Second Stage Plan
if (_plan2nd.IsNullOrEmpty())
{
var plan1 = _plan1st.Dequeue();
if (plan1.ContainMultipleSteps)
{
SecondStagePlan[] items = await GetSecondStagePlanAsync(router, messageId, plan1, dialogs);
foreach (var item in items)
{
_plan2nd.Enqueue(item);
}
}
else
{
_plan2nd.Enqueue(new SecondStagePlan
{
Description = plan1.Task,
Tables = plan1.Tables,
Parameters = plan1.Parameters,
Results = plan1.Results,
});
}
}
var plan2 = _plan2nd.Dequeue();
var secondStagePrompt = GetSecondStageTaskPrompt(router, plan2);
var inst = new FunctionCallFromLlm
{
AgentName = "SQL Driver",
Response = secondStagePrompt,
Function = "route_to_agent"
};
inst.HandleDialogsByPlanner = true;
_isTaskCompleted = _plan1st.IsNullOrEmpty() && _plan2nd.IsNullOrEmpty();
return inst;*/
}
public List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var question = inst.Response;
if (_executionContext.Count > 0)
{
var content = GetContext();
question = $"CONTEXT:\r\n{content}\r\n" + inst.Response;
}
else
{
question = $"CONTEXT:\r\n{question}";
}
var taskAgentDialogs = new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, question)
{
MessageId = message.MessageId,
}
};
return taskAgentDialogs;
}
public bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
{
dialogs.AddRange(taskAgentDialogs.Skip(1));
// Keep execution context
_executionContext.Add(taskAgentDialogs.Last().Content);
return true;
}
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
return true;
}
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<IRoutingContext>();
if (message.StopCompletion || _isTaskCompleted)
{
context.Empty(reason: $"Agent queue is cleared by {nameof(TwoStagePlanner)}");
return false;
}
var routing = _services.GetRequiredService<IRoutingService>();
routing.ResetRecursiveCounter();
return true;
}
public string GetContext()
{
var content = "";
foreach (var c in _executionContext)
{
content += $"* {c}\r\n";
}
return content;
}
private async Task<FirstStagePlan[]> GetFirstStagePlanAsync(Agent router, string messageId, List<RoleDialogModel> dialogs)
{
/*var fn = _services.GetRequiredService<IRoutingService>();
await fn.InvokeFunction("plan_primary_stage", message);
var items = message.Content.JsonArrayContent<SecondStagePlan>();*/
var firstStagePlanPrompt = await GetFirstStagePlanPrompt(router);
var plan = new FirstStagePlan[0];
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
var provider = router.LlmConfig.Provider ?? "openai";
var model = llmProviderService.GetProviderModel(provider, router.LlmConfig.Model ?? "gpt-4o");
// chat completion
var completion = CompletionProvider.GetChatCompletion(_services,
provider: provider,
model: model.Name);
string text = string.Empty;
try
{
var response = await completion.GetChatCompletions(new Agent
{
Id = router.Id,
Name = nameof(TwoStagePlanner),
Instruction = firstStagePlanPrompt
}, dialogs);
text = response.Content;
plan = response.Content.JsonArrayContent<FirstStagePlan>();
}
catch (Exception ex)
{
_logger.LogError($"{ex.Message}: {text}");
}
return plan;
}
private async Task<string> GetFirstStagePlanPrompt(Agent router)
{
var template = router.Templates.First(x => x.Name == "two_stage.1st.plan").Content;
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
{
Parameters = new JsonDocument[] { JsonDocument.Parse("{}") },
Results = new string[] { "" }
});
var relevantKnowledges = new List<string>();
var hooks = _services.GetServices<IKnowledgeHook>();
foreach (var hook in hooks)
{
var k = await hook.GetRelevantKnowledges();
relevantKnowledges.AddRange(k);
}
var render = _services.GetRequiredService<ITemplateRender>();
return render.Render(template, new Dictionary<string, object>
{
{ "response_format", responseFormat },
{ "relevant_knowledges", relevantKnowledges.ToArray() }
});
}
private string GetFirstStageNextPrompt(Agent router)
{
var template = router.Templates.First(x => x.Name == "first_stage.next").Content;
var responseFormat = JsonSerializer.Serialize(new FirstStagePlan
{
});
var render = _services.GetRequiredService<ITemplateRender>();
return render.Render(template, new Dictionary<string, object>
{
{ "response_format", responseFormat },
});
}
private async Task<SecondStagePlan[]> GetSecondStagePlanAsync(Agent router, string messageId, FirstStagePlan plan1st, List<RoleDialogModel> dialogs)
{
var secondStagePrompt = GetSecondStagePlanPrompt(router, plan1st);
var firstStageSystemPrompt = await GetFirstStagePlanPrompt(router);
var plan = new SecondStagePlan[0];
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
var model = llmProviderService.GetProviderModel("azure-openai", "gpt-4");
// chat completion
var completion = CompletionProvider.GetChatCompletion(_services,
provider: "azure-openai",
model: model.Name);
string text = string.Empty;
var conversations = dialogs.Where(x => x.Role != AgentRole.Function).ToList();
conversations.Add(new RoleDialogModel(AgentRole.User, secondStagePrompt)
{
CurrentAgentId = router.Id,
MessageId = messageId,
});
try
{
var response = await completion.GetChatCompletions(new Agent
{
Id = router.Id,
Name = nameof(TwoStagePlanner),
Instruction = firstStageSystemPrompt
}, conversations);
text = response.Content;
plan = response.Content.JsonArrayContent<SecondStagePlan>();
}
catch (Exception ex)
{
_logger.LogError($"{ex.Message}: {text}");
}
return plan;
}
private string GetSecondStageTaskPrompt(Agent router, SecondStagePlan plan)
{
var template = router.Templates.First(x => x.Name == "planner_prompt.two_stage.2nd.task").Content;
var render = _services.GetRequiredService<ITemplateRender>();
return render.Render(template, new Dictionary<string, object>
{
{ "task_description", plan.Description },
{ "related_tables", plan.Tables },
{ "input_arguments", JsonSerializer.Serialize(plan.Parameters) },
{ "output_results", JsonSerializer.Serialize(plan.Results) },
});
}
private string GetSecondStagePlanPrompt(Agent router, FirstStagePlan plan)
{
var template = router.Templates.First(x => x.Name == "planner_prompt.two_stage.2nd.plan").Content;
var responseFormat = JsonSerializer.Serialize(new SecondStagePlan
{
Tool = "tool name if task solution provided",
Parameters = new JsonDocument[] { JsonDocument.Parse("{}") },
Results = new string[] { "" }
});
var context = GetContext();
var render = _services.GetRequiredService<ITemplateRender>();
return render.Render(template, new Dictionary<string, object>
{
{ "task_description", plan.Task },
{ "response_format", responseFormat }
});
}
}

View file

@ -0,0 +1,17 @@
{
"id": "282a7128-69a1-44b0-878c-a9159b88f3b9",
"name": "Planner",
"description": "Plan feasible implementation steps for complex problems",
"type": "task",
"createdDateTime": "2023-08-27T10:39:00Z",
"updatedDateTime": "2023-08-27T14:39:00Z",
"iconUrl": "https://e7.pngegg.com/pngimages/775/350/png-clipart-action-plan-computer-icons-plan-miscellaneous-text-thumbnail.png",
"disabled": false,
"isPublic": true,
"profiles": [ "tool" ],
"utilities": [ "two-stage-planner" ],
"llmConfig": {
"provider": "openai",
"model": "gpt-4o-mini"
}
}

View file

@ -0,0 +1,3 @@
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.
Call plan_summary to summarize the final planning steps.

View file

@ -8,15 +8,11 @@
"type": "string",
"description": "User original requirements in detail, don't miss any information especially for those line items, values and numbers."
},
"has_knowledge_reference": {
"type": "boolean",
"description": "If there is knowledge retrieved from memory"
},
"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"
}
},
"required": [ "requirement_detail", "has_knowledge_reference", "question" ]
"required": [ "requirement_detail", "question" ]
}
}

View file

@ -1,18 +1,18 @@
//{
// "name": "plan_secondary_stage",
// "description": "Based on the main tasks of the first phase, plan the implementation steps of the second phase.",
// "parameters": {
// "type": "object",
// "properties": {
// "task_description": {
// "type": "string",
// "description": "task description from primary steps"
// },
// "solution_search_question": {
// "type": "string",
// "description": "Provide solution query text"
// }
// },
// "required": [ "task_description", "solution_search_question" ]
// }
//}
{
"name": "plan_secondary_stage",
"description": "Based on the primary stage planning, make more detail steps of the second stage if the primary stage needs more information.",
"parameters": {
"type": "object",
"properties": {
"task_description": {
"type": "string",
"description": "task description from primary steps"
},
"solution_search_question": {
"type": "string",
"description": "Provide solution query text"
}
},
"required": [ "task_description", "solution_search_question" ]
}
}

View file

@ -0,0 +1,10 @@
{
"name": "plan_summary",
"description": "Based on the planning steps, summarize the planning steps and output final steps.",
"parameters": {
"type": "object",
"properties": {
},
"required": []
}
}

View file

@ -1 +1 @@
For every primary step, you have to call plan_secondary_stage to plan the detail steps to complete the primary step.
For every primary step, if need_additional_information is true, you have to call plan_secondary_stage to plan the detail steps to complete the primary step.

View file

@ -0,0 +1 @@
Remove the unnecessary information, and output the final planning steps.