Skip to content

Commit b6a0a28

Browse files
authored
Merge pull request #60 from jianghoy/main
Use buffer instead of sliding buffer and remove LinkedList
2 parents 06f1826 + a87ad65 commit b6a0a28

File tree

2 files changed

+15
-38
lines changed

2 files changed

+15
-38
lines changed

src/wkok/openai_clojure/api.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@
448448
([params]
449449
(create-assistant params nil))
450450
([params options]
451-
(let [opts (assoc-in options [:openai-beta] "assistants=v1")]
451+
(let [opts (assoc-in options [:openai-beta] "assistants=v2")]
452452
(core/response-for :create-assistant params opts))))
453453

454454

src/wkok/openai_clojure/sse.clj

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
[hato.middleware :as hm]
66
[clojure.core.async :as a]
77
[clojure.string :as string]
8-
[cheshire.core :as json]
9-
[clojure.core.async.impl.protocols :as impl])
10-
(:import (java.io InputStream)
11-
(clojure.lang Counted)
12-
(java.util LinkedList)))
8+
[cheshire.core :as json])
9+
(:import (java.io InputStream)))
1310

1411
(def event-mask (re-pattern (str "(?s).+?\n\n")))
1512

@@ -31,39 +28,17 @@
3128
(-> (subs raw-event data-idx)
3229
(json/parse-string true)))))
3330

34-
(deftype InfiniteBuffer [^LinkedList buf]
35-
impl/UnblockingBuffer
36-
impl/Buffer
37-
(full? [_this]
38-
false)
39-
(remove! [_this]
40-
(.removeLast buf))
41-
(add!* [this itm]
42-
(.addFirst buf itm)
43-
this)
44-
(close-buf! [_this])
45-
Counted
46-
(count [_this]
47-
(.size buf)))
48-
49-
(defn infinite-buffer []
50-
(InfiniteBuffer. (LinkedList.)))
31+
; Per this discussion: https://community.openai.com/t/clarification-for-max-tokens/19576
32+
; if the max_tokens is not provided, the response will try to use all the available
33+
; tokens to generate response, hence DEFAULT_BUFFER_SIZE should be large enough
34+
(def ^:private DEFAULT_BUFFER_SIZE 100000)
5135

5236
(defn calc-buffer-size
53-
"- Use stream_buffer_len if provided.
54-
- Otherwise, buffer size should be at least equal to max_tokens
55-
plus the [DONE] terminator if it is provided.
56-
- Else fallbacks on ##Inf and use an infinite-buffer instead"
57-
[{:keys [stream_buffer_len max_tokens]}]
58-
(or stream_buffer_len
59-
(when max_tokens (inc max_tokens))
60-
##Inf))
61-
62-
(defn make-buffer [params]
63-
(let [size (calc-buffer-size params)]
64-
(if (= size ##Inf)
65-
(infinite-buffer)
66-
(a/sliding-buffer size))))
37+
"Buffer size should be at least equal to max_tokens
38+
plus the [DONE] terminator"
39+
[{:keys [max_tokens]
40+
:or {max_tokens DEFAULT_BUFFER_SIZE}}]
41+
(inc max_tokens))
6742

6843
(defn sse-events
6944
"Returns a core.async channel with events as clojure data structures.
@@ -72,7 +47,8 @@
7247
(let [event-stream ^InputStream (:body (http/request (merge request
7348
params
7449
{:as :stream})))
75-
events (a/chan (make-buffer params) (map parse-event))]
50+
buffer-size (calc-buffer-size params)
51+
events (a/chan (a/buffer buffer-size) (map parse-event))]
7652
(a/thread
7753
(loop [byte-coll []]
7854
(let [byte-arr (byte-array (max 1 (.available event-stream)))
@@ -181,3 +157,4 @@
181157
(assoc ctx :response (if (:stream params)
182158
(sse-request ctx')
183159
(http/request request'))))))})
160+

0 commit comments

Comments
 (0)