ExLLM.Adapters.OpenAI (ex_llm v0.5.0)

View Source

OpenAI 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.Adapters.OpenAI.chat(messages, config_provider: ExLLM.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.ConfigProvider.Static.start_link(config)
ExLLM.Adapters.OpenAI.chat(messages, config_provider: provider)

Example Usage

messages = [
  %{role: "user", content: "Hello, how are you?"}
]

# Simple chat
{:ok, response} = ExLLM.Adapters.OpenAI.chat(messages)
IO.puts(response.content)

# Streaming chat
{:ok, stream} = ExLLM.Adapters.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.

Complete an upload and create the final File object.

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_upload_part(upload_id, data, options \\ [])

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 Upload
  • data - The chunk of bytes for this part
  • options - Additional options

Examples

{:ok, part} = OpenAI.add_upload_part("upload_abc123", chunk_data)
part["id"] # => "part_def456"

cancel_upload(upload_id, options \\ [])

Cancel an upload.

No parts may be added after an upload is cancelled.

Parameters

  • upload_id - The ID of the Upload to cancel
  • options - Additional options

Examples

{:ok, cancelled} = OpenAI.cancel_upload("upload_abc123")
cancelled["status"] # => "cancelled"

complete_upload(upload_id, part_ids, options \\ [])

Complete an upload and create the final File object.

Parameters

  • upload_id - The ID of the Upload
  • part_ids - Ordered list of Part IDs
  • options - 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_assistant(assistant_params, options \\ [])

Create an assistant.

create_batch(requests, options \\ [])

Create a batch request for processing multiple inputs.

create_upload(params, options \\ [])

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 upload
  • filename - The name of the file
  • mime_type - The MIME type (e.g., "text/jsonl")
  • purpose - The intended purpose of the file
  • options - 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_file(file_id, options \\ [])

Delete an uploaded file.

generate_image(prompt, options \\ [])

Generate images using OpenAI's DALL-E API.

get_file(file_id, options \\ [])

Get metadata for a specific file.

list_files(options \\ [])

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(input, options \\ [])

Moderate content using OpenAI's moderation API.

parse_response(response, model)

retrieve_file_content(file_id, options \\ [])

Retrieve the content of an uploaded file.

transcribe_audio(file_path, options \\ [])

Transcribe audio using OpenAI's Whisper API.

upload_file(file_path, purpose, options \\ [])

Upload a file to OpenAI for use with assistants or other endpoints.

Parameters

  • file_path - Path to the file to upload
  • purpose - 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

validate_functions_parameter(functions)