semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Fix hugging face embedding

Open N-E-W-T-O-N opened this issue 1 year ago • 17 comments

Motivation and Context

Fix the Bug #6635 HuggingFace Embedding: Unable to Deserialization for certain models

Description

As mentioned in the issue, the HuggingFace Embedding API interface returns responses typically in the form of List<ReadOnlyMemory<float>> and occasionally as List<List<List<ReadOnlyMemory<float>>>>. Currently, only the latter format is handled correctly, leading to deserialization issues.

To address this, I propose the following solution:

try {
    // Attempt to parse data as List<ReadOnlyMemory<float>> and return the parsed data
}
catch (KernelException ex1) {
    try {
        // If the first attempt fails, attempt to parse data as List<List<List<ReadOnlyMemory<float>>>>` and return the parsed data
    }
    catch (KernelException ex2) {
        // If both attempts fail, handle the exception (e.g., the model doesn't exist ,the model has still been loading, or an HTTP exception occurred) and rethrow the error
    }
}

Contribution Checklist

N-E-W-T-O-N avatar Jun 11 '24 18:06 N-E-W-T-O-N

Hello @dmytrostruk Can you please suggest to me where I can add the above custom HttpClient Example in main code .

N-E-W-T-O-N avatar Jun 12 '24 18:06 N-E-W-T-O-N

Hello @dmytrostruk Can you please suggest to me where I can add the above custom HttpClient Example in main code .

@N-E-W-T-O-N That's awesome, thanks for validating this idea and creating an example for this! I think you can put it here: https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Memory/HuggingFace_EmbeddingGeneration.cs

dmytrostruk avatar Jun 12 '24 20:06 dmytrostruk

I want to add two more parameters in the api's payload body namely 'use_cache' & 'wait_for_model'

I want to send this values as addition parameters in the HuggingFaceClient

https://huggingface.co/docs/api-inference/en/detailed_parameters

N-E-W-T-O-N avatar Jun 12 '24 21:06 N-E-W-T-O-N

@dmytrostruk Added my example in https://github.com/microsoft/semantic-kernel/commit/4f9eb974f6480c92679a25c4579de2244ceb2a32 . Please merge my PR.. Let me know for anymore Improvement.

N-E-W-T-O-N avatar Jun 15 '24 08:06 N-E-W-T-O-N

@RogerBarreto It might be possible that changes in TextEmbeddingResponse class will lead to test case failure

N-E-W-T-O-N avatar Jun 19 '24 19:06 N-E-W-T-O-N

@dmytrostruk

Remove the whitespace error. Please re run the workflow

N-E-W-T-O-N avatar Jun 22 '24 10:06 N-E-W-T-O-N

@N-E-W-T-O-N there are still formatting errors to be addressed. Suggest you run dotnet format on the solution.

markwallace-microsoft avatar Jun 22 '24 11:06 markwallace-microsoft

@dmytrostruk please check it now. Resolve check-format issues .Sorry for inconvenience

N-E-W-T-O-N avatar Jun 23 '24 13:06 N-E-W-T-O-N

@dmytrostruk may I ask how to test unit test cases locally

N-E-W-T-O-N avatar Jun 24 '24 20:06 N-E-W-T-O-N

may I ask how to test unit test cases locally

@N-E-W-T-O-N

cd dotnet/src/Connectors/Connectors.HuggingFace.UnitTests
dotnet test

dmytrostruk avatar Jun 24 '24 20:06 dmytrostruk

@dmytrostruk The issue is unit tests are still based on the previous version of embedding dimensions as noted by@RogerBarreto .Would you like me to update the unit test accordingly?

N-E-W-T-O-N avatar Jun 25 '24 05:06 N-E-W-T-O-N

The issue is unit tests are still based on the previous version of embedding dimensions as noted by@RogerBarreto .Would you like me to update the unit test accordingly?

@N-E-W-T-O-N Yes, if we are changing the behavior, then unit tests should be updated as well. Thanks!

dmytrostruk avatar Jun 25 '24 19:06 dmytrostruk

@dmytrostruk moved the class inside with access modifier 'private sealed'

Resolve the unit test issue

N-E-W-T-O-N avatar Jun 27 '24 19:06 N-E-W-T-O-N

@dmytrostruk anything to add from my side

N-E-W-T-O-N avatar Jun 30 '24 08:06 N-E-W-T-O-N

@dmytrostruk anything to add from my side

@N-E-W-T-O-N I think we are ready to merge this to main. I added a couple of small fixes to the example. Thanks for your contribution!

dmytrostruk avatar Jul 01 '24 22:07 dmytrostruk

@dmytrostruk updated the code based on the suggestion of @westey-m

N-E-W-T-O-N avatar Jul 03 '24 20:07 N-E-W-T-O-N

@dmytrostruk Please run the workflow test

N-E-W-T-O-N avatar Jul 04 '24 20:07 N-E-W-T-O-N

@dmytrostruk Please rerun the workflow. Resolved the issue

N-E-W-T-O-N avatar Jul 06 '24 17:07 N-E-W-T-O-N

@dmytrostruk Thanks for accepting my PR

N-E-W-T-O-N avatar Jul 08 '24 19:07 N-E-W-T-O-N

@N-E-W-T-O-N Thank you for contribution!

dmytrostruk avatar Jul 08 '24 19:07 dmytrostruk