From 075e9da5bfcbc99bd27a4382857f6270ede32f3e Mon Sep 17 00:00:00 2001
From: Jicheng Lu <103353@smsassist.com>
Date: Tue, 12 Sep 2023 17:08:58 -0500
Subject: [PATCH] add agent template
---
.../Agents/Models/Agent.cs | 12 ++++++++++
.../Agents/Models/AgentTemplate.cs | 18 ++++++++++++++
.../Repositories/IBotSharpRepository.cs | 2 ++
.../Services/AgentService.CreateAgent.cs | 22 +++++++++++++++++
.../Repository/BotSharpDbContext.cs | 5 ++++
.../Repository/FileRepository.cs | 24 +++++++++++++++++--
.../ViewModels/Agents/AgentCreationModel.cs | 2 ++
.../ViewModels/Agents/AgentUpdateModel.cs | 12 ++++++++--
.../ViewModels/Agents/AgentViewModel.cs | 2 ++
.../Collections/AgentCollection.cs | 1 +
.../Repository/MongoRepository.cs | 11 +++++++++
11 files changed, 107 insertions(+), 4 deletions(-)
create mode 100644 src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentTemplate.cs
diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
index 0950f97f..9719f27a 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
@@ -16,6 +16,12 @@ public class Agent
[JsonIgnore]
public string Instruction { get; set; }
+ ///
+ /// Templates
+ ///
+ [JsonIgnore]
+ public List Templates { get; set; }
+
///
/// Samples
///
@@ -70,6 +76,12 @@ public class Agent
return this;
}
+ public Agent SetTemplates(List templates)
+ {
+ Templates = templates;
+ return this;
+ }
+
public Agent SetFunctions(List functions)
{
Functions = functions ?? new List();
diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentTemplate.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentTemplate.cs
new file mode 100644
index 00000000..1d065e47
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentTemplate.cs
@@ -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;
+ }
+}
diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs
index 350fbaa4..2582c993 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs
@@ -36,4 +36,6 @@ public interface IBotSharpRepository
Conversation GetConversation(string conversationId);
List GetConversations(string userId);
+
+ string GetAgentTemplate(string agentId, string templateName);
}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs
index 4089d430..b942d7ea 100644
--- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs
@@ -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 FetchTemplatesFromFile(string fileDir)
+ {
+ var templates = new List();
+ 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 FetchFunctionsFromFile(string fileDir)
{
var file = Path.Combine(fileDir, "functions.json");
diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
index 8747735e..e54d0d13 100644
--- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
+++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
@@ -151,4 +151,9 @@ public class BotSharpDbContext : Database, IBotSharpRepository
{
throw new NotImplementedException();
}
+
+ public string GetAgentTemplate(string agentId, string templateName)
+ {
+ throw new NotImplementedException();
+ }
}
diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
index 107cbd50..ef5489ae 100644
--- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
+++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
@@ -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>(File.ReadAllText(filePath).ToLower(), _options);
+ _routingProfiles = JsonSerializer.Deserialize>(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;
+ }
}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs
index 24e92f37..a8e233fd 100644
--- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs
@@ -7,6 +7,7 @@ public class AgentCreationModel
public string Name { get; set; }
public string Description { get; set; }
public string Instruction { get; set; }
+ public List Templates { get; set; }
public List Functions { get; set; }
public List 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
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs
index af38f81a..3d88efc5 100644
--- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs
@@ -12,6 +12,11 @@ public class AgentUpdateModel
///
public string? Instruction { get; set; }
+ ///
+ /// Templates
+ ///
+ public List? Templates { get; set; }
+
///
/// Samples
///
@@ -20,12 +25,12 @@ public class AgentUpdateModel
///
/// Functions
///
- public List Functions { get; set; }
+ public List? Functions { get; set; }
///
/// Routes
///
- public List Responses { get; set; }
+ public List? 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;
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs
index e0d1ebb5..bfa54538 100644
--- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs
@@ -8,6 +8,7 @@ public class AgentViewModel
public string Name { get; set; }
public string Description { get; set; }
public string Instruction { get; set; }
+ public List Templates { get; set; }
public List Functions { get; set; }
public List 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,
diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs
index adfb89d2..aa84af03 100644
--- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs
+++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs
@@ -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 Templates { get; set; }
public List Functions { get; set; }
public List Responses { get; set; }
public bool IsPublic { get; set; }
diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs
index eaf1a4ce..228d926b 100644
--- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs
+++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs
@@ -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;
+ }
}