forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Http consistency: Pinecone (microsoft#1262)
### Description This PR introduces the following changes: 1. The PineconeClient class accepts a custom HTTP client, allowing the hosting application/client code to provide their own instances. 2. A new extension method, `WithPineconeMemoryStore`, has been added to the `KernelBuilder` class. This method simplifies the registration of the Pinecone memory store, aligns the registration process with other connector registrations, and enforces the usage of an HTTP client and retry handlers configured in the SK SDK. 3. The global static `HttpHandlers` class is removed because it goes against the agreed approach for the SK SDK's HTTP stack that assumes the usage of a custom HTTP client for fine-tuning instead of relying on global static variables. Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
- Loading branch information
1 parent
fc3202a
commit b18138e
Showing
7 changed files
with
143 additions
and
35 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
dotnet/src/Connectors/Connectors.Memory.Pinecone/Http/SecureHttpHandler.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Net.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel.Connectors.Memory.Pinecone; | ||
|
||
#pragma warning disable IDE0130 | ||
namespace Microsoft.SemanticKernel; | ||
#pragma warning restore IDE0130 | ||
|
||
/// <summary> | ||
/// Provides extension methods for the <see cref="KernelBuilder"/> class to configure Pinecone connectors. | ||
/// </summary> | ||
public static class PineconeKernelBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Registers Pinecone Memory Store. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="KernelBuilder"/> instance</param> | ||
/// <param name="environment">The environment for Pinecone.</param> | ||
/// <param name="apiKey">The API key for accessing Pinecone services.</param> | ||
/// <param name="httpClient">An optional HttpClient instance for making HTTP requests.</param> | ||
/// <returns>Self instance</returns> | ||
public static KernelBuilder WithPineconeMemoryStore(this KernelBuilder builder, | ||
string environment, | ||
string apiKey, | ||
HttpClient? httpClient = null) | ||
{ | ||
builder.WithMemoryStorage((parameters) => | ||
{ | ||
var client = new PineconeClient( | ||
environment, | ||
apiKey, | ||
parameters.Logger, | ||
GetHttpClient(parameters.Config, httpClient, parameters.Logger)); | ||
return new PineconeMemoryStore(client, parameters.Logger); | ||
}); | ||
|
||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves an instance of HttpClient. | ||
/// </summary> | ||
/// <param name="config">The kernel configuration.</param> | ||
/// <param name="httpClient">An optional pre-existing instance of HttpClient.</param> | ||
/// <param name="logger">An optional logger.</param> | ||
/// <returns>An instance of HttpClient.</returns> | ||
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; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...c/Connectors/Connectors.UnitTests/Memory/Pinecone/PineconeKernelBuilderExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Linq; | ||
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.Pinecone; | ||
|
||
public sealed class PineconeKernelBuilderExtensionsTests : IDisposable | ||
{ | ||
private HttpMessageHandlerStub messageHandlerStub; | ||
private HttpClient httpClient; | ||
|
||
public PineconeKernelBuilderExtensionsTests() | ||
{ | ||
this.messageHandlerStub = new HttpMessageHandlerStub(); | ||
|
||
this.httpClient = new HttpClient(this.messageHandlerStub, false); | ||
} | ||
|
||
[Fact] | ||
public async Task PineconeMemoryStoreShouldBeProperlyInitialized() | ||
{ | ||
//Arrange | ||
this.messageHandlerStub.ResponseToReturn.Content = new StringContent("[\"fake-index1\"]", Encoding.UTF8, MediaTypeNames.Application.Json); | ||
|
||
var builder = new KernelBuilder(); | ||
builder.WithPineconeMemoryStore("fake-environment", "fake-api-key", this.httpClient); | ||
builder.WithAzureTextEmbeddingGenerationService("fake-deployment-name", "https://fake-random-test-host/fake-path", "fake -api-key"); | ||
var kernel = builder.Build(); //This call triggers the internal factory registered by WithPineconeMemoryStore method to create an instance of the PineconeMemoryStore class. | ||
|
||
//Act | ||
await kernel.Memory.GetCollectionsAsync(); //This call triggers a subsequent call to Pinecone memory store. | ||
|
||
//Assert | ||
Assert.Equal("https://controller.fake-environment.pinecone.io/databases", this.messageHandlerStub?.RequestUri?.AbsoluteUri); | ||
|
||
var headerValues = Enumerable.Empty<string>(); | ||
var headerExists = this.messageHandlerStub?.RequestHeaders?.TryGetValues("Api-Key", out headerValues); | ||
Assert.True(headerExists); | ||
Assert.Contains(headerValues!, (value) => value == "fake-api-key"); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.httpClient.Dispose(); | ||
this.messageHandlerStub.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters