#openai-api #api-client #interact #gpt #organization

gpt-rs

This crate provides a simple way to interact with the OpenAI API

2 releases

0.1.1 Jul 26, 2023
0.1.0 Jul 26, 2023

#1741 in Web programming

41 downloads per month

MIT license

21KB
576 lines

gpt

This crate provides a simple way to interact with the OpenAI API from Rust.

Example

This asynchronous example uses Tokio and enables some optional features, so your Cargo.toml could look like this:

[dependencies]
gpt = { git="https://github.com/Kobayashi-takumi/gpt-rs" }
tokio = { version = "1", features = ["full"] }

And then the code:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new(Config {
        api_key:"<Your API_KEY>".to_string(),
        organization: Some("<Your ORGANIZATION>"),
    })?;
    let res = Chat::builder()
        .config(Default::default())
        .request(vec!["hi".into()])
        .build()?
        .execute(&client)
        .await?;
}

Create chat completion

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new(Config {
        api_key:"<Your API_KEY>".to_string(),
        organization: Some("<Your ORGANIZATION>".to_string()),
    })?;
    let res = Chat::builder()
        .config(Default::default())
        .request(vec!["hi".into()])
        .build()?
        .execute(&client)
        .await?;
    Ok(())
}

Create image

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new(Config {
        api_key:"<Your API_KEY>".to_string(),
        organization: Some("<Your ORGANIZATION>"),
    })?;
    let res = CreateImage::builder()
        .request("doc".into())
        .build()?
        .execute(&client)
        .await;
    Ok(())
}

Dependencies

~6–21MB
~296K SLoC