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

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

Open
MrCee opened this issue Jul 10, 2024 · 2 comments
Open

Comments

@MrCee
Copy link

MrCee commented Jul 10, 2024

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 MrCee changed the title api_key_cmd = "pass show api/tokens/openai" using pass password-store ChatGPT api_key_cmd = "pass show api/tokens/openai" using pass password-store Jul 10, 2024
@MrCee
Copy link
Author

MrCee commented Jul 10, 2024

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

MrCee commented Jul 12, 2024

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.

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

1 participant