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

add common agent hook #783

Merged
merged 1 commit into from
Dec 6, 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
73 changes: 0 additions & 73 deletions src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using BotSharp.Abstraction.Agents.Settings;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing;
using Microsoft.Extensions.DependencyInjection;
using System.Data;

namespace BotSharp.Abstraction.Agents;

Expand Down Expand Up @@ -60,73 +55,5 @@ public virtual void OnAgentLoaded(Agent agent)

public virtual void OnAgentUtilityLoaded(Agent agent)
{
if (agent.Type == AgentType.Routing) return;

var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
if (!isConvMode) return;

agent.Functions ??= [];
agent.Utilities ??= [];

var (functions, templates) = GetUtilityContent(agent);

foreach (var fn in functions)
{
if (!agent.Functions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
{
agent.Functions.Add(fn);
}
}

foreach (var prompt in templates)
{
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
}
}

private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(Agent agent)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var (functionNames, templateNames) = GetUniqueContent(agent.Utilities);

if (agent.MergeUtility)
{
var routing = _services.GetRequiredService<IRoutingContext>();
var entryAgentId = routing.EntryAgentId;
if (!string.IsNullOrEmpty(entryAgentId))
{
var entryAgent = db.GetAgent(entryAgentId);
var (fns, tps) = GetUniqueContent(entryAgent?.Utilities);
functionNames = functionNames.Concat(fns).Distinct().ToList();
templateNames = templateNames.Concat(tps).Distinct().ToList();
}
}

var ua = db.GetAgent(BuiltInAgentId.UtilityAssistant);
var functions = ua?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
var templates = ua?.Templates?.Where(x => templateNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.Select(x => x.Content)?.ToList() ?? [];
return (functions, templates);
}

private (IEnumerable<string>, IEnumerable<string>) GetUniqueContent(IEnumerable<AgentUtility>? utilities)
{
if (utilities.IsNullOrEmpty())
{
return ([], []);
}

var prefix = "util-";
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
var functionNames = utilities.SelectMany(x => x.Functions)
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
.Select(x => x.Name)
.Distinct().ToList();
var templateNames = utilities.SelectMany(x => x.Templates)
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
.Select(x => x.Name)
.Distinct().ToList();

return (functionNames, templateNames);
}
}
2 changes: 2 additions & 0 deletions src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BotSharp.Abstraction.Settings;
using BotSharp.Abstraction.Templating;
using BotSharp.Abstraction.Users.Enums;
using BotSharp.Core.Agents.Hooks;
using Microsoft.Extensions.Configuration;

namespace BotSharp.Core.Agents;
Expand Down Expand Up @@ -30,6 +31,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<ILlmProviderService, LlmProviderService>();
services.AddScoped<IAgentService, AgentService>();
services.AddScoped<IAgentHook, CommonAgentHook>();

services.AddScoped(provider =>
{
Expand Down
83 changes: 83 additions & 0 deletions src/Infrastructure/BotSharp.Core/Agents/Hooks/CommonAgentHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace BotSharp.Core.Agents.Hooks;

public class CommonAgentHook : AgentHookBase
{
public override string SelfId => string.Empty;

public CommonAgentHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
}

public override void OnAgentUtilityLoaded(Agent agent)
{
if (agent.Type == AgentType.Routing) return;

var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
if (!isConvMode) return;

agent.Functions ??= [];
agent.Utilities ??= [];

var (functions, templates) = GetUtilityContent(agent);

foreach (var fn in functions)
{
if (!agent.Functions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
{
agent.Functions.Add(fn);
}
}

foreach (var prompt in templates)
{
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
}
}

private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(Agent agent)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var (functionNames, templateNames) = GetUniqueContent(agent.Utilities);

if (agent.MergeUtility)
{
var routing = _services.GetRequiredService<IRoutingContext>();
var entryAgentId = routing.EntryAgentId;
if (!string.IsNullOrEmpty(entryAgentId))
{
var entryAgent = db.GetAgent(entryAgentId);
var (fns, tps) = GetUniqueContent(entryAgent?.Utilities);
functionNames = functionNames.Concat(fns).Distinct().ToList();
templateNames = templateNames.Concat(tps).Distinct().ToList();
}
}

var ua = db.GetAgent(BuiltInAgentId.UtilityAssistant);
var functions = ua?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
var templates = ua?.Templates?.Where(x => templateNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.Select(x => x.Content)?.ToList() ?? [];
return (functions, templates);
}

private (IEnumerable<string>, IEnumerable<string>) GetUniqueContent(IEnumerable<AgentUtility>? utilities)
{
if (utilities.IsNullOrEmpty())
{
return ([], []);
}

var prefix = "util-";
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
var functionNames = utilities.SelectMany(x => x.Functions)
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
.Select(x => x.Name)
.Distinct().ToList();
var templateNames = utilities.SelectMany(x => x.Templates)
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
.Select(x => x.Name)
.Distinct().ToList();

return (functionNames, templateNames);
}
}