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

Support for TextCompletion Streaming API #63

Open
jodendaal opened this issue Mar 18, 2023 · 2 comments
Open

Support for TextCompletion Streaming API #63

jodendaal opened this issue Mar 18, 2023 · 2 comments

Comments

@jodendaal
Copy link

jodendaal commented Mar 18, 2023

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

  • HttpCompletionOption.ResponseHeadersRead
  • Reading response stream as it is returned

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
Copy link
Collaborator

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants