Introduction to OpenAI.ex
OpenAI.ex is an unofficial, community-maintained wrapper designed to support developers in interfacing with OpenAI's REST APIs. This wrapper simplifies the process for users wishing to access OpenAI's various AI models and services by providing a structured and easy-to-use Elixir-based library.
Installation
To start using OpenAI.ex in your Elixir project, you need to add it as a dependency in your mix.exs
file. This simple step involves pasting a single line into the dependencies section:
def deps do
[
{:openai, "~> 0.6.2"}
]
end
Configuration
Configuring OpenAI.ex to communicate with OpenAI's platform requires specifying API keys and other settings in your mix configuration files. For those utilizing the Phoenix framework, these settings must be included in your config/dev.exs|test.exs|prod.exs
files. Below is a sample configuration:
import Config
config :openai,
api_key: "your-api-key",
organization_key: "your-organization-key",
beta: "assistants=v1",
http_options: [recv_timeout: 30_000],
api_url: "http://localhost/" # For local testing
To maintain flexibility, you can load environment variables for your API key using System.get_env("OPENAI_API_KEY")
, especially when operating in different environments (development, testing, production). This ensures that sensitive information such as your API keys are not hardcoded into your application.
Configuration Overrides
In cases where you need to change configurations on-the-fly, OpenAI.ex allows runtime overrides by passing an %OpenAI.Config{}
struct as the last argument to a function. For example:
config_override = %OpenAI.Config{api_key: "test-api-key"}
OpenAI.chat_completion([
model: "gpt-3.5-turbo",
messages: [
%{role: "system", content: "You are a helpful assistant."},
%{role: "user", content: "Who won the world series in 2020?"}
]
], config_override)
Usage Overview
Retrieving Models
To explore the models available through OpenAI, the models()
function fetches a list of models:
OpenAI.models()
Creating Completions
OpenAI.ex offers sophisticated completion functions to generate text based on an input prompt. Here's a basic example:
OpenAI.completions(
model: "finetuned-model",
prompt: "once upon a time",
max_tokens: 5,
temperature: 1
)
Chat Completions
For conversational interfaces, chat completion assists in generating AI-driven dialogue:
OpenAI.chat_completion(
model: "gpt-3.5-turbo",
messages: [
%{role: "system", content: "You are a helpful assistant."},
%{role: "user", content: "Who won the world series in 2020?"}
]
)
Generating and Editing Images
From generating new images to editing existing ones, OpenAI.ex provides functionality like images_generations
and images_edits
to manipulate visuals based on specified prompts or existing images.
Textual Edits
For correction and enhancement of existing text inputs, the edits()
function acts to refine content:
OpenAI.edits(
model: "text-davinci-edit-001",
input: "What day of the wek is it?",
instruction: "Fix the spelling mistakes"
)
Audio Processing
This library enables users to perform tasks such as audio transcription and speech generation. Functions like audio_transcription
and audio_translation
address these needs by converting spoken language into text or translating audio content:
OpenAI.audio_transcription("./path_to_file/blade_runner.mp3", model: "whisper-1")
Conclusion
OpenAI.ex streamlines the process of engaging with OpenAI’s cutting-edge models and APIs via an Elixir environment. Despite being an unofficial library, its community-driven development ensures robustness while adapting to evolving AI capabilities. Users should keep an eye on updates and community discussions to leverage its full potential effectively.