fix: invalid response err (#19)

This commit is contained in:
fish 2024-08-16 13:40:41 +08:00 committed by GitHub
parent 2674872945
commit 78be877a3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -237,11 +237,13 @@ local function call_openai_api_stream(question, code_lang, code_content, on_chun
error("Error: failed to parse json: " .. parsed)
return
end
if parsed and parsed.choices and parsed.choices[1].delta.content then
on_chunk(parsed.choices[1].delta.content)
elseif parsed and parsed.choices and parsed.choices[1].finish_reason == "stop" then
-- Stream request completed
if parsed and parsed.choices and parsed.choices[1] then
local choice = parsed.choices[1]
if choice.finish_reason == "stop" then
on_complete(nil)
elseif choice.delta and choice.delta.content then
on_chunk(choice.delta.content)
end
end
end)
end