ExLLM.Providers.OpenAI (ex_llm v0.8.1)
View SourceOpenAI GPT API adapter for ExLLM.
Configuration
This adapter requires an OpenAI API key and optionally a base URL.
Using Environment Variables
# Set environment variables
export OPENAI_API_KEY="your-api-key"
export OPENAI_MODEL="gpt-4-turbo" # optional
export OPENAI_API_BASE="https://api.openai.com/v1" # optional
# Use with default environment provider
ExLLM.Providers.OpenAI.chat(messages, config_provider: ExLLM.Infrastructure.ConfigProvider.Env)
Using Static Configuration
config = %{
openai: %{
api_key: "your-api-key",
model: "gpt-4-turbo",
base_url: "https://api.openai.com/v1" # optional
}
}
{:ok, provider} = ExLLM.Infrastructure.ConfigProvider.Static.start_link(config)
ExLLM.Providers.OpenAI.chat(messages, config_provider: provider)
Example Usage
messages = [
%{role: "user", content: "Hello, how are you?"}
]
# Simple chat
{:ok, response} = ExLLM.Providers.OpenAI.chat(messages)
IO.puts(response.content)
# Streaming chat
{:ok, stream} = ExLLM.Providers.OpenAI.stream_chat(messages)
for chunk <- stream do
if chunk.content, do: IO.write(chunk.content)
end
Summary
Functions
Add a part to an existing upload.
Cancel an upload.
Complete an upload and create the final File object.
Create an assistant.
Create a batch request for processing multiple inputs.
Create an Upload object for multipart file uploads.
Delete an uploaded file.
Generate images using OpenAI's DALL-E API.
Get metadata for a specific file.
List all uploaded files.
Moderate content using OpenAI's moderation API.
Retrieve the content of an uploaded file.
Transcribe audio using OpenAI's Whisper API.
Upload a file to OpenAI for use with assistants or other endpoints.
Functions
Add a part to an existing upload.
Each part can be at most 64 MB. Parts can be added in parallel.
Parameters
upload_id
- The ID of the Uploaddata
- The chunk of bytes for this partoptions
- Additional options
Examples
{:ok, part} = OpenAI.add_upload_part("upload_abc123", chunk_data)
part["id"] # => "part_def456"
Cancel an upload.
No parts may be added after an upload is cancelled.
Parameters
upload_id
- The ID of the Upload to canceloptions
- Additional options
Examples
{:ok, cancelled} = OpenAI.cancel_upload("upload_abc123")
cancelled["status"] # => "cancelled"
Complete an upload and create the final File object.
Parameters
upload_id
- The ID of the Uploadpart_ids
- Ordered list of Part IDsoptions
- Additional options (can include:md5
for checksum verification)
Examples
{:ok, completed} = OpenAI.complete_upload(
"upload_abc123",
["part_def456", "part_ghi789"]
)
completed["status"] # => "completed"
completed["file"]["id"] # => "file-xyz321"
Create an assistant.
Create a batch request for processing multiple inputs.
Create an Upload object for multipart file uploads.
Use this for files larger than the regular file upload limit. An Upload can accept at most 8 GB and expires after 1 hour.
Parameters
bytes
- The total number of bytes to uploadfilename
- The name of the filemime_type
- The MIME type (e.g., "text/jsonl")purpose
- The intended purpose of the fileoptions
- Additional options
Examples
{:ok, upload} = OpenAI.create_upload(
bytes: 2_147_483_648,
filename: "training_examples.jsonl",
mime_type: "text/jsonl",
purpose: "fine-tune"
)
upload["id"] # => "upload_abc123"
upload["status"] # => "pending"
Delete an uploaded file.
Generate images using OpenAI's DALL-E API.
Get metadata for a specific file.
List all uploaded files.
Options
:after
- Cursor for pagination (object ID):limit
- Number of objects to return (1-10000, default: 10000):order
- Sort order: "asc" or "desc" (default: "desc"):purpose
- Filter by file purpose
Examples
# List all files
{:ok, files} = OpenAI.list_files()
# List with pagination
{:ok, files} = OpenAI.list_files(limit: 100, after: "file-abc123")
# Filter by purpose
{:ok, files} = OpenAI.list_files(purpose: "fine-tune")
Moderate content using OpenAI's moderation API.
Retrieve the content of an uploaded file.
Transcribe audio using OpenAI's Whisper API.
Upload a file to OpenAI for use with assistants or other endpoints.
Parameters
file_path
- Path to the file to uploadpurpose
- The intended purpose of the file. Supported values:"fine-tune"
- For fine-tuning models"fine-tune-results"
- Fine-tuning results (system-generated)"assistants"
- For use with Assistants API"assistants_output"
- Assistant outputs (system-generated)"batch"
- For batch API input"batch_output"
- Batch API results (system-generated)"vision"
- For vision fine-tuning"user_data"
- Flexible file type for any purpose"evals"
- For evaluation datasets
options
- Additional options including config_provider
Examples
{:ok, file} = OpenAI.upload_file("/path/to/data.jsonl", "fine-tune")
file["id"] # => "file-abc123"
file["expires_at"] # => 1680202602
File Object Structure
The returned file object contains:
"id"
- The file identifier (e.g., "file-abc123")"object"
- Always "file""bytes"
- Size of the file in bytes"created_at"
- Unix timestamp when created"expires_at"
- Unix timestamp when the file expires"filename"
- The name of the uploaded file"purpose"
- The intended purpose of the file"status"
- (Deprecated) Upload status"status_details"
- (Deprecated) Validation error details