Provides wrappers for OpenAI REST APIs See https://beta.openai.com/docs/api-reference/introduction for further info on REST endpoints
Add :openai as a dependency in your mix.exs file.
def deps do
[
{:openai, "~> 0.2.3"}
]
end
You can configure openai in your mix config.exs (default $project_root/config/config.exs). If you're using Phoenix add the configuration in your config/dev.exs|test.exs|prod.exs files. An example config is:
use Mix.Config
config :openai,
api_key: "your-api-key", # find it at https://beta.openai.com/account/api-keys
organization_key: "your-organization-key", # find it at https://beta.openai.com/account/api-keys
http_options: [recv_timeout: 30_000] # optional, passed to [HTTPoison.Request](https://hexdocs.pm/httpoison/HTTPoison.Request.html) options
Get your API key from https://beta.openai.com/docs/developer-quickstart/your-api-keys
Get the list of available engines
OpenAI.engines()
{:ok, %{
"data" => [
%{"id" => "davinci", "object" => "engine", "max_replicas": ...},
...,
...
]
}
See: https://beta.openai.com/docs/api-reference/engines/list
Retrieve specific engine info
OpenAI.engines("davinci")
{:ok, %{
"id" => "davinci",
"object" => "engine",
"max_replicas": ...
}
}
See: https://beta.openai.com/docs/api-reference/engines/retrieve
It returns one or more predicted completions given a prompt. The function accepts as arguments the "engine_id" and the set of parameters used by the Completions OpenAI api.
OpenAI.completions(
"davinci", # engine_id
prompt: "once upon a time",
max_tokens: 5,
temperature: 1,
...
)
{:ok, %{
choices: [
%{
"finish_reason" => "length",
"index" => 0,
"logprobs" => nil,
"text" => "\" thing we are given"
}
],
created: 1617147958,
id: "...",
model: "...",
object: "text_completion"
}
}
See: https://beta.openai.com/docs/api-reference/completions/create for the complete list of parameters you can pass to the completions function
It returns a rank of each document passed to the function, based on its semantic similarity to the passed query. The function accepts as arguments the engine_id and theset of parameters used by the Search OpenAI api
OpenAI.search(
"babbage", #engine_id
documents: ["White House", "hospital", "school"],
query: "the president"
)
{:ok,
%{
data: [
%{"document" => 0, "object" => "search_result", "score" => 218.676},
%{"document" => 1, "object" => "search_result", "score" => 17.797},
%{"document" => 2, "object" => "search_result", "score" => 29.65}
],
model: "...",
object: "list"
}
}
See: https://beta.openai.com/docs/api-reference/searches for the complete list of parameters you can pass to the search function
It returns the most likely label for the query passed to the function. The function accepts as arguments a set of parameters that will be passed to the Classifications OpenAI api
Given a query and a set of labeled examples, the model will predict the most likely label for the query. Useful as a drop-in replacement for any ML classification or text-to-label task.
OpenAI.classifications(
examples: [
["A happy moment", "Positive"],
["I am sad.", "Negative"],
["I am feeling awesome", "Positive"]
],
labels: ["Positive", "Negative", "Neutral"],
query: "It is a raining day :(",
search_model: "ada",
model: "curie"
)
{:ok,
%{
completion: "cmpl-2jIXZYg7Buyg1DDRYtozkre50TSMb",
label: "Negative",
model: "curie:2020-05-03",
object: "classification",
search_model: "ada",
selected_examples: [
%{"document" => 1, "label" => "Negative", "text" => "I am sad."},
%{"document" => 0, "label" => "Positive", "text" => "A happy moment"},
%{"document" => 2, "label" => "Positive", "text" => "I am feeling awesome"}
]
}
}
See: https://beta.openai.com/docs/api-reference/classifications for the complete list of parameters you can pass to the classifications function
The endpoint first searches over provided documents or files to find relevant context. The relevant context is combined with the provided examples and question to create the prompt for completion.
OpenAI.answers(
model: "curie",
documents: ["Puppy A is happy.", "Puppy B is sad."],
question: "which puppy is happy?",
search_model: "ada",
examples_context: "In 2017, U.S. life expectancy was 78.6 years.",
examples: [["What is human life expectancy in the United States?", "78 years."]],
max_tokens: 5
)
{:ok,
%{
answers: ["puppy A."],
completion: "cmpl-2kdRgXcoUfaAXxlPjmZXBT8AlKWfB",
model: "curie:2020-05-03",
object: "answer",
search_model: "ada",
selected_documents: [
%{"document" => 0, "text" => "Puppy A is happy. "},
%{"document" => 1, "text" => "Puppy B is sad. "}
]
}
}
See: https://beta.openai.com/docs/api-reference/answers
List your organization's fine-tuning jobs.
OpenAI.finetunes()
{:ok,
%{
object: "list",
data: [%{
"id" => "t-AF1WoRqd3aJAHsqc9NY7iL8F",
"object" => "fine-tune",
"model" => "curie",
"created_at" => 1614807352,
"fine_tuned_model" => null,
"hyperparams" => { ... },
"organization_id" => "org-...",
"result_files" = [],
"status": "pending",
"validation_files" => [],
"training_files" => [ { ... } ],
"updated_at" => 1614807352,
}],
}
}
See: https://beta.openai.com/docs/api-reference/fine-tunes/list
Gets info about a fine-tune job.
OpenAI.finetunes("t-AF1WoRqd3aJAHsqc9NY7iL8F")
{:ok,
%{
object: "list",
data: [%{
"id" => "t-AF1WoRqd3aJAHsqc9NY7iL8F",
"object" => "fine-tune",
"model" => "curie",
"created_at" => 1614807352,
"fine_tuned_model" => null,
"hyperparams" => { ... },
"organization_id" => "org-...",
"result_files" = [],
"status": "pending",
"validation_files" => [],
"training_files" => [ { ... } ],
"updated_at" => 1614807352,
}],
}
}
See: https://beta.openai.com/docs/api-reference/fine-tunes/retrieve
Create, edit, or create variations of images.
Generate a new image by AI. If needed, you can pass a second argument to the function to add specific http options for this specific call (i.e. increasing the timeout)
OpenAI.Images.Generations.fetch(
[prompt: "A developer writing a test", size: "256x256"],
recv_timeout: 10 * 60 * 1000
)
{:ok,
%{
created: 1670341737,
data: [
%{
"url" => ...Returned url
}
]
}}
See: https://beta.openai.com/docs/api-reference/images/create
Edit an existing image based on prompt If needed, you can pass a second argument to the function to add specific http options for this specific call (i.e. increasing the timeout)
OpenAI.image_edits.fetch(
"/home/developer/myImg.png",
{ "prompt", "A developer writing a test", "size": "256x256"},
recv_timeout: 10 * 60 * 1000
)
{:ok,
%{
created: 1670341737,
data: [
%{
"url" => ...Returned url
}
]
}}
See: https://beta.openai.com/docs/api-reference/images/create-edits
OpenAI.image_variations.fetch(
"/home/developer/myImg.png",
{ "n": "5"},
recv_timeout: 10 * 60 * 1000
)
{:ok,
%{
created: 1670341737,
data: [
%{
"url" => ...Returned url
}
]
}}
See: https://beta.openai.com/docs/api-reference/images/create-variation
- introduce missing new apis (models, edits, embeddings, files, moderations)
- deprecate old apis (answers, search)
The package is available as open source under the terms of the MIT License.