Skip to content

Commit

Permalink
.Net: Removing obsolete code for connectors and their registration. (#…
Browse files Browse the repository at this point in the history
…2013)

### Description

All code marked by the Obsolete attribute related to for connectors and
their registration is removed.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#dev-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
  • Loading branch information
SergeyMenshykh committed Jul 17, 2023
1 parent 8a33404 commit dfe3912
Show file tree
Hide file tree
Showing 35 changed files with 52 additions and 1,526 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static async Task RunAsync()
.Build();

// Load native skill
using var bingConnector = new BingConnector(Env.Var("BING_API_KEY"));
var bingConnector = new BingConnector(Env.Var("BING_API_KEY"));
var bing = new WebSearchEngineSkill(bingConnector);
var search = kernel.ImportSkill(bing, "bing");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static async Task RunAsync()
.Build();

// Load Bing skill
using var bingConnector = new BingConnector(Env.Var("BING_API_KEY"));
var bingConnector = new BingConnector(Env.Var("BING_API_KEY"));
kernel.ImportSkill(new WebSearchEngineSkill(bingConnector), "bing");

// Load Google skill
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/KernelSyntaxExamples/Example46_Weaviate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static async Task RunAsync()
{
string endpoint = Env.Var("WEAVIATE_ENDPOINT");
string apiKey = Env.Var("WEAVIATE_APIKEY");
using WeaviateMemoryStore memoryStore = new(endpoint, apiKey, ConsoleLogger.Log);
WeaviateMemoryStore memoryStore = new(endpoint, apiKey, ConsoleLogger.Log);
IKernel kernel = Kernel.Builder
.WithLogger(ConsoleLogger.Log)
.WithOpenAITextCompletionService("text-davinci-003", Env.Var("OPENAI_API_KEY"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static async Task RunChatCompletion(string question)

public static async Task RunWithQuestion(IKernel kernel, string question)
{
using var bingConnector = new BingConnector(Env.Var("BING_API_KEY"));
var bingConnector = new BingConnector(Env.Var("BING_API_KEY"));
var webSearchEngineSkill = new WebSearchEngineSkill(bingConnector);

kernel.ImportSkill(webSearchEngineSkill, "WebSearch");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,18 @@ namespace Microsoft.SemanticKernel.Connectors.AI.HuggingFace.TextCompletion;
/// <summary>
/// HuggingFace text completion service.
/// </summary>
public sealed class HuggingFaceTextCompletion : ITextCompletion, IDisposable
#pragma warning disable CA1001 // Types that own disposable fields should be disposable. No need to dispose the Http client here. It can either be an internal client using NonDisposableHttpClientHandler or an external client managed by the calling code, which should handle its disposal.
public sealed class HuggingFaceTextCompletion : ITextCompletion
#pragma warning restore CA1001 // Types that own disposable fields should be disposable. No need to dispose the Http client here. It can either be an internal client using NonDisposableHttpClientHandler or an external client managed by the calling code, which should handle its disposal.
{
private const string HttpUserAgent = "Microsoft-Semantic-Kernel";
private const string HuggingFaceApiEndpoint = "https://api-inference.huggingface.co/models";

private readonly string _model;
private readonly string? _endpoint;
private readonly HttpClient _httpClient;
private readonly bool _disposeHttpClient = true;
private readonly string? _apiKey;

/// <summary>
/// Initializes a new instance of the <see cref="HuggingFaceTextCompletion"/> class.
/// </summary>
/// <param name="endpoint">Endpoint for service API call.</param>
/// <param name="model">Model to use for service API call.</param>
/// <param name="httpClientHandler">Instance of <see cref="HttpClientHandler"/> to setup specific scenarios.</param>
[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 HuggingFaceTextCompletion(Uri endpoint, string model, HttpClientHandler httpClientHandler)
{
Verify.NotNull(endpoint);
Verify.NotNullOrWhiteSpace(model);

this._endpoint = endpoint.AbsoluteUri;
this._model = model;

this._httpClient = new(httpClientHandler);
}

/// <summary>
/// Initializes a new instance of the <see cref="HuggingFaceTextCompletion"/> class.
/// Using default <see cref="HttpClientHandler"/> implementation.
Expand All @@ -60,39 +43,6 @@ public HuggingFaceTextCompletion(Uri endpoint, string model)
this._model = model;

this._httpClient = new HttpClient(NonDisposableHttpClientHandler.Instance, disposeHandler: false);
this._disposeHttpClient = false; // Disposal is unnecessary as a non-disposable handler is used.
}

/// <summary>
/// Initializes a new instance of the <see cref="HuggingFaceTextCompletion"/> class.
/// Using HuggingFace API for service call, see https://huggingface.co/docs/api-inference/index.
/// </summary>
/// <param name="apiKey">HuggingFace API key, see https://huggingface.co/docs/api-inference/quicktour#running-inference-with-api-requests.</param>
/// <param name="model">Model to use for service API call.</param>
/// <param name="httpClientHandler">Instance of <see cref="HttpClientHandler"/> to setup specific scenarios.</param>
/// <param name="endpoint">Endpoint for service API call.</param>
[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 HuggingFaceTextCompletion(string apiKey, string model, HttpClientHandler httpClientHandler, string endpoint = HuggingFaceApiEndpoint)
: this(new Uri(endpoint), model, httpClientHandler)
{
Verify.NotNullOrWhiteSpace(apiKey);
this._apiKey = apiKey;
}

/// <summary>
/// Initializes a new instance of the <see cref="HuggingFaceTextCompletion"/> class.
/// Using HuggingFace API for service call, see https://huggingface.co/docs/api-inference/index.
/// Using default <see cref="HttpClientHandler"/> implementation.
/// </summary>
/// <param name="apiKey">HuggingFace API key, see https://huggingface.co/docs/api-inference/quicktour#running-inference-with-api-requests.</param>
/// <param name="model">Model to use for service API call.</param>
/// <param name="endpoint">Endpoint for service API call.</param>
[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 HuggingFaceTextCompletion(string apiKey, string model, string endpoint = HuggingFaceApiEndpoint)
: this(new Uri(endpoint), model)
{
Verify.NotNullOrWhiteSpace(apiKey);
this._apiKey = apiKey;
}

/// <summary>
Expand All @@ -112,7 +62,6 @@ public HuggingFaceTextCompletion(string model, string? apiKey = null, HttpClient
this._apiKey = apiKey;
this._httpClient = httpClient ?? new HttpClient(NonDisposableHttpClientHandler.Instance, disposeHandler: false);
this._endpoint = endpoint;
this._disposeHttpClient = false; // Disposal is unnecessary as we either use a non-disposable handler or utilize a custom HTTP client that we should not dispose.
}

/// <inheritdoc/>
Expand All @@ -136,16 +85,6 @@ public HuggingFaceTextCompletion(string model, string? apiKey = null, HttpClient
return await this.ExecuteGetCompletionsAsync(text, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc/>
[Obsolete("This method is deprecated and will be removed in one of the next SK SDK versions.")]
public void Dispose()
{
if (this._disposeHttpClient)
{
this._httpClient.Dispose();
}
}

#region private ================================================================================

private async Task<IReadOnlyList<ITextStreamingResult>> ExecuteGetCompletionsAsync(string text, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,15 @@ namespace Microsoft.SemanticKernel.Connectors.AI.HuggingFace.TextEmbedding;
/// <summary>
/// HuggingFace embedding generation service.
/// </summary>
public sealed class HuggingFaceTextEmbeddingGeneration : ITextEmbeddingGeneration, IDisposable
#pragma warning disable CA1001 // Types that own disposable fields should be disposable. No need to dispose the Http client here. It can either be an internal client using NonDisposableHttpClientHandler or an external client managed by the calling code, which should handle its disposal.
public sealed class HuggingFaceTextEmbeddingGeneration : ITextEmbeddingGeneration
#pragma warning restore CA1001 // Types that own disposable fields should be disposable. No need to dispose the Http client here. It can either be an internal client using NonDisposableHttpClientHandler or an external client managed by the calling code, which should handle its disposal.
{
private const string HttpUserAgent = "Microsoft-Semantic-Kernel";

private readonly string _model;
private readonly string? _endpoint;
private readonly HttpClient _httpClient;
private readonly bool _disposeHttpClient = true;

/// <summary>
/// Initializes a new instance of the <see cref="HuggingFaceTextEmbeddingGeneration"/> class.
/// </summary>
/// <param name="endpoint">Endpoint for service API call.</param>
/// <param name="model">Model to use for service API call.</param>
/// <param name="httpClientHandler">Instance of <see cref="HttpClientHandler"/> to setup specific scenarios.</param>
[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 HuggingFaceTextEmbeddingGeneration(Uri endpoint, string model, HttpClientHandler httpClientHandler)
{
Verify.NotNull(endpoint);
Verify.NotNullOrWhiteSpace(model);

this._endpoint = endpoint.AbsoluteUri;
this._model = model;

this._httpClient = new(httpClientHandler);
}

/// <summary>
/// Initializes a new instance of the <see cref="HuggingFaceTextEmbeddingGeneration"/> class.
Expand All @@ -58,7 +41,6 @@ public HuggingFaceTextEmbeddingGeneration(Uri endpoint, string model)
this._model = model;

this._httpClient = new HttpClient(NonDisposableHttpClientHandler.Instance, disposeHandler: false);
this._disposeHttpClient = false; // Disposal is unnecessary as we either use a non-disposable handler or utilize a custom HTTP client that we should not dispose.
}

/// <summary>
Expand All @@ -75,7 +57,6 @@ public HuggingFaceTextEmbeddingGeneration(string model, string endpoint)
this._endpoint = endpoint;

this._httpClient = new HttpClient(NonDisposableHttpClientHandler.Instance, disposeHandler: false);
this._disposeHttpClient = false; // Disposal is unnecessary as we either use a non-disposable handler or utilize a custom HTTP client that we should not dispose.
}

/// <summary>
Expand All @@ -99,8 +80,6 @@ public HuggingFaceTextEmbeddingGeneration(string model, HttpClient httpClient, s
AIException.ErrorCodes.InvalidConfiguration,
"The HttpClient BaseAddress and endpoint are both null or empty. Please ensure at least one is provided.");
}

this._disposeHttpClient = false; // We should not dispose custom HTTP clients.
}

/// <inheritdoc/>
Expand All @@ -109,16 +88,6 @@ public async Task<IList<Embedding<float>>> GenerateEmbeddingsAsync(IList<string>
return await this.ExecuteEmbeddingRequestAsync(data, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc/>
[Obsolete("This method is deprecated and will be removed in one of the next SK SDK versions.")]
public void Dispose()
{
if (this._disposeHttpClient)
{
this._httpClient.Dispose();
}
}

#region private ================================================================================

/// <summary>
Expand Down
Loading

0 comments on commit dfe3912

Please sign in to comment.