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
best_of_option()
@type best_of_option() :: term()
completions_option()
@type completions_option() :: integer()
echo_option()
@type echo_option() :: term()
error_tuple()
@type error_tuple() :: {:error, details :: term()}
frequency_penalty_option()
@type frequency_penalty_option() :: term()
language_option()
@type language_option() :: String.t()
log_probability_option()
@type log_probability_option() :: float()
logit_bias_option()
@type logit_bias_option() :: term()
mask_option()
@type mask_option() :: String.t()
max_tokens_option()
@type max_tokens_option() :: integer()
model_option(allowed)
@type model_option(allowed) :: allowed | String.t()
presence_penalty_option()
@type presence_penalty_option() :: term()
purpose_option()
@type purpose_option() :: String.t()
response_format_option()
@type response_format_option() :: String.t()
size_option()
@type size_option() :: String.t()
stop_option()
@type stop_option() :: term()
stream_option()
@type stream_option() :: boolean()
stream_options()
suffix_option()
@type suffix_option() :: String.t()
temperature_option()
@type temperature_option() :: float()
top_p_option()
@type top_p_option() :: term()
user_option()
@type user_option() :: String.t()
Link to this section Functions
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)