#openai #openai-api #async #openapi #ai #api-key

async-openai-wasi

Async bindings for OpenAI REST API based on OpenAPI spec

5 releases

0.16.4 Nov 23, 2023
0.16.3 Nov 16, 2023
0.16.2 Nov 15, 2023
0.0.2 Nov 13, 2023
0.0.1 Nov 13, 2023

#1254 in Asynchronous

Download history 2/week @ 2023-11-30 7/week @ 2023-12-14 6/week @ 2023-12-21 40/week @ 2023-12-28 39/week @ 2024-01-04 131/week @ 2024-01-11 49/week @ 2024-01-18 51/week @ 2024-01-25 21/week @ 2024-02-01 4/week @ 2024-02-08 24/week @ 2024-02-15 22/week @ 2024-02-22 14/week @ 2024-02-29 14/week @ 2024-03-07 12/week @ 2024-03-14

73 downloads per month

MIT license

200KB
3.5K SLoC

async-openai

Async Rust library for OpenAI

Logo created by this repo itself

Overview

async-openai is an unofficial Rust library for OpenAI.

  • It's based on OpenAI OpenAPI spec
  • Current features:
    • Assistants (Beta)
    • Audio (Whisper/TTS)
    • Chat
    • Completions (Legacy)
    • Edits (Deprecated)
    • Embeddings
    • Files
    • Fine-Tuning
    • Fine-Tunes (Deprecated)
    • Images
    • Microsoft Azure OpenAI Service
    • Models
    • Moderations
  • Support SSE streaming on available APIs
  • All requests including form submissions (except SSE streaming) are retried with exponential backoff when rate limited by the API server.
  • Ergonomic builder pattern for all request objects.

Note on Azure OpenAI Service (AOS): async-openai primarily implements OpenAI spec, and doesn't try to maintain parity with spec of AOS.

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 existing open issues.

License

This project is licensed under MIT license.

Dependencies

~11–28MB
~392K SLoC