File tree Expand file tree Collapse file tree 1 file changed +6
-25
lines changed Expand file tree Collapse file tree 1 file changed +6
-25
lines changed Original file line number Diff line number Diff line change @@ -95,27 +95,6 @@ parse_completion_chunk = types.partial {
9595 }
9696}
9797
98- -- lpeg pattern to read a json data block from the front of a string, returns
99- -- the json blob and the rest of the string if it could parse one
100- consume_json_head = do
101- import C , S , P from require " lpeg"
102-
103- -- this pattern reads from the front just enough characters to consume a
104- -- valid json object
105- consume_json = P ( str, pos) ->
106- str_len = # str
107- for k= pos+ 1 , str_len
108- candidate = str\ sub pos, k
109- parsed = false
110- pcall -> parsed = cjson. decode candidate
111- if parsed
112- return k + 1
113-
114- return nil -- fail
115-
116- S ( " \t\n\r " ) ^ 0 * P ( " data: " ) * C ( consume_json) * C ( P ( 1 ) ^ 0 )
117-
118-
11998parse_error_message = types. partial {
12099 error : types. partial {
121100 message : types. string\ tag " message"
@@ -245,13 +224,15 @@ class OpenAI
245224 accumulation_buffer ..= chunk
246225
247226 while true
248- json_blob , rest = consume_json_head \ match accumulation_buffer
249- unless json_blob
227+ line , rest = accumulation_buffer \ match " ^(.-) \r ? \n\r ? \n (.*)$ "
228+ unless line
250229 break
251230
252231 accumulation_buffer = rest
253- if chunk = parse_completion_chunk cjson. decode json_blob
254- chunk_callback chunk
232+ json_blob = line\ match " ^data:%s+(.-)%s*$"
233+ if json_blob and json_blob~= " [DONE]"
234+ if chunk = parse_completion_chunk cjson. decode json_blob
235+ chunk_callback chunk
255236
256237 ...
257238
You can’t perform that action at this time.
0 commit comments