Skip to content

on-next not called when stream option is true #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yenda opened this issue Nov 9, 2023 · 4 comments
Closed

on-next not called when stream option is true #36

yenda opened this issue Nov 9, 2023 · 4 comments

Comments

@yenda
Copy link

yenda commented Nov 9, 2023

I am calling create-chat-completion with the options :stream true :on-next #(log/info :on-next %) and nothing it logs, instead I have all the "chat.completion.chunk" events dumped at once after some time

I am using azure

@yenda
Copy link
Author

yenda commented Nov 10, 2023

This is the code I had to use to be able to consume the stream:
using aleph and manifold

(defn parse-sse [sse-string]
  (let [lines (str/split sse-string #"\n\n")]
    (for [line lines
          :let [parts (str/split line #"\n")
                data (first (filter #(str/starts-with? % "data:") parts))]]
      (when data
        (str/replace data "data: " "")))))

(defn handle-bytebuf [bytebuf]
  (let [bytes (byte-array (.readableBytes bytebuf))]
    (.getBytes bytebuf 0 bytes)
    (parse-sse (String. bytes "UTF-8"))))

(defn stream-chat-completion
  [messages options on-next]
  (let [{:keys [model temperature max-tokens]} options]
    @(d/chain
      (http/post url
                 {:headers {"api-key" key}
                  :form-params (cond-> {:temperature temperature
                                        :top_p 1
                                        :messages messages
                                        :stream true}
                                 max-tokens (assoc :max_tokens max-tokens))
                  :content-type :json
                  :pool raw-stream-connection-pool
                  })
      :body
      #(stream/consume (fn [chunk]
                         (on-next (handle-bytebuf chunk))) %))))

@wkok
Copy link
Owner

wkok commented Nov 12, 2023

Unfortunately I don't have access to an azure account to play with.

@behrica do you experience the same issue with the current version?

@wkok
Copy link
Owner

wkok commented Nov 24, 2023

v0.14.0 includes some changes related to SSE for openai.
Could you maybe confirm if this issue still persists in azure on this new version?

@wkok
Copy link
Owner

wkok commented Nov 12, 2024

Fixed via PR 73 in v0.21.2

@wkok wkok closed this as completed Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants