From 7e3e58aba166288771f85eff8560e7045a4d7196 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 11 Sep 2023 23:27:04 -0500 Subject: [PATCH 1/4] add json ignore --- .../BotSharp.Abstraction/Agents/Models/Agent.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 68be9939..0950f97f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace BotSharp.Abstraction.Agents.Models; public class Agent @@ -11,26 +13,31 @@ public class Agent /// /// Instruction /// + [JsonIgnore] public string Instruction { get; set; } /// /// Samples /// + [JsonIgnore] public string Samples { get; set; } /// /// Functions /// + [JsonIgnore] public List Functions { get; set; } /// /// Responses /// + [JsonIgnore] public List Responses { get; set; } /// /// Domain knowledges /// + [JsonIgnore] public string Knowledges { get; set; } public bool IsPublic { get; set; } 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 2/4] 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; + } } From 0c92997f84c984f27ef6013f1a8f4247086e05c1 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 12 Sep 2023 17:34:50 -0500 Subject: [PATCH 3/4] add table prefix --- .../Repositories/BotSharpDatabaseSettings.cs | 1 + .../MongoDbContext.cs | 19 +++++++++++-------- .../MongoStoragePlugin.cs | 2 +- src/WebStarter/appsettings.json | 1 + 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs index 54cabe8b..0c902b24 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs @@ -5,6 +5,7 @@ public class BotSharpDatabaseSettings : DatabaseBasicSettings public string[] Assemblies { get; set; } public string FileRepository { get; set; } public string MongoDb { get; set; } + public string TablePrefix { get; set; } public DbConnectionSetting BotSharp { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 7423fed1..4782a5b3 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -6,11 +6,14 @@ public class MongoDbContext { private readonly MongoClient _mongoClient; private readonly string _mongoDbDatabaseName; + private readonly string _collectionPrefix; - public MongoDbContext(string mongoDbConnectionString) + public MongoDbContext(BotSharpDatabaseSettings dbSettings) { + var mongoDbConnectionString = dbSettings.MongoDb; _mongoClient = new MongoClient(mongoDbConnectionString); _mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString); + _collectionPrefix = dbSettings.TablePrefix; } private string GetDatabaseName(string mongoDbConnectionString) @@ -26,23 +29,23 @@ public class MongoDbContext private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } } public IMongoCollection Agents - => Database.GetCollection("OneBrainAgents"); + => Database.GetCollection($"{_collectionPrefix}_Agents"); public IMongoCollection Conversations - => Database.GetCollection("OneBrainConversations"); + => Database.GetCollection($"{_collectionPrefix}_Conversations"); public IMongoCollection ConversationDialogs - => Database.GetCollection("OneBrainConversationDialogs"); + => Database.GetCollection($"{_collectionPrefix}_ConversationDialogs"); public IMongoCollection Users - => Database.GetCollection("OneBrainUsers"); + => Database.GetCollection($"{_collectionPrefix}_Users"); public IMongoCollection UserAgents - => Database.GetCollection("OneBrainUserAgents"); + => Database.GetCollection($"{_collectionPrefix}_UserAgents"); public IMongoCollection RoutingItems - => Database.GetCollection("OneBrainRoutingItems"); + => Database.GetCollection($"{_collectionPrefix}_RoutingItems"); public IMongoCollection RoutingProfiles - => Database.GetCollection("OneBrainRoutingProfiles"); + => Database.GetCollection($"{_collectionPrefix}_RoutingProfiles"); } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs index b4f80895..644737ca 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoStoragePlugin.cs @@ -14,7 +14,7 @@ public class MongoStoragePlugin : IBotSharpPlugin services.AddScoped((IServiceProvider x) => { var dbSettings = x.GetRequiredService(); - return new MongoDbContext(dbSettings.MongoDb); + return new MongoDbContext(dbSettings); }); services.AddScoped(); diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index a3117468..0e026982 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -63,6 +63,7 @@ "Database": { "Default": "FileRepository", + "TablePrefix": "OneBrain", "MongoDb": "", "BotSharp": { "Master": "Data Source=(localdb)\\ProjectModels;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False", From ddfcf59e318538c4f02791427bd4d394b5be2896 Mon Sep 17 00:00:00 2001 From: Haiping Date: Wed, 13 Sep 2023 03:23:59 -0500 Subject: [PATCH 4/4] Update appsettings.json --- src/WebStarter/appsettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 0e026982..dd96fd71 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -63,7 +63,7 @@ "Database": { "Default": "FileRepository", - "TablePrefix": "OneBrain", + "TablePrefix": "BotSharp", "MongoDb": "", "BotSharp": { "Master": "Data Source=(localdb)\\ProjectModels;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False",