diff --git a/src/Infrastructure/BotSharp.Abstraction/Models/KeyValue.cs b/src/Infrastructure/BotSharp.Abstraction/Models/KeyValue.cs new file mode 100644 index 00000000..f981ba6e --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Models/KeyValue.cs @@ -0,0 +1,10 @@ +namespace BotSharp.Abstraction.Models; + +public class KeyValue +{ + [JsonPropertyName("key")] + public string Key { get; set; } + + [JsonPropertyName("value")] + public string Value { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorFilter.cs b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorFilter.cs index 91abcc25..85b9dec2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorFilter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorFilter.cs @@ -4,4 +4,7 @@ public class VectorFilter : StringIdPagination { [JsonPropertyName("with_vector")] public bool WithVector { get; set; } -} + + [JsonPropertyName("search_pairs")] + public IEnumerable? SearchPairs { get; set; } +} \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs index 99d917d4..496a8fc2 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs +++ b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs @@ -51,10 +51,34 @@ public class QdrantDb : IVectorDb return new StringIdPagedItems(); } - var totalPointCount = await client.CountAsync(collectionName); + // Build query filter + Filter? queryFilter = null; + if (!filter.SearchPairs.IsNullOrEmpty()) + { + var conditions = filter.SearchPairs.Select(x => new Condition + { + Field = new FieldCondition + { + Key = x.Key, + Match = new Match { Text = x.Value }, + } + }); + + queryFilter = new Filter + { + Should = + { + conditions + } + }; + } + + var totalPointCount = await client.CountAsync(collectionName, filter: queryFilter); var response = await client.ScrollAsync(collectionName, limit: (uint)filter.Size, - offset: !string.IsNullOrWhiteSpace(filter.StartId) ? new PointId { Uuid = filter.StartId } : 0, + offset: !string.IsNullOrWhiteSpace(filter.StartId) ? new PointId { Uuid = filter.StartId } : null, + filter: queryFilter, vectorsSelector: filter.WithVector); + var points = response?.Result?.Select(x => new VectorCollectionData { Id = x.Id?.Uuid ?? string.Empty, @@ -160,43 +184,26 @@ public class QdrantDb : IVectorDb return results; } - var points = await client.SearchAsync(collectionName, - vector, - limit: (ulong)limit, - scoreThreshold: confidence, - vectorsSelector: new WithVectorsSelector { Enable = withVector }); - - var pickFields = fields != null; - foreach (var point in points) + var payloadSelector = new WithPayloadSelector { Enable = true }; + if (fields != null) { - var data = new Dictionary(); - if (pickFields) - { - foreach (var field in fields) - { - if (point.Payload.ContainsKey(field)) - { - data[field] = point.Payload[field].StringValue; - } - else - { - data[field] = ""; - } - } - } - else - { - data = point.Payload.ToDictionary(k => k.Key, v => v.Value.StringValue); - } - - results.Add(new VectorCollectionData - { - Id = point.Id.Uuid, - Data = data, - Score = point.Score, - Vector = withVector ? point.Vectors?.Vector?.Data?.ToArray() : null - }); + payloadSelector.Include = new PayloadIncludeSelector { Fields = { fields.ToArray() } }; } + + var points = await client.SearchAsync(collectionName, + vector, + limit: (ulong)limit, + scoreThreshold: confidence, + payloadSelector: payloadSelector, + vectorsSelector: withVector); + + results = points.Select(x => new VectorCollectionData + { + Id = x.Id.Uuid, + Data = x.Payload.ToDictionary(x => x.Key, x => x.Value.StringValue), + Score = x.Score, + Vector = x.Vectors?.Vector?.Data?.ToArray() + }).ToList(); return results; } diff --git a/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs b/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs index a797b47f..89510f4d 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 public Task> GetPagedCollectionData(string collectionName, VectorFilter filter) { - throw new System.NotImplementedException(); + throw new NotImplementedException(); } public Task> GetCollectionData(string collectionName, IEnumerable ids,