Crate oaapi

source ·
Expand description

An unofficial Rust client for the OpenAI API.

§Features

[!NOTE] You need to enable the feature flags to use the corresponding APIs.

§Supported APIs

Beta version APIs:

§Usage

  1. Enable API feature flags that you want to use, e.g. chat.
  2. Create a crate::Client with the API key and the other optional settings.
  3. Use the client to call the APIs, e.g. crate::Client::chat_complete.

§Example

An example to call the chat completions API with chat feature:

[dependencies]
oaapi = { version = "0.1.1", features = ["chat"] }

and setting the API key to the environment variable OPENAI_API_KEY:

OPENAI_API_KEY={your-openai-api-key}

is as follows:

use oaapi::Client;
use oaapi::chat::CompletionsRequestBody;
use oaapi::chat::SystemMessage;
use oaapi::chat::UserMessage;
use oaapi::chat::ChatModel;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // 1. Create a client, e.g. with API key loaded from the environment variable: `OPENAI_API_KEY`.
    let client = Client::from_env()?;

    // 2. Create a request body parameters.
    let request_body = CompletionsRequestBody {
        messages: vec![
            SystemMessage::new("Prompt.", None).into(),
            UserMessage::new("Chat message from user.".into(), None).into(),
        ],
        model: ChatModel::Gpt35Turbo,
        ..Default::default()
    };

    // 3. Call the API.
    let response = client
        .chat_complete(request_body)
        .await?;

    Ok(())
}

Re-exports§

Modules§

  • The audio API of the OpenAI API.
  • The chat APIs of the OpenAI API.

Structs§

Enums§

Type Aliases§