valentinegb / openai

An unofficial Rust library for the OpenAI API.
https://crates.io/crates/openai/
MIT License
60 stars 18 forks source link

Add support for setting `stream: true` for EventSource responses #30

Open imtsuki opened 1 year ago

imtsuki commented 1 year ago

Problem

The current version of this library does not support setting stream: true for EventSource responses. This means that when a user makes a request to the API that expects an EventSource response, the library cannot properly parse the incoming events.

Solution

In addition to the existing create() function, add a new function stream() to build something like CompletionStream that implements the futures::Stream trait. Then, the user can use the following to call the API in streaming mode:

let chat_completion_stream = ChatCompletion::builder()
    .model(ModelID::Gpt3_5Turbo)
    .messages(vec![...])
    .stream()
    .unwrap();

while let Some(event) = chat_completion_stream.next().await {
    println!("Got {:?}", event);
}

Alternatives

No response

valentinegb commented 1 year ago

Perfect solution! It'll be implemented in the next alpha :)

valentinegb commented 1 year ago

Perfect solution! It'll be implemented in the next alpha :)

I was wrong ;-; I now remember why I put off implementing this feature before, haha-

imtsuki commented 1 year ago

I can try to help if you don't mind :) I'll look into this issue later today

valentinegb commented 1 year ago

That would be great! Feel free to make a fork and draft a pull request :) I'll try to not let you do all of it-