GithubHelp home page GithubHelp logo

greyblake / async-openai Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 64bit/async-openai

1.0 1.0 0.0 11.25 MB

Async Rust library for OpenAI

Home Page: https://docs.rs/async-openai

License: MIT License

Rust 100.00%

async-openai's Introduction

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
    • Models
    • Moderations
  • All requests including form submissions (except SSE streaming) are retried with exponential backoff when rate limited by the API server.
  • Ergonomic Rust library with builder pattern for all request objects.

Note on Azure OpenAI Service: async-openai primarily implements OpenAI APIs, and exposes same library for Azure OpenAI Service too. In reality Azure OpenAI Service provides only subset of OpenAI APIs.

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.

Complimentary Crates

  • openai-func-enums provides procedural macros that make it easier to use this library with OpenAI API's function calling feature. It also provides derive macros you can add to existing clap application subcommands for natural language use of command line tools.

License

This project is licensed under MIT license.

async-openai's People

Contributors

64bit avatar buraktabn avatar ccollie avatar christopherjmiller avatar czechh avatar davirain-su avatar djrodgerspryor avatar dmweis avatar doordashcon avatar emilgardis avatar frankfralick avatar ifsheldon avatar ironman5366 avatar jonaro00 avatar jonnyloughlin avatar kirillkuzin avatar luketpeterson avatar m1guelpf avatar mikeparisstuff avatar ming08108 avatar oslfmt avatar schneiderfelipe avatar swnb avatar tmlev avatar xuanwo avatar zurawiki avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.