jackMort / ChatGPT.nvim

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

attempt to concatenate field 'OPENAI_API_KEY' (a nil value) #250

Closed 8veStory closed 8 months ago

8veStory commented 1 year ago

Hi!

I'm on Windows 11 attempting to use the api_key_cmd with the Onepassword CLI like so:

-- ChatGPT
require("chatgpt").setup({ -- Requires either ENV, or api_key_cmd to be setup
  api_key_cmd = 'op read "op://Personal/ChatGPT API Key Windows Emacs/credential" --no-newline',
})

I have confirmed that running the command by itself gets the desired secret in both CMD and pwsh, however it appears I run into the following error anytime I try to use the plugin to contact OPENAI:

error.webm

  Error  10:45:53 PM msg_show.lua_error Error executing vim.schedule lua callback: ...ta/Local/nvim-data/lazy/ChatGPT.nvim/lua/chatgpt/api.lua:34: attempt to concatenate field 'OPENAI_API_KEY' (a nil value)
stack traceback:
    ...ta/Local/nvim-data/lazy/ChatGPT.nvim/lua/chatgpt/api.lua:34: in function 'chat_completions'
    ...m-data/lazy/ChatGPT.nvim/lua/chatgpt/flows/chat/base.lua:713: in function 'on_submit'
    .../Local/nvim-data/lazy/ChatGPT.nvim/lua/chatgpt/input.lu
a:72: in function <.../Local/nvim-data/lazy/ChatGPT.nvim/lua/chatgpt/input.lua:60>

Thing I have tried:

It appears that anything I try to do with api_key_cmd does not work, but directly setting the OPENAI_API_KEY env does indeed work.

shakori999 commented 1 year ago

how exactly did you do it? i tried to set the env variable the third option but it turns out it didn't work can you show me where did you write it?

jbpros commented 1 year ago

I'm running into the same issue.

densefog commented 10 months ago

Had the same issue but noticed the docs mentioned:

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.

So I changed the name of my stored credentials in 1password to remove spaces and this worked:

    require("chatgpt").setup {
      api_key_cmd = "op read op://Private/OpenAI_API_Key/password --no-newline",
    }
jbpros commented 10 months ago

Thanks @densefog, that worked for me too. I initially kept escaped double-quotes around that URI and it was still failing. People, make sure you remove double quotes and spaces.

jackMort commented 8 months ago

please reopen if issue still exists in newest version

nikitoshina commented 2 months ago

Ran into the same issue, removed spaces and everything worked!