Expand description
Unofficial OpenAI API client for Rust.
Fieri provides an asynchronous Rust interface for interacting with the OpenAI API, allowing you to easily use OpenAI’s state-of-the-art machine learning models in your Rust projects.
Before you can use the Rust Client for OpenAI, you’ll need to sign up for an API key at the OpenAI Developer Portal. Once you’ve signed up, you’ll be able to find your API key in the API Keys section of the developer portal.
Each request requires a Client, initialized with your API key.
By default, the API key is read from the OPENAI_API_KEY
environment variable.
§Examples
§Generate text based on a prompt
use fieri::{
completion::{create, CompletionParamBuilder},
Client, Error,
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::new();
let param = CompletionParamBuilder::new("ada")
.prompt("Generate a plot for an absurd interstellar parody.")
.max_tokens(500)
.temperature(0.9)
.top_p(1.0)
.frequency_penalty(0.0)
.presence_penalty(0.0)
.build()?;
let resp = create(&client, ¶m).await?;
println!("Generated text: {:#?}", resp);
Ok(())
}
§Generate and stream back text based on a prompt
use fieri::{
completion::{create_with_stream, Completion, CompletionParamBuilder},
Client, Error,
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::new();
let param = CompletionParamBuilder::new("ada")
.prompt("unnecessarily lo")
.temperature(0.5)
.build()?;
let mut resp = create_with_stream(&client, ¶m).await?;
while let Some(chunk) = resp.chunk().await? {
if chunk.to_vec() == b"data: [DONE]\n\n" {
break;
}
let v: Completion = serde_json::from_slice(&chunk[5..])?;
v.choices.iter().for_each(|c| println!("{:?}", c.text));
}
Ok(())
}
§Generate an image based on a prompt and save it locally.
use fieri::{
image::{ImageSize, GenerateImageParamBuilder, generate},
Client, Error,
};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::new();
let param = GenerateImageParamBuilder::new("A bunch of cats dancing tango on top of the highest mountain on Mars.")
.size(ImageSize::S1024x1024)
.n(1)
.build()?;
generate(&client, ¶m)
.await?
.save("/tmp/")
.await?;
Ok(())
}
Modules§
- api_
resources - client
- The Client used to establish a connection and interact with the OpenAI API.
- completion
- Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
- edit
- Given a prompt and an instruction, the model will return an edited version of the prompt.
- embedding
- Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
- error
- A composite error type for errors that can occur while interacting with OpenAI.
- file
- Files are used to upload documents that can be used with features like
Fine-tuning
. - fine_
tune - Manage fine-tuning jobs to tailor a model to your specific training data.
- image
- Given a prompt and/or an input image, the model will generate a new image.
- model
- List and describe the various models available in the API.
- moderation
- Given a input text, outputs if the model classifies it as violating OpenAI’s content policy.
Structs§
- Client
- The Client used to interact with the OpenAI API.
Enums§
- Error
- A set of errors that can occur during interaction with OpenAI.
Type Aliases§
- Result
- Result returned from each interaction with the OpenAI API.