wkok / openai-clojure

Clojure functions to drive the OpenAI API
https://cljdoc.org/d/net.clojars.wkok/openai-clojure
MIT License
208 stars 28 forks source link

Support bb/sci #59

Closed jianghoy closed 6 months ago

jianghoy commented 6 months ago

So, I'm using the library , so far so good. However when I try to use it in babashka script, I'm getting an error saying LinkedList is not supported in bb:

The bb.edn file:

{:paths ["bb"]
 ; todo: remove it
 :deps {net.clojars.wkok/openai-clojure {:mvn/version "0.16.0"}}}

The script:

(require '[wkok.openai-clojure.api :as api])

(defn- get-first-response [res]
  (-> res
      :choices
      first
      :message
      :content))

; TODO: I feel there needs to be a openai.clj that handles request 
; tally of tokens, response returning (what about specific edn schema?) and 
; maybe saving of history.
; 
; Also need a usr-contex thing, like a user-id so that I can tally token
; no matter in web or on cli.
(defn get-openai-response [sys-prompt, usr-prompt]
  (get-first-response
   (api/create-chat-completion
    {:model "gpt-3.5-turbo"
     :messages [{:role "system" :content sys-prompt}
                {:role "user" :content usr-prompt}]})))

(get-openai-response "you're a helpful assistant" "hello")

Error message:

bb tst.clj
----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Unable to resolve classname: java.util.LinkedList
Location: wkok/openai_clojure/sse.clj:10:3

----- Context ------------------------------------------------------------------
 6:    [clojure.core.async :as a]
 7:    [clojure.string :as string]
 8:    [cheshire.core :as json]
 9:    [clojure.core.async.impl.protocols :as impl])
10:   (:import (java.io InputStream)
      ^--- Unable to resolve classname: java.util.LinkedList
11:            (clojure.lang Counted)
12:            (java.util    LinkedList)))
13:
14: (def event-mask (re-pattern (str "(?s).+?\n\n")))
15:

----- Stack trace --------------------------------------------------------------
wkok.openai-clojure.sse   - wkok/openai_clojure/sse.clj:10:3
wkok.openai-clojure.azure - wkok/openai_clojure/azure.clj:2:3
wkok.openai-clojure.core  - wkok/openai_clojure/core.clj:2:3
wkok.openai-clojure.api   - wkok/openai_clojure/api.clj:2:3
user                      - /Users/jianghongying/code/conavi/tst.clj:1:1

Any thoughts on adding support to it? I believe the culprit must be how the LinkedList is used the first place.

jianghoy commented 6 months ago

https://github.com/wkok/openai-clojure/blob/main/src/wkok/openai_clojure/sse.clj#L10-L12

Since babashka doesn't support loading arbitrary java classes: https://book.babashka.org/#libraries, the way it imports a LinkedList will not work in babashka.

wkok commented 6 months ago

The usage of LinkedList got introduced in this PR 47 in order to address Issue 43

I have not had a chance to look into it but you're welcome to raise a PR that either

wkok commented 6 months ago

Fixed by https://github.com/wkok/openai-clojure/pull/60