diff --git a/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs b/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs index d7d9d528..9a99da6c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Graph.Models; -using BotSharp.Abstraction.Models; using BotSharp.Abstraction.VectorStorage.Models; namespace BotSharp.Abstraction.Knowledges; @@ -8,7 +7,7 @@ public interface IKnowledgeService { #region Vector Task ExistVectorCollection(string collectionName); - Task CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model); + Task CreateVectorCollection(string collectionName, string collectionType, VectorCollectionCreateOptions options); Task DeleteVectorCollection(string collectionName); Task> GetVectorCollections(string? type = null); Task GetVectorCollectionDetails(string collectionName); diff --git a/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs index 95d6257c..1a2d5865 100644 --- a/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs +++ b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs @@ -16,7 +16,7 @@ public interface IVectorDb => throw new NotImplementedException(); Task> GetCollectionData(string collectionName, IEnumerable ids, VectorQueryOptions? options = null) => throw new NotImplementedException(); - Task CreateCollection(string collectionName, int dimension) + Task CreateCollection(string collectionName, VectorCollectionCreateOptions options) => throw new NotImplementedException(); Task DeleteCollection(string collectionName) => throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorCollectionCreateOptions.cs b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorCollectionCreateOptions.cs new file mode 100644 index 00000000..343bfb30 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorCollectionCreateOptions.cs @@ -0,0 +1,8 @@ +namespace BotSharp.Abstraction.VectorStorage.Models; + +public class VectorCollectionCreateOptions +{ + public int Dimension { get; set; } + public string Provider { get; set; } = null!; + public string Model { get; set; } = null!; +} diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs index f8bcacef..8c2b94a5 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs @@ -45,7 +45,13 @@ public class KnowledgeBaseController : ControllerBase [HttpPost("knowledge/vector/create-collection")] public async Task CreateVectorCollection([FromBody] CreateVectorCollectionRequest request) { - return await _knowledgeService.CreateVectorCollection(request.CollectionName, request.CollectionType, request.Dimension, request.Provider, request.Model); + var options = new VectorCollectionCreateOptions + { + Provider = request.Provider, + Model = request.Model, + Dimension = request.Dimension + }; + return await _knowledgeService.CreateVectorCollection(request.CollectionName, request.CollectionType, options); } [HttpDelete("knowledge/vector/{collection}/delete-collection")] diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemoryVectorDb.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemoryVectorDb.cs index 27823202..ef7277db 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemoryVectorDb.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemoryVectorDb.cs @@ -17,9 +17,9 @@ public class MemoryVectorDb : IVectorDb return false; } - public async Task CreateCollection(string collectionName, int dimension) + public async Task CreateCollection(string collectionName, VectorCollectionCreateOptions options) { - _collections[collectionName] = dimension; + _collections[collectionName] = options.Dimension; _vectors[collectionName] = new List(); return true; } diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs index 76f4ac30..caaed8b3 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs @@ -24,7 +24,7 @@ public partial class KnowledgeService return !configs.IsNullOrEmpty(); } - public async Task CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model) + public async Task CreateVectorCollection(string collectionName, string collectionType, VectorCollectionCreateOptions options) { try { @@ -46,9 +46,9 @@ public partial class KnowledgeService }, TextEmbedding = new KnowledgeEmbeddingConfig { - Provider = provider, - Model = model, - Dimension = dimension + Provider = options.Provider, + Model = options.Model, + Dimension = options.Dimension } } }); @@ -56,7 +56,7 @@ public partial class KnowledgeService if (created) { var vectorDb = GetVectorDb(); - created = await vectorDb.CreateCollection(collectionName, dimension); + created = await vectorDb.CreateCollection(collectionName, options); } return created; @@ -182,7 +182,6 @@ public partial class KnowledgeService } } - public async Task UpdateVectorCollectionData(string collectionName, VectorUpdateModel update) { try @@ -245,7 +244,12 @@ public partial class KnowledgeService var textEmbedding = GetTextEmbedding(collectionName); var vector = await textEmbedding.GetVectorAsync(update.Text); var payload = update.Payload ?? new(); - payload[KnowledgePayloadName.DataSource] = !string.IsNullOrWhiteSpace(update.DataSource) ? update.DataSource : VectorDataSource.Api; + + if (!payload.TryGetValue(KnowledgePayloadName.DataSource, out _)) + { + payload[KnowledgePayloadName.DataSource] = !string.IsNullOrWhiteSpace(update.DataSource) ? + update.DataSource : VectorDataSource.Api; + } return await db.Upsert(collectionName, guid, vector, update.Text, payload); } @@ -266,7 +270,7 @@ public partial class KnowledgeService } var db = GetVectorDb(); - return await db.DeleteCollectionData(collectionName, new List { guid }); + return await db.DeleteCollectionData(collectionName, [guid]); } catch (Exception ex) { diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs index 40f6d236..a667b148 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs +++ b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs @@ -57,7 +57,7 @@ public class QdrantDb : IVectorDb return await client.CollectionExistsAsync(collectionName); } - public async Task CreateCollection(string collectionName, int dimension) + public async Task CreateCollection(string collectionName, VectorCollectionCreateOptions options) { var exist = await DoesCollectionExist(collectionName); @@ -69,14 +69,14 @@ public class QdrantDb : IVectorDb var client = GetClient(); await client.CreateCollectionAsync(collectionName, new VectorParams() { - Size = (ulong)dimension, + Size = (ulong)options.Dimension, Distance = Distance.Cosine }); return true; } catch (Exception ex) { - _logger.LogWarning($"Error when create collection (Name: {collectionName}, Dimension: {dimension})."); + _logger.LogWarning($"Error when create collection (Name: {collectionName}, Dimension: {options.Dimension})."); return false; } } diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs index e87ea423..ba58c870 100644 --- a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs +++ b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs @@ -31,7 +31,7 @@ namespace BotSharp.Plugin.SemanticKernel return false; } - public async Task CreateCollection(string collectionName, int dimension) + public async Task CreateCollection(string collectionName, VectorCollectionCreateOptions options) { await _memoryStore.CreateCollectionAsync(collectionName); return true;