3 unstable releases

0.2.0 Mar 21, 2024
0.1.1 Mar 9, 2024
0.1.0 Mar 7, 2024

#789 in Machine learning

Download history 2/week @ 2024-12-09

179 downloads per month

MIT/Apache

215KB
4.5K SLoC

oaapi

An unofficial Rust client for the OpenAI API.

Installation

Run the following Cargo command in your project directory:

cargo add oaapi

or add the following line to your Cargo.toml:

[dependencies]
oaapi = "0.2.0"

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.

Examples

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

[dependencies]
oaapi = { version = "0.2.0", 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 with the API key from the environment variable: "OPENAI_API_KEY"
    let client = Client::from_env()?;
    // or specify the API key directly.
    // let client = Client::new(oaapi::ApiKey::new("OPENAI_API_KEY"), None, None);

    // 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?;

    // 4. Use the response.
    println!("Result:\n{}", response);

    Ok(())
}

See also examples in documents of each feature module for more details.

Other examples

See the ./examples directory.

Changelog

See CHANGELOG.

License

Licensed under either of the Apache License, Version 2.0 or the MIT license at your option.

Dependencies

~5–17MB
~229K SLoC

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy