diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs index 34e8ac19..346bcf31 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs @@ -18,6 +18,7 @@ public enum AgentField Sample, LlmConfig, Utility, + KnowledgeBase, MaxMessageCount } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 80d6db07..540f13ca 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -99,6 +99,11 @@ public class Agent /// public List Utilities { get; set; } = new(); + /// + /// Agent knowledge bases + /// + public List KnowledgeBases { get; set; } = []; + /// /// Inherit from agent /// diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentKnowledgeBase.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentKnowledgeBase.cs new file mode 100644 index 00000000..8727701b --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentKnowledgeBase.cs @@ -0,0 +1,23 @@ +namespace BotSharp.Abstraction.Agents.Models; + +public class AgentKnowledgeBase +{ + public string? Name { get; set; } + public bool Disabled { get; set; } + + public AgentKnowledgeBase() + { + + } + + public AgentKnowledgeBase(string name, bool enabled) + { + Name = name; + Disabled = enabled; + } + + public override string ToString() + { + return Name ?? string.Empty; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index ffea8f8d..31585a09 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -39,6 +39,7 @@ public partial class AgentService record.Responses = agent.Responses ?? []; record.Samples = agent.Samples ?? []; record.Utilities = agent.Utilities ?? []; + record.KnowledgeBases = agent.KnowledgeBases ?? []; if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit) { record.LlmConfig = agent.LlmConfig; diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index a838870f..0a8d5b65 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -57,6 +57,9 @@ namespace BotSharp.Core.Repository case AgentField.Utility: UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities); break; + case AgentField.KnowledgeBase: + UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); + break; case AgentField.MaxMessageCount: UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); break; @@ -168,6 +171,19 @@ namespace BotSharp.Core.Repository File.WriteAllText(agentFile, json); } + private void UpdateAgentKnowledgeBases(string agentId, List knowledgeBases) + { + if (knowledgeBases == null) return; + + var (agent, agentFile) = GetAgentFromFile(agentId); + if (agent == null) return; + + agent.KnowledgeBases = knowledgeBases; + agent.UpdatedDateTime = DateTime.UtcNow; + var json = JsonSerializer.Serialize(agent, _options); + File.WriteAllText(agentFile, json); + } + private void UpdateAgentRoutingRules(string agentId, List rules) { if (rules == null) return; @@ -310,6 +326,7 @@ namespace BotSharp.Core.Repository agent.Type = inputAgent.Type; agent.Profiles = inputAgent.Profiles; agent.Utilities = inputAgent.Utilities; + agent.KnowledgeBases = inputAgent.KnowledgeBases; agent.RoutingRules = inputAgent.RoutingRules; agent.LlmConfig = inputAgent.LlmConfig; agent.MaxMessageCount = inputAgent.MaxMessageCount; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs index 141f0662..66618186 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs @@ -39,6 +39,13 @@ public class AgentUpdateModel /// public List? Utilities { get; set; } + /// + /// knowledge bases + /// + /// + [JsonPropertyName("knowledge_bases")] + public List? KnowledgeBases { get; set; } + /// /// Functions /// @@ -90,6 +97,7 @@ public class AgentUpdateModel Functions = Functions ?? new List(), Responses = Responses ?? new List(), Utilities = Utilities ?? new List(), + KnowledgeBases = KnowledgeBases ?? [], LlmConfig = LlmConfig }; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs index 36a8900f..e9479109 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs @@ -25,6 +25,9 @@ public class AgentViewModel public bool MergeUtility { get; set; } public List Utilities { get; set; } + [JsonPropertyName("knowledge_bases")] + public List KnowledgeBases { get; set; } + [JsonPropertyName("is_public")] public bool IsPublic { get; set; } @@ -75,6 +78,7 @@ public class AgentViewModel Responses = agent.Responses, Samples = agent.Samples, Utilities = agent.Utilities, + KnowledgeBases = agent.KnowledgeBases, IsPublic= agent.IsPublic, Disabled = agent.Disabled, MergeUtility = agent.MergeUtility, diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs index 3a775025..19945124 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentDocument.cs @@ -18,6 +18,7 @@ public class AgentDocument : MongoBase public List Responses { get; set; } public List Samples { get; set; } public List Utilities { get; set; } + public List KnowledgeBases { get; set; } public List Profiles { get; set; } public List RoutingRules { get; set; } public AgentLlmConfigMongoElement? LlmConfig { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentKnowledgeBaseMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentKnowledgeBaseMongoElement.cs new file mode 100644 index 00000000..f9ee8c34 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentKnowledgeBaseMongoElement.cs @@ -0,0 +1,26 @@ +using BotSharp.Abstraction.Agents.Models; + +namespace BotSharp.Plugin.MongoStorage.Models; + +public class AgentKnowledgeBaseMongoElement +{ + public string Name { get; set; } + public bool Disabled { get; set; } + public static AgentKnowledgeBaseMongoElement ToMongoElement(AgentKnowledgeBase knowledgeBase) + { + return new AgentKnowledgeBaseMongoElement + { + Name = knowledgeBase.Name ?? string.Empty, + Disabled = knowledgeBase.Disabled, + }; + } + + public static AgentKnowledgeBase ToDomainElement(AgentKnowledgeBaseMongoElement knowledgeBase) + { + return new AgentKnowledgeBase + { + Name = knowledgeBase.Name, + Disabled = knowledgeBase.Disabled + }; + } +} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 9bd72e3e..afa6a5a9 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -58,6 +58,9 @@ public partial class MongoRepository case AgentField.Utility: UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities); break; + case AgentField.KnowledgeBase: + UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); + break; case AgentField.MaxMessageCount: UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); break; @@ -239,6 +242,20 @@ public partial class MongoRepository _dc.Agents.UpdateOne(filter, update); } + private void UpdateAgentKnowledgeBases(string agentId, List knowledgeBases) + { + if (knowledgeBases == null) return; + + var elements = knowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToMongoElement(x))?.ToList() ?? []; + + var filter = Builders.Filter.Eq(x => x.Id, agentId); + var update = Builders.Update + .Set(x => x.KnowledgeBases, elements) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + + _dc.Agents.UpdateOne(filter, update); + } + private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config) { var llmConfig = AgentLlmConfigMongoElement.ToMongoElement(config); @@ -279,6 +296,7 @@ public partial class MongoRepository .Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList()) .Set(x => x.Samples, agent.Samples) .Set(x => x.Utilities, agent.Utilities.Select(u => AgentUtilityMongoElement.ToMongoElement(u)).ToList()) + .Set(x => x.KnowledgeBases, agent.KnowledgeBases.Select(u => AgentKnowledgeBaseMongoElement.ToMongoElement(u)).ToList()) .Set(x => x.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig)) .Set(x => x.IsPublic, agent.IsPublic) .Set(x => x.UpdatedTime, DateTime.UtcNow);