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 @@ -97,27 +97,6 @@ parse_completion_chunk = types.partial {
9797 }
9898}
9999
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-
121100parse_error_message = types. partial {
122101 error : types. partial {
123102 message : types. string\ tag " message"
@@ -248,13 +227,15 @@ class OpenAI
248227 accumulation_buffer ..= chunk
249228
250229 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
253232 break
254233
255234 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
258239
259240 ...
260241
You can’t perform that action at this time.
0 commit comments