microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.4k stars 3.15k forks source link

PlatformNotSupportedException using Semantic Kernel in Blazor Wasm #1792

Closed antpass79 closed 1 year ago

antpass79 commented 1 year ago

Describe the bug Using SK and Azure Open AI in Blazor WebAssembly, a PlatformNotSupportException is logged in DevTools.

To Reproduce Steps to reproduce the behavior:

  1. Follow the example "Prompt chaining" here https://github.com/microsoft/semantic-kernel/blob/main/dotnet/README.md
  2. Add WithLogger (without it, calling RunAsync, the output is the same of the input with any error/warning/message)
  3. Run the application
  4. See the exception in DevTools

Error: The type initializer for 'NonDisposableHttpClientHandler' threw an exception. System.TypeInitializationException: The type initializer for 'NonDisposableHttpClientHandler' threw an exception. ---> System.PlatformNotSupportedException: Operation is not supported on this platform. at System.Net.Http.BrowserHttpHandler.get_SslOptions() at .........

Expected behavior As a mantion in the tutorial:

ΔE = 0, ΔSuniv > 0, S = 0 at 0K.

Web:

Additional context I tried to change the HttpHandler using WithRetryHandlerFactory before the Build call. I tried also Configure and SetHttpRetryHandlerFactory before the Build call. But the stack was the same. Investigating the code I found here https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/InternalUtilities/src/Http/HttpClientProvider.cs#L30 that the GetHttpClient uses correctly the HttpHandler that I defined by the Factory function, but the InnerHandler is set with "NonDisposableHttpClientHandler.Instance" and I cannot change it. Based on the stack, I think that the issue could be related to it.

markwallace-microsoft commented 1 year ago

Hi @antpass79

the problem seems to be the default code we have to create a HttpClient instance.

You can create the HttpClient and pass it to the kernel

I created a Blazor WebAssembly App and added the following component. Can you try this out and let me know if it works for you. We will look at adding a sample for this.

@page "/promptChaining"

<PageTitle>Prompt Chaining</PageTitle>

<h1>Prompt Chaining</h1>

<p role="status">Response: @response</p>

@code {
    private string response = "Loading...";

    protected override async Task OnInitializedAsync()
    {
        IKernel kernel = new KernelBuilder()
            .WithAzureTextCompletionService(
                deploymentName: "text-davinci-003", 
                endpoint: "https://<your endpoint>.openai.azure.com/", 
                apiKey: "<your API key>",
                httpClient: new HttpClient()
            )
            .Build();

        string translationPrompt = @"{{$input}}

Translate the text to math.";

        string summarizePrompt = @"{{$input}}

Give me a TLDR with the fewest words.";

        var translator = kernel.CreateSemanticFunction(translationPrompt);
        var summarize = kernel.CreateSemanticFunction(summarizePrompt);

        translator.SetAIService(() => kernel.GetService<ITextCompletion>());
        summarize.SetAIService(() => kernel.GetService<ITextCompletion>());

        string inputText = @"
1st Law of Thermodynamics - Energy cannot be created or destroyed.
2nd Law of Thermodynamics - For a spontaneous process, the entropy of the universe increases.
3rd Law of Thermodynamics - A perfect crystal at zero Kelvin has zero entropy.";

        // Run two prompts in sequence (prompt chaining)
        var output = await kernel.RunAsync(inputText, translator, summarize);

        if (output.ErrorOccurred)
        {
            response = output.LastErrorDescription;

            Console.Error.WriteLine(output.LastException?.ToString());
        }
        else
        {
            response = output.Result;
        }
    }
}
antpass79 commented 1 year ago

Hi @markwallace-microsoft it works perfectly!

Sorry, I didn't see the httpClient parameter to use.

Thank you, Antonio

imranshams commented 3 months ago

Hi @markwallace-microsoft

I had the same issue, and after using your suggestion, I'm getting another exception:

Unhandled exception rendering component: Invalid URI: The format of the URI could not be determined. System.UriFormatException: Invalid URI: The format of the URI could not be determined.

Do you have any suggestion?