File tree 1 file changed +6
-25
lines changed
1 file changed +6
-25
lines changed Original file line number Diff line number Diff line change @@ -97,27 +97,6 @@ parse_completion_chunk = types.partial {
97
97
}
98
98
}
99
99
100
- -- lpeg pattern to read a json data block from the front of a string, returns
101
- -- the json blob and the rest of the string if it could parse one
102
- consume_json_head = do
103
- import C , S , P from require " lpeg"
104
-
105
- -- this pattern reads from the front just enough characters to consume a
106
- -- valid json object
107
- consume_json = P ( str, pos) ->
108
- str_len = # str
109
- for k= pos+ 1 , str_len
110
- candidate = str\ sub pos, k
111
- parsed = false
112
- pcall -> parsed = cjson. decode candidate
113
- if parsed
114
- return k + 1
115
-
116
- return nil -- fail
117
-
118
- S ( " \t\n\r " ) ^ 0 * P ( " data: " ) * C ( consume_json) * C ( P ( 1 ) ^ 0 )
119
-
120
-
121
100
parse_error_message = types. partial {
122
101
error : types. partial {
123
102
message : types. string\ tag " message"
@@ -248,13 +227,15 @@ class OpenAI
248
227
accumulation_buffer ..= chunk
249
228
250
229
while true
251
- json_blob , rest = consume_json_head \ match accumulation_buffer
252
- unless json_blob
230
+ line , rest = accumulation_buffer \ match " ^(.-) \r ? \n\r ? \n (.-)$ "
231
+ unless line
253
232
break
254
233
255
234
accumulation_buffer = rest
256
- if chunk = parse_completion_chunk cjson. decode json_blob
257
- chunk_callback chunk
235
+ json_blob = line\ match " ^data:%s+(.-)%s*$"
236
+ if json_blob and json_blob~= " [DONE]"
237
+ if chunk = parse_completion_chunk cjson. decode json_blob
238
+ chunk_callback chunk
258
239
259
240
...
260
241
You can’t perform that action at this time.
0 commit comments