Skip to content

change to agent utility #524

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

Merged
merged 2 commits into from
Jul 2, 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 @@ -17,7 +17,7 @@ public enum AgentField
Response,
Sample,
LlmConfig,
Tool
Utility
}

public enum AgentTaskField
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ public interface IAgentService

PluginDef GetPlugin(string agentId);

IEnumerable<string> GetAgentTools();
IEnumerable<string> GetAgentUtilities();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Agents;

public interface IAgentUtilityHook
{
void AddUtilities(List<string> utilities);
}
10 changes: 5 additions & 5 deletions src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -127,7 +127,7 @@ public static Agent Clone(Agent 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,
Expand Down Expand Up @@ -169,9 +169,9 @@ public Agent SetSamples(List<string> samples)
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
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;
Expand Down Expand Up @@ -94,7 +94,7 @@ public async Task<string> UpdateAgentFromFile(string id)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses)
.SetSamples(foundAgent.Samples)
.SetTools(foundAgent.Tools)
.SetUtilities(foundAgent.Utilities)
.SetLlmConfig(foundAgent.LlmConfig);

_db.UpdateAgent(clonedAgent, AgentField.All);
Expand Down
11 changes: 5 additions & 6 deletions src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ public List<Agent> GetAgentsByUser(string userId)
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();
}
}
2 changes: 1 addition & 1 deletion src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
services.AddScoped<IBotSharpFileService, BotSharpFileService>();

services.AddScoped<IAgentHook, FileAnalyzerHook>();
services.AddScoped<IAgentToolHook, FileAnalyzerToolHook>();
services.AddScoped<IAgentUtilityHook, FileAnalyzerUtilityHook>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,7 +31,7 @@ public async Task<bool> Execute(RoleDialogModel message)
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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,7 +15,7 @@ public override void OnAgentLoaded(Agent agent)
{
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)
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public override void OnAgentLoaded(Agent agent)
{
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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BotSharp.Core.Files.Hooks;

public class FileAnalyzerUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(AgentUtility.FileAnalyzer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void UpdateAgent(Agent agent, AgentField field)
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);
Expand Down Expand Up @@ -149,14 +149,14 @@ private void UpdateAgentProfiles(string agentId, List<string> profiles)
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);
Expand Down Expand Up @@ -301,7 +301,7 @@ private void UpdateAgentAllFields(Agent inputAgent)
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
You are a tool agent.
You are a utility agent.
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ public async Task<bool> DeleteAgent([FromRoute] string agentId)
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();
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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; }

Expand All @@ -58,7 +59,7 @@ public Agent ToAgent()
Functions = Functions,
Responses = Responses,
Samples = Samples,
Tools = Tools,
Utilities = Utilities,
IsPublic = IsPublic,
Type = Type,
Disabled = Disabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,7 +76,7 @@ public Agent ToAgent()
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
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -64,7 +64,7 @@ public static AgentViewModel FromAgent(Agent agent)
Functions = agent.Functions,
Responses = agent.Responses,
Samples = agent.Samples,
Tools = agent.Tools,
Utilities = agent.Utilities,
IsPublic= agent.IsPublic,
Disabled = agent.Disabled,
IconUrl = agent.IconUrl,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BotSharp.Plugin.HttpHandler.Enums;

public class Tool
public class Utility
{
public const string HttpHandler = "http-handler";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,7 +21,7 @@ public override void OnAgentLoaded(Agent agent)
{
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)
{
Expand Down Expand Up @@ -51,7 +51,7 @@ public override void OnAgentLoaded(Agent agent)
{
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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
});

services.AddScoped<IAgentHook, HttpHandlerHook>();
services.AddScoped<IAgentToolHook, HttpHandlerToolHook>();
services.AddScoped<IAgentUtilityHook, HttpHandlerUtilityHook>();
}
}
Loading