Liking cljdoc? Tell your friends :D

Usage - OpenAI

Clojure functions to drive the OpenAI API

Configuration

Add the openai-clojure dependency

deps.edn

net.clojars.wkok/openai-clojure {:mvn/version "0.16.0"}

Leiningen project.clj

[net.clojars.wkok/openai-clojure "0.16.0"]

Authentication

API Key

Set the environment variable OPENAI_API_KEY to your OpenAI API key.

An API key can be generated in your OpenAI account

Organization

Optional - If your OpenAI account uses multiple organizations, set the environment variable OPENAI_ORGANIZATION to the one used for your app.

Endpoint

It is not necessary to specify the endpoint url if using the default OpenAI service. If you do need to point to a different endpoint set the environment variable OPENAI_API_ENDPOINT for example: https://myendpoint.my-openai.com

Options

Alternatively the api-key and/or organization and/or api-endpoint can be passed in the options argument of each api function

(api/create-completion {:model "text-davinci-003"
                        :prompt "Say this is a test"}
                       {:api-key "xxxxx"
                        :organization "abcd"
                        :api-endpoint "https://myendpoint.my-openai.com"})

Quickstart

See the full API Reference api documentation for examples of all the supported OpenAI APIs.

Require the api namespace

(:require [wkok.openai-clojure.api :as api])

A simple chat conversation with ChatGPT could be:

(api/create-chat-completion {:model "gpt-3.5-turbo"
                             :messages [{:role "system" :content "You are a helpful assistant."}
                                        {:role "user" :content "Who won the world series in 2020?"}
                                        {:role "assistant" :content "The Los Angeles Dodgers won the World Series in 2020."}
                                        {:role "user" :content "Where was it played?"}]})

Result:

{:id "chatcmpl-6srOKLabYTpTRwRUQxjkcBxw3uf1H",
 :object "chat.completion",
 :created 1678532968,
 :model "gpt-3.5-turbo-0301",
 :usage {:prompt_tokens 56, :completion_tokens 19, :total_tokens 75},
 :choices
 [{:message
   {:role "assistant",
    :content
    "The 2020 World Series was played at Globe Life Field in Arlington, Texas."},
   :finish_reason "stop",
   :index 0}]}

Advanced configuration

Request options

Request options may be set on the underlying hato http client by adding a :request map to :options for example setting the request timeout:

(api/create-chat-completion {:model "gpt-3.5-turbo"
                             :messages [{:role "system" :content "You are a helpful assistant."}
                                        {:role "user" :content "Who won the world series in 2020?"}
                                        {:role "assistant" :content "The Los Angeles Dodgers won the World Series in 2020."}
                                        {:role "user" :content "Where was it played?"}]}

                            {:request {:timeout 60000}})    ;; <== This

Any of these supported request options may be passed.

Supported OpenAI APIs

Models

Also see the OpenAI documentation

Completions

Also see the OpenAI documentation

Chat

Also see the OpenAI documentation

Images

Also see the OpenAI documentation

Embeddings

Also see the OpenAI documentation

Audio

Also see the OpenAI documentation

Files

Also see the OpenAI documentation

Fine-tuning

Also see the OpenAI documentation

Moderations

Assistants

Threads

Messages

Runs

Also see the OpenAI documentation

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close