Elixir SDK for OpenAI APIs

Hex.pm VersionHex DocsHex.pm Download Total

ExOpenAI is an (unofficial) Elixir SDK for interacting with the OpenAI APIs. This SDK is fully auto-generated using metaprogramming and always reflects the latest state of the OpenAI API.

Features

Installation

Add :ex_openai as a dependency in your mix.exs file:

def deps do
  [
    {:ex_openai, "~> 1.8.0"}
  ]
end

Quick Start

Configuration

import Config

config :ex_openai,
  api_key: System.get_env("OPENAI_API_KEY"),
  organization_key: System.get_env("OPENAI_ORGANIZATION_KEY"),
  # Optional settings
  base_url: System.get_env("OPENAI_API_URL"),
  http_options: [recv_timeout: 50_000],
  http_headers: [{"OpenAI-Beta", "assistants=v2"}]

Basic Usage

# List available models
{:ok, models} = ExOpenAI.Models.list_models()

# Create a completion
{:ok, completion} = ExOpenAI.Completions.create_completion("gpt-3.5-turbo-instruct", "The sky is")

# Chat completion
messages = [
  %ExOpenAI.Components.ChatCompletionRequestUserMessage{role: :user, content: "What is the capital of France?"}
]
{:ok, response} = ExOpenAI.Chat.create_chat_completion(messages, "gpt-4")

# Responses
{:ok, response} = ExOpenAI.Responses.create_response(
  "Tell me a joke about programming",
  "gpt-4o-mini"
)

# Continue the conversation
{:ok, follow_up} = ExOpenAI.Responses.create_response(
  "Explain why that joke is funny",
  "gpt-4o-mini",
  previous_response_id: response.id
)

More examples in Examples

API Overview

ExOpenAI supports all OpenAI API endpoints, organized into logical modules:

For detailed documentation on each module, see the API Documentation.

Advanced Usage

Streaming Responses

# Using a callback function
callback = fn
  :finish -> IO.puts "Done"
  {:data, data} -> IO.puts "Data: #{inspect(data)}"
  {:error, err} -> IO.puts "Error: #{inspect(err)}"
end

ExOpenAI.Completions.create_completion(
  "gpt-3.5-turbo-instruct",
  "Tell me a story",
  stream: true,
  stream_to: callback
)

For more advanced streaming options, see the Streaming Guide.

File Uploads

# Simple file upload
image_data = File.read!("path/to/image.png")
{:ok, result} = ExOpenAI.Images.create_image_variation(image_data)

# With filename information
audio_data = File.read!("path/to/audio.wav")
{:ok, transcript} = ExOpenAI.Audio.create_transcription({"audio.wav", audio_data}, "whisper-1")

Documentation

Contributing

Contributions are welcome! If you find a bug or want to add a feature, please open an issue or submit a PR.

To update the SDK when OpenAI changes their API:

mix update_openai_docs

Projects Using ExOpenAI

Add yours with a PR!

License

Available as open source under the terms of the MIT License.