jackMort / ChatGPT.nvim

ChatGPT Neovim Plugin: Effortless Natural Language Generation with OpenAI's ChatGPT API
Apache License 2.0
3.71k stars 310 forks source link

curl: option -H: requires parameter when running :ChatGPT #303

Closed timjp87 closed 11 months ago

timjp87 commented 11 months ago

Hi,

I'm using NvChad as base neovim config and added the plugin on macOS. Before that I successfully used the plugin with my own NeoVim config from scratch, which ended up being NvChad. I exported the key as environment variable for testing which also worked before but now when I try the standard :ChatGPT command I get:

Bildschirmfoto 2023-10-12 um 16 32 12

When I try :ChatGPTCompleteCode I get: An Error Occurred ...
Error executing vim.schedule lua callback: Vim:E474: Attempt to decode a blank string
stack traceback:
[C]: in function 'json_decode'
.../.local/share/nvim/lazy/ChatGPT.nvim/lua/chatgpt/api.lua:125: in function ''
vim/_editor.lua: in function <vim/_editor.lua:0>

My plugin config, where I simply added the plugin to the end: https://pastebin.com/xb9zVvyV

lukeacparsons commented 11 months ago

I reverted to commit d4aa4d9e31d620a0c01006e59f4449ffc7eb33ce and it's working again.

greygoody commented 11 months ago

I reverted to commit d4aa4d9 and it's working again.

won't fix mine ! which version of neovim do you have ? mine is nvim v0.10.0 , LuaJIT 2.1.0-beta3 and nvchad

greygoody commented 11 months ago

After removing my self-compiled version of Neovim and replacing it with Neovim v0.9.0 / LuaJIT 2.1.0 from the official release image, and reverting to commit d4aa4d9, the issue has been successfully resolved.

Armadillidiid commented 11 months ago

Experiencing the same issue. Here is my neovim and chatGPT version:

ChatGPT.nvim": { "branch": "main", "commit": "7db348d7b45a84d6144354d90d5d16315c422e5a" }

NVIM v0.10.0-dev-1063+g5e3cf9fb4b Build type: RelWithDebInfo LuaJIT 2.1.1694285958

timjp87 commented 11 months ago

Experiencing the same issue. Here is my neovim and chatGPT version:

ChatGPT.nvim": { "branch": "main", "commit": "7db348d7b45a84d6144354d90d5d16315c422e5a" }

NVIM v0.10.0-dev-1063+g5e3cf9fb4b Build type: RelWithDebInfo LuaJIT 2.1.1694285958

Try using as commit d4aa4d9e31d620a0c01006e59f4449ffc7eb33ce it worked for me. I'm leaving this open in case this get's fixed in a pull request which can be referenced, so we can use the latest version again.

BenediktHofirek commented 11 months ago

I have the same issue

vitosans commented 11 months ago

Same issue

ekaj2 commented 11 months ago

Same issue... :(

ekaj2 commented 11 months ago

Seems to be related to this change here from this PR that tries to add support for AZURE environment variables as well.

I'm looking into this a bit more, but @jackMort or @jackyu1996 could probably put an update in place faster if they see this. The latest version is very much broken and unusable.

Screenshot 2023-10-14 at 12 04 13 AM

ekaj2 commented 11 months ago

Api.AUTHORIZATION_HEADER is nil in Api.chat_completions...

The callback passed to loadApiKey is not guaranteed to be called in the case that the environment variable for OPENAI_API_KEY exists.

local function loadConfigFromEnv(envName, configName)
  local variable = os.getenv(envName)
  if not variable then
    return
  end
  Api[configName] = variable:gsub("%s+$", "")
end

-- ...

local function loadApiKey(envName, configName, optionName, callback, defaultValue)
  loadConfigFromEnv(envName, configName)
  if not Api[configName] then
    if Config.options[optionName] ~= nil and Config.options[optionName] ~= "" then
      loadConfigFromCommand(Config.options[optionName], optionName, callback, defaultValue)
    else
      logger.warn(envName .. " environment variable not set")
      return
    end
  end
end

A simple solution is to pass the callback function in to handle handle if the environment variable is picked up in the config. I'm making a PR for that now.


One potential workaround although I haven't confirmed it is to delete the environment variable and use the passed in parameter in your packer.lua or similar.

See this part of the readme:

Secrets Management

Providing the OpenAI API key via an environment variable is dangerous, as it leaves the API key easily readable by any process that can access the environment variables of other processes. In addition, it encourages the user to store the credential in clear-text in a configuration file.

As an alternative to providing the API key via the OPENAI_API_KEY environment variable, the user is encouraged to use the api_key_cmd configuration option. The api_key_cmd configuration option takes a string, which is executed at startup, and whose output is used as the API key.

The following configuration would use 1Passwords CLI, op, to fetch the API key from the credential field of the OpenAI entry.

require("chatgpt").setup({
    api_key_cmd = "op read op://private/OpenAI/credential --no-newline"
})

The following configuration would use GPG to decrypt a local file containing the API key

local home = vim.fn.expand("$HOME")
require("chatgpt").setup({
    api_key_cmd = "gpg --decrypt " .. home .. "/secret.txt.gpg"
})

Note that the api_key_cmd arguments are split by whitespace. If you need whitespace inside an argument (for example to reference a path with spaces), you can wrap it in a separate script.

ekaj2 commented 11 months ago

PR here: https://github.com/jackMort/ChatGPT.nvim/pull/305

molleweide commented 10 months ago

This issue seems to persists for me on the latest version.

jurdunnn commented 10 months ago

I'm also still getting this issue on the latest version

jackyu1996 commented 10 months ago

@molleweide @jurdunnn Hi, could you please test whether the second to last commit (9f8062c) works?

jurdunnn commented 10 months ago

Yeah it looks like that commit is working fine

jumski commented 9 months ago

I also have this problem, tested commit 9f8062c but it haven't fixed it

jackyu1996 commented 9 months ago

@jumski Hi, does your neovim config require recompilation after you switched commit?

I currently don't have the time to look into this as I am using my own fork which uses sync reading of API related configs. Maybe I will investigate the newest commit when I have the time.

jumski commented 9 months ago

@jackyu1996 what do you mean by recompilation? I didn't noticed thus, but if you ask about updating (really downgrading) the plugin I confirm I have done that

jackyu1996 commented 9 months ago

@jumski If you are using packer to manage your neovim plugins (or any neovim distribution using it), you might need to run PackerCompile to manually recompile the plugin. I don't know whether any plugin managers are similar but it's worth a try. Or lazy can be used which doesn't require manual compilation of Lua plugins. Or perhaps you can try a fresh install of all plugins?

jumski commented 9 months ago

I'm using Lazy. I verified the OPENAI_API_KEY env var is properly set via running

:lua print(os.getenv('OPENAI_API_KEY'))

What possible issue you are thinking of while suggesting to recompile plugins @jackyu1996 ?

I've opened a clean instance and verified if the Api.AUTHORIZATION_HEADER is properly set and seems like it is

:lua print(require('chatgpt.api').AUTHORIZATION_HEADER)
-- "Authorization: Bearer sk-XXXXXXXXXXXXXXXXXXXX"
jackyu1996 commented 9 months ago

@jumski When you reverted to the second to last commit (back then and was 9f8062c), it should normally work out of the box but in your case it didn't so I naturally assumed it was a recompilation issue. Since you are using Lazy, the reason why you are still unable to use this plugin with commit 9f8062c just got more curious. The change introduced in the child commit (b50fdaf) seems to be concatenating OPENAI_API_HOST string so you might want check that part instead.

jumski commented 7 months ago

@jackyu1996 i just gave it another try and it works correctly now! sorry for such a delay - not replied immediately and later forgot about the issue

cheers!

vkbytes commented 7 months ago

I am still seeing this issue happening. is anyone having the issue even at the latest commit

kekscode commented 7 months ago

Very strange: It worked with the same neovim config on my arm mac but not on my intel mac. It was not the curl version (8.4 ships with the mac, 8.6 can be installed using e.g. the homebrew package manager). Re-installing the plugin in lazy did also not change anything.

I tried to uninstall curl 8.6 on the intel machine and it did not fix things, BUT FWIW: After i reinstalled curl 8.6 with homebrew, it suddenly started to work on the intel machine. It is the very same version though, i think (curl -V shows Release-Date: 2024-01-31). I didn't compare the hashes of the binary though.

Any idea why this could have fixed things? @vkbytes if you're also on macos, maybe you could check on your machine if 1) a reinstall fixes things for you and 2) what it is that changes between removing/adding curl?

1-800-jono commented 7 months ago

Same problem here. Installed for the first time today (using Lazy), it worked once then I started a new session <C-n> and it never worked again. Error message:

curl: option -H: requires parameter
curl: try 'curl --help' or 'curl --manual' for more information

Update: I noticed another error message about api_key_cmd and was able to find a fix to the problem here.

vkbytes commented 7 months ago

I still see the same problem!!

On Tue, Mar 5, 2024 at 9:43 PM Jonathan @.***> wrote:

Same problem here. Installed for the first time today (using Lazy), it worked once then I started a new session and it never worked again. Error message:

curl: option -H: requires parameter curl: try 'curl --help' or 'curl --manual' for more information

— Reply to this email directly, view it on GitHub https://github.com/jackMort/ChatGPT.nvim/issues/303#issuecomment-1979686106, or unsubscribe https://github.com/notifications/unsubscribe-auth/AS4MFJAHZJKM6OC4JM3PGA3YWY37DAVCNFSM6AAAAAA55XU2VGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNZZGY4DMMJQGY . You are receiving this because you were mentioned.Message ID: @.***>

kirylvarykau commented 5 months ago

I installed latest curl (8.7.1) from brew and it seems to work :)