feat: supports openrouter reasoning (#1174)

This commit is contained in:
yetone 2025-02-04 01:38:18 +08:00 committed by GitHub
parent ffb2a4f48d
commit ef4b6077ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,6 +28,7 @@ local P = require("avante.providers")
---@field role? "user" | "system" | "assistant" ---@field role? "user" | "system" | "assistant"
---@field content? string ---@field content? string
---@field reasoning_content? string ---@field reasoning_content? string
---@field reasoning? string
--- ---
---@class AvanteProviderFunctor ---@class AvanteProviderFunctor
local M = {} local M = {}
@ -39,6 +40,8 @@ M.role_map = {
assistant = "assistant", assistant = "assistant",
} }
M.is_openrouter = function(url) return url:match("^https://openrouter%.ai/") end
---@param opts AvantePromptOptions ---@param opts AvantePromptOptions
M.get_user_message = function(opts) M.get_user_message = function(opts)
vim.deprecate("get_user_message", "parse_messages", "0.1.0", "avante.nvim") vim.deprecate("get_user_message", "parse_messages", "0.1.0", "avante.nvim")
@ -126,6 +129,13 @@ M.parse_response = function(ctx, data_stream, _, opts)
end end
ctx.last_think_content = choice.delta.reasoning_content ctx.last_think_content = choice.delta.reasoning_content
opts.on_chunk(choice.delta.reasoning_content) opts.on_chunk(choice.delta.reasoning_content)
elseif choice.delta.reasoning and choice.delta.reasoning ~= vim.NIL then
if ctx.returned_think_start_tag == nil or not ctx.returned_think_start_tag then
ctx.returned_think_start_tag = true
opts.on_chunk("<think>\n")
end
ctx.last_think_content = choice.delta.reasoning
opts.on_chunk(choice.delta.reasoning)
elseif choice.delta.content then elseif choice.delta.content then
if if
ctx.returned_think_start_tag ~= nil and (ctx.returned_think_end_tag == nil or not ctx.returned_think_end_tag) ctx.returned_think_start_tag ~= nil and (ctx.returned_think_end_tag == nil or not ctx.returned_think_end_tag)
@ -174,6 +184,12 @@ M.parse_curl_args = function(provider, code_opts)
headers["Authorization"] = "Bearer " .. api_key headers["Authorization"] = "Bearer " .. api_key
end end
if M.is_openrouter(base.endpoint) then
headers["HTTP-Referer"] = "https://github.com/yetone/avante.nvim"
headers["X-Title"] = "Avante.nvim"
body_opts.include_reasoning = true
end
-- NOTE: When using "o" series set the supported parameters only -- NOTE: When using "o" series set the supported parameters only
local stream = true local stream = true
if M.is_o_series_model(base.model) then if M.is_o_series_model(base.model) then