Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
timjp87 opened this issue Oct 12, 2023 · 27 comments
Closed

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

timjp87 opened this issue Oct 12, 2023 · 27 comments

Comments

@timjp87
Copy link

timjp87 commented Oct 12, 2023

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
Copy link

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

@greygoody
Copy link

greygoody commented Oct 13, 2023

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
Copy link

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
Copy link

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
Copy link
Author

timjp87 commented Oct 13, 2023

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 d4aa4d9 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
Copy link

I have the same issue

@vitosans
Copy link

Same issue

@ekaj2
Copy link
Contributor

ekaj2 commented Oct 14, 2023

Same issue... :(

@ekaj2
Copy link
Contributor

ekaj2 commented Oct 14, 2023

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
Copy link
Contributor

ekaj2 commented Oct 14, 2023

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 added a commit to ekaj2/ChatGPT.nvim that referenced this issue Oct 14, 2023
curl: option -H: requires parameter error

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

https://github.com/jackMort/ChatGPT.nvim/pull/293/files

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.
@ekaj2
Copy link
Contributor

ekaj2 commented Oct 14, 2023

PR here: #305

@molleweide
Copy link

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

@jurdunnn
Copy link

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

@jackyu1996
Copy link
Contributor

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

@jurdunnn
Copy link

Yeah it looks like that commit is working fine

@jumski
Copy link

jumski commented Dec 12, 2023

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

@jackyu1996
Copy link
Contributor

@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
Copy link

jumski commented Dec 13, 2023

@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
Copy link
Contributor

@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
Copy link

jumski commented Dec 15, 2023

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
Copy link
Contributor

@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
Copy link

jumski commented Feb 20, 2024

@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
Copy link

vkbytes commented Feb 25, 2024

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

@kekscode
Copy link

kekscode commented Feb 29, 2024

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
Copy link

1-800-jono commented Mar 5, 2024

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
Copy link

vkbytes commented Mar 6, 2024 via email

@kirylvarykau
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests