Openai.ex

Hex.pm Version Hex.pm Download Total

ExOpenAI is an Elixir SDK for interacting with the OpenAI APIs

This SDK is auto-generated using metaprogramming and should reflect the latest state of the OpenAI API.

Note: Due to the nature of auto-generating something, you may encounter stuff that isn't working yet. Make sure to report if you find anything acting up.

Features

Installation

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

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

What's working

answers

chat

classifications

completions

edits

embeddings

engines

files

fine-tunes

images

models

moderations

searches

What's not working yet

audio

files

images

Configuration

import Config
config :ex_openai,
api_key: "your-api-key", # find it at https://platform.openai.com/account/api-keys
organization_key: "your-organization-key", # find it at https://platform.openai.com/account/api-keys
http_options: [recv_timeout: 30_000] # optional, passed to [HTTPoison.Request](https://hexdocs.pm/httpoison/HTTPoison.Request.html) options

Usage

ExOpenAI.Models.list_models
{:ok,
%{
data: [
%{
"created" => 1649358449,
"id" => "babbage",
"object" => "model",
"owned_by" => "openai",
"parent" => nil,
"permission" => [
%{
"allow_create_engine" => false,
"allow_fine_tuning" => false,
"allow_logprobs" => true,
"allow_sampling" => true,
"allow_search_indices" => false,
"allow_view" => true,
"created" => 1669085501,
"group" => nil,
"id" => "modelperm-49FUp5v084tBB49tC4z8LPH5",
"is_blocking" => false,
"object" => "model_permission",
"organization" => "*"
}
],
"root" => "babbage"
},
...

Required parameters are converted into function arguments, optional parameters into the opts keyword list:

ExOpenAI.Completions.create_completion "text-davinci-003", prompt: "The sky is"
{:ok,
%{
choices: [
%{
"finish_reason" => "length",
"index" => 0,
"logprobs" => nil,
"text" => " blue\n\nThe sky is a light blue hue that may have a few white"
}
],
created: 1677929239,
id: "cmpl-6qKKllDPsQRtyJ5oHTbkQVS9w7iKM",
model: "text-davinci-003",
object: "text_completion",
usage: %{
"completion_tokens" => 16,
"prompt_tokens" => 3,
"total_tokens" => 19
}
}}

How to update once OpenAI chnages something?

Run mix update_docs and commit the new docs.yaml file

License

The package is available as open source under the terms of the MIT License.

Attribution