add agent template

This commit is contained in:
Jicheng Lu 2023-09-12 17:08:58 -05:00
parent 7e3e58aba1
commit 075e9da5bf
11 changed files with 107 additions and 4 deletions

View file

@ -16,6 +16,12 @@ public class Agent
[JsonIgnore]
public string Instruction { get; set; }
/// <summary>
/// Templates
/// </summary>
[JsonIgnore]
public List<AgentTemplate> Templates { get; set; }
/// <summary>
/// Samples
/// </summary>
@ -70,6 +76,12 @@ public class Agent
return this;
}
public Agent SetTemplates(List<AgentTemplate> templates)
{
Templates = templates;
return this;
}
public Agent SetFunctions(List<string> functions)
{
Functions = functions ?? new List<string>();

View file

@ -0,0 +1,18 @@
namespace BotSharp.Abstraction.Agents.Models;
public class AgentTemplate
{
public string Name { get; set; }
public string Content { get; set; }
public AgentTemplate()
{
}
public AgentTemplate(string name, string content)
{
Name = name;
Content = content;
}
}

View file

@ -36,4 +36,6 @@ public interface IBotSharpRepository
Conversation GetConversation(string conversationId);
List<Conversation> GetConversations(string userId);
string GetAgentTemplate(string agentId, string templateName);
}

View file

@ -38,6 +38,7 @@ public partial class AgentService
.SetDescription(foundAgent.Description)
.SetIsPublic(foundAgent.IsPublic)
.SetInstruction(foundAgent.Instruction)
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses);
}
@ -79,7 +80,9 @@ public partial class AgentService
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
return agent.SetInstruction(instruction)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses);
}
@ -97,6 +100,25 @@ public partial class AgentService
return instruction;
}
private List<AgentTemplate> FetchTemplatesFromFile(string fileDir)
{
var templates = new List<AgentTemplate>();
foreach (var file in Directory.GetFiles(fileDir))
{
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
var splits = fileName.ToLower().Split('.');
var name = splits[0];
var extension = splits[1];
if (name != "instruction" && extension == "liquid")
{
var content = File.ReadAllText(file);
templates.Add(new AgentTemplate(name, content));
}
}
return templates;
}
private List<string> FetchFunctionsFromFile(string fileDir)
{
var file = Path.Combine(fileDir, "functions.json");

View file

@ -151,4 +151,9 @@ public class BotSharpDbContext : Database, IBotSharpRepository
{
throw new NotImplementedException();
}
public string GetAgentTemplate(string agentId, string templateName)
{
throw new NotImplementedException();
}
}

View file

@ -4,7 +4,6 @@ using FunctionDef = BotSharp.Abstraction.Functions.Models.FunctionDef;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core.Repository;
public class FileRepository : IBotSharpRepository
@ -158,7 +157,7 @@ public class FileRepository : IBotSharpRepository
var filePath = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, "routing-profile.json");
if (File.Exists(filePath))
{
_routingProfiles = JsonSerializer.Deserialize<List<RoutingProfile>>(File.ReadAllText(filePath).ToLower(), _options);
_routingProfiles = JsonSerializer.Deserialize<List<RoutingProfile>>(File.ReadAllText(filePath), _options);
}
return _routingProfiles.AsQueryable();
@ -560,4 +559,25 @@ public class FileRepository : IBotSharpRepository
return records;
}
public string GetAgentTemplate(string agentId, string templateName)
{
var fileDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId);
if (!Directory.Exists(fileDir)) return string.Empty;
var lowerTemplateName = templateName?.ToLower();
foreach (var file in Directory.GetFiles(fileDir))
{
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
var splits = fileName.ToLower().Split('.');
var name = splits[0];
var extension = splits[1];
if (name == lowerTemplateName && extension == "liquid")
{
return File.ReadAllText(file);
}
}
return string.Empty;
}
}

View file

@ -7,6 +7,7 @@ public class AgentCreationModel
public string Name { get; set; }
public string Description { get; set; }
public string Instruction { get; set; }
public List<AgentTemplate> Templates { get; set; }
public List<string> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
public bool IsPublic { get; set; }
@ -18,6 +19,7 @@ public class AgentCreationModel
Name = Name,
Description = Description,
Instruction = Instruction,
Templates = Templates,
Functions = Functions,
Responses = Responses,
IsPublic = IsPublic

View file

@ -12,6 +12,11 @@ public class AgentUpdateModel
/// </summary>
public string? Instruction { get; set; }
/// <summary>
/// Templates
/// </summary>
public List<AgentTemplate>? Templates { get; set; }
/// <summary>
/// Samples
/// </summary>
@ -20,12 +25,12 @@ public class AgentUpdateModel
/// <summary>
/// Functions
/// </summary>
public List<string> Functions { get; set; }
public List<string>? Functions { get; set; }
/// <summary>
/// Routes
/// </summary>
public List<AgentResponse> Responses { get; set; }
public List<AgentResponse>? Responses { get; set; }
public Agent ToAgent()
{
@ -40,6 +45,9 @@ public class AgentUpdateModel
if (Instruction != null)
agent.Instruction = Instruction;
if (!Templates.IsNullOrEmpty())
agent.Templates = Templates;
if (Samples != null)
agent.Samples = Samples;

View file

@ -8,6 +8,7 @@ public class AgentViewModel
public string Name { get; set; }
public string Description { get; set; }
public string Instruction { get; set; }
public List<AgentTemplate> Templates { get; set; }
public List<string> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
public bool IsPublic { get; set; }
@ -21,6 +22,7 @@ public class AgentViewModel
Name = agent.Name,
Description = agent.Description,
Instruction = agent.Instruction,
Templates = agent.Templates,
Functions = agent.Functions,
Responses = agent.Responses,
IsPublic= agent.IsPublic,

View file

@ -7,6 +7,7 @@ public class AgentCollection : MongoBase
public string Name { get; set; }
public string Description { get; set; }
public string Instruction { get; set; }
public List<AgentTemplate> Templates { get; set; }
public List<string> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
public bool IsPublic { get; set; }

View file

@ -39,6 +39,7 @@ public class MongoRepository : IBotSharpRepository
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Templates = x.Templates,
Functions = x.Functions,
Responses = x.Responses,
IsPublic = x.IsPublic,
@ -250,6 +251,7 @@ public class MongoRepository : IBotSharpRepository
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Templates = x.Templates,
Functions = x.Functions,
Responses = x.Responses,
IsPublic = x.IsPublic,
@ -264,6 +266,7 @@ public class MongoRepository : IBotSharpRepository
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.Set(x => x.Instruction, agent.Instruction)
.Set(x => x.Templates, agent.Templates)
.Set(x => x.Functions, agent.Functions)
.Set(x => x.Responses, agent.Responses)
.Set(x => x.IsPublic, agent.IsPublic)
@ -596,4 +599,12 @@ public class MongoRepository : IBotSharpRepository
return records;
}
public string GetAgentTemplate(string agentId, string templateName)
{
var agent = Agents.FirstOrDefault(x => x.Id == agentId);
if (agent == null) return string.Empty;
return agent.Templates?.FirstOrDefault(x => x.Name == templateName.ToLower())?.Content ?? string.Empty;
}
}