Skip to content

Flexiana/clj-openai

Repository files navigation

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

Docs

Function parameters:

  1. config map
  2. prompt string

Example usage:

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

Chat completions

Docs

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

When introducing a new version, bump the version number in build.clj. After it's merged into master branch, create a git tag on the last commit and push the tag, it will trigger deploy to Clojars.

There are utility scripts in the bin/ directory for some common tasks:

  • ./bin/nrepl starts nREPL
  • ./bin/test runs all tests (accepts all -X arguments, docs)
  • ./bin/test-ns clj-openai.core-test runs tests in a single namespace
  • ./bin/test-var clj-openai.core-test/completion runs a single test
  • ./bin/lint runs kondo and eastwood
  • ./bin/coverage runs test coverage check
  • ./bin/ci run the project's CI pipeline and builds a jar
  • ./bin/local-install installs the jar into local maven repo
  • ./bin/deploy deploys the jar to clojars, CLOJARS_USERNAME and CLOJARS_PASSWORD env vars must be set

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.