Liking cljdoc? Tell your friends :D

Usage

Visit the official OpenAI documentation for more information.

Configuration

Add the openai-clj dependency:

Leiningen/Boot

[org.clojars.ygorsimoes/openai-clj "0.1.0"]

Clojure CLI/deps.edn

org.clojars.ygorsimoes/openai-clj {:mvn/version "0.1.0"}

Gradle

implementation("org.clojars.ygorsimoes:openai-clj:0.1.0")

Maven

<dependency>
    <groupId>org.clojars.ygorsimoes</groupId>
    <artifactId>openai-clj</artifactId>
    <version>0.1.1-SNAPSHOT</version>
</dependency>

Authentication

You can configure the library by setting environment variables:

OPENAI_API_KEY: You can generate your API Key by accessing your OpenAI account.

OPENAI_ORGANIZATION (Optional): You can find your organization by accessing your OpenAI account.

Alternatively, you can pass the :api-key and/or :organization in the options argument of each api function:

{:api-key      "YOUR-API-KEY"
 :organization "YOUR-ORGANIZATION"}

Quickstart

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

Add namespace in your project:

(:require [openai-clj.chat :as chat])

A simple chat conversation with ChatGPT could be:

(chat/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?"}]})

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

(chat/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?"}]
                              :api-key      "YOUR-API-KEY"
                              :organization "YOUR-ORGANIZATION"})

Result:

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-3.5-turbo-0613",
  "system_fingerprint": "fp_44709d6fcb",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "\n\nHello there, how may I assist you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

Can you improve this documentation?Edit on GitHub

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

× close