Noizu.OpenAI (Noizu Labs: OpenAI v0.1.3)

Noizu.OpenAI is a library providing a simple wrapper around OpenAI's API calls. It handles various API features such as completions, chat, edit, image generation, image editing, image variation, embeddings, audio transcription, audio translation, file management, and content moderation.

configuration

Configuration

To configure the library, you need to set the OpenAI API key and optionally, the OpenAI organization in your application's configuration:

config :noizu_labs_open_ai,
  openai_api_key: "your_api_key_here",
  openai_org: "your_organization_id (optional)"

Link to this section Summary

Functions

A helper function to make API calls to the OpenAI API. This function handles both non-stream and stream API calls.

Link to this section Types

Link to this type

best_of_option()

@type best_of_option() :: term()
Link to this type

completions_option()

@type completions_option() :: integer()
Link to this type

echo_option()

@type echo_option() :: term()
Link to this type

error_tuple()

@type error_tuple() :: {:error, details :: term()}
Link to this type

frequency_penalty_option()

@type frequency_penalty_option() :: term()
Link to this type

language_option()

@type language_option() :: String.t()
Link to this type

log_probability_option()

@type log_probability_option() :: float()
Link to this type

logit_bias_option()

@type logit_bias_option() :: term()
Link to this type

mask_option()

@type mask_option() :: String.t()
Link to this type

max_tokens_option()

@type max_tokens_option() :: integer()
Link to this type

model_option(allowed)

@type model_option(allowed) :: allowed | String.t()
Link to this type

presence_penalty_option()

@type presence_penalty_option() :: term()
Link to this type

purpose_option()

@type purpose_option() :: String.t()
Link to this type

response_format_option()

@type response_format_option() :: String.t()
Link to this type

size_option()

@type size_option() :: String.t()
Link to this type

stop_option()

@type stop_option() :: term()
Link to this type

stream_option()

@type stream_option() :: boolean()
Link to this type

stream_options()

@type stream_options() :: %{optional(:stream) => boolean()} | Keyword.t() | nil
Link to this type

suffix_option()

@type suffix_option() :: String.t()
Link to this type

temperature_option()

@type temperature_option() :: float()
Link to this type

top_p_option()

@type top_p_option() :: term()
Link to this type

user_option()

@type user_option() :: String.t()

Link to this section Functions

Link to this function

api_call(type, url, body, model, options \\ nil)

A helper function to make API calls to the OpenAI API. This function handles both non-stream and stream API calls.

parameters

Parameters

  • type: The HTTP request method (e.g., :get, :post, :put, :patch, :delete)
  • url: The full URL for the API endpoint
  • body: The request body in map format
  • model: The model to be used for the response processing
  • options
    • stream: A boolean value to indicate whether the request should be processed as a stream or not (default: false)
    • raw: return raw response
    • response_log_callback: function(finch) callback for request log.
    • response_log_callback: function(finch, start_ms) callback for response log.

returns

Returns

Returns a tuple {:ok, response} on successful API call, where response is the decoded JSON response in map format. Returns {:error, term} on failure, where term contains error details.

example

Example

url = "https://api.openai.com/v1/completions"
body = %{
  prompt: "Once upon a time",
  model: "text-davinci-003",
  max_tokens: 50,
  temperature: 0.7
}
{:ok, response} = Noizu.OpenAI.api_call(:post, url, body, Noizu.OpenAI.Completion, stream: false)
Link to this function

generic_stream_provider(callback)

Link to this function

put_field(body, field, options, default \\ nil)