-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net Hugging Face HttpClient Connector Update (#5075)
### Motivation and Context Resolves Partially #4947 Update Hugging Face connector segregating the responsibilities of the TextGenerationService, TextEmbeddingService, HttpClient, JsonParsing, EndPointResolving. ### Description This enables paving the way to support additional Hugging Face modalities --------- Co-authored-by: Krzysztof Kasprowicz <60486987+Krzysztof318@users.noreply.github.com>
- Loading branch information
1 parent
f9771c6
commit 316ff5d
Showing
36 changed files
with
3,954 additions
and
319 deletions.
There are no files selected for viewing
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
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
8 changes: 8 additions & 0 deletions
8
dotnet/src/Connectors/Connectors.HuggingFace.UnitTests/.editorconfig
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,8 @@ | ||
# Suppressing errors for Test projects under dotnet folder | ||
[*.cs] | ||
dotnet_diagnostic.CA2007.severity = none # Do not directly await a Task | ||
dotnet_diagnostic.VSTHRD111.severity = none # Use .ConfigureAwait(bool) is hidden by default, set to none to prevent IDE from changing on autosave | ||
dotnet_diagnostic.CS1591.severity = none # Missing XML comment for publicly visible type or member | ||
dotnet_diagnostic.IDE1006.severity = warning # Naming rule violations | ||
|
||
resharper_convert_constructor_to_member_initializers_highlighting = false # Disable highlighting for "Convert constructor to member initializers" quick-fix |
57 changes: 57 additions & 0 deletions
57
...t/src/Connectors/Connectors.HuggingFace.UnitTests/Connectors.HuggingFace.UnitTests.csproj
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,57 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>SemanticKernel.Connectors.HuggingFace.UnitTests</AssemblyName> | ||
<RootNamespace>SemanticKernel.Connectors.HuggingFace.UnitTests</RootNamespace> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<LangVersion>12</LangVersion> | ||
<RollForward>LatestMajor</RollForward> | ||
<IsTestProject>true</IsTestProject> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>disable</ImplicitUsings> | ||
<IsPackable>false</IsPackable> | ||
<NoWarn>CA2007,CA1806,CA1869,CA1861,IDE0300,VSTHRD111,SKEXP0001,SKEXP0002,SKEXP0003,SKEXP0004,SKEXP0005,SKEXP0010,SKEXP0011,SKEXP0012,SKEXP0013,SKEXP0014,SKEXP0015,SKEXP0020,SKEXP0021,SKEXP0022,SKEXP0023,SKEXP0024,SKEXP0025,SKEXP0026,SKEXP0027,SKEXP0028,SKEXP0029,SKEXP0030,SKEXP0031,SKEXP0032,SKEXP0052</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<!-- Use newest available compiler to permit LangVersion 12. --> | ||
<!-- This can be removed once we no longer target the .NET 6 SDK in CI. --> | ||
<PackageReference Include="Microsoft.Net.Compilers.Toolset" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="Moq" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="System.Numerics.Tensors" /> | ||
<PackageReference Include="System.Text.Json" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/test/AssertExtensions.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj" /> | ||
<ProjectReference Include="..\Connectors.HuggingFace\Connectors.HuggingFace.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="TestData\*"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="OpenAI\TestData\*"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
68 changes: 68 additions & 0 deletions
68
...nnectors/Connectors.HuggingFace.UnitTests/Core/HuggingFacePromptExecutionSettingsTests.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,68 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text.Json; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Connectors.HuggingFace; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Connectors.HuggingFace.UnitTests.Core; | ||
|
||
public class HuggingFacePromptExecutionSettingsTests | ||
{ | ||
[Fact] | ||
public void FromExecutionSettingsWhenAlreadyHuggingFaceShouldReturnSameAsync() | ||
{ | ||
// Arrange | ||
var executionSettings = new HuggingFacePromptExecutionSettings(); | ||
|
||
// Act | ||
var huggingFaceExecutionSettings = HuggingFacePromptExecutionSettings.FromExecutionSettings(executionSettings); | ||
|
||
// Assert | ||
Assert.Same(executionSettings, huggingFaceExecutionSettings); | ||
} | ||
|
||
[Fact] | ||
public void FromExecutionSettingsWhenNullShouldReturnDefaultAsync() | ||
{ | ||
// Arrange | ||
HuggingFacePromptExecutionSettings? executionSettings = null; | ||
|
||
// Act | ||
var huggingFaceExecutionSettings = HuggingFacePromptExecutionSettings.FromExecutionSettings(executionSettings); | ||
|
||
// Assert | ||
Assert.Equal(HuggingFacePromptExecutionSettings.DefaultTextMaxTokens, huggingFaceExecutionSettings.MaxTokens); | ||
} | ||
|
||
[Fact] | ||
public void FromExecutionSettingsWhenSerializedHasPropertiesShouldPopulateSpecialized() | ||
{ | ||
string jsonSettings = """ | ||
{ | ||
"temperature": 0.5, | ||
"top_k": 50, | ||
"max_tokens": 100, | ||
"max_time": 10.0, | ||
"top_p": 0.9, | ||
"repetition_penalty": 1.0, | ||
"use_cache": true, | ||
"results_per_prompt": 1, | ||
"wait_for_model": false | ||
} | ||
"""; | ||
|
||
var executionSettings = JsonSerializer.Deserialize<PromptExecutionSettings>(jsonSettings); | ||
var huggingFaceExecutionSettings = HuggingFacePromptExecutionSettings.FromExecutionSettings(executionSettings); | ||
|
||
Assert.Equal(0.5, huggingFaceExecutionSettings.Temperature); | ||
Assert.Equal(50, huggingFaceExecutionSettings.TopK); | ||
Assert.Equal(100, huggingFaceExecutionSettings.MaxTokens); | ||
Assert.Equal(10.0, huggingFaceExecutionSettings.MaxTime); | ||
Assert.Equal(0.9, huggingFaceExecutionSettings.TopP); | ||
Assert.Equal(1.0, huggingFaceExecutionSettings.RepetitionPenalty); | ||
Assert.True(huggingFaceExecutionSettings.UseCache); | ||
Assert.Equal(1, huggingFaceExecutionSettings.ResultsPerPrompt); | ||
Assert.False(huggingFaceExecutionSettings.WaitForModel); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
dotnet/src/Connectors/Connectors.HuggingFace.UnitTests/HttpMessageHandlerStub.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,48 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SemanticKernel.Connectors.HuggingFace.UnitTests; | ||
|
||
internal sealed class HttpMessageHandlerStub : DelegatingHandler | ||
{ | ||
public HttpRequestHeaders? RequestHeaders { get; private set; } | ||
|
||
public HttpContentHeaders? ContentHeaders { get; private set; } | ||
|
||
public byte[]? RequestContent { get; private set; } | ||
|
||
public Uri? RequestUri { get; private set; } | ||
|
||
public HttpMethod? Method { get; private set; } | ||
|
||
public HttpResponseMessage ResponseToReturn { get; set; } | ||
|
||
public HttpMessageHandlerStub() | ||
{ | ||
this.ResponseToReturn = new HttpResponseMessage(System.Net.HttpStatusCode.OK); | ||
this.ResponseToReturn.Content = new StringContent("{}", Encoding.UTF8, "application/json"); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
this.Method = request.Method; | ||
this.RequestUri = request.RequestUri; | ||
this.RequestHeaders = request.Headers; | ||
if (request.Content is not null) | ||
{ | ||
#pragma warning disable CA2016 // Forward the 'CancellationToken' parameter to methods; overload doesn't exist on .NET Framework | ||
this.RequestContent = await request.Content.ReadAsByteArrayAsync(); | ||
#pragma warning restore CA2016 | ||
} | ||
|
||
this.ContentHeaders = request.Content?.Headers; | ||
|
||
return await Task.FromResult(this.ResponseToReturn); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...rc/Connectors/Connectors.HuggingFace.UnitTests/HuggingFaceKernelBuilderExtensionsTests.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,67 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Connectors.HuggingFace; | ||
using Microsoft.SemanticKernel.Embeddings; | ||
using Microsoft.SemanticKernel.TextGeneration; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Connectors.HuggingFace.UnitTests; | ||
|
||
public class HuggingFaceKernelBuilderExtensionsTests | ||
{ | ||
[Fact] | ||
public void AddHuggingFaceTextGenerationCreatesService() | ||
{ | ||
var builder = Kernel.CreateBuilder(); | ||
builder.AddHuggingFaceTextGeneration("model"); | ||
|
||
var kernel = builder.Build(); | ||
var service = kernel.GetRequiredService<ITextGenerationService>(); | ||
|
||
Assert.NotNull(kernel); | ||
Assert.NotNull(service); | ||
Assert.IsType<HuggingFaceTextGenerationService>(service); | ||
} | ||
|
||
[Fact] | ||
public void AddHuggingFaceTextGenerationToServiceCollection() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddHuggingFaceTextGeneration("model"); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
var service = serviceProvider.GetRequiredService<ITextGenerationService>(); | ||
|
||
Assert.NotNull(service); | ||
Assert.IsType<HuggingFaceTextGenerationService>(service); | ||
} | ||
|
||
[Fact] | ||
public void AddHuggingFaceTextEmbeddingGenerationCreatesService() | ||
{ | ||
var builder = Kernel.CreateBuilder(); | ||
builder.AddHuggingFaceTextEmbeddingGeneration("model"); | ||
|
||
var kernel = builder.Build(); | ||
var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>(); | ||
|
||
Assert.NotNull(kernel); | ||
Assert.NotNull(service); | ||
Assert.IsType<HuggingFaceTextEmbeddingGenerationService>(service); | ||
} | ||
|
||
[Fact] | ||
public void AddHuggingFaceTextEmbeddingsGenerationToServiceCollection() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddHuggingFaceTextEmbeddingGeneration("model"); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
var service = serviceProvider.GetRequiredService<ITextEmbeddingGenerationService>(); | ||
|
||
Assert.NotNull(service); | ||
Assert.IsType<HuggingFaceTextEmbeddingGenerationService>(service); | ||
} | ||
} |
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
Oops, something went wrong.