Clojure functions to drive the OpenAI API
Add the openai-clojure dependency
net.clojars.wkok/openai-clojure {:mvn/version "0.13.0"}
[net.clojars.wkok/openai-clojure "0.13.0"]
Set the environment variable OPENAI_API_KEY to your OpenAI API key.
An API key can be generated in your OpenAI account
Optional - If your OpenAI account uses multiple organizations, set the environment variable OPENAI_ORGANIZATION to the one used for your app.
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
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"})
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}]}
Also see the OpenAI documentation
Also see the OpenAI documentation
Also see the OpenAI documentation
Also see the OpenAI documentation
Also see the OpenAI documentation
Also see the OpenAI documentation
Also see the OpenAI documentation
Also see the OpenAI documentation
Also see the OpenAI documentation
Also see the OpenAI documentation
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |