Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is basically the middleware pattern except that each function is responsible for calling the next function in the chain. As a consequence, and contrary to the classic middleware pattern, the chain goes both ways, up to thhe api call, and down returning the result. The first expr is indeed special. It is in this context a level 0 function: it takes a context, does stuff, and returns the context. The other functions in the threading/-> expression are level 1 function: they return a level-0 function, in short they act like a factory. That returned function will be then threaded into the following level 1 function and so on. In the end the defined 'chatgpt' element is the whole chain. Call order is to be read from bottom to top.

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.


Thanks. Have you found good libraries for working with openai api's in clj?


According to https://github.com/search?q=language:clojure%20gpt&type=repo...

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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: