Crate libopenai

Source
Expand description

GitHub Workflow Status GitHub Crates.io docs.rs

§libopenai - Rust client to interact with OpenAI’s API

Rust client for OpenAI’s API, written with tokio and reqwest

§How to use

To add libopenai to your project, you just need to run the following command on your project’s main foler:

cargo add libopenai

§Example

use libopenai::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    // OPTIONAL: Load variables in a `.env` file into the enviroment
    dotenv::dotenv().unwrap();

    let client = Client::new(
        None, // Gets api key from `OPENAI_API_KEY` enviroment variable
        None, // No organization specified
    )?;

    // Send basic completion request
    let basic = Completion::new(
        "text-davinci-003",
        "Whats the best way to calculate a factorial?",
        &client,
    )
    .await?;

    // Print the result
    println!("{:#?}", basic);
    return Ok(());
}

§Features

§Cargo features

Currently, the only feature available is tracing, which enables some minor logging

Modules§

audio
Learn how to turn audio into text.
chat
Given a chat conversation, the model will return a chat completion response.
common
Structures and methods commonly used throughout the library
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.
embeddings
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
error
Library’s error types
file
Files are used to upload documents that can be used with features like fine-tuning.
finetune
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.
moderations
Given a input text, outputs if the model classifies it as violating OpenAI’s content policy.
prelude

Structs§

Client
A client that’s used to connect to the OpenAI API
OpenAiStream
A Stream of server-sent events, sent by OpenAI