Crate async_openai

source ·
Expand description

Async Rust library for OpenAI REST API based on OpenAPI spec.

Creating client

use async_openai::Client;

// Create a client with api key from env var OPENAI_API_KEY and default base url.
let client = Client::new();

// OR use API key from different source
let api_key = "sk-..."; // This secret could be from a file, or environment variable.
let client = Client::new().with_api_key(api_key);

// Use organization other than default when making requests
let client = Client::new().with_org_id("the-org");

// Use custom reqwest client
let http_client = reqwest::ClientBuilder::new().user_agent("async-openai").build().unwrap();
let client = Client::new().with_http_client(http_client);

Making requests

 use async_openai::{Client, types::{CreateCompletionRequestArgs}};

 // Create client
 let client = Client::new();

 // Create request using builder pattern
 // Every request struct has companion builder struct with same name + Args suffix
 let request = CreateCompletionRequestArgs::default()
     .prompt("Tell me the recipe of alfredo pasta")

 // Call API
 let response = client
     .completions()      // Get the API "group" (completions, images, etc.) from the client
     .create(request)    // Make the API call in that "group"

 println!("{}", response.choices.first().unwrap().text);


For full working examples for all supported features see examples directory in the repository.


  • Errors originating from API calls, parsing responses, and reading-or-writing to the file system.
  • Types used in OpenAI API requests and responses. These types are created from component schemas in the OpenAPI spec


  • Turn audio into text Related guide: Speech to text
  • Given a chat conversation, the model will return a chat completion response.
  • Client is a container for api key, base url, organization id, and backoff configuration used to make API calls.
  • Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
  • Given a prompt and an instruction, the model will return an edited version of the prompt.
  • Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
  • Files are used to upload documents that can be used with features like Fine-tuning.
  • Manage fine-tuning jobs to tailor a model to your specific training data.
  • Given a prompt and/or an input image, the model will generate a new image.
  • List and describe the various models available in the API. You can refer to the Models documentation to understand what models are available and the differences between them.
  • Given a input text, outputs if the model classifies it as violating OpenAI’s content policy.