View Source OpenAiClient (open_ai_client v2.1.0)

A client for the OpenAI API.

This client supports all options provided by the Req library, as well as additional options:

  • :breaker - a circuit breaker module (default: ExBreak)
  • :openai_organization - the OpenAI organization ID

Summary

Functions

Sends a GET request to the OpenAI API.

Sends a POST request to the OpenAI API.

Functions

@spec get(String.t(), Keyword.t()) :: {:ok, map()} | {:error, any()}

Sends a GET request to the OpenAI API.

Examples

iex> OpenAiClient.get("https://api.openai.com/v1/models")
{:ok, %{"models" => [%{"id" => "gpt-3.5-turbo", "object" => "model", "created" => 1646183485, "name" => "gpt-3.5-turbo"}]}}
Link to this function

post(url, options \\ [])

View Source
@spec post(String.t(), Keyword.t()) :: {:ok, map()} | {:error, any()}

Sends a POST request to the OpenAI API.

Examples

iex> OpenAiClient.post("https://api.openai.com/v1/chat/completions",
...> [json: %{model: "gpt-3.5-turbo", messages: [%{role: "system", content: "You are a helpful assistant."}, %{role: "user", content: "Who won the world series in 2020?"}]}])
{:ok, %{"id" => "chatcmpl-3o4Bz2rawi6wk8c4L8QR4W7eFCDj", "object" => "chat.completion", "created" => 1646183485, "model" => "gpt-3.5-turbo", "choices" => [%{"message" => %{ "role" => "assistant", "content" => "The Los Angeles Dodgers won the World Series in 2020."}, "finish_reason" => "stop", "index" => 0}]}}