File tree 1 file changed +6
-2
lines changed
1 file changed +6
-2
lines changed Original file line number Diff line number Diff line change @@ -219,11 +219,14 @@ class OpenAI
219
219
assert types. function( chunk_callback) , " Must provide chunk_callback function when streaming response"
220
220
221
221
accumulation_buffer = " "
222
+ streamed = false
222
223
223
224
( ... ) ->
224
225
chunk = ...
225
226
226
- if type ( chunk) == " string"
227
+ if chunk == nil
228
+ assert not streamed or accumulation_buffer\ match" ^%s*$" , " buffer not empty"
229
+ elseif type ( chunk) == " string"
227
230
accumulation_buffer ..= chunk
228
231
229
232
while true
@@ -236,8 +239,9 @@ class OpenAI
236
239
field, value, rest_evt = event\ match " ^(.-):%s+([^\r\n ]+)(.-)$"
237
240
switch field
238
241
when " data"
242
+ streamed = true
239
243
unless value== " [DONE]"
240
- chunk_callback ( json . decode value)
244
+ chunk_callback ( cjson . decode value)
241
245
when " event" , " id" , " retry" , " " -- comment
242
246
nil -- noop
243
247
when nil
You can’t perform that action at this time.
0 commit comments