From f7d42450d7092dfbe420208708dceedc44c1f145 Mon Sep 17 00:00:00 2001 From: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> Date: Fri, 9 Jun 2023 17:20:16 +0100 Subject: [PATCH] Http consistency: Qdrant (#1385) ### Description This PR brings the following changes: - The Qdrant-related classes, namely QdrantMemoryStore and QdrantVectorDbClient, have extended support for various creation methods based on Qdrant endpoint, HttpClient, or both. - The KernelBuilder class has new extension methods called "WithQdrantMemoryStore" that simplify the registration of the Qdrant memory store. These methods align the registration process with other connector registrations, enforce the use of a HttpClient with the preconfigured retry handler, and allow the hosting application to provide a custom HttpClient if needed. - Minor bug fix. --- .../HuggingFaceKernelBuilderExtensions.cs | 26 +-- .../OpenAIKernelBuilderExtensions.cs | 40 ++--- .../PineconeKernelBuilderExtensions.cs | 22 +-- .../Connectors.Memory.Qdrant.csproj | 5 +- .../Diagnostics/NullableAttributes.cs | 167 ------------------ .../Http/ApiSchema/GetVectorsRequest.cs | 2 +- .../QdrantKernelBuilderExtensions.cs | 66 +++++++ .../QdrantMemoryStore.cs | 46 ++++- .../QdrantVectorDbClient.cs | 136 +++++++++----- .../QdrantKernelBuilderExtensionsTests.cs | 49 +++++ .../Memory/Qdrant/QdrantMemoryStoreTests.cs | 24 +-- .../Memory/Qdrant/QdrantMemoryStoreTests2.cs | 31 ++-- .../Memory/Qdrant/QdrantMemoryStoreTests3.cs | 96 +++++++++- .../Qdrant/QdrantVectorDbClientTests.cs | 58 ++++++ .../Http/HttpClientProvider.cs | 30 ++++ .../TextCompletionServiceExtensions.cs | 2 +- .../Example19_Qdrant.cs | 5 +- 17 files changed, 482 insertions(+), 323 deletions(-) delete mode 100644 dotnet/src/Connectors/Connectors.Memory.Qdrant/Diagnostics/NullableAttributes.cs create mode 100644 dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantKernelBuilderExtensions.cs create mode 100644 dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantKernelBuilderExtensionsTests.cs create mode 100644 dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantVectorDbClientTests.cs create mode 100644 dotnet/src/InternalUtilities/Http/HttpClientProvider.cs diff --git a/dotnet/src/Connectors/Connectors.AI.HuggingFace/HuggingFaceKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.AI.HuggingFace/HuggingFaceKernelBuilderExtensions.cs index 11daa22ce8..8867c71c0a 100644 --- a/dotnet/src/Connectors/Connectors.AI.HuggingFace/HuggingFaceKernelBuilderExtensions.cs +++ b/dotnet/src/Connectors/Connectors.AI.HuggingFace/HuggingFaceKernelBuilderExtensions.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. using System.Net.Http; -using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.AI.Embeddings; using Microsoft.SemanticKernel.AI.TextCompletion; using Microsoft.SemanticKernel.Connectors.AI.HuggingFace.TextCompletion; @@ -41,7 +40,7 @@ public static class HuggingFaceKernelBuilderExtensions new HuggingFaceTextCompletion( model, apiKey, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), endpoint), setAsDefault); @@ -66,7 +65,7 @@ public static class HuggingFaceKernelBuilderExtensions builder.WithAIService(serviceId, (parameters) => new HuggingFaceTextEmbeddingGeneration( model, - GetHttpClient(parameters.Config, httpClient: null, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient: null, parameters.Logger), endpoint), setAsDefault); @@ -93,29 +92,10 @@ public static class HuggingFaceKernelBuilderExtensions builder.WithAIService(serviceId, (parameters) => new HuggingFaceTextEmbeddingGeneration( model, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), endpoint), setAsDefault); return builder; } - - /// - /// Retrieves an instance of HttpClient. - /// - /// The kernel configuration. - /// An optional pre-existing instance of HttpClient. - /// An optional logger. - /// An instance of HttpClient. - private static HttpClient GetHttpClient(KernelConfig config, HttpClient? httpClient, ILogger? logger) - { - if (httpClient == null) - { - var retryHandler = config.HttpHandlerFactory.Create(logger); - retryHandler.InnerHandler = NonDisposableHttpClientHandler.Instance; - return new HttpClient(retryHandler, false); // We should refrain from disposing the underlying SK default HttpClient handler as it would impact other HTTP clients that utilize the same handler. - } - - return httpClient; - } } diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/OpenAIKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/OpenAIKernelBuilderExtensions.cs index ab41b39e3c..31583e78e7 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/OpenAIKernelBuilderExtensions.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/OpenAIKernelBuilderExtensions.cs @@ -46,7 +46,7 @@ public static class OpenAIKernelBuilderExtensions deploymentName, endpoint, apiKey, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger), setAsDefault); @@ -78,7 +78,7 @@ public static class OpenAIKernelBuilderExtensions deploymentName, endpoint, credentials, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger), setAsDefault); @@ -110,7 +110,7 @@ public static class OpenAIKernelBuilderExtensions modelId, apiKey, orgId, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger), setAsDefault); return builder; @@ -145,7 +145,7 @@ public static class OpenAIKernelBuilderExtensions deploymentName, endpoint, apiKey, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger), setAsDefault); return builder; @@ -176,7 +176,7 @@ public static class OpenAIKernelBuilderExtensions deploymentName, endpoint, credential, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger), setAsDefault); return builder; @@ -207,7 +207,7 @@ public static class OpenAIKernelBuilderExtensions modelId, apiKey, orgId, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger), setAsDefault); return builder; @@ -243,7 +243,7 @@ public static class OpenAIKernelBuilderExtensions deploymentName, endpoint, apiKey, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger); builder.WithAIService(serviceId, Factory, setAsDefault); @@ -283,7 +283,7 @@ public static class OpenAIKernelBuilderExtensions deploymentName, endpoint, credentials, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger); builder.WithAIService(serviceId, Factory, setAsDefault); @@ -323,7 +323,7 @@ public static class OpenAIKernelBuilderExtensions modelId, apiKey, orgId, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger); builder.WithAIService(serviceId, Factory, setAsDefault); @@ -362,7 +362,7 @@ public static class OpenAIKernelBuilderExtensions new OpenAIImageGeneration( apiKey, orgId, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger), setAsDefault); @@ -392,7 +392,7 @@ public static class OpenAIKernelBuilderExtensions new AzureOpenAIImageGeneration( endpoint, apiKey, - GetHttpClient(parameters.Config, httpClient, parameters.Logger), + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), parameters.Logger, maxRetryCount), setAsDefault); @@ -400,23 +400,5 @@ public static class OpenAIKernelBuilderExtensions return builder; } - /// - /// Retrieves an instance of HttpClient. - /// - /// The kernel configuration. - /// An optional pre-existing instance of HttpClient. - /// An optional logger. - /// An instance of HttpClient. - private static HttpClient GetHttpClient(KernelConfig config, HttpClient? httpClient, ILogger? logger) - { - if (httpClient == null) - { - var retryHandler = config.HttpHandlerFactory.Create(logger); - retryHandler.InnerHandler = NonDisposableHttpClientHandler.Instance; - return new HttpClient(retryHandler, false); // We should refrain from disposing the underlying SK default HttpClient handler as it would impact other HTTP clients that utilize the same handler. - } - - return httpClient; - } #endregion } diff --git a/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs index e43b84ef56..000864986e 100644 --- a/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs +++ b/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. using System.Net.Http; -using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.Connectors.Memory.Pinecone; #pragma warning disable IDE0130 @@ -32,30 +31,11 @@ public static class PineconeKernelBuilderExtensions environment, apiKey, parameters.Logger, - GetHttpClient(parameters.Config, httpClient, parameters.Logger)); + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger)); return new PineconeMemoryStore(client, parameters.Logger); }); return builder; } - - /// - /// Retrieves an instance of HttpClient. - /// - /// The kernel configuration. - /// An optional pre-existing instance of HttpClient. - /// An optional logger. - /// An instance of HttpClient. - private static HttpClient GetHttpClient(KernelConfig config, HttpClient? httpClient, ILogger? logger) - { - if (httpClient == null) - { - var retryHandler = config.HttpHandlerFactory.Create(logger); - retryHandler.InnerHandler = NonDisposableHttpClientHandler.Instance; - return new HttpClient(retryHandler, false); // We should refrain from disposing the underlying SK default HttpClient handler as it would impact other HTTP clients that utilize the same handler. - } - - return httpClient; - } } diff --git a/dotnet/src/Connectors/Connectors.Memory.Qdrant/Connectors.Memory.Qdrant.csproj b/dotnet/src/Connectors/Connectors.Memory.Qdrant/Connectors.Memory.Qdrant.csproj index 1436da3b86..4600412ac9 100644 --- a/dotnet/src/Connectors/Connectors.Memory.Qdrant/Connectors.Memory.Qdrant.csproj +++ b/dotnet/src/Connectors/Connectors.Memory.Qdrant/Connectors.Memory.Qdrant.csproj @@ -9,6 +9,7 @@ + @@ -16,10 +17,6 @@ Qdrant connector for Semantic Kernel skills and semantic memory - - - - diff --git a/dotnet/src/Connectors/Connectors.Memory.Qdrant/Diagnostics/NullableAttributes.cs b/dotnet/src/Connectors/Connectors.Memory.Qdrant/Diagnostics/NullableAttributes.cs deleted file mode 100644 index c63b1cb297..0000000000 --- a/dotnet/src/Connectors/Connectors.Memory.Qdrant/Diagnostics/NullableAttributes.cs +++ /dev/null @@ -1,167 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -// This was copied from https://github.com/dotnet/runtime/blob/39b9607807f29e48cae4652cd74735182b31182e/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs -// and updated to have the scope of the attributes be internal. - -#pragma warning disable IDE0130 // Namespace does not match folder structure -// ReSharper disable once CheckNamespace -namespace System.Diagnostics.CodeAnalysis; -#pragma warning restore IDE0130 - -#if !NETCOREAPP - -/// Specifies that null is allowed as an input even if the corresponding type disallows it. -[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] -internal sealed class AllowNullAttribute : Attribute -{ -} - -/// Specifies that null is disallowed as an input even if the corresponding type allows it. -[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] -internal sealed class DisallowNullAttribute : Attribute -{ -} - -/// Specifies that an output may be null even if the corresponding type disallows it. -[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] -internal sealed class MaybeNullAttribute : Attribute -{ -} - -/// Specifies that an output will not be null even if the corresponding type allows it. -[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] -internal sealed class NotNullAttribute : Attribute -{ -} - -/// Specifies that when a method returns , the parameter may be null even if the corresponding type disallows it. -[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -internal sealed class MaybeNullWhenAttribute : Attribute -{ - /// Initializes the attribute with the specified return value condition. - /// - /// The return value condition. If the method returns this value, the associated parameter may be null. - /// - public MaybeNullWhenAttribute(bool returnValue) => this.ReturnValue = returnValue; - - /// Gets the return value condition. - public bool ReturnValue { get; } -} - -/// Specifies that when a method returns , the parameter will not be null even if the corresponding type allows it. -[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -internal sealed class NotNullWhenAttribute : Attribute -{ - /// Initializes the attribute with the specified return value condition. - /// - /// The return value condition. If the method returns this value, the associated parameter will not be null. - /// - public NotNullWhenAttribute(bool returnValue) => this.ReturnValue = returnValue; - - /// Gets the return value condition. - public bool ReturnValue { get; } -} - -/// Specifies that the output will be non-null if the named parameter is non-null. -[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] -internal sealed class NotNullIfNotNullAttribute : Attribute -{ - /// Initializes the attribute with the associated parameter name. - /// - /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null. - /// - public NotNullIfNotNullAttribute(string parameterName) => this.ParameterName = parameterName; - - /// Gets the associated parameter name. - public string ParameterName { get; } -} - -/// Applied to a method that will never return under any circumstance. -[AttributeUsage(AttributeTargets.Method, Inherited = false)] -internal sealed class DoesNotReturnAttribute : Attribute -{ -} - -/// Specifies that the method will not return if the associated Boolean parameter is passed the specified value. -[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -internal sealed class DoesNotReturnIfAttribute : Attribute -{ - /// Initializes the attribute with the specified parameter value. - /// - /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to - /// the associated parameter matches this value. - /// - public DoesNotReturnIfAttribute(bool parameterValue) => this.ParameterValue = parameterValue; - - /// Gets the condition parameter value. - public bool ParameterValue { get; } -} - -#endif - -#if !NETCOREAPP || NETCOREAPP3_1 - -/// Specifies that the method or property will ensure that the listed field and property members have not-null values. -[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] -internal sealed class MemberNotNullAttribute : Attribute -{ - /// Initializes the attribute with a field or property member. - /// - /// The field or property member that is promised to be not-null. - /// - [SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")] - public MemberNotNullAttribute(string member) => this.Members = new[] { member }; - - /// Initializes the attribute with the list of field and property members. - /// - /// The list of field and property members that are promised to be not-null. - /// - public MemberNotNullAttribute(params string[] members) => this.Members = members; - - /// Gets field or property member names. - public string[] Members { get; } -} - -/// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. -[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] -internal sealed class MemberNotNullWhenAttribute : Attribute -{ - /// Initializes the attribute with the specified return value condition and a field or property member. - /// - /// The return value condition. If the method returns this value, the associated parameter will not be null. - /// - /// - /// The field or property member that is promised to be not-null. - /// - [SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")] - public MemberNotNullWhenAttribute(bool returnValue, string member) - { - this.ReturnValue = returnValue; - this.Members = new[] { member }; - } - - /// Initializes the attribute with the specified return value condition and list of field and property members. - /// - /// The return value condition. If the method returns this value, the associated parameter will not be null. - /// - /// - /// The list of field and property members that are promised to be not-null. - /// - public MemberNotNullWhenAttribute(bool returnValue, params string[] members) - { - this.ReturnValue = returnValue; - this.Members = members; - } - - /// Gets the return value condition. - public bool ReturnValue { get; } - - /// Gets field or property member names. - public string[] Members { get; } -} - -#endif diff --git a/dotnet/src/Connectors/Connectors.Memory.Qdrant/Http/ApiSchema/GetVectorsRequest.cs b/dotnet/src/Connectors/Connectors.Memory.Qdrant/Http/ApiSchema/GetVectorsRequest.cs index 22f075df2a..6abb67ec42 100644 --- a/dotnet/src/Connectors/Connectors.Memory.Qdrant/Http/ApiSchema/GetVectorsRequest.cs +++ b/dotnet/src/Connectors/Connectors.Memory.Qdrant/Http/ApiSchema/GetVectorsRequest.cs @@ -75,7 +75,7 @@ internal sealed class GetVectorsRequest public HttpRequestMessage Build() { return HttpRequest.CreatePostRequest( - $"/collections/{this.Collection}/points", + $"collections/{this.Collection}/points", payload: this); } diff --git a/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantKernelBuilderExtensions.cs new file mode 100644 index 0000000000..2e6e6e3a39 --- /dev/null +++ b/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantKernelBuilderExtensions.cs @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Net.Http; +using Microsoft.SemanticKernel.Connectors.Memory.Qdrant; + +#pragma warning disable IDE0130 +namespace Microsoft.SemanticKernel; +#pragma warning restore IDE0130 + +/// +/// Provides extension methods for the class to configure Qdrant memory connector. +/// +public static class QdrantKernelBuilderExtensions +{ + /// + /// Registers Qdrant memory connector. + /// + /// The instance. + /// The Qdrant Vector Database endpoint. + /// The size of the vectors. + /// Self instance + public static KernelBuilder WithQdrantMemoryStore(this KernelBuilder builder, + string endpoint, + int vectorSize) + { + builder.WithMemoryStorage((parameters) => + { + var client = new QdrantVectorDbClient( + HttpClientProvider.GetHttpClient(parameters.Config, null, parameters.Logger), + vectorSize, + endpoint, + parameters.Logger); + + return new QdrantMemoryStore(client, parameters.Logger); + }); + + return builder; + } + + /// + /// Registers Qdrant memory connector. + /// + /// The instance + /// The optional instance used for making HTTP requests. + /// The size of the vectors. + /// The Qdrant Vector Database endpoint. If not specified, the base address of the HTTP client is used. + /// Self instance + public static KernelBuilder WithQdrantMemoryStore(this KernelBuilder builder, + HttpClient httpClient, + int vectorSize, + string? endpoint = null) + { + builder.WithMemoryStorage((parameters) => + { + var client = new QdrantVectorDbClient( + HttpClientProvider.GetHttpClient(parameters.Config, httpClient, parameters.Logger), + vectorSize, + endpoint, + parameters.Logger); + + return new QdrantMemoryStore(client, parameters.Logger); + }); + + return builder; + } +} diff --git a/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantMemoryStore.cs index 9bc4796436..38753c57c8 100644 --- a/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantMemoryStore.cs @@ -15,34 +15,72 @@ using Microsoft.SemanticKernel.Memory; namespace Microsoft.SemanticKernel.Connectors.Memory.Qdrant; /// -/// An implementation of for Qdrant Vector database. +/// An implementation of for Qdrant Vector Database. /// -/// The Embedding data is saved to a Qdrant Vector database instance specified in the constructor by url and port. +/// The Embedding data is saved to a Qdrant Vector Database instance specified in the constructor by url and port. /// The embedding data persists between subsequent instances and has similarity search capability. /// public class QdrantMemoryStore : IMemoryStore { /// - /// The Qdrant Vector database memory store logger. + /// The Qdrant Vector Database memory store logger. /// private readonly ILogger? _logger; /// - /// Constructor for a memory store backed by a Qdrant Vector database instance. + /// Constructor for a memory store backed by a Qdrant Vector Database instance. /// /// /// /// /// + [Obsolete("This constructor is deprecated and will be removed in one of the next SK SDK versions. Please use one of the alternative constructors.")] public QdrantMemoryStore(string host, int port, int vectorSize, ILogger? logger = null) { this._logger = logger; this._qdrantClient = new QdrantVectorDbClient(endpoint: host, port: port, vectorSize: vectorSize, log: logger); } + /// + /// Initializes a new instance of the class. + /// + /// The Qdrant Vector Database endpoint. + /// The size of the vectors used. + /// Optional logger instance. + public QdrantMemoryStore(string endpoint, int vectorSize, ILogger? logger = null) + { + this._qdrantClient = new QdrantVectorDbClient(endpoint, vectorSize, logger); + this._logger = logger; + } + + /// + /// Initializes a new instance of the class. + /// + /// The instance used for making HTTP requests. + /// The size of the vectors used in the Qdrant Vector Database. + /// The optional endpoint URL for the Qdrant Vector Database. If not specified, the base address of the HTTP client is used. + /// Optional logger instance. + public QdrantMemoryStore(HttpClient httpClient, int vectorSize, string? endpoint = null, ILogger? logger = null) + { + this._qdrantClient = new QdrantVectorDbClient(httpClient, vectorSize, endpoint, logger); + this._logger = logger; + } + + /// + /// Initializes a new instance of the class. + /// + /// The Qdrant Db client for interacting with Qdrant Vector Database. + /// Optional logger instance. + public QdrantMemoryStore(IQdrantVectorDbClient client, ILogger? logger = null) + { + this._qdrantClient = client; + this._logger = logger; + } + /// /// Constructor for a memory store backed by a /// + [Obsolete("This constructor is deprecated and will be removed in one of the next SK SDK versions. Please use one of the alternative constructors.")] public QdrantMemoryStore(IQdrantVectorDbClient client) { this._qdrantClient = client; diff --git a/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantVectorDbClient.cs b/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantVectorDbClient.cs index c8b5f6003b..0c3e634b28 100644 --- a/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantVectorDbClient.cs +++ b/dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantVectorDbClient.cs @@ -11,6 +11,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.SemanticKernel.AI; using Microsoft.SemanticKernel.Connectors.Memory.Qdrant.Diagnostics; using Microsoft.SemanticKernel.Connectors.Memory.Qdrant.Http; using Microsoft.SemanticKernel.Connectors.Memory.Qdrant.Http.ApiSchema; @@ -18,19 +19,23 @@ using Microsoft.SemanticKernel.Connectors.Memory.Qdrant.Http.ApiSchema; namespace Microsoft.SemanticKernel.Connectors.Memory.Qdrant; /// -/// An implementation of a client for the Qdrant VectorDB. This class is used to -/// connect, create, delete, and get embeddings data from a Qdrant VectorDB instance. +/// An implementation of a client for the Qdrant Vector Database. This class is used to +/// connect, create, delete, and get embeddings data from a Qdrant Vector Database instance. /// -public class QdrantVectorDbClient : IQdrantVectorDbClient +#pragma warning disable CA1001 // Types that own disposable fields should be disposable. Explanation - In this case, there is no need to dispose because either the NonDisposableHttpClientHandler or a custom HTTP client is being used. +public sealed class QdrantVectorDbClient : IQdrantVectorDbClient +#pragma warning restore CA1001 // Types that own disposable fields should be disposable. Explanation - In this case, there is no need to dispose because either the NonDisposableHttpClientHandler or a custom HTTP client is being used. { /// /// The endpoint for the Qdrant service. /// + [Obsolete("This property is deprecated and will be removed in one of the next SK SDK versions.")] public string BaseAddress => this._httpClient.BaseAddress.ToString(); /// /// The port for the Qdrant service. /// + [Obsolete("This property is deprecated and will be removed in one of the next SK SDK versions.")] public int Port => this._httpClient.BaseAddress.Port; /// @@ -41,6 +46,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient /// /// /// + [Obsolete("This constructor is deprecated and will be removed in one of the next SK SDK versions. Please use one of the alternative constructors.")] public QdrantVectorDbClient( string endpoint, int vectorSize, @@ -51,16 +57,59 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient Verify.ArgNotNullOrEmpty(endpoint, "Qdrant endpoint cannot be null or empty"); this._vectorSize = vectorSize; - this._log = log ?? NullLogger.Instance; + this._logger = log ?? NullLogger.Instance; this._httpClient = httpClient ?? new HttpClient(HttpHandlers.CheckCertificateRevocation); this._httpClient.BaseAddress = SanitizeEndpoint(endpoint, port); } + /// + /// Initializes a new instance of the class. + /// + /// The Qdrant Vector Database endpoint. + /// The size of the vectors used in the Qdrant Vector Database. + /// Optional logger instance. + public QdrantVectorDbClient( + string endpoint, + int vectorSize, + ILogger? logger = null) + { + this._vectorSize = vectorSize; + this._httpClient = new HttpClient(NonDisposableHttpClientHandler.Instance, disposeHandler: false); + this._httpClient.BaseAddress = SanitizeEndpoint(endpoint); + this._logger = logger ?? NullLogger.Instance; + } + + /// + /// Initializes a new instance of the class. + /// + /// The instance used for making HTTP requests. + /// The size of the vectors used in the Qdrant Vector Database. + /// The optional endpoint URL for the Qdrant Vector Database. If not specified, the base address of the HTTP client is used. + /// Optional logger instance. + public QdrantVectorDbClient( + HttpClient httpClient, + int vectorSize, + string? endpoint = null, + ILogger? logger = null) + { + if (string.IsNullOrEmpty(httpClient.BaseAddress?.AbsoluteUri) && string.IsNullOrEmpty(endpoint)) + { + throw new AIException( + AIException.ErrorCodes.InvalidConfiguration, + "The HttpClient BaseAddress and endpoint are both null or empty. Please ensure at least one is provided."); + } + + this._httpClient = httpClient; + this._vectorSize = vectorSize; + this._endpointOverride = string.IsNullOrEmpty(endpoint) ? null : SanitizeEndpoint(endpoint!); + this._logger = logger ?? NullLogger.Instance; + } + /// public async IAsyncEnumerable GetVectorsByIdAsync(string collectionName, IEnumerable pointIds, bool withVectors = false, [EnumeratorCancellation] CancellationToken cancellationToken = default) { - this._log.LogDebug("Searching vectors by point ID"); + this._logger.LogDebug("Searching vectors by point ID"); using HttpRequestMessage request = GetVectorsRequest.Create(collectionName) .WithPointIDs(pointIds) @@ -75,7 +124,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient } catch (HttpRequestException e) { - this._log.LogDebug("Vectors not found {0}", e.Message); + this._logger.LogDebug("Vectors not found {0}", e.Message); yield break; } @@ -83,13 +132,13 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient if (data == null) { - this._log.LogWarning("Unable to deserialize Get response"); + this._logger.LogWarning("Unable to deserialize Get response"); yield break; } if (!data.Result.Any()) { - this._log.LogWarning("Vectors not found"); + this._logger.LogWarning("Vectors not found"); yield break; } @@ -125,7 +174,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient } catch (HttpRequestException e) { - this._log.LogDebug("Request for vector with payload ID failed {0}", e.Message); + this._logger.LogDebug("Request for vector with payload ID failed {0}", e.Message); return null; } @@ -133,13 +182,13 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient if (data == null) { - this._log.LogWarning("Unable to deserialize Search response"); + this._logger.LogWarning("Unable to deserialize Search response"); return null; } if (!data.Results.Any()) { - this._log.LogDebug("Vector not found"); + this._logger.LogDebug("Vector not found"); return null; } @@ -150,7 +199,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient embedding: point.Vector ?? Array.Empty(), payload: point.Payload, tags: null); - this._log.LogDebug("Vector found}"); + this._logger.LogDebug("Vector found}"); return record; } @@ -158,7 +207,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient /// public async Task DeleteVectorsByIdAsync(string collectionName, IEnumerable pointIds, CancellationToken cancellationToken = default) { - this._log.LogDebug("Deleting vector by point ID"); + this._logger.LogDebug("Deleting vector by point ID"); Verify.NotNullOrEmpty(collectionName, "Collection name is empty"); Verify.NotNull(pointIds, "Qdrant point IDs are NULL"); @@ -174,16 +223,16 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient var result = JsonSerializer.Deserialize(responseContent); if (result?.Status == "ok") { - this._log.LogDebug("Vector being deleted"); + this._logger.LogDebug("Vector being deleted"); } else { - this._log.LogWarning("Vector delete failed"); + this._logger.LogWarning("Vector delete failed"); } } catch (HttpRequestException e) { - this._log.LogError(e, "Vector delete request failed: {0}", e.Message); + this._logger.LogError(e, "Vector delete request failed: {0}", e.Message); } } @@ -194,11 +243,11 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient if (existingRecord == null) { - this._log.LogDebug("Vector not found, nothing to delete"); + this._logger.LogDebug("Vector not found, nothing to delete"); return; } - this._log.LogDebug("Vector found, deleting"); + this._logger.LogDebug("Vector found, deleting"); using var request = DeleteVectorsRequest .DeleteFrom(collectionName) @@ -213,23 +262,23 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient var result = JsonSerializer.Deserialize(responseContent); if (result?.Status == "ok") { - this._log.LogDebug("Vector being deleted"); + this._logger.LogDebug("Vector being deleted"); } else { - this._log.LogWarning("Vector delete failed"); + this._logger.LogWarning("Vector delete failed"); } } catch (HttpRequestException e) { - this._log.LogError(e, "Vector delete request failed: {0}", e.Message); + this._logger.LogError(e, "Vector delete request failed: {0}", e.Message); } } /// public async Task UpsertVectorsAsync(string collectionName, IEnumerable vectorData, CancellationToken cancellationToken = default) { - this._log.LogDebug("Upserting vectors"); + this._logger.LogDebug("Upserting vectors"); Verify.NotNull(vectorData, "The vector data entries are NULL"); Verify.NotNullOrEmpty(collectionName, "Collection name is empty"); @@ -244,16 +293,16 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient var result = JsonSerializer.Deserialize(responseContent); if (result?.Status == "ok") { - this._log.LogDebug("Vectors upserted"); + this._logger.LogDebug("Vectors upserted"); } else { - this._log.LogWarning("Vector upserts failed"); + this._logger.LogWarning("Vector upserts failed"); } } catch (HttpRequestException e) { - this._log.LogError(e, "Vector upserts request failed: {0}", e.Message); + this._logger.LogError(e, "Vector upserts request failed: {0}", e.Message); } } @@ -267,7 +316,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient IEnumerable? requiredTags = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { - this._log.LogDebug("Searching top {0} nearest vectors", top); + this._logger.LogDebug("Searching top {0} nearest vectors", top); Verify.NotNull(target, "The given vector is NULL"); @@ -285,7 +334,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient if (response.StatusCode == HttpStatusCode.NotFound) { - this._log.LogWarning("No vectors were found."); + this._logger.LogWarning("No vectors were found."); yield break; } @@ -295,13 +344,13 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient if (data == null) { - this._log.LogWarning("Unable to deserialize Search response"); + this._logger.LogWarning("Unable to deserialize Search response"); yield break; } if (!data.Results.Any()) { - this._log.LogWarning("Nothing found"); + this._logger.LogWarning("Nothing found"); yield break; } @@ -328,7 +377,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient /// public async Task CreateCollectionAsync(string collectionName, CancellationToken cancellationToken = default) { - this._log.LogDebug("Creating collection {0}", collectionName); + this._logger.LogDebug("Creating collection {0}", collectionName); using var request = CreateCollectionRequest .Create(collectionName, this._vectorSize, QdrantDistanceType.Cosine) @@ -348,7 +397,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient } catch (HttpRequestException e) { - this._log.LogError(e, "Collection upsert failed: {0}, {1}", e.Message, responseContent); + this._logger.LogError(e, "Collection upsert failed: {0}, {1}", e.Message, responseContent); throw; } } @@ -356,7 +405,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient /// public async Task DeleteCollectionAsync(string collectionName, CancellationToken cancellationToken = default) { - this._log.LogDebug("Deleting collection {0}", collectionName); + this._logger.LogDebug("Deleting collection {0}", collectionName); using var request = DeleteCollectionRequest.Create(collectionName).Build(); (HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); @@ -373,7 +422,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient } catch (HttpRequestException e) { - this._log.LogError(e, "Collection deletion failed: {0}, {1}", e.Message, responseContent); + this._logger.LogError(e, "Collection deletion failed: {0}, {1}", e.Message, responseContent); throw; } } @@ -381,7 +430,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient /// public async Task DoesCollectionExistAsync(string collectionName, CancellationToken cancellationToken = default) { - this._log.LogDebug("Fetching collection {0}", collectionName); + this._logger.LogDebug("Fetching collection {0}", collectionName); using var request = GetCollectionsRequest.Create(collectionName).Build(); (HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); @@ -396,7 +445,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient } else { - this._log.LogError("Collection fetch failed: {0}, {1}", response.StatusCode, responseContent); + this._logger.LogError("Collection fetch failed: {0}, {1}", response.StatusCode, responseContent); return false; } } @@ -404,7 +453,7 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient /// public async IAsyncEnumerable ListCollectionsAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) { - this._log.LogDebug("Listing collections"); + this._logger.LogDebug("Listing collections"); using var request = ListCollectionsRequest.Create().Build(); (HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false); @@ -419,11 +468,12 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient #region private ================================================================================ - private readonly ILogger _log; + private readonly ILogger _logger; private readonly HttpClient _httpClient; private readonly int _vectorSize; + private readonly Uri? _endpointOverride = null; - private static Uri SanitizeEndpoint(string endpoint, int? port) + private static Uri SanitizeEndpoint(string endpoint, int? port = null) { Verify.IsValidUrl(nameof(endpoint), endpoint, false, true, false); @@ -437,16 +487,22 @@ public class QdrantVectorDbClient : IQdrantVectorDbClient HttpRequestMessage request, CancellationToken cancellationToken = default) { + //Apply endpoint override if it's specified. + if (this._endpointOverride != null) + { + request.RequestUri = new Uri(this._endpointOverride, request.RequestUri); + } + HttpResponseMessage response = await this._httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (response.IsSuccessStatusCode) { - this._log.LogTrace("Qdrant responded successfully"); + this._logger.LogTrace("Qdrant responded successfully"); } else { - this._log.LogTrace("Qdrant responded with error"); + this._logger.LogTrace("Qdrant responded with error"); } return (response, responseContent); diff --git a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantKernelBuilderExtensionsTests.cs b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantKernelBuilderExtensionsTests.cs new file mode 100644 index 0000000000..aee72a868d --- /dev/null +++ b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantKernelBuilderExtensionsTests.cs @@ -0,0 +1,49 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; +using Microsoft.SemanticKernel; +using Xunit; + +namespace SemanticKernel.Connectors.UnitTests.Memory.Qdrant; + +public sealed class QdrantKernelBuilderExtensionsTests : IDisposable +{ + private HttpMessageHandlerStub messageHandlerStub; + private HttpClient httpClient; + + public QdrantKernelBuilderExtensionsTests() + { + this.messageHandlerStub = new HttpMessageHandlerStub(); + + this.httpClient = new HttpClient(this.messageHandlerStub, false); + } + + [Fact] + public async Task QdrantMemoryStoreShouldBeProperlyInitialized() + { + //Arrange + this.httpClient.BaseAddress = new Uri("https://fake-random-qdrant-host"); + this.messageHandlerStub.ResponseToReturn.Content = new StringContent("{\"result\":{\"collections\":[]}}", Encoding.UTF8, MediaTypeNames.Application.Json); + + var builder = new KernelBuilder(); + builder.WithQdrantMemoryStore(this.httpClient, 123); + builder.WithAzureTextEmbeddingGenerationService("fake-deployment-name", "https://fake-random-text-embedding-generation-host/fake-path", "fake-api-key"); + var kernel = builder.Build(); //This call triggers the internal factory registered by WithQdrantMemoryStore method to create an instance of the QdrantMemoryStore class. + + //Act + await kernel.Memory.GetCollectionsAsync(); //This call triggers a subsequent call to Qdrant memory store. + + //Assert + Assert.Equal("https://fake-random-qdrant-host/collections", this.messageHandlerStub?.RequestUri?.AbsoluteUri); + } + + public void Dispose() + { + this.httpClient.Dispose(); + this.messageHandlerStub.Dispose(); + } +} diff --git a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests.cs b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests.cs index f849d55c3b..545a2ff3a0 100644 --- a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests.cs +++ b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests.cs @@ -6,7 +6,9 @@ using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.AI.Embeddings; +using Microsoft.SemanticKernel.Connectors.Memory.Pinecone; using Microsoft.SemanticKernel.Connectors.Memory.Qdrant; using Microsoft.SemanticKernel.Connectors.Memory.Qdrant.Diagnostics; using Microsoft.SemanticKernel.Memory; @@ -32,8 +34,10 @@ public class QdrantMemoryStoreTests private readonly Embedding _embedding = new(new float[] { 1, 1, 1 }); private readonly Embedding _embedding2 = new(new float[] { 2, 2, 2 }); private readonly Embedding _embedding3 = new(new float[] { 3, 3, 3 }); + private readonly Mock> _mockLogger = new(); [Fact] + [Obsolete("This method is deprecated and will be removed in one of the next SK SDK versions.")] public void ConnectionCanBeInitialized() { // Arrange @@ -53,7 +57,7 @@ public class QdrantMemoryStoreTests mockQdrantClient .Setup(x => x.CreateCollectionAsync(It.IsAny(), It.IsAny())); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act await vectorStore.CreateCollectionAsync("test"); @@ -76,7 +80,7 @@ public class QdrantMemoryStoreTests mockQdrantClient .Setup(x => x.CreateCollectionAsync(It.IsAny(), It.IsAny())); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act await vectorStore.CreateCollectionAsync("test"); @@ -97,7 +101,7 @@ public class QdrantMemoryStoreTests .Setup>(x => x.ListCollectionsAsync(It.IsAny())) .Returns((new string[] { "test1", "test2" }).ToAsyncEnumerable()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var collections = await vectorStore.GetCollectionsAsync().ToListAsync(); @@ -120,7 +124,7 @@ public class QdrantMemoryStoreTests .Setup(x => x.DoesCollectionExistAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(true)); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act await vectorStore.DeleteCollectionAsync("test"); @@ -154,7 +158,7 @@ public class QdrantMemoryStoreTests .Setup(x => x.UpsertVectorsAsync(It.IsAny(), It.IsAny>(), It.IsAny())) .Throws(); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Assert await Assert.ThrowsAsync(() => vectorStore.UpsertAsync("test_collection", memoryRecord)); @@ -184,7 +188,7 @@ public class QdrantMemoryStoreTests mockQdrantClient .Setup(x => x.UpsertVectorsAsync(It.IsAny(), It.IsAny>(), It.IsAny())); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act string guidString = await vectorStore.UpsertAsync("test_collection", memoryRecord); @@ -226,7 +230,7 @@ public class QdrantMemoryStoreTests mockQdrantClient .Setup(x => x.UpsertVectorsAsync(It.IsAny(), It.IsAny>(), It.IsAny())); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act string guidString = await vectorStore.UpsertAsync("test_collection", memoryRecord); @@ -278,7 +282,7 @@ public class QdrantMemoryStoreTests mockQdrantClient .Setup(x => x.UpsertVectorsAsync(It.IsAny(), It.IsAny>(), It.IsAny())); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act string guidString = await vectorStore.UpsertAsync("test_collection", memoryRecord); @@ -329,7 +333,7 @@ public class QdrantMemoryStoreTests mockQdrantClient .Setup(x => x.UpsertVectorsAsync(It.IsAny(), It.IsAny>(), It.IsAny())); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act string guidString = await vectorStore.UpsertAsync("test_collection", memoryRecord); @@ -381,7 +385,7 @@ public class QdrantMemoryStoreTests mockQdrantClient .Setup(x => x.UpsertVectorsAsync(It.IsAny(), It.IsAny>(), It.IsAny())); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var keys = await vectorStore.UpsertBatchAsync("test_collection", new[] { memoryRecord, memoryRecord2, memoryRecord3 }).ToListAsync(); diff --git a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests2.cs b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests2.cs index 34eee41e8a..2b173851d5 100644 --- a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests2.cs +++ b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests2.cs @@ -5,7 +5,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.AI.Embeddings; +using Microsoft.SemanticKernel.Connectors.Memory.Pinecone; using Microsoft.SemanticKernel.Connectors.Memory.Qdrant; using Microsoft.SemanticKernel.Memory; using Moq; @@ -30,13 +32,14 @@ public class QdrantMemoryStoreTests2 private readonly Embedding _embedding = new(new float[] { 1, 1, 1 }); private readonly Embedding _embedding2 = new(new float[] { 2, 2, 2 }); private readonly Embedding _embedding3 = new(new float[] { 3, 3, 3 }); + private readonly Mock> _mockLogger = new(); [Fact] public async Task GetAsyncCallsDoNotRequestVectorsUnlessSpecifiedAsync() { // Arrange var mockQdrantClient = new Mock(); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); var guidString = Guid.NewGuid().ToString(); var guidString2 = Guid.NewGuid().ToString(); @@ -104,7 +107,7 @@ public class QdrantMemoryStoreTests2 .Setup>(x => x.GetVectorByPayloadIdAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync((QdrantVectorRecord?)null); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getResult = await vectorStore.GetAsync("test_collection", this._id, false); @@ -137,7 +140,7 @@ public class QdrantMemoryStoreTests2 .Setup>(x => x.GetVectorByPayloadIdAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(qdrantVectorRecord); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getResult = await vectorStore.GetAsync("test_collection", this._id, true); @@ -202,7 +205,7 @@ public class QdrantMemoryStoreTests2 .Setup>(x => x.GetVectorByPayloadIdAsync(It.IsAny(), this._id3, It.IsAny(), It.IsAny())) .ReturnsAsync(qdrantVectorRecord3); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getBatchResult = await vectorStore.GetBatchAsync("test_collection", new List { this._id, this._id2, this._id3 }, false).ToListAsync(); @@ -267,7 +270,7 @@ public class QdrantMemoryStoreTests2 .Setup>(x => x.GetVectorByPayloadIdAsync(It.IsAny(), this._id3, It.IsAny(), It.IsAny())) .ReturnsAsync((QdrantVectorRecord?)null); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getBatchResult = await vectorStore.GetBatchAsync("test_collection", new List { this._id, this._id2, this._id3 }, false).ToListAsync(); @@ -306,7 +309,7 @@ public class QdrantMemoryStoreTests2 .Setup>(x => x.GetVectorByPayloadIdAsync(It.IsAny(), this._id3, It.IsAny(), It.IsAny())) .ReturnsAsync((QdrantVectorRecord?)null); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getBatchResult = await vectorStore.GetBatchAsync("test_collection", new List { this._id, this._id2, this._id3 }, false).ToListAsync(); @@ -337,7 +340,7 @@ public class QdrantMemoryStoreTests2 x.GetVectorsByIdAsync(It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())) .Returns(AsyncEnumerable.Empty()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getResult = await vectorStore.GetWithPointIdAsync("test_collection", key, false); @@ -372,7 +375,7 @@ public class QdrantMemoryStoreTests2 x.GetVectorsByIdAsync(It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())) .Returns(new[] { qdrantVectorRecord }.ToAsyncEnumerable()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getResult = await vectorStore.GetWithPointIdAsync("test_collection", memoryRecord.Key, true); @@ -434,7 +437,7 @@ public class QdrantMemoryStoreTests2 x.GetVectorsByIdAsync(It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())) .Returns(new[] { qdrantVectorRecord, qdrantVectorRecord2, qdrantVectorRecord3 }.ToAsyncEnumerable()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getBatchResult = await vectorStore.GetWithPointIdBatchAsync("test_collection", new List { key, key2, key3 }, false).ToListAsync(); @@ -468,7 +471,7 @@ public class QdrantMemoryStoreTests2 x.GetVectorsByIdAsync(It.IsAny(), It.IsAny>(), It.IsAny(), It.IsAny())) .Returns(AsyncEnumerable.Empty()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var getBatchResult = await vectorStore.GetWithPointIdBatchAsync("test_collection", new List { key, key2, key3 }, false).ToListAsync(); @@ -490,7 +493,7 @@ public class QdrantMemoryStoreTests2 x.DeleteVectorByPayloadIdAsync(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act await vectorStore.RemoveAsync("test_collection", this._id); @@ -510,7 +513,7 @@ public class QdrantMemoryStoreTests2 x.DeleteVectorByPayloadIdAsync(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.CompletedTask); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act await vectorStore.RemoveBatchAsync("test_collection", new[] { this._id, this._id2, this._id3 }); @@ -536,7 +539,7 @@ public class QdrantMemoryStoreTests2 x.DeleteVectorsByIdAsync(It.IsAny(), It.IsAny>(), It.IsAny())) .Returns(Task.CompletedTask); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); var key = Guid.NewGuid().ToString(); // Act @@ -557,7 +560,7 @@ public class QdrantMemoryStoreTests2 x.DeleteVectorsByIdAsync(It.IsAny(), It.IsAny>(), It.IsAny())) .Returns(Task.CompletedTask); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); var key = Guid.NewGuid().ToString(); var key2 = Guid.NewGuid().ToString(); var key3 = Guid.NewGuid().ToString(); diff --git a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests3.cs b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests3.cs index fe91c75150..b1c42eb161 100644 --- a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests3.cs +++ b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantMemoryStoreTests3.cs @@ -8,7 +8,9 @@ using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.AI.Embeddings; +using Microsoft.SemanticKernel.Connectors.Memory.Pinecone; using Microsoft.SemanticKernel.Connectors.Memory.Qdrant; using Microsoft.SemanticKernel.Memory; using Moq; @@ -26,6 +28,7 @@ public class QdrantMemoryStoreTests3 private readonly string _text = "text"; private readonly string _description = "description"; private readonly Embedding _embedding = new(new float[] { 1, 1, 1 }); + private readonly Mock> _mockLogger = new(); [Fact] public async Task GetNearestMatchesAsyncCallsDoNotReturnVectorsUnlessSpecifiedAsync() @@ -43,7 +46,7 @@ public class QdrantMemoryStoreTests3 It.IsAny())) .Returns(AsyncEnumerable.Empty<(QdrantVectorRecord, double)>()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act _ = await vectorStore.GetNearestMatchAsync( @@ -122,7 +125,7 @@ public class QdrantMemoryStoreTests3 It.IsAny())) .Returns(AsyncEnumerable.Empty<(QdrantVectorRecord, double)>()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var similarityResult = await vectorStore.GetNearestMatchAsync( @@ -174,7 +177,7 @@ public class QdrantMemoryStoreTests3 It.IsAny())) .Returns(new[] { (qdrantVectorRecord, 0.5) }.ToAsyncEnumerable()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var similarityResult = await vectorStore.GetNearestMatchAsync( @@ -216,7 +219,7 @@ public class QdrantMemoryStoreTests3 It.IsAny())) .Returns(AsyncEnumerable.Empty<(QdrantVectorRecord, double)>()); - var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object); + var vectorStore = new QdrantMemoryStore(mockQdrantClient.Object, this._mockLogger.Object); // Act var similarityResults = await vectorStore.GetNearestMatchesAsync( @@ -230,7 +233,8 @@ public class QdrantMemoryStoreTests3 } [Fact] - public async Task ScoredVectorSupportsIntegerIds() + [Obsolete("This method is deprecated and will be removed in one of the next SK SDK versions.")] + public async Task ScoredVectorSupportsIntegerIdsObsolete() { // Arrange var payloadId = "payloadId"; @@ -269,7 +273,47 @@ public class QdrantMemoryStoreTests3 } [Fact] - public async Task ScoredVectorSupportsStringIds() + public async Task ScoredVectorSupportsIntegerIds() + { + // Arrange + var payloadId = "payloadId"; + var metadataId = "metadataId"; + var expectedId = 100; + + var scoredPointJsonWithIntegerId = + "{" + + "\"result\": " + + " [{" + + "\"id\": " + expectedId + "," + + "\"version\": 0," + + "\"score\": null," + + "\"payload\": {}," + + "\"vector\": null " + + "}]" + + "}"; + + using (var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(scoredPointJsonWithIntegerId) }) + { + var mockHttpMessageHandler = new Mock(); + mockHttpMessageHandler.Protected() + .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) + .ReturnsAsync(httpResponseMessage); + + //Act + using var httpClient = new HttpClient(mockHttpMessageHandler.Object); + { + var client = new QdrantVectorDbClient(httpClient, 1536, "https://fake-random-test-host"); + var result = await client.GetVectorByPayloadIdAsync(payloadId, metadataId); + + //Assert + Assert.Equal(result!.PointId, expectedId.ToString(CultureInfo.InvariantCulture)); + } + } + } + + [Fact] + [Obsolete("This method is deprecated and will be removed in one of the next SK SDK versions.")] + public async Task ScoredVectorSupportsStringIdsObsolete() { // Arrange var payloadId = "payloadId"; @@ -306,4 +350,44 @@ public class QdrantMemoryStoreTests3 } } } + + [Fact] + [Obsolete("This method is deprecated and will be removed in one of the next SK SDK versions.")] + public async Task ScoredVectorSupportsStringIds() + { + // Arrange + var payloadId = "payloadId"; + var metadataId = "metadataId"; + var expectedId = Guid.NewGuid().ToString(); + + var scoredPointJsonWithIntegerId = + "{" + + "\"result\": " + + " [{" + + "\"id\": \"" + expectedId + "\"," + + "\"version\": 0," + + "\"score\": null," + + "\"payload\": {}," + + "\"vector\": null " + + "}]" + + "}"; + + using (var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(scoredPointJsonWithIntegerId) }) + { + var mockHttpMessageHandler = new Mock(); + mockHttpMessageHandler.Protected() + .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) + .ReturnsAsync(httpResponseMessage); + + //Act + using var httpClient = new HttpClient(mockHttpMessageHandler.Object); + { + var client = new QdrantVectorDbClient(httpClient, 1536, "https://fake-random-test-host"); + var result = await client.GetVectorByPayloadIdAsync(payloadId, metadataId); + + //Assert + Assert.Equal(result!.PointId, expectedId); + } + } + } } diff --git a/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantVectorDbClientTests.cs b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantVectorDbClientTests.cs new file mode 100644 index 0000000000..09e29e49f3 --- /dev/null +++ b/dotnet/src/Connectors/Connectors.UnitTests/Memory/Qdrant/QdrantVectorDbClientTests.cs @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.SemanticKernel.Connectors.Memory.Qdrant; +using Xunit; + +namespace SemanticKernel.Connectors.UnitTests.Memory.Qdrant; + +public sealed class QdrantVectorDbClientTests : IDisposable +{ + private HttpMessageHandlerStub messageHandlerStub; + private HttpClient httpClient; + + public QdrantVectorDbClientTests() + { + this.messageHandlerStub = new HttpMessageHandlerStub(); + + this.httpClient = new HttpClient(this.messageHandlerStub, false); + } + + [Fact] + public async Task BaseAddressOfHttpClientShouldBeUsedIfNotOverrideProvided() + { + //Arrange + this.httpClient.BaseAddress = new Uri("https://fake-random-test-host:123/fake-path/"); + + var sut = new QdrantVectorDbClient(this.httpClient, 123); + + //Act + await sut.DoesCollectionExistAsync("fake-collection"); + + //Assert + Assert.Equal("https://fake-random-test-host:123/fake-path/collections/fake-collection", this.messageHandlerStub.RequestUri?.AbsoluteUri); + } + + [Fact] + public async Task EndpointOverrideShouldBeUsedIfProvided() + { + //Arrange + this.httpClient.BaseAddress = new Uri("https://fake-random-test-host:123/fake-path/"); + + var sut = new QdrantVectorDbClient(this.httpClient, 123, "https://fake-random-test-host-override:123/"); + + //Act + await sut.DoesCollectionExistAsync("fake-collection"); + + //Assert + Assert.Equal("https://fake-random-test-host-override:123/collections/fake-collection", this.messageHandlerStub.RequestUri?.AbsoluteUri); + } + + public void Dispose() + { + this.httpClient.Dispose(); + this.messageHandlerStub.Dispose(); + } +} diff --git a/dotnet/src/InternalUtilities/Http/HttpClientProvider.cs b/dotnet/src/InternalUtilities/Http/HttpClientProvider.cs new file mode 100644 index 0000000000..c62d22ee60 --- /dev/null +++ b/dotnet/src/InternalUtilities/Http/HttpClientProvider.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Net.Http; +using Microsoft.Extensions.Logging; +using Microsoft.SemanticKernel; + +/// +/// Provides functionality for retrieving instances of HttpClient. +/// +internal static class HttpClientProvider +{ + /// + /// Retrieves an instance of HttpClient. + /// + /// The kernel configuration. + /// An optional pre-existing instance of HttpClient. + /// An optional logger. + /// An instance of HttpClient. + public static HttpClient GetHttpClient(KernelConfig config, HttpClient? httpClient, ILogger? logger) + { + if (httpClient == null) + { + var retryHandler = config.HttpHandlerFactory.Create(logger); + retryHandler.InnerHandler = NonDisposableHttpClientHandler.Instance; + return new HttpClient(retryHandler, false); // We should refrain from disposing the underlying SK default HttpClient handler as it would impact other HTTP clients that utilize the same handler. + } + + return httpClient; + } +} diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionServiceExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionServiceExtensions.cs index 2d592c5f6c..5a0a9308af 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionServiceExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionServiceExtensions.cs @@ -20,7 +20,7 @@ public static class TextCompletionServiceExtensions /// Thrown when no suitable service is found. public static ITextCompletion GetTextCompletionServiceOrDefault( this IAIServiceProvider services, - string? serviceId = null) => services.GetService() + string? serviceId = null) => services.GetService(serviceId) ?? throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, "Text completion service not found"); /// diff --git a/samples/dotnet/kernel-syntax-examples/Example19_Qdrant.cs b/samples/dotnet/kernel-syntax-examples/Example19_Qdrant.cs index b9af42703b..c87e1c5dd5 100644 --- a/samples/dotnet/kernel-syntax-examples/Example19_Qdrant.cs +++ b/samples/dotnet/kernel-syntax-examples/Example19_Qdrant.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. using System; -using System.Globalization; using System.Threading.Tasks; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Connectors.Memory.Qdrant; @@ -15,13 +14,13 @@ public static class Example19_Qdrant public static async Task RunAsync() { - int qdrantPort = int.Parse(Env.Var("QDRANT_PORT"), CultureInfo.InvariantCulture); - QdrantMemoryStore memoryStore = new(Env.Var("QDRANT_ENDPOINT"), qdrantPort, vectorSize: 1536, ConsoleLogger.Log); + QdrantMemoryStore memoryStore = new(Env.Var("QDRANT_ENDPOINT"), 1536, ConsoleLogger.Log); IKernel kernel = Kernel.Builder .WithLogger(ConsoleLogger.Log) .WithOpenAITextCompletionService("text-davinci-003", Env.Var("OPENAI_API_KEY")) .WithOpenAITextEmbeddingGenerationService("text-embedding-ada-002", Env.Var("OPENAI_API_KEY")) .WithMemoryStorage(memoryStore) + //.WithQdrantMemoryStore(Env.Var("QDRANT_ENDPOINT"), 1536) // This method offers an alternative approach to registering Qdrant memory store. .Build(); Console.WriteLine("== Printing Collections in DB ==");