View Source OpenaiEx README

Mix.install([
  {:openai_ex, "~> 0.1.0"}
])

introduction

Introduction

OpenaiEx is an Elixir library that provides a community-maintained client for the OpenAI API.

The library closely follows the structure of the official OpenAI API client libraries for Python and JavaScript, making it easy to understand and reuse existing documentation and code.

This file is an executable Livebook, which means you can interactively run and modify the code samples provided. We encourage you to open it in Livebook and try out the code for yourself!

installation

Installation

You can install OpenaiEx using Mix:

in-livebook

In Livebook

Add the following code to the first connection cell:

Mix.install(
  [
    {:openai_ex, "~> 0.1.0"}
  ],
)```

in-a-mix-project

In a Mix Project

Add the following to your mix.exs file:

def deps do
  [
    {:openai_ex, "~> 0.1.0"}
  ]
end

usage

Usage

OpenaiEx provides a thin wrapper around the OpenAI API client, following the same API structure as the official Python and JS clients. To learn how to use OpenaiEx, you can refer to the relevant parts of the official OpenAI API reference documentation, which we link to in the following sections.

authentication

Authentication

To authenticate with the OpenAI API, you will need an API key. We recommend storing your API key in an environment variable, like this:

apikey = System.fetch_env!("LB_OPENAI_API_KEY")
openai = OpenaiEx.new(apikey)

You can also specify an organization if you are a member of more than one:

# organization = System.fetch_env!("LB_OPENAI_ORGANIZATION")
# openai = OpenAIEx.new(apikey, organization)

For more information on authentication, see the OpenAI API Authentication reference.

model

Model

alias OpenaiEx.Model

To list all available models, use the Model.list() function:

openai |> Model.list()

To retrieve information about a specific model, use the Model.retrieve(model_id) function:

openai |> Model.retrieve("text-davinci-003")

For more information on using models, see the OpenAI API Models reference.

completion

Completion

To generate a completion, you first need to define a completion request structure using the Completion.new() function. This function takes several parameters, such as the model ID, the prompt, the maximum number of tokens, and the temperature.

alias OpenaiEx.Completion

completion =
  Completion.new(
    model: "text-davinci-003",
    prompt: "Say this is a test",
    max_tokens: 100,
    temperature: 0
  )

Once you have defined the completion request structure, you can generate a completion using the Completion.create() function:

comp_response = openai |> Completion.create(completion)

For more information on generating completions, see the OpenAI API Completions reference.

chat-completion

Chat Completion

alias OpenaiEx.ChatCompletion
alias OpenaiEx.ChatMessage

To generate a chat completion, you need to define a chat completion request structure using the ChatCompletion.new() function. This function takes several parameters, such as the model ID and a list of chat messages. We have a module ChatMessage which helps in message creation.

chat_completion =
  ChatCompletion.new(model: "gpt-3.5-turbo", messages: [ChatMessage.user("Hello")])

You can generate a chat completion using the ChatCompletion.create() function:

chat_response = openai |> ChatCompletion.create(chat_completion)

For more information on generating chat completions, see the OpenAI API Chat Completions reference.