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
- Enable API feature flags that you want to use, e.g.
chat. - Create a
crate::Clientwith the API key and the other optional settings. - 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§
pub use reqwest;
Modules§
- The audio API of the OpenAI API.
- The chat APIs of the OpenAI API.
Structs§
- The API key of the OpenAI API.
- The client of the OpenAI API.
- The organization ID of the OpenAI API.
- The prompt for generations.
- The temperature for generations.
- The error of a validation.
Enums§
- The error of an API calling.
Type Aliases§
- The result of an API calling.
- The result of a validation.