Skip to content

Rust client for OpenAI API supporting streaming mode of the chat completion endpoint.

License

Notifications You must be signed in to change notification settings

akr4/openai-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openai-api

Rust client for OpenAI API supporting streaming mode of the chat completion endpoint.

Supported Endpoints

Getting Started

Add the following to your Cargo.toml:

openai-api = { git = "https://github.com/akr4/openai-api.git" }

Put this in your crate root:

use std::env;
use openai_api::chat;

#[tokio::main]
async fn main() {
    // create request
    let request = chat::CompletionRequest {
        model: chat::Model::Gpt35Turbo,
        temperature: Some(1.0),
        messages: vec![
            chat::Message {
                role: chat::MessageRole::User,
                content: "Hello".to_string(),
            }
        ],
    };

    // call completion endpoint
    let response = chat::completion(
        env::var("OPENAI_API_KEY").expect("environment variable OPENAI_API_KEY is not found."),
        &request).await.unwrap();

    // show response text
    println!("{}", response.choices[0].message.content);
}

Streaming Mode

use std::env;
use futures_util::StreamExt;
use openai_api::{chat, chat_stream};

#[tokio::main]
async fn main() {
    // create request
    let request = chat::CompletionRequest {
        model: chat::Model::Gpt35Turbo,
        temperature: Some(1.0),
        messages: vec![
            chat::Message {
                role: chat::MessageRole::User,
                content: "Hello".to_string(),
            }
        ],
    };

    // call completion endpoint
    let mut response = chat_stream::completion(
        env::var("OPENAI_API_KEY").expect("environment variable OPENAI_API_KEY is not found."),
        &request).await.unwrap();

    // receive response
    let mut response_text = String::new();
    while let Some(response) = response.next().await {
        match response {
            Ok(response) => {
                if let Some(text_chunk) = response.choices[0].delta.content.clone() {
                    print!("{text_chunk}");
                    response_text.push_str(&text_chunk);
                }
            }
            Err(err) => {
                println!("{:?}", err);
                break;
            }
        }
    }

    // show response text
    println!("\nResponse Text: {response_text}");
}

About

Rust client for OpenAI API supporting streaming mode of the chat completion endpoint.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages