diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 374271b9f..c29d39ab4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -233,6 +233,8 @@ bool DeleteKnowledgeCollectionConfig(string collectionName) => throw new NotImplementedException(); IEnumerable GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter) => throw new NotImplementedException(); + VectorCollectionConfig GetKnowledgeCollectionConfig(string collectionName, string vectorStroageProvider) + => throw new NotImplementedException(); bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) => throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs index 3c3ba64bc..73421a0be 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs @@ -110,6 +110,17 @@ public IEnumerable GetKnowledgeCollectionConfigs(VectorC return configs; } + + [SharpCache(10)] + public VectorCollectionConfig GetKnowledgeCollectionConfig(string collectionName, string vectorStroageProvider) + { + var configs = GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter + { + CollectionNames = [collectionName], + VectorStroageProviders = [vectorStroageProvider] + }); + return configs?.FirstOrDefault(); + } #endregion diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Helpers/KnowledgeSettingHelper.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Helpers/KnowledgeSettingHelper.cs index 2384d9ebb..112e3e19b 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Helpers/KnowledgeSettingHelper.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Helpers/KnowledgeSettingHelper.cs @@ -10,23 +10,12 @@ public static ITextEmbedding GetTextEmbeddingSetting(IServiceProvider services, var db = services.GetRequiredService(); // Get collection config from db - var configs = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter - { - CollectionNames = [collectionName], - VectorStroageProviders = [settings.VectorDb.Provider] - }); - - var found = configs?.FirstOrDefault()?.TextEmbedding; - var provider = found?.Provider ?? string.Empty; - var model = found?.Model ?? string.Empty; - var dimension = found?.Dimension ?? 0; + var config = db.GetKnowledgeCollectionConfig(collectionName, settings.VectorDb.Provider); - if (found == null) - { - provider = settings.Default.TextEmbedding.Provider; - model = settings.Default.TextEmbedding.Model; - dimension = settings.Default.TextEmbedding.Dimension; - } + var textEmbeddingConfig = config?.TextEmbedding; + var provider = textEmbeddingConfig?.Provider ?? settings.Default.TextEmbedding.Provider; + var model = textEmbeddingConfig?.Model ?? settings.Default.TextEmbedding.Model; + var dimension = textEmbeddingConfig?.Dimension ?? settings.Default.TextEmbedding.Dimension; // Set up text embedding var embedding = services.GetServices().FirstOrDefault(x => x.Provider == provider); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj index 268430353..8ca160b06 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj @@ -12,6 +12,7 @@ + diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index 6611c3524..deac97d25 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -49,9 +49,17 @@ public void CreateNewConversation(Conversation conversation) UpdatedTime = utcNow }; - _dc.Conversations.InsertOne(convDoc); - _dc.ConversationDialogs.InsertOne(dialogDoc); - _dc.ConversationStates.InsertOne(stateDoc); + var convFilter = Builders.Filter.Eq(x => x.Id, convDoc.Id); + var update = Builders.Update + .Set(x => x.UpdatedTime, DateTime.UtcNow) + .SetOnInsert(x => x, convDoc); + + var updateResult = _dc.Conversations.UpdateOne(convFilter, update, new UpdateOptions { IsUpsert = true}); + if (updateResult.UpsertedId != null) + { + _dc.ConversationDialogs.InsertOne(dialogDoc); + _dc.ConversationStates.InsertOne(stateDoc); + } } public bool DeleteConversations(IEnumerable conversationIds) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs index 734968395..18631cbba 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs @@ -115,6 +115,17 @@ public IEnumerable GetKnowledgeCollectionConfigs(VectorC TextEmbedding = KnowledgeEmbeddingConfigMongoModel.ToDomainModel(x.TextEmbedding) }); } + + [SharpCache(10)] + public VectorCollectionConfig GetKnowledgeCollectionConfig(string collectionName, string vectorStroageProvider) + { + var configs = GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter + { + CollectionNames = [collectionName], + VectorStroageProviders = [vectorStroageProvider] + }); + return configs?.FirstOrDefault(); + } #endregion #region Documents