Crates.io | openai-api-stream-rs |
lib.rs | openai-api-stream-rs |
version | 0.1.1 |
source | src |
created_at | 2023-05-12 10:15:57.890975 |
updated_at | 2023-05-12 12:20:21.981523 |
description | A Rust crate that provides a simple function for interacting with the OpenAI API and performing language-based tasks. This crate focuses on streaming responses from the API, enabling real-time processing of large amounts of data. |
homepage | |
repository | |
max_upload_size | |
id | 862860 |
size | 15,943 |
A Rust crate that provides a simple function for interacting with the OpenAI API and performing language-based tasks. This crate focuses on streaming responses from the API, enabling real-time processing of large amounts of data.
GptStream
struct provides a convenient interface for interacting with the OpenAI chat completions endpoint.To use this crate, simply create an instance of the OpenAIStream
struct, passing your API key as a parameter. Then, call the gpt_stream
method with your desired input and await the response. The returned GptStream
object allows you to asynchronously iterate over the streaming API response.
Example:
[dependencies]
"openai-api-stream-rs" = "0.1.0"
"tokio" = { version = "1.12.0", features = ["full"] }
"futures" = "0.3.19"
use openai_api_stream_rs::OpenAIStream;
use futures::stream::StreamExt;
#[tokio::main]
async fn main() {
let api_key = "your_api_key";
let openai_stream = OpenAIStream::new(api_key.to_string());
let input = r#"
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Write a simple advanced usage of Rust in one sentence"
}
]
}
"#;
let gpt_stream = openai_stream.gpt_stream(input).await.unwrap();
let mut gpt_stream = Box::pin(gpt_stream);
while let Some(response) = gpt_stream.next().await {
println!("{}", response);
}
}
Note: Replace "your_api_key" with your actual OpenAI API key.
For more details and advanced configuration options, please refer to the crate documentation.
Note: This crate is still in development and may be subject to changes and updates.