Introduction to the OpenAI API Client Library for Rust
The OpenAI API client library for Rust is an unofficial tool designed to provide efficient access to the OpenAI API within Rust applications. This library enables developers to leverage various functionalities offered by OpenAI with ease and versatility.
Getting Started
To begin utilizing this library, it's necessary to include it in your Rust project's dependencies. This is done by adding the following line to your Cargo.toml
file:
[dependencies]
openai-api-rs = "5.0.13"
Configuration
The library requires an API key associated with your OpenAI account. This key can be obtained from the OpenAI platform. It's recommended to save this key as an environment variable to keep your credentials secure.
Setting the API Key
Here's how to set your API key as an environment variable:
$ export OPENAI_API_KEY=sk-xxxxxxx
Creating a Client
To interact with the OpenAI API, you'll need to create a client instance using your API key:
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
Making API Requests
Once your client is set up, you can proceed to create requests. For example, to make a chat completion request, you can structure it as follows:
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
name: None,
tool_calls: None,
tool_call_id: None,
}],
);
Subsequently, you can send this request and handle the response:
let result = client.chat_completion(req)?;
println!("Content: {:?}", result.choices[0].message.content);
Optional Base URL Configuration
Optionally, you can also set the base URL for the API:
$ export OPENAI_API_BASE=https://api.openai.com/v1
Example Application
Here is a succinct example demonstrating a chat completion using this library:
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use openai_api_rs::v1::common::GPT4_O;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
name: None,
tool_calls: None,
tool_call_id: None,
}],
);
let result = client.chat_completion(req).await?;
println!("Content: {:?}", result.choices[0].message.content);
println!("Response Headers: {:?}", result.headers);
Ok(())
}
For additional examples, you can refer to the examples directory in the project's repository.
Supported API Features
The library supports a wide range of OpenAI API features, including but not limited to:
- Completions
- Chat
- Edits
- Images
- Embeddings
- Audio
- Files
- Fine-tuning
- Moderations
- Function calling
- Assistants
- Batch processing
- Real-time applications
Licensing
The OpenAI API client library for Rust is distributed under the MIT license, enabling users to freely use and modify the software. Full licensing details are available in the LICENSE file in the project repository.
For in-depth API documentation and a comprehensive guide to all available functions, developers can visit the official OpenAI API documentation.