Crates.io | async-openai |
lib.rs | async-openai |
version | |
source | src |
created_at | 2022-12-02 08:26:48.919974+00 |
updated_at | 2025-03-03 02:38:54.443236+00 |
description | Rust library for OpenAI |
homepage | https://github.com/64bit/async-openai |
repository | https://github.com/64bit/async-openai |
max_upload_size | |
id | 728237 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Async Rust library for OpenAI
async-openai
is an unofficial Rust library for OpenAI.
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-...'
async-openai
.Only types for Realtime API are implemented, and can be enabled with feature flag realtime
.
These types were written before OpenAI released official specs.
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(())
}
Enable methods whose input and outputs are generics with byot
feature. It creates a new method with same name and _byot
suffix.
For example, to use serde_json::Value
as request and response type:
let response: Value = client
.chat()
.create_byot(json!({
"messages": [
{
"role": "developer",
"content": "You are a helpful assistant"
},
{
"role": "user",
"content": "What do you think about life?"
}
],
"model": "gpt-4o",
"store": false
}))
.await?;
This can be useful in many scenarios:
serde
.Visit examples/bring-your-own-type directory to learn more.
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:
This project adheres to Rust Code of Conduct
This project is licensed under MIT license.