Merge pull request #524 from iceljc/features/add-agent-utility

change to agent utility
This commit is contained in:
C. Oceania 2024-07-02 15:44:36 -05:00 committed by GitHub
commit 85c01d5c01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 84 additions and 86 deletions

View file

@ -17,7 +17,7 @@ public enum AgentField
Response,
Sample,
LlmConfig,
Tool
Utility
}
public enum AgentTaskField

View file

@ -1,6 +1,6 @@
namespace BotSharp.Abstraction.Agents.Enums;
public class AgentTool
public class AgentUtility
{
public const string FileAnalyzer = "file-analyzer";
public const string ImageGenerator = "image-generator";

View file

@ -52,5 +52,5 @@ public interface IAgentService
PluginDef GetPlugin(string agentId);
IEnumerable<string> GetAgentTools();
IEnumerable<string> GetAgentUtilities();
}

View file

@ -1,6 +0,0 @@
namespace BotSharp.Abstraction.Agents;
public interface IAgentToolHook
{
void AddTools(List<string> tools);
}

View file

@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Agents;
public interface IAgentUtilityHook
{
void AddUtilities(List<string> utilities);
}

View file

@ -91,9 +91,9 @@ public class Agent
= new List<string>();
/// <summary>
/// Useful tools
/// Agent utilities
/// </summary>
public List<string> Tools { get; set; }
public List<string> Utilities { get; set; }
= new List<string>();
/// <summary>
@ -127,7 +127,7 @@ public class Agent
Functions = agent.Functions,
Responses = agent.Responses,
Samples = agent.Samples,
Tools = agent.Tools,
Utilities = agent.Utilities,
Knowledges = agent.Knowledges,
IsPublic = agent.IsPublic,
Disabled = agent.Disabled,
@ -169,9 +169,9 @@ public class Agent
return this;
}
public Agent SetTools(List<string> tools)
public Agent SetUtilities(List<string> utilities)
{
Tools = tools ?? new List<string>();
Utilities = utilities ?? new List<string>();
return this;
}

View file

@ -6,7 +6,6 @@ public class AgentSettings
public string TemplateFormat { get; set; } = "liquid";
public string HostAgentId { get; set; } = string.Empty;
public bool EnableTranslator { get; set; } = false;
public bool EnableHttpHandler { get; set; } = false;
/// <summary>
/// This is the default LLM config for agent

View file

@ -32,7 +32,7 @@ public partial class AgentService
record.Templates = agent.Templates ?? new List<AgentTemplate>();
record.Responses = agent.Responses ?? new List<AgentResponse>();
record.Samples = agent.Samples ?? new List<string>();
record.Tools = agent.Tools ?? new List<string>();
record.Utilities = agent.Utilities ?? new List<string>();
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
{
record.LlmConfig = agent.LlmConfig;
@ -94,7 +94,7 @@ public partial class AgentService
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses)
.SetSamples(foundAgent.Samples)
.SetTools(foundAgent.Tools)
.SetUtilities(foundAgent.Utilities)
.SetLlmConfig(foundAgent.LlmConfig);
_db.UpdateAgent(clonedAgent, AgentField.All);

View file

@ -55,15 +55,14 @@ public partial class AgentService : IAgentService
return agents;
}
public IEnumerable<string> GetAgentTools()
public IEnumerable<string> GetAgentUtilities()
{
var tools = new List<string>();
var hooks = _services.GetServices<IAgentToolHook>();
var utilities = new List<string>();
var hooks = _services.GetServices<IAgentUtilityHook>();
foreach (var hook in hooks)
{
hook.AddTools(tools);
hook.AddUtilities(utilities);
}
return tools.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().OrderBy(x => x).ToList();
return utilities.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().OrderBy(x => x).ToList();
}
}

View file

@ -18,6 +18,6 @@ public class FilePlugin : IBotSharpPlugin
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
services.AddScoped<IAgentHook, FileAnalyzerHook>();
services.AddScoped<IAgentToolHook, FileAnalyzerToolHook>();
services.AddScoped<IAgentUtilityHook, FileAnalyzerUtilityHook>();
}
}

View file

@ -12,7 +12,7 @@ public class LoadAttachmentFn : IFunctionCallback
private readonly ILogger<LoadAttachmentFn> _logger;
private readonly IEnumerable<string> _imageTypes = new List<string> { "image", "images", "png", "jpg", "jpeg" };
private readonly IEnumerable<string> _pdfTypes = new List<string> { "pdf" };
private static string TOOL_ASSISTANT = Guid.Empty.ToString();
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
public LoadAttachmentFn(
IServiceProvider services,
@ -31,7 +31,7 @@ public class LoadAttachmentFn : IFunctionCallback
var wholeDialogs = conv.GetDialogHistory();
var fileTypes = args?.FileTypes?.Split(",", StringSplitOptions.RemoveEmptyEntries)?.ToList() ?? new List<string>();
var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs, fileTypes);
var agent = await agentService.LoadAgent(TOOL_ASSISTANT);
var agent = await agentService.LoadAgent(UTILITY_ASSISTANT);
var fileAgent = new Agent
{
Id = agent?.Id ?? Guid.Empty.ToString(),

View file

@ -2,7 +2,7 @@ namespace BotSharp.Core.Files.Hooks;
public class FileAnalyzerHook : AgentHookBase
{
private static string TOOL_ASSISTANT = Guid.Empty.ToString();
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
public override string SelfId => string.Empty;
@ -15,7 +15,7 @@ public class FileAnalyzerHook : AgentHookBase
{
var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
var isEnabled = !agent.Tools.IsNullOrEmpty() && agent.Tools.Contains(AgentTool.FileAnalyzer);
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(AgentUtility.FileAnalyzer);
if (isConvMode && isEnabled)
{
@ -45,7 +45,7 @@ public class FileAnalyzerHook : AgentHookBase
{
var fn = "load_attachment";
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgent(TOOL_ASSISTANT);
var agent = db.GetAgent(UTILITY_ASSISTANT);
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{fn}.fn"))?.Content ?? string.Empty;
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(fn));
return (prompt, loadAttachmentFn);

View file

@ -1,10 +0,0 @@
namespace BotSharp.Core.Files.Hooks;
public class FileAnalyzerToolHook : IAgentToolHook
{
public void AddTools(List<string> tools)
{
tools.Add(AgentTool.FileAnalyzer);
}
}

View file

@ -0,0 +1,9 @@
namespace BotSharp.Core.Files.Hooks;
public class FileAnalyzerUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(AgentUtility.FileAnalyzer);
}
}

View file

@ -54,8 +54,8 @@ namespace BotSharp.Core.Repository
case AgentField.LlmConfig:
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
break;
case AgentField.Tool:
UpdateAgentTools(agent.Id, agent.Tools);
case AgentField.Utility:
UpdateAgentUtilities(agent.Id, agent.Utilities);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
@ -149,14 +149,14 @@ namespace BotSharp.Core.Repository
File.WriteAllText(agentFile, json);
}
private void UpdateAgentTools(string agentId, List<string> tools)
private void UpdateAgentUtilities(string agentId, List<string> utilities)
{
if (tools == null) return;
if (utilities == null) return;
var (agent, agentFile) = GetAgentFromFile(agentId);
if (agent == null) return;
agent.Tools = tools;
agent.Utilities = utilities;
agent.UpdatedDateTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
@ -301,7 +301,7 @@ namespace BotSharp.Core.Repository
agent.Disabled = inputAgent.Disabled;
agent.Type = inputAgent.Type;
agent.Profiles = inputAgent.Profiles;
agent.Tools = inputAgent.Tools;
agent.Utilities = inputAgent.Utilities;
agent.RoutingRules = inputAgent.RoutingRules;
agent.LlmConfig = inputAgent.LlmConfig;
agent.UpdatedDateTime = DateTime.UtcNow;

View file

@ -1,7 +1,7 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"name": "Tool Assistant",
"description": "Tool assistant that can be used to complete many different tasks",
"name": "Utility Assistant",
"description": "Utility assistant that can be used to complete many different tasks",
"type": "static",
"createdDateTime": "2023-06-24T10:39:32.2349685Z",
"updatedDateTime": "2023-06-24T14:39:32.2349686Z",

View file

@ -1 +1 @@
You are a tool agent.
You are a utility agent.

View file

@ -141,9 +141,9 @@ public class AgentController : ControllerBase
return await _agentService.DeleteAgent(agentId);
}
[HttpGet("/agent/tools")]
public IEnumerable<string> GetAgentTools()
[HttpGet("/agent/utilities")]
public IEnumerable<string> GetAgentUtilities()
{
return _agentService.GetAgentTools();
return _agentService.GetAgentUtilities();
}
}

View file

@ -1,6 +1,7 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Core.Infrastructures;
namespace BotSharp.OpenAPI.ViewModels.Agents;
@ -43,7 +44,7 @@ public class AgentCreationModel
/// Combine different Agents together to form a Profile.
/// </summary>
public List<string> Profiles { get; set; } = new List<string>();
public List<string> Tools { get; set; } = new List<string>();
public List<string> Utilities { get; set; } = new List<string>();
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new List<RoutingRuleUpdateModel>();
public AgentLlmConfig? LlmConfig { get; set; }
@ -58,7 +59,7 @@ public class AgentCreationModel
Functions = Functions,
Responses = Responses,
Samples = Samples,
Tools = Tools,
Utilities = Utilities,
IsPublic = IsPublic,
Type = Type,
Disabled = Disabled,

View file

@ -26,9 +26,9 @@ public class AgentUpdateModel
public List<string>? Samples { get; set; }
/// <summary>
/// Tools
/// Utilities
/// </summary>
public List<string>? Tools { get; set; }
public List<string>? Utilities { get; set; }
/// <summary>
/// Functions
@ -76,7 +76,7 @@ public class AgentUpdateModel
Templates = Templates ?? new List<AgentTemplate>(),
Functions = Functions ?? new List<FunctionDef>(),
Responses = Responses ?? new List<AgentResponse>(),
Tools = Tools ?? new List<string>(),
Utilities = Utilities ?? new List<string>(),
LlmConfig = LlmConfig
};

View file

@ -17,7 +17,7 @@ public class AgentViewModel
public List<FunctionDef> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
public List<string> Samples { get; set; }
public List<string> Tools { get; set; }
public List<string> Utilities { get; set; }
[JsonPropertyName("is_public")]
public bool IsPublic { get; set; }
@ -64,7 +64,7 @@ public class AgentViewModel
Functions = agent.Functions,
Responses = agent.Responses,
Samples = agent.Samples,
Tools = agent.Tools,
Utilities = agent.Utilities,
IsPublic= agent.IsPublic,
Disabled = agent.Disabled,
IconUrl = agent.IconUrl,

View file

@ -1,6 +1,6 @@
namespace BotSharp.Plugin.HttpHandler.Enums;
public class Tool
public class Utility
{
public const string HttpHandler = "http-handler";
}

View file

@ -8,7 +8,7 @@ namespace BotSharp.Plugin.HttpHandler.Hooks;
public class HttpHandlerHook : AgentHookBase
{
private static string TOOL_ASSISTANT = Guid.Empty.ToString();
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
public override string SelfId => string.Empty;
@ -21,7 +21,7 @@ public class HttpHandlerHook : AgentHookBase
{
var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
var isEnabled = !agent.Tools.IsNullOrEmpty() && agent.Tools.Contains(Tool.HttpHandler);
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(Utility.HttpHandler);
if (isConvMode && isEnabled)
{
@ -51,7 +51,7 @@ public class HttpHandlerHook : AgentHookBase
{
var fn = "handle_http_request";
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgent(TOOL_ASSISTANT);
var agent = db.GetAgent(UTILITY_ASSISTANT);
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{fn}.fn"))?.Content ?? string.Empty;
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(fn));
return (prompt, loadAttachmentFn);

View file

@ -1,12 +0,0 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Plugin.HttpHandler.Enums;
namespace BotSharp.Plugin.HttpHandler.Hooks;
public class HttpHandlerToolHook : IAgentToolHook
{
public void AddTools(List<string> tools)
{
tools.Add(Tool.HttpHandler);
}
}

View file

@ -0,0 +1,12 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Plugin.HttpHandler.Enums;
namespace BotSharp.Plugin.HttpHandler.Hooks;
public class HttpHandlerUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(Utility.HttpHandler);
}
}

View file

@ -22,6 +22,6 @@ public class HttpHandlerPlugin : IBotSharpPlugin
});
services.AddScoped<IAgentHook, HttpHandlerHook>();
services.AddScoped<IAgentToolHook, HttpHandlerToolHook>();
services.AddScoped<IAgentUtilityHook, HttpHandlerUtilityHook>();
}
}

View file

@ -14,7 +14,7 @@ public class AgentDocument : MongoBase
public List<FunctionDefMongoElement> Functions { get; set; }
public List<AgentResponseMongoElement> Responses { get; set; }
public List<string> Samples { get; set; }
public List<string> Tools { get; set; }
public List<string> Utilities { get; set; }
public bool IsPublic { get; set; }
public bool Disabled { get; set; }
public List<string> Profiles { get; set; }

View file

@ -55,8 +55,8 @@ public partial class MongoRepository
case AgentField.LlmConfig:
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
break;
case AgentField.Tool:
UpdateAgentTools(agent.Id, agent.Tools);
case AgentField.Utility:
UpdateAgentUtilities(agent.Id, agent.Utilities);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
@ -219,13 +219,13 @@ public partial class MongoRepository
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentTools(string agentId, List<string> tools)
private void UpdateAgentUtilities(string agentId, List<string> utilities)
{
if (tools == null) return;
if (utilities == null) return;
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
var update = Builders<AgentDocument>.Update
.Set(x => x.Tools, tools)
.Set(x => x.Utilities, utilities)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
@ -257,7 +257,7 @@ public partial class MongoRepository
.Set(x => x.Functions, agent.Functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList())
.Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList())
.Set(x => x.Samples, agent.Samples)
.Set(x => x.Tools, agent.Tools)
.Set(x => x.Utilities, agent.Utilities)
.Set(x => x.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig))
.Set(x => x.IsPublic, agent.IsPublic)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
@ -383,7 +383,7 @@ public partial class MongoRepository
.Select(r => AgentResponseMongoElement.ToMongoElement(r))?
.ToList() ?? new List<AgentResponseMongoElement>(),
Samples = x.Samples ?? new List<string>(),
Tools = x.Tools ?? new List<string>(),
Utilities = x.Utilities ?? new List<string>(),
IsPublic = x.IsPublic,
Type = x.Type,
InheritAgentId = x.InheritAgentId,
@ -473,7 +473,7 @@ public partial class MongoRepository
.Select(r => AgentResponseMongoElement.ToDomainElement(r))
.ToList() : new List<AgentResponse>(),
Samples = agentDoc.Samples ?? new List<string>(),
Tools = agentDoc.Tools ?? new List<string>(),
Utilities = agentDoc.Utilities ?? new List<string>(),
IsPublic = agentDoc.IsPublic,
Disabled = agentDoc.Disabled,
Type = agentDoc.Type,

View file

@ -50,7 +50,7 @@ public partial class MongoRepository
.Select(r => AgentResponseMongoElement.ToMongoElement(r))?
.ToList() ?? new List<AgentResponseMongoElement>(),
Samples = x.Samples ?? new List<string>(),
Tools = x.Tools ?? new List<string>(),
Utilities = x.Utilities ?? new List<string>(),
IsPublic = x.IsPublic,
Type = x.Type,
InheritAgentId = x.InheritAgentId,
@ -75,7 +75,7 @@ public partial class MongoRepository
.Set(x => x.Functions, agent.Functions)
.Set(x => x.Responses, agent.Responses)
.Set(x => x.Samples, agent.Samples)
.Set(x => x.Tools, agent.Tools)
.Set(x => x.Utilities, agent.Utilities)
.Set(x => x.IsPublic, agent.IsPublic)
.Set(x => x.Type, agent.Type)
.Set(x => x.InheritAgentId, agent.InheritAgentId)