Skip to content

lbkolev/fieri

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

fieri

Note: fieri's master branch might contain breaking changes. For the most recently released code, look to the latest tag.

Overview

Unofficial Rust client for the OpenAI API.

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.

Prerequisites

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.

Installation

Run this command in your terminal to add the latest version of fieri.

$ cargo add fieri

Basic Usage

Generate text based on a prompt

use fieri::{
    completion::{create, CompletionParamBuilder},
    Client, Error,
};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let client = Client::new(env::var("OPENAI_API_KEY")?);

    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, &param).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,
};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let client = Client::new(env::var("OPENAI_API_KEY")?);

    let param = CompletionParamBuilder::new("ada")
        .prompt("unnecessarily lo")
        .temperature(0.5)
        .build()?;

    let mut resp = create_with_stream(&client, &param).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,
};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let client = Client::new(env::var("OPENAI_API_KEY")?);

    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, &param)
        .await?
        .save("/tmp/")
        .await?;

    Ok(())
}

Examples for each endpoint can be found in the docs.

Limitations

Note that the Rust Client for OpenAI is provided as-is, and is not officially supported by OpenAI. While we will do our best to keep the library up-to-date and bug-free, we cannot guarantee that it will always work as expected.

Additionally, the API has usage limits that may affect your ability to use the models. You can view your current usage and limits in the Usage section of your account.

License

fieri is provided under the MIT license. See LICENSE.