View Source ExOpenAi.Api (ex_open_ai v2.0.1)
The main module for the ExOpenAi API client.
Provides a wrapper around Httpoison for making requests to the OpenAI API.
Link to this section Summary
Functions
Builds the authorization header for the request.
Builds the actual authorization header for the request.
Create a new resource in the OpenAI API with a POST request.
Creates a stream using the provided module, data, and options.
Create with a file upload.
Perform a DELETE request.
Perform a DELETE request.
Perform a GET request.
Perform a GET request.
Perform a HEAD request.
Perform a HEAD request.
List all the resource in the OpenAI API.
Perform a OPTIONS request.
Perform a OPTIONS request.
Perform a PATCH request.
Perform a PATCH request.
Perform a POST request.
Perform a POST request.
Perform a PUT request.
Perform a PUT request.
Delete a resource from the OpenAI API.
Perform a request.
Perform request and raise in case of error.
Retrieve a resource from the OpenAI API.
Perform a TRACE request.
Perform a TRACE request.
Link to this section Types
@type option() :: {:method, Tesla.Env.method()} | {:url, Tesla.Env.url()} | {:query, Tesla.Env.query()} | {:headers, Tesla.Env.headers()} | {:body, Tesla.Env.body()} | {:opts, Tesla.Env.opts()}
Options that may be passed to a request function. See request/2
for detailed descriptions.
Link to this section Functions
Builds the authorization header for the request.
Builds the actual authorization header for the request.
examples
Examples
iex> ExOpenAi.Api.auth_header([], {"api123", "org345"}) [{"Authorization", "Bearer api123"}, {"OpenAI-Organization", "org345"}]
iex> ExOpenAi.Api.auth_header([], {"api123", nil}) [{"Authorization", "Bearer api123"}]
@spec create(atom(), data :: list() | map(), list()) :: ExOpenAi.Parser.success() | ExOpenAi.Parser.error()
Create a new resource in the OpenAI API with a POST request.
examples
Examples
ExOpenAi.Api.create(ExOpenAi.Completion, [prompt: "tell me a joke", max_tokens: 500])
{:ok, %Completion{ ... }}
ExOpenAi.Api.create(ExOpenAi.Completion, [])
{:error, %{"model" => "model is required"}, 400}
Creates a stream using the provided module, data, and options.
The stream is created using the Stream.resource/3
function, where the initial function posts the data with Api.post!/4
, the intermediate function processes the async response with StreamProcessor.handle_async_response/2
, and the after function closes the async response with StreamProcessor.close_async_response/1
.
parameters
Parameters
module
- The module to be used for handling the data stream.data
- A keyword list containing the data to be sent in the request.options
- An optional keyword list of additional options for the request.
returns
Returns
- A stream that is created using the
Stream.resource/3
function, allowing processing of the data as it is received.
usage
Usage
This function is used to create a stream with the specified module, process ID, data, and options. This enables efficient handling of streaming data from the OpenAI API.
note-that-this-is-the-only-function-that-uses-httpoison-tesla-does-not-have-good-support-for-streaming
Note that this is the only function that uses HTTPoison, Tesla does not have good support for streaming.
@spec create_with_file(atom(), data :: list() | map(), atom(), list()) :: ExOpenAi.Parser.success() | ExOpenAi.Parser.error()
Create with a file upload.
Thing to note:
- The
file
parameter is required - Additional data that has a list of binaries in it is currently supported. But if the nature of the form shifts so that nested data within a value may not work.
examples
Examples
ExOpenAi.Api.create_with_file(ExOpenAi.Completion, [prompt: "tell me a joke", max_tokens: 500])
{:ok, %Completion{ ... }}
ExOpenAi.Api.create_with_file(ExOpenAi.Completion, [])
{:error, %{"model" => "model is required"}, 400}
@spec delete(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.result()
Perform a DELETE request.
See request/1
or request/2
for options definition.
delete("/users")
delete("/users", query: [scope: "admin"])
delete(client, "/users")
delete(client, "/users", query: [scope: "admin"])
delete(client, "/users", body: %{name: "Jon"})
@spec delete!(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.t() | no_return()
Perform a DELETE request.
See request!/1
or request!/2
for options definition.
delete!("/users")
delete!("/users", query: [scope: "admin"])
delete!(client, "/users")
delete!(client, "/users", query: [scope: "admin"])
delete!(client, "/users", body: %{name: "Jon"})
@spec get(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.result()
Perform a GET request.
See request/1
or request/2
for options definition.
get("/users")
get("/users", query: [scope: "admin"])
get(client, "/users")
get(client, "/users", query: [scope: "admin"])
get(client, "/users", body: %{name: "Jon"})
@spec get!(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.t() | no_return()
Perform a GET request.
See request!/1
or request!/2
for options definition.
get!("/users")
get!("/users", query: [scope: "admin"])
get!(client, "/users")
get!(client, "/users", query: [scope: "admin"])
get!(client, "/users", body: %{name: "Jon"})
@spec head(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.result()
Perform a HEAD request.
See request/1
or request/2
for options definition.
head("/users")
head("/users", query: [scope: "admin"])
head(client, "/users")
head(client, "/users", query: [scope: "admin"])
head(client, "/users", body: %{name: "Jon"})
@spec head!(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.t() | no_return()
Perform a HEAD request.
See request!/1
or request!/2
for options definition.
head!("/users")
head!("/users", query: [scope: "admin"])
head!(client, "/users")
head!(client, "/users", query: [scope: "admin"])
head!(client, "/users", body: %{name: "Jon"})
@spec list(atom(), list()) :: ExOpenAi.Parser.success() | ExOpenAi.Parser.error()
List all the resource in the OpenAI API.
examples
Examples
ExOpenAi.Api.list(ExOpenAi.Completion)
{:ok, [%Completion{ ... }]}
@spec options(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.result()
Perform a OPTIONS request.
See request/1
or request/2
for options definition.
options("/users")
options("/users", query: [scope: "admin"])
options(client, "/users")
options(client, "/users", query: [scope: "admin"])
options(client, "/users", body: %{name: "Jon"})
@spec options!(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.t() | no_return()
Perform a OPTIONS request.
See request!/1
or request!/2
for options definition.
options!("/users")
options!("/users", query: [scope: "admin"])
options!(client, "/users")
options!(client, "/users", query: [scope: "admin"])
options!(client, "/users", body: %{name: "Jon"})
@spec patch(Tesla.Env.client(), Tesla.Env.url(), Tesla.Env.body(), [option()]) :: Tesla.Env.result()
Perform a PATCH request.
See request/1
or request/2
for options definition.
patch("/users", %{name: "Jon"})
patch("/users", %{name: "Jon"}, query: [scope: "admin"])
patch(client, "/users", %{name: "Jon"})
patch(client, "/users", %{name: "Jon"}, query: [scope: "admin"])
@spec patch!(Tesla.Env.client(), Tesla.Env.url(), Tesla.Env.body(), [option()]) :: Tesla.Env.t() | no_return()
Perform a PATCH request.
See request!/1
or request!/2
for options definition.
patch!("/users", %{name: "Jon"})
patch!("/users", %{name: "Jon"}, query: [scope: "admin"])
patch!(client, "/users", %{name: "Jon"})
patch!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])
@spec post(Tesla.Env.client(), Tesla.Env.url(), Tesla.Env.body(), [option()]) :: Tesla.Env.result()
Perform a POST request.
See request/1
or request/2
for options definition.
post("/users", %{name: "Jon"})
post("/users", %{name: "Jon"}, query: [scope: "admin"])
post(client, "/users", %{name: "Jon"})
post(client, "/users", %{name: "Jon"}, query: [scope: "admin"])
@spec post!(Tesla.Env.client(), Tesla.Env.url(), Tesla.Env.body(), [option()]) :: Tesla.Env.t() | no_return()
Perform a POST request.
See request!/1
or request!/2
for options definition.
post!("/users", %{name: "Jon"})
post!("/users", %{name: "Jon"}, query: [scope: "admin"])
post!(client, "/users", %{name: "Jon"})
post!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])
@spec put(Tesla.Env.client(), Tesla.Env.url(), Tesla.Env.body(), [option()]) :: Tesla.Env.result()
Perform a PUT request.
See request/1
or request/2
for options definition.
put("/users", %{name: "Jon"})
put("/users", %{name: "Jon"}, query: [scope: "admin"])
put(client, "/users", %{name: "Jon"})
put(client, "/users", %{name: "Jon"}, query: [scope: "admin"])
@spec put!(Tesla.Env.client(), Tesla.Env.url(), Tesla.Env.body(), [option()]) :: Tesla.Env.t() | no_return()
Perform a PUT request.
See request!/1
or request!/2
for options definition.
put!("/users", %{name: "Jon"})
put!("/users", %{name: "Jon"}, query: [scope: "admin"])
put!(client, "/users", %{name: "Jon"})
put!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])
@spec remove(atom(), String.t(), list()) :: ExOpenAi.Parser.success() | ExOpenAi.Parser.error()
Delete a resource from the OpenAI API.
examples
Examples
ExOpenAi.Api.delete(ExOpenAi.Modle, "gpt-4-turbo")
{:ok, %Completion{ ... }}
@spec request(Tesla.Env.client(), [option()]) :: Tesla.Env.result()
Perform a request.
options
Options
:method
- the request method, one of [:head
,:get
,:delete
,:trace
,:options
,:post
,:put
,:patch
]:url
- either full url e.g. "http://example.com/some/path" or just "/some/path" if usingTesla.Middleware.BaseUrl
:query
- a keyword list of query params, e.g.[page: 1, per_page: 100]
:headers
- a keyworld list of headers, e.g.[{"content-type", "text/plain"}]
:body
- depends on used middleware:- by default it can be a binary
- if using e.g. JSON encoding middleware it can be a nested map
- if adapter supports it it can be a Stream with any of the above
:opts
- custom, per-request middleware or adapter options
examples
Examples
ExampleApi.request(method: :get, url: "/users/path")
# use shortcut methods
ExampleApi.get("/users/1")
ExampleApi.post(client, "/users", %{name: "Jon"})
@spec request!(Tesla.Env.client(), [option()]) :: Tesla.Env.t() | no_return()
Perform request and raise in case of error.
This is similar to request/2
behaviour from Tesla 0.x
See request/2
for list of available options.
@spec retrieve(atom(), String.t(), list()) :: ExOpenAi.Parser.success() | ExOpenAi.Parser.error()
Retrieve a resource from the OpenAI API.
examples
Examples
ExOpenAi.Api.retrieve(ExOpenAi.Modle, "gpt-4-turbo")
{:ok, %Completion{ ... }}
@spec trace(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.result()
Perform a TRACE request.
See request/1
or request/2
for options definition.
trace("/users")
trace("/users", query: [scope: "admin"])
trace(client, "/users")
trace(client, "/users", query: [scope: "admin"])
trace(client, "/users", body: %{name: "Jon"})
@spec trace!(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.t() | no_return()
Perform a TRACE request.
See request!/1
or request!/2
for options definition.
trace!("/users")
trace!("/users", query: [scope: "admin"])
trace!(client, "/users")
trace!(client, "/users", query: [scope: "admin"])
trace!(client, "/users", body: %{name: "Jon"})