openai_dive 0.1.4

OpenAI Dive is an async Rust API client that allows you to interact with the OpenAI API.
Documentation

OpenAI Dive

OpenAI Dive is an async Rust API client that allows you to interact with the OpenAI API.

[dependencies]
openai_dive = "0.1"

Endpoints

List models

Lists the currently available models, and provides basic information about each one such as the owner and availability.

URL https://api.openai.com/v1/models

Method GET

use std::io::Result;
use openai_dive::v1::api::Client;

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = "YOUR API KEY".to_string();

    let client = Client::new(api_key);

    let result = client.models().list().await.unwrap();

    println!("{:?}", result);
}

More information: List models

Retrieve model

Retrieves a model instance, providing basic information about the model such as the owner and permissioning.

URL https://api.openai.com/v1/models/{model}

Method GET

use std::io::Result;
use openai_dive::v1::api::Client;
use openai_dive::v1::models::OpenAIModel;

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = "YOUR API KEY".to_string();

    let model_id = OpenAIModel::TextDavinci003.to_string(); // text-davinci-003

    let client = Client::new(api_key);

    let result = client.models().get(model_id).await.unwrap();

    println!("{:?}", result);
}

More information: Retrieve models

Create completion

Creates a completion for the provided prompt and parameters.

URL https://api.openai.com/v1/completions

Method POST

use std::io::Result;
use openai_dive::v1::api::Client;
use openai_dive::v1::resources::completion::CompletionParameters;
use openai_dive::v1::models::OpenAIModel;

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = "YOUR API KEY".to_string();

    let parameters = CompletionParameters {
        model: OpenAIModel::TextDavinci003.to_string(), // text-davinci-003
        prompt: "Say this is a test".to_string(),
        max_tokens: 10,
        temperature: None,
        suffix: None,
    };

    let client = Client::new(api_key);

    let result = client.completions().create(parameters).await.unwrap();

    println!("{:?}", result);
}

More information: Create completion

Create chat completion

Creates a completion for the chat message.

URL https://api.openai.com/v1/chat/completions

Method POST

use std::io::Result;
use openai_dive::v1::api::Client;
use openai_dive::v1::resources::chat_completion::{ChatCompletionParameters, ChatMessage};
use openai_dive::v1::models::OpenAIModel;

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = "YOUR API KEY".to_string();

    let parameters = ChatCompletionParameters {
        model: OpenAIModel::Chat3X5Turbo0301.to_string(), // gpt-3.5-turbo-0301
        messages: vec![
            ChatMessage {
                role: "user".to_string(),
                content: "Hello!".to_string(),
            },
        ],
        max_tokens: 12,
        temperature: None,
    };

    let client = Client::new(api_key);

    let result = client.chat().create(parameters).await.unwrap();

    println!("{:?}", result);
}

More information: Create chat completion

Create edit

Creates a new edit for the provided input, instruction, and parameters.

URL https://api.openai.com/v1/edits

Method POST

use std::io::Result;
use openai_dive::v1::api::Client;
use openai_dive::v1::resources::edit::EditParameters;
use openai_dive::v1::models::OpenAIModel;

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = "YOUR API KEY".to_string();

    let parameters = EditParameters {
        model: OpenAIModel::TextDavinciEdit001.to_string(), // text-davinci-edit-001
        input: "What day of the wek is it?".to_string(),
        instruction: "Fix the spelling mistakes".to_string(),
        temperature: None,
    };

    let client = Client::new(api_key);

    let result = client.edits().create(parameters).await.unwrap();

    println!("{:?}", result);
}

More information: Create edit

Create image

Creates an image given a prompt.

URL https://api.openai.com/v1/images/generations

Method POST

use std::io::Result;
use openai_dive::v1::api::Client;
use openai_dive::v1::resources::image::{CreateImageParameters, ImageSize};

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = "YOUR API KEY".to_string();

    let parameters = CreateImageParameters {
        prompt: "A cute baby dog".to_string(),
        number_of_images: Some(1),
        image_size: Some(ImageSize::Size256X256),
        response_format: None,
    };

    let client = Client::new(api_key);

    let result = client.images().create(parameters).await.unwrap();

    println!("{:?}", result);
}

More information: Create image

Edit image

Creates an edited or extended image given an original image and a prompt.

URL https://api.openai.com/v1/images/edits

Method POST

use std::io::Result;
use openai_dive::v1::api::Client;
use openai_dive::v1::resources::image::{EditImageParameters, ImageSize};

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = "YOUR API KEY".to_string();

    let parameters = EditImageParameters {
        image: "./images/image_edit_original.png".to_string(), // https://github.com/betalgo/openai/blob/master/OpenAI.Playground/SampleData/image_edit_original.png
        mask: Some("./images/image_edit_mask.png".to_string()), // https://github.com/betalgo/openai/blob/master/OpenAI.Playground/SampleData/image_edit_mask.png
        prompt: "A cute baby sea otter weaing a beret".to_string(),
        number_of_images: Some(1),
        image_size: Some(ImageSize::Size256X256),
        response_format: None,
    };

    let client = Client::new(api_key);

    let result = client.images().edit(parameters).await.unwrap();

    println!("{:?}", result);
}

More information: Create image edit

Image variation

Creates a variation of a given image.

URL https://api.openai.com/v1/images/variations

Method POST

use std::io::Result;
use openai_dive::v1::api::Client;
use openai_dive::v1::resources::image::{CreateImageVariationParameters, ImageSize};

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = "YOUR API KEY".to_string();

    let parameters = CreateImageVariationParameters {
        image: "./images/image_edit_original.png".to_string(), // https://github.com/betalgo/openai/blob/master/OpenAI.Playground/SampleData/image_edit_original.png
        number_of_images: Some(1),
        image_size: Some(ImageSize::Size256X256),
        response_format: None,
    };

    let client = Client::new(api_key);

    let result = client.images().variation(parameters).await.unwrap();

    println!("{:?}", result);
}

More information: Create image variation