Syn latest state before rendering prompt.

This commit is contained in:
Haiping Chen 2024-03-12 11:54:20 -05:00
parent 31805fb413
commit a36347ea42
5 changed files with 19 additions and 32 deletions

View file

@ -1,26 +0,0 @@
namespace BotSharp.Core.Agents;
public class AgentConversationHook : ConversationHookBase
{
private readonly IServiceProvider _services;
public AgentConversationHook(IServiceProvider services)
{
_services = services;
}
public override async Task OnStateChanged(string name, string preValue, string currentValue)
{
// Apply new states to agent TemplateDict
var routing = _services.GetRequiredService<IRoutingContext>();
var agentId = routing.GetCurrentAgentId();
if (string.IsNullOrEmpty(agentId))
{
return;
}
var agentService = _services.GetRequiredService<IAgentService>();
var agent = agentService.LoadAgent(agentId).Result;
agent.TemplateDict[name] = currentValue;
}
}

View file

@ -21,7 +21,6 @@ public class AgentPlugin : IBotSharpPlugin
{
services.AddScoped<ILlmProviderService, LlmProviderService>();
services.AddScoped<IAgentService, AgentService>();
services.AddScoped<IConversationHook, AgentConversationHook>();
services.AddScoped(provider =>
{

View file

@ -95,12 +95,24 @@ public partial class AgentService
// render liquid template
var render = _services.GetRequiredService<ITemplateRender>();
var template = agent.Templates.First(x => x.Name == templateName).Content;
// update states
var conv = _services.GetRequiredService<IConversationService>();
foreach (var t in conv.States.GetStates())
{
agent.TemplateDict[t.Key] = t.Value;
}
return render.Render(template, agent.TemplateDict);
}
public string RenderedInstruction(Agent agent)
{
var render = _services.GetRequiredService<ITemplateRender>();
// update states
var conv = _services.GetRequiredService<IConversationService>();
foreach (var t in conv.States.GetStates())
{
agent.TemplateDict[t.Key] = t.Value;
}
return render.Render(agent.Instruction, agent.TemplateDict);
}

View file

@ -1,8 +1,3 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Abstraction.Templating;
@ -93,6 +88,12 @@ public class HFPlanner : IPlaner
{
var template = router.Templates.First(x => x.Name == "planner_prompt.hf").Content;
var render = _services.GetRequiredService<ITemplateRender>();
// update states
var conv = _services.GetRequiredService<IConversationService>();
foreach (var t in conv.States.GetStates())
{
router.TemplateDict[t.Key] = t.Value;
}
var prompt = render.Render(template, router.TemplateDict);
return prompt.Trim();
}

View file

@ -54,6 +54,7 @@ public class UserService : IUserService
db.CreateUser(record);
_logger.LogWarning($"Created new user account: {record.Id} {record.UserName}");
Utilities.ClearCache();
return record;