add merge utility

This commit is contained in:
Jicheng Lu 2024-11-20 17:57:44 -06:00
parent b7c619750e
commit 970c449247
9 changed files with 49 additions and 20 deletions

View file

@ -87,8 +87,12 @@ public class Agent
/// <summary>
/// Profile by channel
/// </summary>
public List<string> Profiles { get; set; }
= new List<string>();
public List<string> Profiles { get; set; } = new();
/// <summary>
/// Merge utilities from entry agent
/// </summary>
public bool MergeUtility { get; set; }
/// <summary>
/// Agent utilities
@ -215,6 +219,12 @@ public class Agent
return this;
}
public Agent SetMergeUtility(bool merge)
{
MergeUtility = merge;
return this;
}
public Agent SetAgentType(string type)
{
Type = type;

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Repositories.Enums;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Enums;
using BotSharp.Abstraction.Users.Models;
using System.IO;
@ -28,16 +27,17 @@ public partial class AgentService
record.Description = agent.Description ?? string.Empty;
record.IsPublic = agent.IsPublic;
record.Disabled = agent.Disabled;
record.MergeUtility = agent.MergeUtility;
record.Type = agent.Type;
record.Profiles = agent.Profiles ?? new List<string>();
record.RoutingRules = agent.RoutingRules ?? new List<RoutingRule>();
record.Profiles = agent.Profiles ?? [];
record.RoutingRules = agent.RoutingRules ?? [];
record.Instruction = agent.Instruction ?? string.Empty;
record.ChannelInstructions = agent.ChannelInstructions ?? new List<ChannelInstruction>();
record.Functions = agent.Functions ?? new List<FunctionDef>();
record.Templates = agent.Templates ?? new List<AgentTemplate>();
record.Responses = agent.Responses ?? new List<AgentResponse>();
record.Samples = agent.Samples ?? new List<string>();
record.Utilities = agent.Utilities ?? new List<AgentUtility>();
record.ChannelInstructions = agent.ChannelInstructions ?? [];
record.Functions = agent.Functions ?? [];
record.Templates = agent.Templates ?? [];
record.Responses = agent.Responses ?? [];
record.Samples = agent.Samples ?? [];
record.Utilities = agent.Utilities ?? [];
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
{
record.LlmConfig = agent.LlmConfig;
@ -90,6 +90,7 @@ public partial class AgentService
.SetDescription(foundAgent.Description)
.SetIsPublic(foundAgent.IsPublic)
.SetDisabled(foundAgent.Disabled)
.SetMergeUtility(foundAgent.MergeUtility)
.SetAgentType(foundAgent.Type)
.SetProfiles(foundAgent.Profiles)
.SetRoutingRules(foundAgent.RoutingRules)

View file

@ -55,7 +55,7 @@ namespace BotSharp.Core.Repository
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
break;
case AgentField.Utility:
UpdateAgentUtilities(agent.Id, agent.Utilities);
UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
@ -151,13 +151,14 @@ namespace BotSharp.Core.Repository
File.WriteAllText(agentFile, json);
}
private void UpdateAgentUtilities(string agentId, List<AgentUtility> utilities)
private void UpdateAgentUtilities(string agentId, bool mergeUtility, List<AgentUtility> utilities)
{
if (utilities == null) return;
var (agent, agentFile) = GetAgentFromFile(agentId);
if (agent == null) return;
agent.MergeUtility = mergeUtility;
agent.Utilities = utilities;
agent.UpdatedDateTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
@ -291,6 +292,7 @@ namespace BotSharp.Core.Repository
agent.Description = inputAgent.Description;
agent.IsPublic = inputAgent.IsPublic;
agent.Disabled = inputAgent.Disabled;
agent.MergeUtility = inputAgent.MergeUtility;
agent.Type = inputAgent.Type;
agent.Profiles = inputAgent.Profiles;
agent.Utilities = inputAgent.Utilities;

View file

@ -48,6 +48,9 @@ public class AgentCreationModel
/// Combine different Agents together to form a Profile.
/// </summary>
public List<string> Profiles { get; set; } = new();
public bool MergeUtility { get; set; }
public List<AgentUtility> Utilities { get; set; } = new();
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new();
public AgentLlmConfig? LlmConfig { get; set; }
@ -68,6 +71,7 @@ public class AgentCreationModel
IsPublic = IsPublic,
Type = Type,
Disabled = Disabled,
MergeUtility = MergeUtility,
Profiles = Profiles,
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),
LlmConfig = LlmConfig

View file

@ -31,6 +31,9 @@ public class AgentUpdateModel
/// </summary>
public List<string>? Samples { get; set; }
[JsonPropertyName("merge_utility")]
public bool MergeUtility { get; set; }
/// <summary>
/// Utilities
/// </summary>
@ -73,6 +76,7 @@ public class AgentUpdateModel
Description = Description ?? string.Empty,
IsPublic = IsPublic,
Disabled = Disabled,
MergeUtility = MergeUtility,
Type = Type,
Profiles = Profiles ?? new List<string>(),
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),

View file

@ -20,6 +20,9 @@ public class AgentViewModel
public List<FunctionDef> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
public List<string> Samples { get; set; }
[JsonPropertyName("merge_utility")]
public bool MergeUtility { get; set; }
public List<AgentUtility> Utilities { get; set; }
[JsonPropertyName("is_public")]
@ -33,8 +36,7 @@ public class AgentViewModel
[JsonPropertyName("icon_url")]
public string IconUrl { get; set; }
public List<string> Profiles { get; set; }
= new List<string>();
public List<string> Profiles { get; set; } = new();
[JsonPropertyName("routing_rules")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
@ -74,6 +76,7 @@ public class AgentViewModel
Utilities = agent.Utilities,
IsPublic= agent.IsPublic,
Disabled = agent.Disabled,
MergeUtility = agent.MergeUtility,
IconUrl = agent.IconUrl,
Profiles = agent.Profiles ?? new List<string>(),
RoutingRules = agent.RoutingRules,

View file

@ -8,14 +8,15 @@ public class AgentDocument : MongoBase
public string? InheritAgentId { get; set; }
public string? IconUrl { get; set; }
public string Instruction { get; set; }
public bool IsPublic { get; set; }
public bool Disabled { get; set; }
public bool MergeUtility { get; set; }
public List<ChannelInstructionMongoElement> ChannelInstructions { get; set; }
public List<AgentTemplateMongoElement> Templates { get; set; }
public List<FunctionDefMongoElement> Functions { get; set; }
public List<AgentResponseMongoElement> Responses { get; set; }
public List<string> Samples { get; set; }
public List<AgentUtilityMongoElement> Utilities { get; set; }
public bool IsPublic { get; set; }
public bool Disabled { get; set; }
public List<string> Profiles { get; set; }
public List<RoutingRuleMongoElement> RoutingRules { get; set; }
public AgentLlmConfigMongoElement? LlmConfig { get; set; }

View file

@ -2,7 +2,6 @@ using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing.Models;
using MongoDB.Driver;
namespace BotSharp.Plugin.MongoStorage.Repository;
@ -57,7 +56,7 @@ public partial class MongoRepository
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
break;
case AgentField.Utility:
UpdateAgentUtilities(agent.Id, agent.Utilities);
UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities);
break;
case AgentField.All:
UpdateAgentAllFields(agent);
@ -224,7 +223,7 @@ public partial class MongoRepository
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentUtilities(string agentId, List<AgentUtility> utilities)
private void UpdateAgentUtilities(string agentId, bool mergeUtility, List<AgentUtility> utilities)
{
if (utilities == null) return;
@ -232,6 +231,7 @@ public partial class MongoRepository
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
var update = Builders<AgentDocument>.Update
.Set(x => x.MergeUtility, mergeUtility)
.Set(x => x.Utilities, elements)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
@ -256,6 +256,7 @@ public partial class MongoRepository
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.Set(x => x.Disabled, agent.Disabled)
.Set(x => x.MergeUtility, agent.MergeUtility)
.Set(x => x.Type, agent.Type)
.Set(x => x.Profiles, agent.Profiles)
.Set(x => x.RoutingRules, agent.RoutingRules.Select(r => RoutingRuleMongoElement.ToMongoElement(r)).ToList())
@ -418,6 +419,7 @@ public partial class MongoRepository
Type = x.Type,
InheritAgentId = x.InheritAgentId,
Disabled = x.Disabled,
MergeUtility = x.MergeUtility,
Profiles = x.Profiles,
RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [],
LlmConfig = AgentLlmConfigMongoElement.ToMongoElement(x.LlmConfig),
@ -507,6 +509,7 @@ public partial class MongoRepository
Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [],
IsPublic = agentDoc.IsPublic,
Disabled = agentDoc.Disabled,
MergeUtility = agentDoc.MergeUtility,
Type = agentDoc.Type,
InheritAgentId = agentDoc.InheritAgentId,
Profiles = agentDoc.Profiles,

View file

@ -9,6 +9,7 @@
"disabled": false,
"isPublic": true,
"profiles": [ "planning" ],
"mergeUtility": true,
"utilities": [
{
"name": "two-stage-planner",