OpenAI Dive
OpenAI Dive is an unofficial async Rust library that allows you to interact with the OpenAI API.
Sign up for an account on https://platform.openai.com/overview to get your API key.
[dependencies]
openai_dive = "0.6"
Get started
use Client;
let api_key = var.expect;
let client = new; // or Client::new_from_env()
let result = client
.models
.list
.await?;
Endpoints
- Chat
- Images
- Audio
- Models
- Files
- Embeddings
- Moderation
- Uploads
- Fine-tuning
- Batches
- Assistants
- Administration
Chat
Given a list of messages comprising a conversation, the model will return a response.
Create chat completion
Creates a model response for the given chat conversation.
let parameters = default
.model
.messages
.response_format
.build?;
let result = client
.chat
.create
.await?;
More information: Create chat completion
Vision
Learn how to use vision capabilities to understand images.
let parameters = default
.model
.messages
.max_tokens
.build?;
let result = client
.chat
.create
.await?;
More information: Vision
Function calling
In an API call, you can describe functions and have the model intelligently choose to output a JSON object containing arguments to call one or many functions. The Chat Completions API does not call the function; instead, the model generates JSON that you can use to call the function in your code.
let messages = vec!;
let parameters = default
.model
.messages
.tools
.build?;
let result = client
.chat
.create
.await?;
let message = result.choices.message.clone;
if let Assistant = message
More information: Function calling
Structured outputs
Structured Outputs is a feature that guarantees the model will always generate responses that adhere to your supplied JSON Schema, so you don't need to worry about the model omitting a required key, or hallucinating an invalid enum value.
let parameters = default
.model
.messages
.response_format
.build?;
let result = client.chat.create.await?;
More information: Structured outputs
Images
Given a prompt and/or an input image, the model will generate a new image.
Create image
Creates an image given a prompt.
let parameters = default
.prompt
.model
.n
.quality
.response_format
.size
.style
.build?;
let result = client
.images
.create
.await?;
let paths = result
.save
.await?;
More information: Create image
Create image edit
Creates an edited or extended image given an original image and a prompt.
let parameters = default
.image
.prompt
.mask
.n
.size
.build?;
let result = client
.images
.edit
.await?;
More information: Create image edit
Create image variation
Creates a variation of a given image.
let parameters = default
.image
.n
.size
.build?;
let result = client
.images
.variation
.await?;
More information: Create image variation
Audio
Learn how to turn audio into text or text into audio.
Create speech
Generates audio from the input text.
let parameters = default
.model
.input
.voice
.response_format
.build?;
let response = client
.audio
.create_speech
.await?;
response
.save
.await?;
More information: Create speech
Create transcription
Transcribes audio into the input language.
let parameters = default
.file
.model
.response_format
.build?;
let result = client
.audio
.create_transcription
.await?;
More information: Create transcription
Create translation
Translates audio into English.
let parameters = default
.file
.model
.response_format
.build?;
let result = client
.audio
.create_translation
.await?;
More information: Create translation
Models
List and describe the various models available in the API.
For more information see the examples in the examples/models directory.
- List models
- Retrieve model
- Delete fine-tune model
More information Models
Files
Files are used to upload documents that can be used with features like Assistants, Fine-tuning, and Batch API.
For more information see the examples in the examples/files directory.
- List files
- Upload file
- Delete file
- Retrieve file
- Retrieve file content
More information Files
Embeddings
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
For more information see the examples in the examples/embeddings directory.
- Create embeddings
More information: Embeddings
Moderation
Given some input text, outputs if the model classifies it as potentially harmful across several categories.
For more information see the examples in the examples/moderation directory.
- Create moderation
More information Moderation
Uploads
Creates an intermediate Upload object that you can add Parts to. Currently, an Upload can accept at most 8 GB in total and expires after an hour after you create it.
Once you complete the Upload, we will create a File object that contains all the parts you uploaded. This File is usable in the rest of our platform as a regular File object.
For more information see the examples in the examples/uploads directory.
- Create upload
- Add upload part
- Complete upload
- Cancel upload
More information Uploads
Fine-tuning
Manage fine-tuning jobs to tailor a model to your specific training data.
For more information see the examples in the examples/fine_tuning directory.
- Create fine-tuning job
- List fine-tuning jobs
- Retrieve fine-tuning job
- Cancel fine-tuning job
- List fine-tuning events
- List fine-tuning checkpoints
More information Fine-tuning
Batches
Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount.
For more information see the examples in the examples/batches directory.
- Create batch
- List batches
- Retrieve batch
- Cancel batch
More information Batch
Assistants
Build assistants that can call models and use tools to perform tasks.
For more information see the examples in the examples/assistants directory.
- Assistants
- Threads
- Messages
- Runs
- Run Steps
More information Assistants
Administration
Programmatically manage your organization.
For more information see the examples in the examples/administration directory.
- Users
- Invites
- Projects
- Project Users
- Project Service Accounts
- Project API Keys
Configuration
Set API key
Add the OpenAI API key to your environment variables.
# Windows PowerShell
$Env:OPENAI_API_KEY='sk-...'
# Windows cmd
# Linux/macOS
Set organization/project ID
You can create multiple organizations and projects in the OpenAI platform. This allows you to group files, fine-tuned models and other resources.
You can set the organization ID and/or project ID on the client via the set_organization
and set_project
methods. If you don't set the organization and/or project ID, the client will use the default organization and default project.
let mut client = new;
client
.set_organization
.set_project;
Add proxy
This crate uses reqwest
as HTTP Client. Reqwest has proxies enabled by default. You can set the proxy via the system environment variable or by overriding the default client.
Example: set system environment variable
You can set the proxy in the system environment variables (https://docs.rs/reqwest/latest/reqwest/#proxies).
Example: overriding the default client
use Client;
let http_client = builder
.proxy
.build?;
let api_key = var.expect;
let client = Client ;
Available Models
You can use these predefined constants to set the model in the parameters or use any string representation (ie. for your custom models).
- O1Engine
- O1Preview
o1-preview
(alias) - O1Mini
o1-mini
(alias)
- O1Preview
- Gpt4Engine
- Gpt4O
gpt-4o
(alias) - Gpt4OMini
gpt-4o-mini
(alias) - Gpt4
gpt-4
(alias) - Gpt4Turbo
gpt-4-turbo
(alias) - Gpt4TurboPreview
gpt-4-turbo-preview
(alias)
- Gpt4O
- Gpt35Engine
- Gpt35Turbo
gpt-3.5-turbo
(alias) - Gpt35Turbo1106
gpt-3.5-turbo-1106
- Gpt35Turbo
- DallEEngine
- DallE3
dall-e-2
- DallE2
dall-e-3
- DallE3
- TTSEngine
- Tts1
tts-1
- Tts1HD
tts-1-hd
- Tts1
- WhisperEngine
- Whisper1
whisper-1
- Whisper1
- EmbeddingsEngine
- TextEmbedding3Small
text-embedding-3-small
- TextEmbedding3Large
text-embedding-3-large
- TextEmbeddingAda002
text-embedding-ada-002
- TextEmbedding3Small
- ModerationsEngine
- OmniModerationLatest
omni-moderation-latest
(alias) - TextModerationLatest
text-moderation-latest
(alias) - TextModerationStable
text-moderation-stable
(alias)
- OmniModerationLatest
More information: Models