hassanhabib / Standard.AI.OpenAI

Standard-Compliant .NET library for Open AI
MIT License
262 stars 82 forks source link

Support for TextCompletion Streaming API #63

Open jodendaal opened 1 year ago

jodendaal commented 1 year ago

Would be good to support the streaming method for the TextCompletion API.

In order to implement the streaming TextCompletion API RestFulSense may need to support a Streaming method that returns IAsyncEnumerable.

The key bits are

Example of why it would be usefull https://youtu.be/hRkVGSMijjs?t=647

Example code from existing project of how I done it.

public static async IAsyncEnumerable<OpenAIHttpOperationResult<T, TError>> PostStream<T, TError>(this HttpClient httpClient, string? path, Object @object, JsonSerializerOptions? jsonSerializerOptions = null)
        {

            using (HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, path))
            {
                req.Content = new StringContent(JsonSerializer.Serialize(@object, jsonSerializerOptions), UnicodeEncoding.UTF8, "application/json");

                var response = await httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);

                if (response.IsSuccessStatusCode)
                {
                    var responseStream = await response.Content.ReadAsStreamAsync();
                    using var reader = new StreamReader(responseStream);
                    string? line = null;
                    while ((line = await reader.ReadLineAsync()) != null)
                    {
                        if (line.StartsWith("data: "))
                            line = line.Substring("data: ".Length);

                        if (!string.IsNullOrWhiteSpace(line) && line != "[DONE]")
                        {
                            var t = JsonSerializer.Deserialize<T>(line.Trim(), jsonSerializerOptions);
                            yield return new OpenAIHttpOperationResult<T, TError>(t, response.StatusCode);
                        }
                    }
                }
            }
        }
BrianLParker commented 1 year ago

I have been looking at this. It appears RESTFulSense does not support posting an object and returning a stream. This may require another update to RESTFulSense.

BrianLParker commented 1 year ago

@jodendaal I have a PR on RESTFulSense to support this.