fix: support legacy finish_reason (#706)

Many OpenAI compatible alternative servers are still returning a
`finish_reason` of `eos_token` instead of `stop`. This commit adds
support for that to support more of these servers/options.
This commit is contained in:
Aaron Batilo 2024-10-11 07:46:34 -06:00 committed by GitHub
parent faaa7f223b
commit f92c3a60f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,7 +20,7 @@ local P = require("avante.providers")
--- ---
---@class OpenAIResponseChoiceComplete ---@class OpenAIResponseChoiceComplete
---@field message OpenAIMessage ---@field message OpenAIMessage
---@field finish_reason "stop" | "length" ---@field finish_reason "stop" | "length" | "eos_token"
---@field index integer ---@field index integer
---@field logprobs integer ---@field logprobs integer
--- ---
@ -84,7 +84,7 @@ M.parse_response = function(data_stream, _, opts)
local json = vim.json.decode(data_stream) local json = vim.json.decode(data_stream)
if json.choices and json.choices[1] then if json.choices and json.choices[1] then
local choice = json.choices[1] local choice = json.choices[1]
if choice.finish_reason == "stop" then if choice.finish_reason == "stop" or choice.finish_reason == "eos_token" then
opts.on_complete(nil) opts.on_complete(nil)
elseif choice.delta.content then elseif choice.delta.content then
if choice.delta.content ~= vim.NIL then opts.on_chunk(choice.delta.content) end if choice.delta.content ~= vim.NIL then opts.on_chunk(choice.delta.content) end