openai-clojure icon indicating copy to clipboard operation
openai-clojure copied to clipboard

Make sure sse async channel gets closed.

Open phronmophobic opened this issue 9 months ago • 3 comments

The events async channel does not ever get closed. Closing a channel is the best way to signal that a channel doesn't have any more info. Currently, the channel signals that it's done by putting a :done value on the channel. I think it would be preferable to just close the channel rather than send an unqualified :done as a sentinel, but I did not make that change since it would be backwards incompatible.

Closing the channel when the stream is finished also makes the channel compatible with existing builtin functions. For example:

(def response
  (openai/create-chat-completion {:model "gpt-4o"
                                  :messages [{:role "system" :content "You are a helpful assistant."}
                                             {:role "user" :content "hi"}]
                                  :stream true}
                                 {:api-key openai-key}))


(def messages (async/<!! (async/into [] response))) 

The above works after applying the attached change.

phronmophobic avatar May 27 '24 17:05 phronmophobic