Skip to content

claudfernandes/async-openai

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

async-openai

Async Rust library for OpenAI

Overview

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

  • It's based on OpenAI OpenAPI spec
  • Non-streaming requests are retried with exponential backoff when rate limited by the API server.
  • Current features:
    • Completions (including SSE streaming)
    • Edits
    • Embeddings
    • Files (List, Upload, Delete, Retrieve, Retrieve Content)
    • Fine-tunes
    • Images (Generation, Edit, Variation)
    • Microsoft Azure Endpoints / AD Authentication
    • Models
    • Moderations

Being a young project there are 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 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
    // Each url download and save happens in dedicated Tokio task
    // (creates directory when it doesn't exist)
    response.save("./data").await?;

    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!

License

This project is licensed under MIT license.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in async-openai by you, shall be licensed as MIT, without any additional terms or conditions.

About

Async Rust library for OpenAI

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%