Note for core.async.flow users. If you start a new thread in the :init function of a process, if there are any errors or a crash in that thread, they will not be reported on the error-chan returned when starting the flow. This took some debugging time for me. In hindsight, it seems evident as there isnβt a catch mechanism for this, but it still might be good for others to know. Code in the replies π
(def end-call-judge
"Process that judges when the AI should end the call."
(flow/process
{:describe (fn [] {:ins {:in "Channel for frames containing what the assistant is saying"}
:params {:openai/api-key "Api key for llm"
:end-phrases "Dictionary of phrases that when the assistant says, the call should be ended"}})
:init
(fn [params]
(let [llm-write (a/chan 100)
end-time-speech (atom nil)
end-call-f (atom (:end-call-f params))
total-assistant-messages (atom "")
write-to-llm
#(loop []
;; βΌοΈ IMPORTANT: Errors in this loop are not reported on `flow` `error-chan`
(when-let [frame (a/ (openai/normal-chat-completion
{:api-key (:openai/api-key params)
:messages [{:role :system
:content (str "You are a ending call judge. You will oversee the output of an LLM assistant and judge when the call should be ended. When the assistant says any of the end-call phrases you should end the call. Here are the end-call phrases: " (str/join ", " (:end-phrases params)))}
{:role :user
:content (str "Assistant message:" @total-assistant-messages)}]
:response-format response-format})
:body
u/parse-if-json
content
u/parse-if-json)]
(t/log! :debug ["SHOULD END " should_end "FOR " @total-assistant-messages])
(if should_end
(let [now (mono-time)
diff (- @end-time-speech now)]
(t/log! :debug ["ENDING CALL IN " diff "FOR " @total-assistant-messages])
(a/Excuse the bad code. I wrote this in a big hurry, but the point remains
On the beginning of the new scicloj-ai-meetups series today, @ovidiu.stoica1094 and @jpmonettas discussed, among other things, applications built with core.async.flow and the way they an be debugged using a new #flow-storm plugin.
https://clojurians.slack.com/archives/C03RZRRMP/p1740262448869799
Thank you, Rich! I'm happy you liked it!