by StackOverlord · 3y ago · 0 comments
(def chatgpt
(-> (endpoint :chat "gpt-4" auth)
(chat/set-opts [:endpoint] {:stream true})
chat/retry
chat/catch-unkown-commands
;; chat/history
chat/string-input
(chat/file-io "file.txt" :stream true)
#_(chat/preserve-ctx chatgpt-ctx)))
These higher-level functions take as input the next function in the chain, and return a function that is responsible for calling it and takes a context hashmap as argument. Additionally, you can pass messages to these functions like :history/reset, impacting the atom defined in the let part of lolol. I use the same pattern for higher level constructs:[1]https://letoverlambda.com/index.cl/guest/chap2.html#sec_6
I was thinking about doing something similar with dynamic scoping in Emacs Lisp.
Here's how level-1 functions are defined. 'ctx-fn' is just an indirection macro for 'fn' I haven't really used yet.
(defn preserve-ctx [f & [at]]
(let [mem (or at (atom {:ctx true}))]
(ctx-fn
:preserve-ctx [ctx]
(if (->> ctx :it (cmd? :preserve-ctx))
(do (case (:it ctx)
:preserve-ctx/get @mem
:preserve-ctx/reset (doto {:ctx true}
(->> (reset! mem))))
(println (-> ctx :it) "done."))
(-> ctx
(when-not-> (-> :states :preserved)
(as-> $ (ctx-it (merge @mem $) (-> $ :it))))
f
(assoc-in [:states :preserved] true)
(doto (->> (reset! mem))))))))
Edit: reading what I wrote, 'endpoint' is in fact a function returning function, except that since it's the "end" of the chain, it doesn't take a function to call next as argument.The most advanced lib for dealing with LLMs is
https://github.com/zmedelis/bosquet
There is also https://github.com/cjbarre/multi-gpt/tree/main but it hasn't been update in 3 months and seems rather basic.
Alternatively, you can shoot me an email at
(->> '(102 117 110 116 97 105 110 64 109 101 46 99 111 109) (map char) (apply str))
and I'll prepare a repo for what I've been working on. It's usable but I wanted to clear some things up before a public release.