#openai #async #openapi #ai

async-openai

Async bindings for OpenAI REST API based on OpenAPI spec

27 releases (9 breaking)

new 0.10.1 Mar 25, 2023
0.9.5 Mar 18, 2023
0.5.0 Dec 30, 2022

#61 in Web programming

Download history 95/week @ 2022-12-09 44/week @ 2022-12-16 325/week @ 2022-12-23 132/week @ 2022-12-30 48/week @ 2023-01-06 16/week @ 2023-01-13 66/week @ 2023-01-20 225/week @ 2023-01-27 494/week @ 2023-02-03 426/week @ 2023-02-10 639/week @ 2023-02-17 519/week @ 2023-02-24 1494/week @ 2023-03-03 1099/week @ 2023-03-10 4158/week @ 2023-03-17 2464/week @ 2023-03-24

9,363 downloads per month
Used in 11 crates (9 directly)

MIT license

98KB
2K SLoC

async-openai

Async Rust library for OpenAI

Logo created by this repo itself

Overview

async-openai is an unofficial Rust library for OpenAI REST API.

  • It's based on OpenAI OpenAPI spec
  • Current features:
    • Audio
    • Chat (including SSE streaming)
    • Completions (including SSE streaming)
    • Edits
    • Embeddings
    • Files
    • Fine-Tuning (including SSE streaming)
    • Images
    • Microsoft Azure Endpoints / AD Authentication (see issue)
    • Models
    • Moderations
  • Non-streaming requests are retried with exponential backoff when rate limited by the API server.
  • Ergonomic Rust library with builder pattern for all request objects.

Being a young project there could be rough edges.

Usage

The library reads API key from the environment variable OPENAI_API_KEY.

# On macOS/Linux
export OPENAI_API_KEY='sk-...'
# On Windows Powershell
$Env:OPENAI_API_KEY='sk-...'

Image Generation Example

use async_openai::{
    types::{CreateImageRequestArgs, ImageSize, ResponseFormat},
    Client,
};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // create client, reads OPENAI_API_KEY environment variable for API key.
    let client = Client::new();

    let request = CreateImageRequestArgs::default()
        .prompt("cats on sofa and carpet in living room")
        .n(2)
        .response_format(ResponseFormat::Url)
        .size(ImageSize::S256x256)
        .user("async-openai")
        .build()?;

    let response = client.images().create(request).await?;

    // Download and save images to ./data directory.
    // Each url is downloaded and saved in dedicated Tokio task.
    // Directory is created if it doesn't exist.
    let paths = response.save("./data").await?;

    paths
        .iter()
        .for_each(|path| println!("Image file path: {}", path.display()));

    Ok(())
}

Scaled up for README, actual size 256x256

Contributing

Thank you for your time to contribute and improve the project, I'd be happy to have you!

A good starting point would be an open issue.

License

This project is licensed under MIT license.

Dependencies

~9–16MB
~325K SLoC