openai icon indicating copy to clipboard operation
openai copied to clipboard

ChatCompletionAsStream() throws PlatformNotSupportedException on iOS

Open ChaseIngersol opened this issue 5 months ago • 2 comments

We have a Blazor Maui Hybrid app that's currently running on Windows, macOS, iOS, and Android. When making a call to OpenAIService.CreateCompletionAsStream() on an iOS device, the following exception occurs:

System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Net.Http.HttpClientHandler.Send(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpMessageInvoker.Send(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.Send(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   at OpenAI.GPT3.Extensions.HttpClientExtensions.PostAsStreamAsync(HttpClient client, String uri, Object requestModel, CancellationToken cancellationToken)
   at OpenAI.GPT3.Managers.OpenAIService.CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreateRequest, String modelId, CancellationToken cancellationToken)+MoveNext()
   at OpenAI.GPT3.Managers.OpenAIService.CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreateRequest, String modelId, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()

The problem appears to be that iOS does not support synchronous HTTP operations, so calling return client.Send(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); in HttpClientExtensions.PostAsStreamAsync() results in an exception.

I'll look into this more deeply when I have a few minutes free, but from a cursor glance it looks like fixing this would require a significant refactor to make this operation truly asynchronous.

  • OS: iOS 18.0 (22A5350a)
  • Language: C#
  • Version: 8.6.2

ChaseIngersol avatar Sep 03 '24 20:09 ChaseIngersol