async-openai 0.1.3

Async bindings for OpenAI REST API based on OpenAPI spec
Documentation

Overview

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

  • It's based on OpenAI OpenAPI spec
  • Current features:
    • Microsoft Azure Endpoints / AD Authentication
    • Completions
    • Edit
    • Embeddings
    • Fine-Tunning
    • Image (Generation/Edit/Variation)
    • Moderation

Usage

The library reads API key from the environment variable OPENAI_API_KEY.

export OPENAI_API_KEY='sk-...'

Image Generation Example

use std::error::Error;

use async_openai as openai;
use openai::{
    types::{CreateImageRequest, ImageSize, ResponseFormat},
    Client, Image,
};

#[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 = CreateImageRequest {
        prompt: "cats on sofa and carpet in living room".to_owned(),
        n: Some(2),
        response_format: Some(ResponseFormat::Url),
        size: Some(ImageSize::S256x256),
        user: Some("async-openai".to_owned()),
    };

    let response = Image::create(&client, request).await?;

    // download and save images to ./data directory
    // (creates directory when it doesn't exist)
    response.save("./data").await?;

    Ok(())
}

License

This project is licensed under MIT license.