64 releases (24 breaking)

0.26.0 Nov 12, 2024
0.24.1 Sep 26, 2024
0.23.4 Jul 9, 2024
0.20.0 Mar 31, 2024
0.5.0 Dec 30, 2022

#79 in Web programming

Download history 7883/week @ 2024-08-02 9633/week @ 2024-08-09 8687/week @ 2024-08-16 8650/week @ 2024-08-23 7893/week @ 2024-08-30 7662/week @ 2024-09-06 7389/week @ 2024-09-13 7940/week @ 2024-09-20 8167/week @ 2024-09-27 7315/week @ 2024-10-04 6891/week @ 2024-10-11 6756/week @ 2024-10-18 7260/week @ 2024-10-25 7354/week @ 2024-11-01 7080/week @ 2024-11-08 6020/week @ 2024-11-15

28,771 downloads per month
Used in 104 crates (84 directly)

MIT license

410KB
7.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 (v2)
    • Audio
    • Batch
    • Chat
    • Completions (Legacy)
    • Embeddings
    • Files
    • Fine-Tuning
    • Images
    • Models
    • Moderations
    • Organizations | Administration
    • Realtime API types (Beta)
    • Uploads
  • SSE streaming on available APIs
  • Requests (except SSE streaming) including form submissions are retried with exponential backoff when rate limited.
  • Ergonomic builder pattern for all request objects.
  • Microsoft Azure OpenAI Service (only for APIs matching OpenAI spec)

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-...'

Realtime API

Only types for Realtime API are implemented, and can be enabled with feature flag realtime. These types may change if/when OpenAI releases official specs for them.

Image Generation Example

use async_openai::{
    types::{CreateImageRequestArgs, ImageSize, ImageResponseFormat},
    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(ImageResponseFormat::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 taking the time to contribute and improve the project. I'd be happy to have you!

All forms of contributions, such as new features requests, bug fixes, issues, documentation, testing, comments, examples etc. are welcome.

A good starting point would be to look at existing open issues.

To maintain quality of the project, a minimum of the following is a must for code contribution:

  • Names & Documentation: All struct names, field names and doc comments are from OpenAPI spec. Nested objects in spec without names leaves room for making appropriate name.
  • Tested: For changes supporting test(s) and/or example is required. Existing examples, doc tests, unit tests, and integration tests should be made to work with the changes if applicable.
  • Scope: Keep scope limited to APIs available in official documents such as API Reference or OpenAPI spec. Other LLMs or AI Providers offer OpenAI-compatible APIs, yet they may not always have full parity. In such cases, the OpenAI spec takes precedence.
  • Consistency: Keep code style consistent across all the "APIs" that library exposes; it creates a great developer experience.

This project adheres to Rust Code of Conduct

Complimentary Crates

  • openai-func-enums provides procedural macros that make it easier to use this library with OpenAI API's tool calling feature. It also provides derive macros you can add to existing clap application subcommands for natural language use of command line tools. It also supports openai's parallel tool calls and allows you to choose between running multiple tool calls concurrently or own their own OS threads.
  • async-openai-wasm provides WASM support.

License

This project is licensed under MIT license.

Dependencies

~10–24MB
~348K SLoC