jackMort / ChatGPT.nvim

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

ChatGPT api_key_cmd = "pass show api/tokens/openai" using pass password-store #454

Open MrCee opened 1 month ago

MrCee commented 1 month ago

I have looked everywhere for an answer, was hoping someone here could steer me in the right direction. Am I incorrect in saying that api_key_cmd should handle the following? the command pass show api/tokens/openai works in terminal and prompts for master password if not currently cached. Should I need to add to this config to capture the master password?

return {
    "jackMort/ChatGPT.nvim",
    event = "VeryLazy",
    dependencies = {
        "MunifTanjim/nui.nvim",
        "nvim-lua/plenary.nvim",
        "folke/trouble.nvim",
        "nvim-telescope/telescope.nvim",
    },
    config = function()
        require("chatgpt").setup({
            api_key_cmd = "pass show api/tokens/openai",
        })
    end,
}

Hoping someone can provide suggestions to try.

MrCee commented 1 month ago

After adjusting my code and system (or at least letting neovim install what it needs to), the below code is what I'm thinking is a 'work-around' for the api_key_cmd we would like to use with any command. Would greatly appreciate any input from anyone who has tried all of this. And let me know if you are getting master passphrase lag where the keyboard is not responsive with characters in your password!

return {
    "jackMort/ChatGPT.nvim",
    event = "VeryLazy",
    dependencies = {
        "MunifTanjim/nui.nvim",
        "nvim-lua/plenary.nvim",
        "folke/trouble.nvim",
        "nvim-telescope/telescope.nvim",
    },
    config = function()
        -- Function to execute the shell command and capture its output
        local function get_api_key()
            local handle = io.popen("pass show api/tokens/openai | head -n 1 | tr -d '\n'")
            local api_key = handle:read("*a")
            handle:close()
            return api_key:gsub("%s+", "") -- trim any extra whitespace
        end

        -- Get the API key
        local api_key = get_api_key()

        -- Set the environment variable
        os.execute("export OPENAI_API_KEY=" .. api_key)

        -- Setup ChatGPT with the API key 
        require("chatgpt").setup({
            api_key_cmd = "echo " .. api_key,
        })
    end,
}
MrCee commented 1 month ago

The best I can come up with so far is the following. I've set event = "VimEnter" which will prompt for master passphrase on opening nvim, my screen was getting corrupted using VeryLazy. I have also removed the os.execute line to set the env variable in the lua file, it looks like api_key_cmd is doing this now. So this plugin is working as expected, but would like to know if anyone has a solution to why entering the master passphrase, the keyboard isn't just slow, sometimes and often it will completely ignore keystrokes.

return {
    "jackMort/ChatGPT.nvim",
    event = "VimEnter",
    dependencies = {
        "MunifTanjim/nui.nvim",
        "nvim-lua/plenary.nvim",
        "folke/trouble.nvim",
        "nvim-telescope/telescope.nvim",
    },
    config = function()
        -- Function to execute the shell command and capture its output
        local function get_api_key()
            local handle = io.popen("pass show api/tokens/openai | head -n 1 | tr -d '\n'")
            local api_key = handle:read("*a")
            handle:close()
            return api_key:gsub("%s+", "") -- trim any extra whitespace
        end

        -- Get the API key
        local api_key = get_api_key()

        -- Setup ChatGPT with the API key
        require("chatgpt").setup({
            api_key_cmd = "echo " .. api_key,
        })
    end,
}

It does work if you carefully watch each character asterisk during input.