Liking cljdoc? Tell your friends :D

Usage - Azure OpenAI

Clojure functions to drive the Azure OpenAI API

Configuration

Add the openai-clojure dependency

deps.edn

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

Leiningen project.clj

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

Authentication

API Key

Set the environment variable AZURE_OPENAI_API_KEY to your Azure OpenAI API key.

An API key can be retrieved from your Azure account

Endpoint

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

Options

Alternatively the api-key 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"
                        :api-endpoint "https://myendpoint.openai.azure.com"
                        :impl :azure})

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-35-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?"}]}
                            {:impl :azure})

Result:

{:id "chatcmpl-6srOKLabYTpTRwRUQxjkcBxw3uf1H",
 :object "chat.completion",
 :created 1678532968,
 :model "gpt-35-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 Azure OpenAI APIs

Completions

Also see the Azure OpenAI documentation

Chat

Also see the Azure OpenAI documentation

Embeddings

Also see the Azure OpenAI documentation

Can you improve this documentation?Edit on GitHub

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

× close