add agent tools

This commit is contained in:
Jicheng Lu 2024-06-24 22:46:13 -05:00
parent 02b20a0732
commit f65eeddb8e
14 changed files with 83 additions and 8 deletions

View file

@ -16,7 +16,8 @@ public enum AgentField
Template,
Response,
Sample,
LlmConfig
LlmConfig,
Tool
}
public enum AgentTaskField

View file

@ -0,0 +1,8 @@
namespace BotSharp.Abstraction.Agents.Enums;
public class AgentTool
{
public const string FileAnalyzer = "file-analyzer";
public const string ImageGenerator = "image-generator";
public const string HttpHandler = "http-handler";
}

View file

@ -90,6 +90,12 @@ public class Agent
public List<string> Profiles { get; set; }
= new List<string>();
/// <summary>
/// Useful tools
/// </summary>
public List<string> Tools { get; set; }
= new List<string>();
/// <summary>
/// Inherit from agent
/// </summary>
@ -121,6 +127,7 @@ public class Agent
Functions = agent.Functions,
Responses = agent.Responses,
Samples = agent.Samples,
Tools = agent.Tools,
Knowledges = agent.Knowledges,
IsPublic = agent.IsPublic,
Disabled = agent.Disabled,
@ -162,6 +169,12 @@ public class Agent
return this;
}
public Agent SetTools(List<string> tools)
{
Tools = tools ?? new List<string>();
return this;
}
public Agent SetResponses(List<AgentResponse> responses)
{
Responses = responses ?? new List<AgentResponse>(); ;

View file

@ -58,4 +58,6 @@ public interface IConversationService
Task<string> GetConversationSummary(IEnumerable<string> conversationId);
Task<Conversation> GetConversationRecordOrCreateNew(string agentId);
bool IsConversationMode();
}

View file

@ -1,8 +1,6 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Repositories.Enums;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Enums;
using Microsoft.EntityFrameworkCore.Metadata;
using System.IO;
namespace BotSharp.Core.Agents.Services;
@ -34,6 +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>();
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
{
record.LlmConfig = agent.LlmConfig;
@ -95,6 +94,7 @@ public partial class AgentService
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses)
.SetSamples(foundAgent.Samples)
.SetTools(foundAgent.Tools)
.SetLlmConfig(foundAgent.LlmConfig);
_db.UpdateAgent(clonedAgent, AgentField.All);

View file

@ -161,4 +161,9 @@ public partial class ConversationService : IConversationService
return converation;
}
public bool IsConversationMode()
{
return !string.IsNullOrWhiteSpace(_conversationId);
}
}

View file

@ -1,5 +1,3 @@
using System.Text.RegularExpressions;
namespace BotSharp.Core.Files.Hooks;
public class AttachmentProcessingHook : AgentHookBase
@ -15,11 +13,11 @@ public class AttachmentProcessingHook : AgentHookBase
public override void OnAgentLoaded(Agent agent)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var conv = _services.GetRequiredService<IConversationService>();
var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId);
var isConvMode = conv.IsConversationMode();
var isEnabled = !agent.Tools.IsNullOrEmpty() && agent.Tools.Contains(AgentTool.FileAnalyzer);
if (hasConvFiles)
if (isConvMode && isEnabled)
{
var (prompt, loadAttachmentFn) = GetLoadAttachmentFn();
if (loadAttachmentFn != null)

View file

@ -53,6 +53,9 @@ namespace BotSharp.Core.Repository
case AgentField.LlmConfig:
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
break;
case AgentField.Tool:
UpdateAgentTools(agent.Id, agent.Tools);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
break;
@ -145,6 +148,19 @@ namespace BotSharp.Core.Repository
File.WriteAllText(agentFile, json);
}
private void UpdateAgentTools(string agentId, List<string> tools)
{
if (tools == null) return;
var (agent, agentFile) = GetAgentFromFile(agentId);
if (agent == null) return;
agent.Tools = tools;
agent.UpdatedDateTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
private void UpdateAgentRoutingRules(string agentId, List<RoutingRule> rules)
{
if (rules == null) return;
@ -271,6 +287,7 @@ namespace BotSharp.Core.Repository
agent.Disabled = inputAgent.Disabled;
agent.Type = inputAgent.Type;
agent.Profiles = inputAgent.Profiles;
agent.Tools = inputAgent.Tools;
agent.RoutingRules = inputAgent.RoutingRules;
agent.LlmConfig = inputAgent.LlmConfig;
agent.UpdatedDateTime = DateTime.UtcNow;

View file

@ -43,6 +43,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<RoutingRuleUpdateModel> RoutingRules { get; set; } = new List<RoutingRuleUpdateModel>();
public AgentLlmConfig? LlmConfig { get; set; }
@ -57,6 +58,7 @@ public class AgentCreationModel
Functions = Functions,
Responses = Responses,
Samples = Samples,
Tools = Tools,
IsPublic = IsPublic,
Type = Type,
Disabled = Disabled,

View file

@ -25,6 +25,11 @@ public class AgentUpdateModel
/// </summary>
public List<string>? Samples { get; set; }
/// <summary>
/// Tools
/// </summary>
public List<string>? Tools { get; set; }
/// <summary>
/// Functions
/// </summary>
@ -71,6 +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>(),
LlmConfig = LlmConfig
};

View file

@ -17,6 +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; }
[JsonPropertyName("is_public")]
public bool IsPublic { get; set; }
@ -63,6 +64,7 @@ public class AgentViewModel
Functions = agent.Functions,
Responses = agent.Responses,
Samples = agent.Samples,
Tools = agent.Tools,
IsPublic= agent.IsPublic,
Disabled = agent.Disabled,
IconUrl = agent.IconUrl,

View file

@ -14,6 +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 bool IsPublic { get; set; }
public bool Disabled { get; set; }
public List<string> Profiles { get; set; }

View file

@ -57,6 +57,9 @@ public partial class MongoRepository
case AgentField.LlmConfig:
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
break;
case AgentField.Tool:
UpdateAgentTools(agent.Id, agent.Tools);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
break;
@ -218,6 +221,18 @@ public partial class MongoRepository
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentTools(string agentId, List<string> tools)
{
if (tools == 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.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config)
{
var llmConfig = AgentLlmConfigMongoElement.ToMongoElement(config);
@ -244,6 +259,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.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig))
.Set(x => x.IsPublic, agent.IsPublic)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
@ -369,6 +385,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>(),
IsPublic = x.IsPublic,
Type = x.Type,
InheritAgentId = x.InheritAgentId,
@ -458,6 +475,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>(),
IsPublic = agentDoc.IsPublic,
Disabled = agentDoc.Disabled,
Type = agentDoc.Type,

View file

@ -53,6 +53,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>(),
IsPublic = x.IsPublic,
Type = x.Type,
InheritAgentId = x.InheritAgentId,
@ -77,6 +78,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.IsPublic, agent.IsPublic)
.Set(x => x.Type, agent.Type)
.Set(x => x.InheritAgentId, agent.InheritAgentId)