diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 540f13ca..566df050 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -155,6 +155,7 @@ public class Agent Profiles = agent.Profiles, RoutingRules = agent.RoutingRules, LlmConfig = agent.LlmConfig, + KnowledgeBases = agent.KnowledgeBases, CreatedDateTime = agent.CreatedDateTime, UpdatedDateTime = agent.UpdatedDateTime, }; @@ -168,43 +169,49 @@ public class Agent public Agent SetChannelInstructions(List instructions) { - ChannelInstructions = instructions ?? new List(); + ChannelInstructions = instructions ?? []; return this; } public Agent SetTemplates(List templates) { - Templates = templates ?? new List(); + Templates = templates ?? []; return this; } public Agent SetTasks(List tasks) { - Tasks = tasks ?? new List(); + Tasks = tasks ?? []; return this; } public Agent SetFunctions(List functions) { - Functions = functions ?? new List(); + Functions = functions ?? []; return this; } public Agent SetSamples(List samples) { - Samples = samples ?? new List(); + Samples = samples ?? []; return this; } public Agent SetUtilities(List utilities) { - Utilities = utilities ?? new List(); + Utilities = utilities ?? []; + return this; + } + + public Agent SetKnowledgeBases(List knowledgeBases) + { + knowledgeBases = knowledgeBases ?? []; return this; } public Agent SetResponses(List responses) { - Responses = responses ?? new List(); ; + Responses = responses ?? []; return this; } @@ -252,13 +259,13 @@ public class Agent public Agent SetProfiles(List profiles) { - Profiles = profiles ?? new List(); + Profiles = profiles ?? []; return this; } public Agent SetRoutingRules(List rules) { - RoutingRules = rules ?? new List(); + RoutingRules = rules ?? []; return this; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentKnowledgeBase.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentKnowledgeBase.cs index 8727701b..3f116871 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentKnowledgeBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentKnowledgeBase.cs @@ -2,7 +2,8 @@ namespace BotSharp.Abstraction.Agents.Models; public class AgentKnowledgeBase { - public string? Name { get; set; } + public string Name { get; set; } + public string Type { get; set; } public bool Disabled { get; set; } public AgentKnowledgeBase() @@ -10,9 +11,10 @@ public class AgentKnowledgeBase } - public AgentKnowledgeBase(string name, bool enabled) + public AgentKnowledgeBase(string name, string type, bool enabled) { Name = name; + Type = type; Disabled = enabled; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs b/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs index 2bc70dc2..3fc90816 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs @@ -9,7 +9,7 @@ public interface IKnowledgeService Task ExistVectorCollection(string collectionName); Task CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model); Task DeleteVectorCollection(string collectionName); - Task> GetVectorCollections(string type); + Task> GetVectorCollections(string? type = null); Task> SearchVectorKnowledge(string query, string collectionName, VectorSearchOptions options); Task> GetPagedVectorCollectionData(string collectionName, VectorFilter filter); Task DeleteVectorCollectionData(string collectionName, string id); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 31585a09..584a4c7e 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -103,6 +103,7 @@ public partial class AgentService .SetResponses(foundAgent.Responses) .SetSamples(foundAgent.Samples) .SetUtilities(foundAgent.Utilities) + .SetKnowledgeBases(foundAgent.KnowledgeBases) .SetLlmConfig(foundAgent.LlmConfig); _db.UpdateAgent(clonedAgent, AgentField.All); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index d8b701c9..1885365d 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -10,7 +10,10 @@ public class AgentController : ControllerBase private readonly IUserIdentity _user; private readonly IServiceProvider _services; - public AgentController(IAgentService agentService, IUserIdentity user, IServiceProvider services) + public AgentController( + IAgentService agentService, + IUserIdentity user, + IServiceProvider services) { _agentService = agentService; _user = user; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs index 5a1a6bda..50b7c528 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs @@ -26,9 +26,10 @@ public class KnowledgeBaseController : ControllerBase } [HttpGet("knowledge/vector/collections")] - public async Task> GetVectorCollections([FromQuery] string type) + public async Task> GetVectorCollections([FromQuery] string? type = null) { - return await _knowledgeService.GetVectorCollections(type); + var collections = await _knowledgeService.GetVectorCollections(type); + return collections.Select(x => VectorCollectionConfigViewModel.From(x)); } [HttpPost("knowledge/vector/create-collection")] diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs index 44c257df..3caba6ae 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Functions.Models; -using BotSharp.Abstraction.Routing.Models; namespace BotSharp.OpenAPI.ViewModels.Agents; @@ -55,6 +54,7 @@ public class AgentCreationModel public List Utilities { get; set; } = new(); public List RoutingRules { get; set; } = new(); + public List KnowledgeBases { get; set; } = new(); public AgentLlmConfig? LlmConfig { get; set; } public Agent ToAgent() @@ -76,8 +76,9 @@ public class AgentCreationModel MergeUtility = MergeUtility, MaxMessageCount = MaxMessageCount, Profiles = Profiles, - RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List(), - LlmConfig = LlmConfig + LlmConfig = LlmConfig, + KnowledgeBases = KnowledgeBases, + RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [], }; } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Knowledges/VectorCollectionConfigViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Knowledges/VectorCollectionConfigViewModel.cs new file mode 100644 index 00000000..e19fda39 --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Knowledges/VectorCollectionConfigViewModel.cs @@ -0,0 +1,17 @@ +using BotSharp.Abstraction.VectorStorage.Models; + +namespace BotSharp.OpenAPI.ViewModels.Knowledges; + +public class VectorCollectionConfigViewModel : VectorCollectionConfig +{ + public static VectorCollectionConfigViewModel From(VectorCollectionConfig model) + { + return new VectorCollectionConfigViewModel + { + Name = model.Name, + Type = model.Type, + VectorStore = model.VectorStore, + TextEmbedding = model.TextEmbedding + }; + } +} diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs index 903eaf69..ebae91ea 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs @@ -69,25 +69,25 @@ public partial class KnowledgeService } } - public async Task> GetVectorCollections(string type) + public async Task> GetVectorCollections(string? type = null) { try { var db = _services.GetRequiredService(); - var collectionNames = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter + var configs = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter { - CollectionTypes = new[] { type }, - VectorStroageProviders = new[] { _settings.VectorDb.Provider } - }).Select(x => x.Name).ToList(); + CollectionTypes = !string.IsNullOrEmpty(type) ? [type] : null, + VectorStroageProviders = [_settings.VectorDb.Provider] + }).ToList(); var vectorDb = GetVectorDb(); - var vectorCollections = await vectorDb.GetCollections(); - return vectorCollections.Where(x => collectionNames.Contains(x)); + var dbCollections = await vectorDb.GetCollections(); + return configs.Where(x => dbCollections.Contains(x.Name)); } catch (Exception ex) { _logger.LogWarning($"Error when getting vector db collections. {ex.Message}\r\n{ex.InnerException}"); - return Enumerable.Empty(); + return Enumerable.Empty(); } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentKnowledgeBaseMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentKnowledgeBaseMongoElement.cs index f9ee8c34..3e6b3500 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentKnowledgeBaseMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentKnowledgeBaseMongoElement.cs @@ -5,13 +5,16 @@ namespace BotSharp.Plugin.MongoStorage.Models; public class AgentKnowledgeBaseMongoElement { public string Name { get; set; } + public string Type { get; set; } public bool Disabled { get; set; } + public static AgentKnowledgeBaseMongoElement ToMongoElement(AgentKnowledgeBase knowledgeBase) { return new AgentKnowledgeBaseMongoElement { - Name = knowledgeBase.Name ?? string.Empty, - Disabled = knowledgeBase.Disabled, + Name = knowledgeBase.Name, + Type = knowledgeBase.Type, + Disabled = knowledgeBase.Disabled }; } @@ -20,6 +23,7 @@ public class AgentKnowledgeBaseMongoElement return new AgentKnowledgeBase { Name = knowledgeBase.Name, + Type = knowledgeBase.Type, 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 afa6a5a9..7f86d144 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -439,21 +439,22 @@ public partial class MongoRepository IconUrl = x.IconUrl, Description = x.Description, Instruction = x.Instruction, - ChannelInstructions = x.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToMongoElement(i))?.ToList() ?? [], - Templates = x.Templates?.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?.ToList() ?? [], - Functions = x.Functions?.Select(f => FunctionDefMongoElement.ToMongoElement(f))?.ToList() ?? [], - Responses = x.Responses?.Select(r => AgentResponseMongoElement.ToMongoElement(r))?.ToList() ?? [], - Samples = x.Samples ?? new List(), - Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [], + Samples = x.Samples ?? [], IsPublic = x.IsPublic, Type = x.Type, InheritAgentId = x.InheritAgentId, Disabled = x.Disabled, MergeUtility = x.MergeUtility, MaxMessageCount = x.MaxMessageCount, - Profiles = x.Profiles, - RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [], + Profiles = x.Profiles ?? [], LlmConfig = AgentLlmConfigMongoElement.ToMongoElement(x.LlmConfig), + ChannelInstructions = x.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToMongoElement(i))?.ToList() ?? [], + Templates = x.Templates?.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?.ToList() ?? [], + Functions = x.Functions?.Select(f => FunctionDefMongoElement.ToMongoElement(f))?.ToList() ?? [], + Responses = x.Responses?.Select(r => AgentResponseMongoElement.ToMongoElement(r))?.ToList() ?? [], + RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [], + Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [], + KnowledgeBases = x.KnowledgeBases?.Select(k => AgentKnowledgeBaseMongoElement.ToMongoElement(k))?.ToList() ?? [], CreatedTime = x.CreatedDateTime, UpdatedTime = x.UpdatedDateTime }).ToList(); @@ -530,21 +531,22 @@ public partial class MongoRepository IconUrl = agentDoc.IconUrl, Description = agentDoc.Description, Instruction = agentDoc.Instruction, - ChannelInstructions = agentDoc.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToDomainElement(i))?.ToList() ?? [], - Templates = agentDoc.Templates?.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?.ToList() ?? [], - Functions = agentDoc.Functions?.Select(f => FunctionDefMongoElement.ToDomainElement(f)).ToList() ?? [], - Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [], - RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [], - LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agentDoc.LlmConfig), Samples = agentDoc.Samples ?? [], - Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [], IsPublic = agentDoc.IsPublic, Disabled = agentDoc.Disabled, MergeUtility = agentDoc.MergeUtility, Type = agentDoc.Type, InheritAgentId = agentDoc.InheritAgentId, Profiles = agentDoc.Profiles, - MaxMessageCount = agentDoc.MaxMessageCount + MaxMessageCount = agentDoc.MaxMessageCount, + LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agentDoc.LlmConfig), + ChannelInstructions = agentDoc.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToDomainElement(i))?.ToList() ?? [], + Templates = agentDoc.Templates?.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?.ToList() ?? [], + Functions = agentDoc.Functions?.Select(f => FunctionDefMongoElement.ToDomainElement(f)).ToList() ?? [], + Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [], + RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [], + Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [], + KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? [] }; } }