fix(openai): enable image support on OpenAI platform only (fixes #282) (#284)

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham 2024-08-27 11:35:25 -04:00 committed by GitHub
parent fe6518f6de
commit d7be4a59c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 28 deletions

View File

@ -13,7 +13,14 @@ local M = {}
M.api_key_name = "AZURE_OPENAI_API_KEY" M.api_key_name = "AZURE_OPENAI_API_KEY"
M.parse_message = O.parse_message M.parse_message = function(opts)
local user_content = O.get_user_message(opts)
return {
{ role = "system", content = opts.system_prompt },
{ role = "user", content = user_content },
}
end
M.parse_response = O.parse_response M.parse_response = O.parse_response
M.parse_curl_args = function(provider, code_opts) M.parse_curl_args = function(provider, code_opts)

View File

@ -42,12 +42,6 @@ M.copilot = nil
local H = {} local H = {}
local version_headers = {
["editor-version"] = "Neovim/" .. vim.version().major .. "." .. vim.version().minor .. "." .. vim.version().patch,
["editor-plugin-version"] = "avante.nvim/0.0.0",
["user-agent"] = "AvanteNvim/0.0.0",
}
---@return string | nil ---@return string | nil
H.find_config_path = function() H.find_config_path = function()
local config = vim.fn.expand("$XDG_CONFIG_HOME") local config = vim.fn.expand("$XDG_CONFIG_HOME")
@ -120,25 +114,31 @@ M.has = function()
return false return false
end end
M.parse_message = O.parse_message M.parse_message = function(opts)
local user_content = O.get_user_message(opts)
return {
{ role = "system", content = opts.system_prompt },
{ role = "user", content = user_content },
}
end
M.parse_response = O.parse_response M.parse_response = O.parse_response
M.parse_curl_args = function(provider, code_opts) M.parse_curl_args = function(provider, code_opts)
local base, body_opts = P.parse_config(provider)
M.refresh_token() M.refresh_token()
local base, body_opts = P.parse_config(provider)
return { return {
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/chat/completions", url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/chat/completions",
timeout = base.timeout,
proxy = base.proxy, proxy = base.proxy,
insecure = base.allow_insecure, insecure = base.allow_insecure,
headers = vim.tbl_deep_extend("error", { headers = {
["authorization"] = "Bearer " .. M.copilot.token.token, ["Authorization"] = "Bearer " .. M.copilot.token.token,
["copilot-integration-id"] = "vscode-chat", ["Content-Type"] = "application/json",
["openai-organization"] = "github-copilot", ["editor-version"] = "Neovim/" .. vim.version().major .. "." .. vim.version().minor .. "." .. vim.version().patch,
["openai-intent"] = "conversation-panel", },
["content-type"] = "application/json",
}, version_headers),
body = vim.tbl_deep_extend("force", { body = vim.tbl_deep_extend("force", {
model = base.model, model = base.model,
n = 1, n = 1,
@ -151,16 +151,23 @@ end
M.on_error = function(result) M.on_error = function(result)
Utils.error("Received error from Copilot API: " .. result.body, { once = true, title = "Avante" }) Utils.error("Received error from Copilot API: " .. result.body, { once = true, title = "Avante" })
Utils.debug(result)
end end
M.refresh_token = function() M.refresh_token = function()
if not M.copilot.token or (M.copilot.token.expires_at and M.copilot.token.expires_at <= math.floor(os.time())) then if not M.copilot.token or (M.copilot.token.expires_at and M.copilot.token.expires_at <= math.floor(os.time())) then
curl.get("https://api.github.com/copilot_internal/v2/token", { curl.get("https://api.github.com/copilot_internal/v2/token", {
timeout = Config.copilot.timeout, timeout = Config.copilot.timeout,
headers = vim.tbl_deep_extend("error", { headers = {
["Authorization"] = "token " .. M.copilot.github_token, ["Authorization"] = "token " .. M.copilot.github_token,
["Accept"] = "application/json", ["Accept"] = "application/json",
}, version_headers), ["editor-version"] = "Neovim/"
.. vim.version().major
.. "."
.. vim.version().minor
.. "."
.. vim.version().patch,
},
proxy = Config.copilot.proxy, proxy = Config.copilot.proxy,
insecure = Config.copilot.allow_insecure, insecure = Config.copilot.allow_insecure,
on_error = function(err) on_error = function(err)
@ -185,9 +192,8 @@ M.setup = function()
if not M.copilot then if not M.copilot then
M.copilot = { token = nil, github_token = github_token } M.copilot = { token = nil, github_token = github_token }
end
M.refresh_token() M.refresh_token()
end
end end
return M return M

View File

@ -301,11 +301,8 @@ function M.refresh(provider)
---@type AvanteProviderFunctor ---@type AvanteProviderFunctor
local p = M[Config.provider] local p = M[Config.provider]
if not p.has() then
E.setup({ provider = p, refresh = true }) E.setup({ provider = p, refresh = true })
else
Utils.info("Switch to provider: " .. provider, { once = true, title = "Avante" }) Utils.info("Switch to provider: " .. provider, { once = true, title = "Avante" })
end
end end
local default_providers = { "openai", "claude", "azure", "gemini", "copilot" } local default_providers = { "openai", "claude", "azure", "gemini", "copilot" }

View File

@ -27,7 +27,8 @@ local M = {}
M.api_key_name = "OPENAI_API_KEY" M.api_key_name = "OPENAI_API_KEY"
M.parse_message = function(opts) ---@param opts AvantePromptOptions
M.get_user_message = function(opts)
local user_prompt = opts.base_prompt local user_prompt = opts.base_prompt
.. "\n\nCODE:\n" .. "\n\nCODE:\n"
.. "```" .. "```"
@ -56,6 +57,10 @@ M.parse_message = function(opts)
.. opts.question .. opts.question
end end
return user_prompt
end
M.parse_message = function(opts)
local user_content = {} local user_content = {}
if Config.behaviour.support_paste_from_clipboard and Clipboard.has_content() then if Config.behaviour.support_paste_from_clipboard and Clipboard.has_content() then
table.insert(user_content, { table.insert(user_content, {
@ -66,7 +71,7 @@ M.parse_message = function(opts)
}) })
end end
table.insert(user_content, { type = "text", text = user_prompt }) table.insert(user_content, { type = "text", text = M.get_user_message(opts) })
return { return {
{ role = "system", content = opts.system_prompt }, { role = "system", content = opts.system_prompt },