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

Allow agent to inherit from other agent. #275

Merged
merged 2 commits into from
Jan 29, 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
11 changes: 9 additions & 2 deletions src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class Agent
/// Templates
/// </summary>
[JsonIgnore]
public List<AgentTemplate>? Templates { get; set; }
public List<AgentTemplate> Templates { get; set; }
= new List<AgentTemplate>();

/// <summary>
/// Samples
Expand All @@ -46,7 +47,8 @@ public class Agent
/// Functions
/// </summary>
[JsonIgnore]
public List<FunctionDef> Functions { get; set; } = new List<FunctionDef>();
public List<FunctionDef> Functions { get; set; }
= new List<FunctionDef>();

/// <summary>
/// Responses
Expand Down Expand Up @@ -80,6 +82,11 @@ public class Agent
public List<string> Profiles { get; set; }
= new List<string>();

/// <summary>
/// Inherit from agent
/// </summary>
public string? InheritAgentId { get; set; }

public List<RoutingRule> RoutingRules { get; set; }
= new List<RoutingRule>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace BotSharp.Abstraction.Plugins;

public class PluginDependencyAttribute : Attribute
{
public string[] PluginNames { get; set; }

public PluginDependencyAttribute(params string[] pluginNames)
{
PluginNames = pluginNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class RoutingRule

public override string ToString()
{
return $"{AgentName} {Field}";
return $"{Type} {AgentName} {Field}";
}

public RoutingRule()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing;

namespace BotSharp.Abstraction.Planning;
namespace BotSharp.Abstraction.Routing.Planning;

public interface IExecutor
{
Task<RoleDialogModel> Execute(IRoutingService routing,
Task<RoleDialogModel> Execute(IRoutingService routing,
FunctionCallFromLlm inst,
RoleDialogModel message,
List<RoleDialogModel> dialogs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BotSharp.Abstraction.Functions.Models;

namespace BotSharp.Abstraction.Planning;
namespace BotSharp.Abstraction.Routing.Planning;

/// <summary>
/// Task breakdown and execution plan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ public async Task<Agent> LoadAgent(string id)
throw new Exception($"Can't load agent by id: {id}");
}

if (agent.InheritAgentId != null)
{
var inheritedAgent = await GetAgent(agent.InheritAgentId);
agent.Templates.AddRange(inheritedAgent.Templates
// exclude private template
.Where(x => !x.Name.StartsWith("."))
// exclude duplicate name
.Where(x => !agent.Templates.Exists(t => t.Name == x.Name)));

agent.Functions.AddRange(inheritedAgent.Functions
// exclude private template
.Where(x => !x.Name.StartsWith("."))
// exclude duplicate name
.Where(x => !agent.Functions.Exists(t => t.Name == x.Name)));

if (agent.Instruction == null)
{
agent.Instruction = inheritedAgent.Instruction;
}
}

agent.TemplateDict = new Dictionary<string, object>();

// Populate state into dictionary
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\instruction.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\agent.json" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\instruction.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\.welcome.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.hf.liquid" />
<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.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\welcome.liquid" />
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\agent.json" />
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\instruction.liquid" />
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.executor.liquid" />
Expand Down Expand Up @@ -87,7 +87,7 @@
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\response_with_function.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\welcome.liquid">
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\.welcome.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\agent.json">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Planning;
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Abstraction.Settings;
using BotSharp.Abstraction.Templating;
using BotSharp.Core.Instructs;
using BotSharp.Core.Messaging;
using BotSharp.Core.Planning;
using BotSharp.Core.Routing.Planning;
using BotSharp.Core.Templating;
using Microsoft.Extensions.Configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
var loger = provider.GetRequiredService<ILogger<LlmProviderPlugin>>();
var llmProviders = settingService.Bind<List<LlmProviderSetting>>("LlmProviders");
foreach (var llmProvider in llmProviders)
{
Console.WriteLine($"Loaded LlmProvider {llmProvider.Provider} settings with {llmProvider.Models.Count} models.");
loger.LogInformation($"Loaded LlmProvider {llmProvider.Provider} settings with {llmProvider.Models.Count} models.");
}
return llmProviders;
});
Expand Down
8 changes: 8 additions & 0 deletions src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public InstructService(IServiceProvider services, ILogger<InstructService> logge
_logger = logger;
}

/// <summary>
/// Execute completion by using specified instruction or template
/// </summary>
/// <param name="agentId">Agent (static agent)</param>
/// <param name="message">Additional message provided by user</param>
/// <param name="templateName">Template name</param>
/// <param name="instruction">System prompt</param>
/// <returns></returns>
public async Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null, string? instruction = null)
{
var agentService = _services.GetRequiredService<IAgentService>();
Expand Down
80 changes: 60 additions & 20 deletions src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ public class PluginLoader
_settings = settings;
}

public void Load(Action<Assembly> loaded)
public void Load(Action<Assembly> loaded, string? plugin = null)
{
_executingDir = Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName;

_settings.Assemblies.ToList().ForEach(assemblyName =>
{
if (plugin != null && plugin != assemblyName)
{
return;
}

var assemblyPath = Path.Combine(_executingDir, assemblyName + ".dll");
if (File.Exists(assemblyPath))
{
Expand All @@ -45,38 +50,66 @@ public void Load(Action<Assembly> loaded)

foreach (var module in modules)
{
module.RegisterDI(_services, _config);
// string classSummary = GetSummaryComment(module.GetType());
var name = string.IsNullOrEmpty(module.Name) ? module.GetType().Name : module.Name;
_modules.Add(module);
_plugins.Add(new PluginDef
if (_plugins.Exists(x => x.Id == module.Id))
{
Id = module.Id,
Name = name,
Module = module,
Description = module.Description,
Assembly = assemblyName,
IconUrl = module.IconUrl,
AgentIds = module.AgentIds
});
Console.Write($"Loaded plugin ");
Console.Write(name, Color.Green);
Console.WriteLine($" from {assemblyName}.");
if (!string.IsNullOrEmpty(module.Description))
continue;
}

// Solve plugin dependency
var attr = module.GetType().GetCustomAttribute<PluginDependencyAttribute>();
if (attr != null)
{
Console.WriteLine(module.Description);
foreach (var plugin in attr.PluginNames)
{
if (!_plugins.Any(x => x.Assembly == plugin))
{
Load(loaded, plugin);
}

if (!_plugins.Any(x => x.Assembly == plugin))
{
Console.WriteLine($"Load dependent plugin {plugin} failed by {module.Name}.", Color.Red);
}
}
}

InitModule(assemblyName, module);
}

loaded(assembly);
}
else
{
Console.WriteLine($"Can't find assemble {assemblyPath}.");
Console.WriteLine($"Can't find assemble {assemblyPath}.", Color.Red);
}
});
}

private void InitModule(string assembly, IBotSharpPlugin module)
{
module.RegisterDI(_services, _config);
// string classSummary = GetSummaryComment(module.GetType());
var name = string.IsNullOrEmpty(module.Name) ? module.GetType().Name : module.Name;
_modules.Add(module);
_plugins.Add(new PluginDef
{
Id = module.Id,
Name = name,
Module = module,
Description = module.Description,
Assembly = assembly,
IconUrl = module.IconUrl,
AgentIds = module.AgentIds
});
Console.Write($"Loaded plugin ");
Console.Write(name, Color.Green);
Console.WriteLine($" from {assembly}.");
if (!string.IsNullOrEmpty(module.Description))
{
Console.WriteLine(module.Description);
}
}

public List<PluginDef> GetPlugins(IServiceProvider services)
{
// Apply user configurations
Expand Down Expand Up @@ -124,6 +157,13 @@ public PluginDef UpdatePluginStatus(IServiceProvider services, string id, bool e
var agent = agentService.LoadAgent(agentId).Result;
agent.Disabled = false;
agentService.UpdateAgent(agent, AgentField.Disabled);

if (agent.InheritAgentId != null)
{
agent = agentService.LoadAgent(agent.InheritAgentId).Result;
agent.Disabled = false;
agentService.UpdateAgent(agent, AgentField.Disabled);
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ private string GetAgentDataDir(string agentId)
return (agent, agentFile);
}

private string FetchInstruction(string fileDir)
private string? FetchInstruction(string fileDir)
{
var file = Path.Combine(fileDir, $"{AGENT_INSTRUCTION_FILE}.{_agentSettings.TemplateFormat}");
if (!File.Exists(file)) return string.Empty;
if (!File.Exists(file)) return null;

var instruction = File.ReadAllText(file);
return instruction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Core.Planning;
using BotSharp.Core.Routing.Planning;

namespace BotSharp.Core.Routing.Handlers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Core.Planning;
using BotSharp.Core.Routing.Planning;

namespace BotSharp.Core.Routing.Handlers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Core.Planning;
using BotSharp.Core.Routing.Planning;

namespace BotSharp.Core.Routing.Handlers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Core.Planning;
using BotSharp.Core.Routing.Planning;

namespace BotSharp.Core.Routing.Handlers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Planning;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Abstraction.Templating;

namespace BotSharp.Core.Planning;
namespace BotSharp.Core.Routing.Planning;

/// <summary>
/// Human feedback based planner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Planning;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Planning;

namespace BotSharp.Core.Planning;
namespace BotSharp.Core.Routing.Planning;

public class InstructExecutor : IExecutor
{
Expand Down
Loading