Merge pull request #338 from hchen2020/master

Syn agent template dict once states has been changed.
This commit is contained in:
C. Oceania 2024-03-11 13:29:11 -05:00 committed by GitHub
commit e0daa513d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 36 additions and 17 deletions

View file

@ -31,7 +31,7 @@ public interface IRoutingService
List<RoutingHandlerDef> GetHandlers(Agent router);
void ResetRecursiveCounter();
Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs);
Task<bool> InvokeFunction(string name, RoleDialogModel message);
Task<bool> InvokeFunction(string name, RoleDialogModel message, bool restoreOriginalFunctionName = true);
Task<RoleDialogModel> InstructLoop(RoleDialogModel message);
/// <summary>

View file

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

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Agents.Services;

View file

@ -12,7 +12,7 @@ public class HumanInterventionNeededHandler : RoutingHandlerBase, IRoutingHandle
{
new ParameterPropertyDef("reason", "why need customer service"),
new ParameterPropertyDef("summary", "the whole conversation summary with important information"),
new ParameterPropertyDef("response", "response content to user")
new ParameterPropertyDef("response", "tell the user that you are being transferred to customer service")
};
public HumanInterventionNeededHandler(IServiceProvider services, ILogger<HumanInterventionNeededHandler> logger, RoutingSettings settings)
@ -23,13 +23,9 @@ public class HumanInterventionNeededHandler : RoutingHandlerBase, IRoutingHandle
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
{
var response = new RoleDialogModel(AgentRole.Assistant, inst.Response)
{
CurrentAgentId = message.CurrentAgentId,
MessageId = message.MessageId,
StopCompletion = true,
FunctionName = inst.Function
};
var response = RoleDialogModel.From(message,
role: AgentRole.Assistant,
content: inst.Response);
_dialogs.Add(response);

View file

@ -4,7 +4,7 @@ namespace BotSharp.Core.Routing;
public partial class RoutingService
{
public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
public async Task<bool> InvokeFunction(string name, RoleDialogModel message, bool restoreOriginalFunctionName = true)
{
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == name);
if (function == null)
@ -56,7 +56,9 @@ public partial class RoutingService
}
// restore original function name
if (!message.StopCompletion && message.FunctionName != originalFunctionName)
if (!message.StopCompletion &&
message.FunctionName != originalFunctionName &&
restoreOriginalFunctionName)
{
message.FunctionName = originalFunctionName;
}

View file

@ -133,11 +133,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
log += $"\r\n```json\r\n{richContent}\r\n```";
}
if (!string.IsNullOrEmpty(message.FunctionName))
{
log += $"\r\n\r\n**{message.FunctionName}**";
}
var input = new ContentLogInputModel(conv.ConversationId, message)
{
Name = agent?.Name,