Liking cljdoc? Tell your friends :D

openai-clojure

CI cljdoc badge

Clojure functions to drive the OpenAI API and Azure OpenAI API

This library aims to hide the small differences between the 2 APIs, and allows therefore to develop tools and applications which can work with both variants.

Documentation

Supported APIs

OpenAIAzure OpenAI
Versionv1v2022-12-01
CompletionXX
EmbeddingsXX
ModelsX
EditsX
ImagesX
FilesX
Fine-tunesX
ModerationsX

Configuration

Clojars Project

Add the openai-clojure dependency

deps.edn

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

Leiningen project.clj

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

Java

Minimum Java 11 required

Authentication

OpenAI

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.

Azure OpenAI

API Key

Set the environment variable AZURE_OPENAI_API_KEY to your Azure OpenAI key.

API endpoint

Set the environment variable AZURE_OPENAI_API_ENDPOINT to your Azure OpenAI endpoint, example: https://myendpoint.openai.azure.com

Quickstart

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

Require the api namespace

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

A simple completion prompt for OpenAI could be:

(api/create-completion {:model "text-davinci-003"
                        :prompt "Say this is a test"
                        :max_tokens 7
                        :temperature 0})

or for Azure OpenAI:

(api/create-completion :azure {:model "text-davinci-003"
                               :prompt "Say this is a test"
                               :max_tokens 7
                               :temperature 0})

Result:

{:id "cmpl-6jY1xInJeGGpzUgsZtkuxDsf5DdBa",
 :object "text_completion",
 :created 1676313593,
 :model "text-davinci-003",
 :choices
 [{:text "\n\nThis is indeed a test",
   :index 0,
   :logprobs nil,
   :finish_reason "length"}],
 :usage {:prompt_tokens 5, :completion_tokens 7, :total_tokens 12}}

Issues and features

Please feel free to raise issues on Github or send pull requests

Acknowledgements

This library uses Martian - An HTTP abstraction library

License

This is an unofficial library, it is not affiliated with nor endorsed by OpenAI

MIT License

Copyright (c) 2023 Werner Kok

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Can you improve this documentation? These fine people already did:
Werner Kok & Carsten Behring
Edit on GitHub

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

× close