Liking cljdoc? Tell your friends :D

clj-openai

A simple ChatGPT client for Clojure.

Installation

Clojars Project

Usage

(require '[clj-openai.core :as ai])

clj-openai provides 2 functions: completions and chat-completions. They both take config map as first argument. They both return string on success and error map on failure.

completions

Function parameters:

  1. config map
  2. prompt string

Example usage:

(ai/completions {:api-key api-key, :base-url base-url}
                "hello")

chat-completions

Function parameters:

  1. config map
  2. vector of message objects

Example usage:

(ai/chat-completions {:api-key api-key, :base-url base-url}
                     [{:role "user", :content "hello"}])

Config map

Config map is passed as first argument to all provided functions. Available config keys:

  • :api-key - mandatory, managed here
  • :base-url - optional, defaults to https://api.openai.com/v1

Errors

Example:

{:error
 {:message "Incorrect API key provided."
  :type "invalid_request_error"
  :param nil
  :code "invalid_api_key"}}

Development

Start nREPL:

$ ./bin/nrepl

Run the project's tests (accepts all -X arguments, docs):

$ ./bin/test

Run tests in single namespace:

$ ./bin/test-ns clj-openai.core-test

Run a single test:

$ ./bin/test-var clj-openai.core-test/completion

Run the project's CI pipeline and build a jar:

$ ./bin/ci

Install the jar locally:

$ clj -T:build local-install

Deploy the jar to clojars ('CLOJARS_USERNAME' and 'CLOJARS_PASSWORD' env vars must be set):

$ ./bin/deploy

License

Copyright © 2023 Staifa

EPLv1.0 is just the default for projects generated by deps-new: you are not required to open source this project, nor are you required to use EPLv1.0! Feel free to remove or change the LICENSE file and remove or update this section of the README.md file!

Distributed under the Eclipse Public License version 1.0.

Can you improve this documentation?Edit on GitHub

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

× close