Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: OpenAI connector in ASP .NET Core buffers the response when using GetStreamingTextContentsAsync #5116

Closed
fabio-sp opened this issue Feb 22, 2024 · 4 comments
Assignees
Labels
.NET Issue or Pull requests regarding .NET code triage

Comments

@fabio-sp
Copy link

Describe the bug
In an ASP .NET Core controller I want to stream back to the client the tokens response of a prompt issued to OpenAI. Despite yield returning the single response tokens, the response seems to be buffered server side untill all the response stream from OpenAI is consumed and only at the end is returned to the client.

To Reproduce
Here is a simple endpoint implementation to reproduce the problem:

[HttpGet("open-ai")]
public async IAsyncEnumerable<string> GetWithOpenAI()
{
    var prompt = "Write a short poem about cats";
    var kernel = Kernel.CreateBuilder().AddOpenAIChatCompletion("model-id", "api-key").Build();
    var textCompletionService = kernel.GetRequiredService<ITextGenerationService>();

   await foreach (var textStreamingResult in textCompletionService.GetStreamingTextContentsAsync(prompt))
    {
        yield return textStreamingResult.Text;
    }
} 

The full example can be found here: https://github.com/fabio-sp/sk-streaming-sample-webapi

Expected behavior
The response is correctly streamed to the client from the controller without waiting the whole LLM response to be completed before returning.

Platform

  • OS: Windows, Mac
  • IDE: Visual Studio, VS Code, JetBrains Rider
  • Language: c#
  • Source: NuGet package version 1.4.0

Additional context
The problem seems to affect both AzureOpenAI and OpenAI connectors, I could not test it with the other connectors as I've no access to the other platforms. The issue also occurs when using the method InvokePromptStreamingAsync directly on the Kernel instance, or when using the IChatCompletionService and the GetStreamingChatMessageContentsAsync method. All the different tests I made can be found in the repository linked above.

We were using the SK version 1.0.0-beta-3 and with the ITextCompletion we had no such problem, a lot of things have been changed since then.

@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage labels Feb 22, 2024
@github-actions github-actions bot changed the title .NET: OpenAI connector in ASP .NET Core buffers the response when using GetStreamingTextContentsAsync .Net: OpenAI connector in ASP .NET Core buffers the response when using GetStreamingTextContentsAsync Feb 22, 2024
@Krzysztof318
Copy link
Contributor

Similar to this issue #4627

@stephentoub
Copy link
Member

stephentoub commented Feb 22, 2024

This is likely this bug in the Azure SDK:
Azure/azure-sdk-for-net#41838

It was fixed in Azure/azure-sdk-for-net#41844 but a new build with the fix hasn't been published to nuget yet.

In the meantime, try adding await Task.Yield() in your foreach loop and see if that improves the streaming.

@markwallace-microsoft markwallace-microsoft self-assigned this Feb 22, 2024
@markwallace-microsoft
Copy link
Member

@fabio-sp based on the response from Stephen looks like this isn't an Semantic Kernel issue so I'm going to close this.

@fabio-sp
Copy link
Author

I can confirm that the workaround suggested by @stephentoub works fine. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Issue or Pull requests regarding .NET code triage
Projects
None yet
Development

No branches or pull requests

4 participants