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

Types

Options that may be passed to a request function. See request/2 for detailed descriptions.

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.

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 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 data() :: map() | list()
@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

Link to this function

auth_header(options \\ [])

View Source
@spec auth_header(options :: list()) :: list()

Builds the authorization header for the request.

Link to this function

auth_header(headers, arg)

View Source
@spec auth_header(header :: list(), tuple()) :: list()

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"}]

Link to this function

create(module, data, options \\ [])

View Source
@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}
Link to this function

create_stream(module, data, options \\ [])

View Source
@spec create_stream(atom(), data :: list() | map(), list()) :: any()

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.

Link to this function

create_with_file(module, data, file_key \\ :atom, options \\ [])

View Source
@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}
Link to this function

delete(client, url, opts)

View Source
@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"})
Link to this function

delete!(client, url, opts)

View Source
@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"})
Link to this function

ensure_valid_filename(filename)

View Source
@spec format_data(list() | map()) :: binary()

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"})

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"})
Link to this function

head!(client, url, opts)

View Source
@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"})
Link to this function

list(module, options \\ [])

View Source

List all the resource in the OpenAI API.

examples

Examples

ExOpenAi.Api.list(ExOpenAi.Completion)
{:ok, [%Completion{ ... }]}
Link to this function

options(client, url, opts)

View Source
@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"})
Link to this function

options!(client, url, opts)

View Source
@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"})
Link to this function

patch(client, url, body, opts)

View Source

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"])
Link to this function

patch!(client, url, body, opts)

View Source

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"])
Link to this function

post(client, url, body, opts)

View Source

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"])
Link to this function

post!(client, url, body, opts)

View Source

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"])
Link to this function

process_request_headers(headers \\ [])

View Source
Link to this function

put(client, url, body, opts)

View Source

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"])
Link to this function

put!(client, url, body, opts)

View Source

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"])
Link to this function

remove(module, id, options \\ [])

View Source

Delete a resource from the OpenAI API.

examples

Examples

ExOpenAi.Api.delete(ExOpenAi.Modle, "gpt-4-turbo")
{:ok, %Completion{ ... }}
Link to this function

request(client \\ %Tesla.Client{}, options)

View Source
@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 using Tesla.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"})
Link to this function

request!(client \\ %Tesla.Client{}, options)

View Source
@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.

Link to this function

retrieve(module, id, options \\ [])

View Source

Retrieve a resource from the OpenAI API.

examples

Examples

ExOpenAi.Api.retrieve(ExOpenAi.Modle, "gpt-4-turbo")
{:ok, %Completion{ ... }}
Link to this function

trace(client, url, opts)

View Source

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"})
Link to this function

trace!(client, url, opts)

View Source
@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"})