/ollama-clj

Ollama Clojure library

Primary LanguageClojureMIT LicenseMIT

ollama-clj

NOTE: Work in progress!

Use ollama within Clojure project.

Usage

(require '[ollama-clj.core :as o])

(def client (o/->Client "http://localhost:11434"))

(def messages
  [{:role "user"
    :content "Why is the sky blue?"}])

(-> (o/chat client "mistral" messages)
    :message
    :content)

or with streaming option:

(require '[ollama-clj.core :as o])

(def client (o/->Client "http://localhost:11434"))

(def messages
  [{:role "user"
    :content "Why is the sky blue?"}])

                                               ;; note the streaming flag!
(doseq [part (o/chat client "mistral" messages {:stream true})]
  (print (-> part :message :content)))

For more usages reach out to examples/ directory.

Internals

Implement your own client

If you want to gain control over the way of executing ollama calls, you can implement your own client simply by using record and protocol like so:

(defrecord MyClient [url]
  o/BaseClient
  (request [_this method endpoint opts]
    :perform-request)

  (stream [_this method endpoint opts]
    :perform-streaming)

  (request-stream [this method endpoint {:keys [stream?] :as opts}]
    (if stream?
      (.stream this method endpoint opts)
      (.request this method endpoint opts))))

References