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.5"
Get started
use Client;
let api_key = var.expect;
let client = new;
let result = client
.models
.list
.await?;
Endpoints
Models
List and describe the various models available in the API.
List models
Lists the currently available models, and provides basic information about each one such as the owner and availability.
let result = client
.models
.list
.await?;
More information: List models
Retrieve model
Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
let result = client
.models
.get
.await?;
More information: Retrieve model
Delete fine-tune model
Delete a fine-tuned model. You must have the Owner role in your organization to delete a model.
let result = client
.models
.delete
.await?;
More information: Delete fine-tune model
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?;
[!NOTE] This endpoint also has
stream
support. See the examples/chat/create_chat_completion_stream example.
More information: Create chat completion
Create chat completion with image
Creates a model response for the given chat conversation.
let parameters = default
.model
.messages
.max_tokens
.build?;
let result = client
.chat
.create
.await?;
More information: Create chat completion
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 Some = message.tool_calls
[!NOTE] This endpoint also has
stream
support. See the examples/chat/function_calling_stream example.
More information: Function calling
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?;
[!NOTE] This endpoint also has
stream
support. See the examples/audio/create_speech_stream example.
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
Embeddings
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
Create embeddings
Creates an embedding vector representing the input text.
let parameters = default
.model
.input
.encoding_format
.build?;
let result = client
.embeddings
.create
.await?;
More information: Create embeddings
Files
Files are used to upload documents that can be used with features like Assistants, Fine-tuning, and Batch API.
List files
Returns a list of files that belong to the user's organization.
let query = ListFilesParameters ;
let result = client
.files
.list
.await?;
More information: List files
Upload file
Upload a file that can be used across various endpoints.
let parameters = default
.file
.purpose
.build?;
let result = client
.files
.upload
.await?;
More information Upload file
Delete file
Delete a file.
let file_id = "file-XXX";
let result = client
.files
.delete
.await?;
More information Delete file
Retrieve file
Returns information about a specific file.
let file_id = "file-XXX";
let result = client
.files
.retrieve
.await?;
More information Retrieve file
Retrieve file content
Returns the contents of the specified file.
let file_id = "file-XXX";
let result = client
.files
.retrieve_content
.await?;
More information Retrieve file content
Moderation
Given some input text, outputs if the model classifies it as potentially harmful across several categories.
Create moderation
Classifies if text is potentially harmful.
let parameters = default
.model
.input
.build?;
let result = client
.moderations
.create
.await?;
More information Create moderation
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
Currency Converter Assistant Example
The Currency Converter Assistant
processes conversion queries. It integrates a function "get_currency_conversion"
to fetch real-time conversion rates with EUR as the base. The assistant is set up by creating a thread and run via the create_thread_and_run
endpoint, checking the run's status, and handling tool outputs when required. Finally, it retrieves the assistant's responses using the list_messages
endpoint to display the conversion results.
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).
- Gpt4Engine
- Gpt4O
gpt-4o
(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
- TextModerationLatest
text-moderation-latest
(alias) - TextModerationStable
text-moderation-stable
(alias) - TextModeration007
text-moderation-007
- TextModerationLatest
More information: Models