Merge pull request #591 from iceljc/features/add-prompt-by-channel

Features/add prompt by channel
This commit is contained in:
iceljc 2024-08-14 10:46:58 -05:00 committed by GitHub
commit 501115f54c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 388 additions and 233 deletions

View file

@ -20,6 +20,13 @@ public interface IAgentService
/// <returns></returns>
Task<Agent> LoadAgent(string id);
/// <summary>
/// Inherit from an agent
/// </summary>
/// <param name="agent"></param>
/// <returns></returns>
Task InheritAgent(Agent agent);
string RenderedInstruction(Agent agent);
string RenderedTemplate(Agent agent, string templateName);

View file

@ -1,6 +1,5 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Tasks.Models;
namespace BotSharp.Abstraction.Agents.Models;
@ -21,8 +20,7 @@ public class Agent
/// Default LLM settings
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public AgentLlmConfig LlmConfig { get; set; }
= new AgentLlmConfig();
public AgentLlmConfig LlmConfig { get; set; } = new();
/// <summary>
/// Instruction
@ -30,33 +28,35 @@ public class Agent
[JsonIgnore]
public string? Instruction { get; set; }
/// <summary>
/// Channel instructions
/// </summary>
[JsonIgnore]
public List<ChannelInstruction> ChannelInstructions { get; set; } = new();
/// <summary>
/// Templates
/// </summary>
[JsonIgnore]
public List<AgentTemplate> Templates { get; set; }
= new List<AgentTemplate>();
public List<AgentTemplate> Templates { get; set; } = new();
/// <summary>
/// Agent tasks
/// </summary>
[JsonIgnore]
public List<AgentTask> Tasks { get; set; }
= new List<AgentTask>();
public List<AgentTask> Tasks { get; set; } = new();
/// <summary>
/// Samples
/// </summary>
[JsonIgnore]
public List<string> Samples { get; set; }
= new List<string>();
public List<string> Samples { get; set; } = new();
/// <summary>
/// Functions
/// </summary>
[JsonIgnore]
public List<FunctionDef> Functions { get; set; }
= new List<FunctionDef>();
public List<FunctionDef> Functions { get; set; } = new();
/// <summary>
/// Responses
@ -93,23 +93,20 @@ public class Agent
/// <summary>
/// Agent utilities
/// </summary>
public List<string> Utilities { get; set; }
= new List<string>();
public List<string> Utilities { get; set; } = new();
/// <summary>
/// Inherit from agent
/// </summary>
public string? InheritAgentId { get; set; }
public List<RoutingRule> RoutingRules { get; set; }
= new List<RoutingRule>();
public List<RoutingRule> RoutingRules { get; set; } = new();
/// <summary>
/// For rendering deferral
/// </summary>
[JsonIgnore]
public Dictionary<string, object> TemplateDict { get; set; }
= new Dictionary<string, object>();
public Dictionary<string, object> TemplateDict { get; set; } = new();
public override string ToString()
=> $"{Name} {Id}";
@ -124,6 +121,7 @@ public class Agent
Description = agent.Description,
Type = agent.Type,
Instruction = agent.Instruction,
ChannelInstructions = agent.ChannelInstructions,
Functions = agent.Functions,
Responses = agent.Responses,
Samples = agent.Samples,
@ -145,6 +143,12 @@ public class Agent
return this;
}
public Agent SetChannelInstructions(List<ChannelInstruction> instructions)
{
ChannelInstructions = instructions ?? new List<ChannelInstruction>();
return this;
}
public Agent SetTemplates(List<AgentTemplate> templates)
{
Templates = templates ?? new List<AgentTemplate>();

View file

@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Agents.Models;
public class ChannelInstruction
{
public string Channel { get; set; }
public string Instruction { get; set; }
}

View file

@ -8,7 +8,12 @@ public class FunctionDef
[JsonPropertyName("description")]
public string Description { get; set; } = null!;
[JsonPropertyName("channels")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<string>? Channels { get; set; }
[JsonPropertyName("visibility_expression")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? VisibilityExpression { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]

View file

@ -2,7 +2,7 @@ namespace BotSharp.Abstraction.Knowledges.Models;
public class KnowledgeSearchResult
{
public IDictionary<string, string> Data { get; set; } = new Dictionary<string, string>();
public Dictionary<string, string> Data { get; set; } = new();
public double Score { get; set; }
public float[]? Vector { get; set; }
}

View file

@ -41,44 +41,52 @@ public partial class AgentService
});
Utilities.ClearCache();
return await Task.FromResult(agentRecord);
}
private Agent FetchAgentFileByName(string agentName, string filePath)
private (string, List<ChannelInstruction>) GetInstructionsFromFile(string fileDir)
{
foreach (var dir in Directory.GetDirectories(filePath))
var defaultInstruction = string.Empty;
var channelInstructions = new List<ChannelInstruction>();
var instructionDir = Path.Combine(fileDir, "instructions");
if (!Directory.Exists(instructionDir))
{
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
if (agent != null && agent.Name.IsEqualTo(agentName))
{
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
var samples = FetchSamplesFromFile(dir);
return agent.SetInstruction(instruction)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses)
.SetSamples(samples);
}
return (defaultInstruction, channelInstructions);
}
return null;
foreach (var file in Directory.GetFiles(instructionDir))
{
var extension = Path.GetExtension(file).Substring(1);
if (!extension.IsEqualTo(_agentSettings.TemplateFormat))
{
continue;
}
var segments = Path.GetFileName(file).Split(".", StringSplitOptions.RemoveEmptyEntries);
if (segments.IsNullOrEmpty() || !segments[0].IsEqualTo("instruction"))
{
continue;
}
if (segments.Length == 2)
{
defaultInstruction = File.ReadAllText(file);
}
else if (segments.Length == 3)
{
var item = new ChannelInstruction
{
Channel = segments[1],
Instruction = File.ReadAllText(file)
};
channelInstructions.Add(item);
}
}
return (defaultInstruction, channelInstructions);
}
private string FetchInstructionFromFile(string fileDir)
{
var file = Path.Combine(fileDir, $"instruction.{_agentSettings.TemplateFormat}");
if (!File.Exists(file)) return null;
var instruction = File.ReadAllText(file);
return instruction;
}
private List<AgentTemplate> FetchTemplatesFromFile(string fileDir)
private List<AgentTemplate> GetTemplatesFromFile(string fileDir)
{
var templates = new List<AgentTemplate>();
var templateDir = Path.Combine(fileDir, "templates");
@ -86,10 +94,10 @@ public partial class AgentService
foreach (var file in Directory.GetFiles(templateDir))
{
var name = Path.GetFileNameWithoutExtension(file);
var extension = Path.GetExtension(file).Substring(1);
if (extension.IsEqualTo(_agentSettings.TemplateFormat))
{
var name = Path.GetFileNameWithoutExtension(file);
var content = File.ReadAllText(file);
templates.Add(new AgentTemplate(name, content));
}
@ -98,7 +106,7 @@ public partial class AgentService
return templates;
}
private List<FunctionDef> FetchFunctionsFromFile(string fileDir)
private List<FunctionDef> GetFunctionsFromFile(string fileDir)
{
var functions = new List<FunctionDef>();
var functionDir = Path.Combine(fileDir, "functions");
@ -125,7 +133,7 @@ public partial class AgentService
return functions;
}
private List<AgentResponse> FetchResponsesFromFile(string fileDir)
private List<AgentResponse> GetResponsesFromFile(string fileDir)
{
var responses = new List<AgentResponse>();
var responseDir = Path.Combine(fileDir, "responses");
@ -143,7 +151,7 @@ public partial class AgentService
return responses;
}
private List<string> FetchSamplesFromFile(string fileDir)
private List<string> GetSamplesFromFile(string fileDir)
{
var file = Path.Combine(fileDir, "samples.txt");
if (!File.Exists(file)) return new List<string>();
@ -152,7 +160,7 @@ public partial class AgentService
return samples?.ToList() ?? new List<string>();
}
private List<AgentTask> FetchTasksFromFile(string fileDir)
private List<AgentTask> GetTasksFromFile(string fileDir)
{
var tasks = new List<AgentTask>();
var taskDir = Path.Combine(fileDir, "tasks");

View file

@ -53,7 +53,29 @@ public partial class AgentService
}
profile.Plugin = GetPlugin(profile.Id);
return profile;
}
public async Task InheritAgent(Agent agent)
{
if (string.IsNullOrWhiteSpace(agent?.InheritAgentId)) return;
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 (string.IsNullOrWhiteSpace(agent.Instruction))
{
agent.Instruction = inheritedAgent.Instruction;
}
}
}

View file

@ -34,31 +34,12 @@ public partial class AgentService
return null;
}
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;
}
}
await InheritAgent(agent);
OverrideInstructionByChannel(agent);
AddOrUpdateParameters(agent);
agent.TemplateDict = new Dictionary<string, object>();
// Populate state into dictionary
agent.TemplateDict = new Dictionary<string, object>();
PopulateState(agent.TemplateDict);
// After agent is loaded
@ -94,6 +75,23 @@ public partial class AgentService
return agent;
}
private void OverrideInstructionByChannel(Agent agent)
{
var instructions = agent.ChannelInstructions;
if (instructions.IsNullOrEmpty()) return;
var state = _services.GetRequiredService<IConversationStateService>();
var channel = state.GetState("channel");
if (string.IsNullOrWhiteSpace(channel))
{
return;
}
var found = instructions.FirstOrDefault(x => x.Channel.IsEqualTo(channel));
agent.Instruction = !string.IsNullOrWhiteSpace(found?.Instruction) ? found.Instruction : agent.Instruction;
}
private void PopulateState(Dictionary<string, object> dict)
{
var conv = _services.GetRequiredService<IConversationService>();
@ -114,18 +112,27 @@ public partial class AgentService
private void AddOrUpdateRoutesParameters(string agentId, List<RoutingRule> routingRules)
{
if(!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes)) parameterTypes = new();
if(!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes))
{
parameterTypes = new();
}
foreach (var rule in routingRules.Where(x => x.Required))
{
if (string.IsNullOrEmpty(rule.FieldType)) continue;
parameterTypes.TryAdd(rule.Field, rule.FieldType);
}
AgentParameterTypes.TryAdd(agentId, parameterTypes);
}
private void AddOrUpdateFunctionsParameters(string agentId, List<FunctionDef> functions)
{
if (!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes)) parameterTypes = new();
if (!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes))
{
parameterTypes = new();
}
var parameters = functions.Select(p => p.Parameters);
foreach (var param in parameters)
{
@ -139,6 +146,7 @@ public partial class AgentService
}
}
}
AgentParameterTypes.TryAdd(agentId, parameterTypes);
}
}

View file

@ -42,19 +42,20 @@ public partial class AgentService
continue;
}
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
var samples = FetchSamplesFromFile(dir);
agent.SetInstruction(instruction)
var (defaultInstruction, channelInstructions) = GetInstructionsFromFile(dir);
var functions = GetFunctionsFromFile(dir);
var responses = GetResponsesFromFile(dir);
var templates = GetTemplatesFromFile(dir);
var samples = GetSamplesFromFile(dir);
agent.SetInstruction(defaultInstruction)
.SetChannelInstructions(channelInstructions)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses)
.SetSamples(samples);
var userAgent = BuildUserAgent(agent.Id, user.Id);
var tasks = FetchTasksFromFile(dir);
var tasks = GetTasksFromFile(dir);
var isAgentDeleted = _db.DeleteAgent(agent.Id);
if (isAgentDeleted)

View file

@ -20,17 +20,32 @@ public partial class AgentService
public bool RenderFunction(Agent agent, FunctionDef def)
{
if (!string.IsNullOrEmpty(def.VisibilityExpression))
var isRender = true;
var channels = def.Channels;
if (channels != null)
{
var state = _services.GetRequiredService<IConversationStateService>();
var channel = state.GetState("channel");
if (!string.IsNullOrWhiteSpace(channel))
{
isRender = isRender && channels.Contains(channel);
}
}
if (!isRender) return false;
if (!string.IsNullOrWhiteSpace(def.VisibilityExpression))
{
var render = _services.GetRequiredService<ITemplateRender>();
var result = render.Render(def.VisibilityExpression, new Dictionary<string, object>
{
{ "states", agent.TemplateDict }
});
return result == "visible";
isRender = isRender && result == "visible";
}
return true;
return isRender;
}
public FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def)

View file

@ -28,6 +28,7 @@ public partial class AgentService
record.Profiles = agent.Profiles ?? new List<string>();
record.RoutingRules = agent.RoutingRules ?? new List<RoutingRule>();
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>();
@ -41,7 +42,6 @@ public partial class AgentService
_db.UpdateAgent(record, updateField);
Utilities.ClearCache();
await Task.CompletedTask;
}
@ -71,7 +71,7 @@ public partial class AgentService
agentSettings.DataDir);
var clonedAgent = Agent.Clone(agent);
var foundAgent = FetchAgentFileById(agent.Id, filePath);
var foundAgent = GetAgentFileById(agent.Id, filePath);
if (foundAgent == null)
{
updateResult = $"Cannot find agent {agent.Name} in file directory: {filePath}";
@ -90,6 +90,7 @@ public partial class AgentService
.SetProfiles(foundAgent.Profiles)
.SetRoutingRules(foundAgent.RoutingRules)
.SetInstruction(foundAgent.Instruction)
.SetChannelInstructions(foundAgent.ChannelInstructions)
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses)
@ -165,7 +166,7 @@ public partial class AgentService
return patchResult;
}
private Agent? FetchAgentFileById(string agentId, string filePath)
private Agent? GetAgentFileById(string agentId, string filePath)
{
if (!Directory.Exists(filePath)) return null;
@ -175,12 +176,13 @@ public partial class AgentService
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
if (agent != null && agent.Id == agentId)
{
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
var samples = FetchSamplesFromFile(dir);
return agent.SetInstruction(instruction)
var (defaultInstruction, channelInstructions) = GetInstructionsFromFile(dir);
var functions = GetFunctionsFromFile(dir);
var responses = GetResponsesFromFile(dir);
var templates = GetTemplatesFromFile(dir);
var samples = GetSamplesFromFile(dir);
return agent.SetInstruction(defaultInstruction)
.SetChannelInstructions(channelInstructions)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses)

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
@ -56,17 +56,17 @@
<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instruction.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instructions\instruction.liquid" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions.json" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions\human_intervention_needed.json" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\instruction.liquid" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\instructions\instruction.liquid" />
<None Remove="data\agents\01fcc3e5-0af7-49e6-ad7a-a760bd12dc4d\agent.json" />
<None Remove="data\agents\01fcc3e5-0af7-49e6-ad7a-a760bd12dc4d\instruction.liquid" />
<None Remove="data\agents\01fcc3e5-0af7-49e6-ad7a-a760bd12dc4d\instructions\instruction.liquid" />
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\agent.json" />
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\instruction.liquid" />
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\instructions\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\instructions\instruction.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\.welcome.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\conversation.summary.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.hf.liquid" />
@ -80,7 +80,7 @@
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\translation_prompt.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_file_prompt.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\instructions\instruction.liquid" />
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.executor.liquid" />
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.reviewer.liquid" />
<None Remove="data\plugins\config.json" />
@ -90,7 +90,7 @@
<Content Include="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\instruction.liquid">
<Content Include="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions\human_intervention_needed.json">
@ -99,19 +99,19 @@
<Content Include="data\agents\01fcc3e5-0af7-49e6-ad7a-a760bd12dc4d\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-0af7-49e6-ad7a-a760bd12dc4d\instruction.liquid">
<Content Include="data\agents\01fcc3e5-0af7-49e6-ad7a-a760bd12dc4d\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\instruction.liquid">
<Content Include="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\instruction.liquid">
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.1st.plan.liquid">
@ -147,7 +147,7 @@
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\instruction.liquid">
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.executor.liquid">
@ -162,7 +162,7 @@
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instruction.liquid">
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_file_prompt.liquid">

View file

@ -1,6 +1,6 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;
using System.IO;
using System.Threading;
namespace BotSharp.Core.Repository
{
@ -37,7 +37,7 @@ namespace BotSharp.Core.Repository
UpdateAgentRoutingRules(agent.Id, agent.RoutingRules);
break;
case AgentField.Instruction:
UpdateAgentInstruction(agent.Id, agent.Instruction);
UpdateAgentInstructions(agent.Id, agent.Instruction, agent.ChannelInstructions);
break;
case AgentField.Function:
UpdateAgentFunctions(agent.Id, agent.Functions);
@ -175,17 +175,30 @@ namespace BotSharp.Core.Repository
File.WriteAllText(agentFile, json);
}
private void UpdateAgentInstruction(string agentId, string instruction)
private void UpdateAgentInstructions(string agentId, string instruction, List<ChannelInstruction> channelInstructions)
{
if (string.IsNullOrWhiteSpace(instruction)) return;
var (agent, agentFile) = GetAgentFromFile(agentId);
if (agent == null) return;
var instructionFile = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir,
agentId, $"{AGENT_INSTRUCTION_FILE}.{_agentSettings.TemplateFormat}");
var instructionDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, AGENT_INSTRUCTIONS_FOLDER);
DeleteBeforeCreateDirectory(instructionDir);
File.WriteAllText(instructionFile, instruction);
// Save default instructions
var instructionFile = Path.Combine(instructionDir, $"{AGENT_INSTRUCTION_FILE}.{_agentSettings.TemplateFormat}");
File.WriteAllText(instructionFile, instruction ?? string.Empty);
Thread.Sleep(100);
// Save channel instructions
foreach (var ci in channelInstructions)
{
if (string.IsNullOrWhiteSpace(ci.Channel)) continue;
var file = Path.Combine(instructionDir, $"{AGENT_INSTRUCTION_FILE}.{ci.Channel}.{_agentSettings.TemplateFormat}");
File.WriteAllText(file, ci.Instruction ?? string.Empty);
Thread.Sleep(100);
}
}
private void UpdateAgentFunctions(string agentId, List<FunctionDef> inputFunctions)
@ -195,14 +208,8 @@ namespace BotSharp.Core.Repository
var (agent, agentFile) = GetAgentFromFile(agentId);
if (agent == null) return;
var functionDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir,
agentId, AGENT_FUNCTIONS_FOLDER);
if (Directory.Exists(functionDir))
{
Directory.Delete(functionDir, true);
}
Directory.CreateDirectory(functionDir);
var functionDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, AGENT_FUNCTIONS_FOLDER);
DeleteBeforeCreateDirectory(functionDir);
foreach (var func in inputFunctions)
{
@ -211,7 +218,7 @@ namespace BotSharp.Core.Repository
var text = JsonSerializer.Serialize(func, _options);
var file = Path.Combine(functionDir, $"{func.Name}.json");
File.WriteAllText(file, text);
Thread.Sleep(200);
Thread.Sleep(100);
}
}
@ -223,16 +230,7 @@ namespace BotSharp.Core.Repository
if (agent == null) return;
var templateDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, AGENT_TEMPLATES_FOLDER);
if (!Directory.Exists(templateDir))
{
Directory.CreateDirectory(templateDir);
}
foreach (var file in Directory.GetFiles(templateDir))
{
File.Delete(file);
}
DeleteBeforeCreateDirectory(templateDir);
foreach (var template in templates)
{
@ -249,15 +247,7 @@ namespace BotSharp.Core.Repository
if (agent == null) return;
var responseDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, AGENT_RESPONSES_FOLDER);
if (!Directory.Exists(responseDir))
{
Directory.CreateDirectory(responseDir);
}
foreach (var file in Directory.GetFiles(responseDir))
{
File.Delete(file);
}
DeleteBeforeCreateDirectory(responseDir);
for (int i = 0; i < responses.Count; i++)
{
@ -308,7 +298,7 @@ namespace BotSharp.Core.Repository
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
UpdateAgentInstruction(inputAgent.Id, inputAgent.Instruction);
UpdateAgentInstructions(inputAgent.Id, inputAgent.Instruction, agent.ChannelInstructions);
UpdateAgentResponses(inputAgent.Id, inputAgent.Responses);
UpdateAgentTemplates(inputAgent.Id, inputAgent.Templates);
UpdateAgentFunctions(inputAgent.Id, inputAgent.Functions);
@ -348,12 +338,13 @@ namespace BotSharp.Core.Repository
var record = JsonSerializer.Deserialize<Agent>(json, _options);
if (record == null) return null;
var instruction = FetchInstruction(dir);
var (defaultInstruction, channelInstructions) = FetchInstructions(dir);
var functions = FetchFunctions(dir);
var samples = FetchSamples(dir);
var templates = FetchTemplates(dir);
var responses = FetchResponses(dir);
return record.SetInstruction(instruction)
return record.SetInstruction(defaultInstruction)
.SetChannelInstructions(channelInstructions)
.SetFunctions(functions)
.SetTemplates(templates)
.SetSamples(samples)
@ -451,13 +442,9 @@ namespace BotSharp.Core.Repository
return true;
}
public void BulkInsertAgents(List<Agent> agents)
{
}
public void BulkInsertAgents(List<Agent> agents) { }
public void BulkInsertUserAgents(List<UserAgent> userAgents)
{
}
public void BulkInsertUserAgents(List<UserAgent> userAgents) { }
public bool DeleteAgents()
{

View file

@ -34,6 +34,7 @@ public partial class FileRepository : IBotSharpRepository
private const string AGENT_TASK_PREFIX = "#metadata";
private const string AGENT_TASK_SUFFIX = "/metadata";
private const string TRANSLATION_MEMORY_FILE = "memory.json";
private const string AGENT_INSTRUCTIONS_FOLDER = "instructions";
private const string AGENT_FUNCTIONS_FOLDER = "functions";
private const string AGENT_TEMPLATES_FOLDER = "templates";
private const string AGENT_RESPONSES_FOLDER = "responses";
@ -123,7 +124,9 @@ public partial class FileRepository : IBotSharpRepository
var agent = JsonSerializer.Deserialize<Agent>(json, _options);
if (agent != null)
{
agent = agent.SetInstruction(FetchInstruction(d))
var (defaultInstruction, channelInstructions) = FetchInstructions(d);
agent = agent.SetInstruction(defaultInstruction)
.SetChannelInstructions(channelInstructions)
.SetFunctions(FetchFunctions(d))
.SetTemplates(FetchTemplates(d))
.SetResponses(FetchResponses(d))
@ -165,6 +168,17 @@ public partial class FileRepository : IBotSharpRepository
#region Private methods
private void DeleteBeforeCreateDirectory(string dir)
{
if (string.IsNullOrWhiteSpace(dir)) return;
if (Directory.Exists(dir))
{
Directory.Delete(dir, true);
}
Directory.CreateDirectory(dir);
}
private string GetAgentDataDir(string agentId)
{
var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId);
@ -186,13 +200,46 @@ public partial class FileRepository : IBotSharpRepository
return (agent, agentFile);
}
private string? FetchInstruction(string fileDir)
private (string, List<ChannelInstruction>) FetchInstructions(string fileDir)
{
var file = Path.Combine(fileDir, $"{AGENT_INSTRUCTION_FILE}.{_agentSettings.TemplateFormat}");
if (!File.Exists(file)) return null;
var defaultInstruction = string.Empty;
var channelInstructions = new List<ChannelInstruction>();
var instruction = File.ReadAllText(file);
return instruction;
var instructionDir = Path.Combine(fileDir, AGENT_INSTRUCTIONS_FOLDER);
if (!Directory.Exists(instructionDir))
{
return (defaultInstruction, channelInstructions);
}
foreach (var file in Directory.GetFiles(instructionDir))
{
var extension = Path.GetExtension(file).Substring(1);
if (!extension.IsEqualTo(_agentSettings.TemplateFormat))
{
continue;
}
var segments = Path.GetFileName(file).Split(".", StringSplitOptions.RemoveEmptyEntries);
if (segments.IsNullOrEmpty() || !segments[0].IsEqualTo(AGENT_INSTRUCTION_FILE))
{
continue;
}
if (segments.Length == 2)
{
defaultInstruction = File.ReadAllText(file);
}
else if (segments.Length == 3)
{
var item = new ChannelInstruction
{
Channel = segments[1],
Instruction = File.ReadAllText(file)
};
channelInstructions.Add(item);
}
}
return (defaultInstruction, channelInstructions);
}
private List<FunctionDef> FetchFunctions(string fileDir)
@ -298,13 +345,14 @@ public partial class FileRepository : IBotSharpRepository
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
if (agent == null) return null;
var instruction = FetchInstruction(agentDir);
var (defaultInstruction, channelInstructions) = FetchInstructions(agentDir);
var functions = FetchFunctions(agentDir);
var samples = FetchSamples(agentDir);
var templates = FetchTemplates(agentDir);
var responses = FetchResponses(agentDir);
return agent.SetInstruction(instruction)
return agent.SetInstruction(defaultInstruction)
.SetChannelInstructions(channelInstructions)
.SetFunctions(functions)
.SetTemplates(templates)
.SetSamples(samples)

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Users.Enums;
namespace BotSharp.OpenAPI.Controllers;
@ -28,17 +27,24 @@ public class AgentController : ControllerBase
[HttpGet("/agent/{id}")]
public async Task<AgentViewModel?> GetAgent([FromRoute] string id)
{
var agents = await GetAgents(new AgentFilter
var pagedAgents = await _agentService.GetAgents(new AgentFilter
{
AgentIds = new List<string> { id }
}, useHook: true);
});
var targetAgent = agents.Items.FirstOrDefault();
if (targetAgent == null) return null;
var foundAgent = pagedAgents.Items.FirstOrDefault();
if (foundAgent == null) return null;
await _agentService.InheritAgent(foundAgent);
var targetAgent = AgentViewModel.FromAgent(foundAgent);
var agentSetting = _services.GetRequiredService<AgentSettings>();
targetAgent.IsHost = targetAgent.Id == agentSetting.HostAgentId;
var redirectAgentIds = targetAgent.RoutingRules
.Where(x => !string.IsNullOrEmpty(x.RedirectTo))
.Select(x => x.RedirectTo).ToList();
.Select(x => x.RedirectTo)
.ToList();
var redirectAgents = await _agentService.GetAgents(new AgentFilter
{
AgentIds = redirectAgentIds
@ -65,39 +71,16 @@ public class AgentController : ControllerBase
}
[HttpGet("/agents")]
public async Task<PagedItems<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter, [FromQuery] bool useHook = false)
public async Task<PagedItems<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter)
{
var agentSetting = _services.GetRequiredService<AgentSettings>();
var pagedAgents = await _agentService.GetAgents(filter);
var items = new List<Agent>();
var agents = new List<AgentViewModel>();
if (useHook)
{
// prerender agent
foreach (var agent in pagedAgents.Items)
{
var renderedAgent = await _agentService.LoadAgent(agent.Id);
items.Add(renderedAgent);
}
// Set IsHost
agents = items.Select(x => AgentViewModel.FromAgent(x)).ToList();
foreach (var agent in agents)
{
agent.IsHost = agentSetting.HostAgentId == agent.Id;
}
}
else
{
items = pagedAgents.Items.ToList();
agents = items.Select(x => AgentViewModel.FromAgent(x)).ToList();
}
var agents = pagedAgents?.Items?.Select(x => AgentViewModel.FromAgent(x))?.ToList() ?? new List<AgentViewModel>();
return new PagedItems<AgentViewModel>
{
Items = agents,
Count = pagedAgents.Count
Count = pagedAgents?.Count ?? 0
};
}

View file

@ -1,7 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Core.Infrastructures;
namespace BotSharp.OpenAPI.ViewModels.Agents;
@ -16,21 +15,26 @@ public class AgentCreationModel
/// </summary>
public string Instruction { get; set; } = string.Empty;
/// <summary>
///
/// </summary>
public List<ChannelInstruction> ChannelInstructions { get; set; } = new();
/// <summary>
/// LLM extensible Instructions in addition to the default Instructions
/// </summary>
public List<AgentTemplate> Templates { get; set; } = new List<AgentTemplate>();
public List<AgentTemplate> Templates { get; set; } = new();
/// <summary>
/// LLM callable function definition
/// </summary>
public List<FunctionDef> Functions { get; set; } = new List<FunctionDef>();
public List<FunctionDef> Functions { get; set; } = new();
/// <summary>
/// Response template
/// </summary>
public List<AgentResponse> Responses { get; set; } = new List<AgentResponse>();
public List<string> Samples { get; set; } = new List<string>();
public List<AgentResponse> Responses { get; set; } = new();
public List<string> Samples { get; set; } = new();
public bool IsPublic { get; set; }
@ -43,9 +47,9 @@ public class AgentCreationModel
/// <summary>
/// Combine different Agents together to form a Profile.
/// </summary>
public List<string> Profiles { get; set; } = new List<string>();
public List<string> Utilities { get; set; } = new List<string>();
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new List<RoutingRuleUpdateModel>();
public List<string> Profiles { get; set; } = new();
public List<string> Utilities { get; set; } = new();
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new();
public AgentLlmConfig? LlmConfig { get; set; }
public Agent ToAgent()
@ -55,6 +59,7 @@ public class AgentCreationModel
Name = Name,
Description = Description,
Instruction = Instruction,
ChannelInstructions = ChannelInstructions,
Templates = Templates,
Functions = Functions,
Responses = Responses,
@ -64,9 +69,7 @@ public class AgentCreationModel
Type = Type,
Disabled = Disabled,
Profiles = Profiles,
RoutingRules = RoutingRules?
.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?
.ToList() ?? new List<RoutingRule>(),
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),
LlmConfig = LlmConfig
};
}

View file

@ -15,6 +15,12 @@ public class AgentUpdateModel
/// </summary>
public string Instruction { get; set; } = string.Empty;
/// <summary>
/// Channel instructions
/// </summary>
[JsonPropertyName("channel_instructions")]
public List<ChannelInstruction>? ChannelInstructions { get; set; }
/// <summary>
/// Templates
/// </summary>
@ -39,11 +45,11 @@ public class AgentUpdateModel
/// Routes
/// </summary>
public List<AgentResponse>? Responses { get; set; }
[JsonPropertyName("is_public")]
public bool IsPublic { get; set; }
[JsonPropertyName("allow_routing")]
[JsonPropertyName("allow_routing")]
public bool AllowRouting { get; set; }
public bool Disabled { get; set; }
@ -52,8 +58,8 @@ public class AgentUpdateModel
/// Profile by channel
/// </summary>
public List<string>? Profiles { get; set; }
[JsonPropertyName("routing_rules")]
[JsonPropertyName("routing_rules")]
public List<RoutingRuleUpdateModel>? RoutingRules { get; set; }
[JsonPropertyName("llm_config")]
@ -69,10 +75,9 @@ public class AgentUpdateModel
Disabled = Disabled,
Type = Type,
Profiles = Profiles ?? new List<string>(),
RoutingRules = RoutingRules?
.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?
.ToList() ?? new List<RoutingRule>(),
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),
Instruction = Instruction ?? string.Empty,
ChannelInstructions = ChannelInstructions ?? new List<ChannelInstruction>(),
Templates = Templates ?? new List<AgentTemplate>(),
Functions = Functions ?? new List<FunctionDef>(),
Responses = Responses ?? new List<AgentResponse>(),

View file

@ -13,6 +13,9 @@ public class AgentViewModel
public string Description { get; set; }
public string Type { get; set; } = AgentType.Task;
public string Instruction { get; set; }
[JsonPropertyName("channel_instructions")]
public List<ChannelInstruction> ChannelInstructions { get; set; }
public List<AgentTemplate> Templates { get; set; }
public List<FunctionDef> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
@ -60,6 +63,7 @@ public class AgentViewModel
Description = agent.Description,
Type = agent.Type,
Instruction = agent.Instruction,
ChannelInstructions = agent.ChannelInstructions,
Templates = agent.Templates,
Functions = agent.Functions,
Responses = agent.Responses,

View file

@ -15,14 +15,14 @@
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_http_request.fn.liquid" />
<None Remove="data\agents\87c458fc-ec5f-40ae-8ed6-05dda8a07523\agent.json" />
<None Remove="data\agents\87c458fc-ec5f-40ae-8ed6-05dda8a07523\functions.json" />
<None Remove="data\agents\87c458fc-ec5f-40ae-8ed6-05dda8a07523\instruction.liquid" />
<None Remove="data\agents\87c458fc-ec5f-40ae-8ed6-05dda8a07523\instructions\instruction.liquid" />
</ItemGroup>
<ItemGroup>
<Content Include="data\agents\87c458fc-ec5f-40ae-8ed6-05dda8a07523\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\87c458fc-ec5f-40ae-8ed6-05dda8a07523\instruction.liquid">
<Content Include="data\agents\87c458fc-ec5f-40ae-8ed6-05dda8a07523\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_http_request.json">

View file

@ -20,7 +20,7 @@
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\agent.json" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\confirm_knowledge_persistence.json" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\memorize_knowledge.json" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instruction.liquid" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instructions\instruction.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\knowledge_retrieval.fn.liquid" />
</ItemGroup>
@ -34,7 +34,7 @@
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\memorize_knowledge.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instruction.liquid">
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\knowledge_retrieval.json">

View file

@ -8,6 +8,7 @@ public class AgentDocument : MongoBase
public string? InheritAgentId { get; set; }
public string? IconUrl { get; set; }
public string Instruction { 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; }

View file

@ -0,0 +1,27 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Plugin.MongoStorage.Models;
public class ChannelInstructionMongoElement
{
public string Channel { get; set; }
public string Instruction { get; set; }
public static ChannelInstructionMongoElement ToMongoElement(ChannelInstruction instruction)
{
return new ChannelInstructionMongoElement
{
Channel = instruction.Channel,
Instruction = instruction.Instruction
};
}
public static ChannelInstruction ToDomainElement(ChannelInstructionMongoElement instruction)
{
return new ChannelInstruction
{
Channel = instruction.Channel,
Instruction = instruction.Instruction
};
}
}

View file

@ -8,6 +8,7 @@ public class FunctionDefMongoElement
{
public string Name { get; set; }
public string Description { get; set; }
public List<string>? Channels { get; set; }
public string? VisibilityExpression { get; set; }
public string? Impact { get; set; }
public FunctionParametersDefMongoElement Parameters { get; set; } = new FunctionParametersDefMongoElement();
@ -23,6 +24,7 @@ public class FunctionDefMongoElement
{
Name = function.Name,
Description = function.Description,
Channels = function.Channels,
VisibilityExpression = function.VisibilityExpression,
Impact = function.Impact,
Parameters = new FunctionParametersDefMongoElement
@ -34,19 +36,20 @@ public class FunctionDefMongoElement
};
}
public static FunctionDef ToDomainElement(FunctionDefMongoElement mongoFunction)
public static FunctionDef ToDomainElement(FunctionDefMongoElement function)
{
return new FunctionDef
{
Name = mongoFunction.Name,
Description = mongoFunction.Description,
VisibilityExpression = mongoFunction.VisibilityExpression,
Impact = mongoFunction.Impact,
Name = function.Name,
Description = function.Description,
Channels = function.Channels,
VisibilityExpression = function.VisibilityExpression,
Impact = function.Impact,
Parameters = new FunctionParametersDef
{
Type = mongoFunction.Parameters.Type,
Properties = JsonSerializer.Deserialize<JsonDocument>(mongoFunction.Parameters.Properties.IfNullOrEmptyAs("{}")),
Required = mongoFunction.Parameters.Required,
Type = function.Parameters.Type,
Properties = JsonSerializer.Deserialize<JsonDocument>(function.Parameters.Properties.IfNullOrEmptyAs("{}")),
Required = function.Parameters.Required,
}
};
}

View file

@ -38,7 +38,7 @@ public partial class MongoRepository
UpdateAgentRoutingRules(agent.Id, agent.RoutingRules);
break;
case AgentField.Instruction:
UpdateAgentInstruction(agent.Id, agent.Instruction);
UpdateAgentInstructions(agent.Id, agent.Instruction, agent.ChannelInstructions);
break;
case AgentField.Function:
UpdateAgentFunctions(agent.Id, agent.Functions);
@ -156,13 +156,17 @@ public partial class MongoRepository
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentInstruction(string agentId, string instruction)
private void UpdateAgentInstructions(string agentId, string instruction, List<ChannelInstruction>? channelInstructions)
{
if (string.IsNullOrWhiteSpace(instruction)) return;
if (string.IsNullOrWhiteSpace(agentId)) return;
var instructionElements = channelInstructions?.Select(x => ChannelInstructionMongoElement.ToMongoElement(x))?
.ToList() ?? new List<ChannelInstructionMongoElement>();
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
var update = Builders<AgentDocument>.Update
.Set(x => x.Instruction, instruction)
.Set(x => x.ChannelInstructions, instructionElements)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
@ -253,6 +257,7 @@ public partial class MongoRepository
.Set(x => x.Profiles, agent.Profiles)
.Set(x => x.RoutingRules, agent.RoutingRules.Select(r => RoutingRuleMongoElement.ToMongoElement(r)).ToList())
.Set(x => x.Instruction, agent.Instruction)
.Set(x => x.ChannelInstructions, agent.ChannelInstructions.Select(i => ChannelInstructionMongoElement.ToMongoElement(i)).ToList())
.Set(x => x.Templates, agent.Templates.Select(t => AgentTemplateMongoElement.ToMongoElement(t)).ToList())
.Set(x => x.Functions, agent.Functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList())
.Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList())
@ -373,6 +378,9 @@ public partial class MongoRepository
IconUrl = x.IconUrl,
Description = x.Description,
Instruction = x.Instruction,
ChannelInstructions = x.ChannelInstructions?
.Select(i => ChannelInstructionMongoElement.ToMongoElement(i))?
.ToList() ?? new List<ChannelInstructionMongoElement>(),
Templates = x.Templates?
.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?
.ToList() ?? new List<AgentTemplateMongoElement>(),
@ -463,6 +471,9 @@ public partial class MongoRepository
IconUrl = agentDoc.IconUrl,
Description = agentDoc.Description,
Instruction = agentDoc.Instruction,
ChannelInstructions = !agentDoc.ChannelInstructions.IsNullOrEmpty() ? agentDoc.ChannelInstructions
.Select(i => ChannelInstructionMongoElement.ToDomainElement(i))
.ToList() : new List<ChannelInstruction>(),
Templates = !agentDoc.Templates.IsNullOrEmpty() ? agentDoc.Templates
.Select(t => AgentTemplateMongoElement.ToDomainElement(t))
.ToList() : new List<AgentTemplate>(),
@ -472,6 +483,10 @@ public partial class MongoRepository
Responses = !agentDoc.Responses.IsNullOrEmpty() ? agentDoc.Responses
.Select(r => AgentResponseMongoElement.ToDomainElement(r))
.ToList() : new List<AgentResponse>(),
RoutingRules = !agentDoc.RoutingRules.IsNullOrEmpty() ? agentDoc.RoutingRules
.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))
.ToList() : new List<RoutingRule>(),
LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agentDoc.LlmConfig),
Samples = agentDoc.Samples ?? new List<string>(),
Utilities = agentDoc.Utilities ?? new List<string>(),
IsPublic = agentDoc.IsPublic,
@ -479,10 +494,6 @@ public partial class MongoRepository
Type = agentDoc.Type,
InheritAgentId = agentDoc.InheritAgentId,
Profiles = agentDoc.Profiles,
RoutingRules = !agentDoc.RoutingRules.IsNullOrEmpty() ? agentDoc.RoutingRules
.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))
.ToList() : new List<RoutingRule>(),
LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agentDoc.LlmConfig)
};
}
}

View file

@ -37,9 +37,12 @@ public partial class MongoRepository
{
Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(),
Name = x.Name,
IconUrl = x.IconUrl,
Description = x.Description,
Instruction = x.Instruction,
IconUrl = x.IconUrl,
ChannelInstructions = x.ChannelInstructions?
.Select(i => ChannelInstructionMongoElement.ToMongoElement(i))?
.ToList() ?? new List<ChannelInstructionMongoElement>(),
Templates = x.Templates?
.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?
.ToList() ?? new List<AgentTemplateMongoElement>(),
@ -71,6 +74,7 @@ public partial class MongoRepository
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.Set(x => x.Instruction, agent.Instruction)
.Set(x => x.ChannelInstructions, agent.ChannelInstructions)
.Set(x => x.Templates, agent.Templates)
.Set(x => x.Functions, agent.Functions)
.Set(x => x.Responses, agent.Responses)

View file

@ -18,7 +18,7 @@
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\lookup_dictionary.json" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_insert.json" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_select.json" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\instruction.liquid" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\instructions\instruction.liquid" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\lookup_dictionary.liquid" />
</ItemGroup>
@ -26,7 +26,7 @@
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\instruction.liquid">
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\lookup_dictionary.liquid">

View file

@ -40,7 +40,7 @@
<None Remove="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\functions\scroll_page.json" />
<None Remove="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\functions\send_http_request.json" />
<None Remove="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\functions\take_screenshot.json" />
<None Remove="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\instruction.liquid" />
<None Remove="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\instructions\instruction.liquid" />
<None Remove="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\templates\extract_data.liquid" />
<None Remove="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\templates\html_parser.liquid" />
<None Remove="README.md" />
@ -50,7 +50,7 @@
<Content Include="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\instruction.liquid">
<Content Include="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\templates\extract_data.liquid">