Skip to content
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

Syn agent template dict once states has been changed. #338

Merged
merged 1 commit into from
Mar 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
26 changes: 26 additions & 0 deletions src/Infrastructure/BotSharp.Core/Agents/AgentConversationHook.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<ILlmProviderService, LlmProviderService>();
services.AddScoped<IAgentService, AgentService>();
services.AddScoped<IConversationHook, AgentConversationHook>();

services.AddScoped(provider =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Templating;

namespace BotSharp.Core.Agents.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,13 +23,9 @@ public HumanInterventionNeededHandler(IServiceProvider services, ILogger<HumanIn

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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -56,7 +56,9 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
}

// restore original function name
if (!message.StopCompletion && message.FunctionName != originalFunctionName)
if (!message.StopCompletion &&
message.FunctionName != originalFunctionName &&
restoreOriginalFunctionName)
{
message.FunctionName = originalFunctionName;
}
Expand Down
5 changes: 0 additions & 5 deletions src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
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,
Expand Down