jackMort / ChatGPT.nvim

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

FR: option to set the default system prompt #399

Open thiswillbeyourgithub opened 4 months ago

thiswillbeyourgithub commented 4 months ago

Hi,

I tried modifying the default system prompt to something different than "You are a helpful assistant" by modifying system_windows.lua but it doesn't seem to work and stops LazyNvim from updating your package.

Is there a way to set the default systeme prompt system wide? I would really like something more concise and truthful.

Thanks!

thiswillbeyourgithub commented 3 months ago

Still nothing?

thiswillbeyourgithub commented 3 months ago

I got fed up with this so I made a dirty workaround.

Here's a lua function to put in lazy.lua to launch :ChatGPT while replacing the system prompt, adding a shortcut ctrl+alt+s to replace automatically the prompt

function openChatGPT()
    -- load and open ChatGPT
    vim.cmd("lua require('chatgpt')")
    vim.cmd("exe \"normal :ChatGPT\\<CR>\"")

    local default_prompt = [[You are my best assistant. You answer my questions using as few words as possible while still remaining friendly and helpful. This is very important to me as I am in a hurry and a bit stressed.
Guidelines:
* If you are certain my question is too vague, ask me first a few clarification questions but answer directly anyway as best as you can.
* Prefer answering using indented markdown format.
* Use code blocks for code but not to wrap your entire answer.
* Answer in the same language as the question.
* Don't acknowledge those rules and answer straight away.
* It is really important that you remain EXTREMELY concise, don't even start your answer by a useless polite sentence.
* If my question is about computer science, know that I'm good at python, running linux, want to understand stuff. I'm not a noob.
* If I'm not asking a question but just writing a concept, write me a summary about it.
Thank you.]]
    function setDefaultPrompt()
        vim.cmd("exe \"normal \\<Esc>\\<C-S>ggVGx\"")
        for line in default_prompt:gmatch("([^\n]*)\n?") do
            vim.api.nvim_put({line}, 'l', false, true)
        end
        vim.cmd("sleep 5ms")
        vim.cmd("exe \"normal \\<C-S>\"")
    end

    -- replace the current system prompt directly
    -- setDefaultPrompt()

    -- make sure that :q actually quits the terminal instead of just the chat window
    vim.api.nvim_set_keymap('n', 'Q', ':normal :qa!<CR>', { noremap = true, silent = true })
    vim.api.nvim_set_keymap('n', ':q', ':normal :qa!<CR>', { noremap = true, silent = true })

    -- add shortcut alt+shift+s to populate the prompt, so that it's easy to do after creating a new session
    vim.api.nvim_set_keymap('n', '<A-s>', '<Cmd>lua setDefaultPrompt()<CR>', { noremap = true, silent = true })

    -- workaround so that resizing the window reloads the UI
    vim.api.nvim_create_autocmd("VimResized", {
        pattern = "*",
        callback = function()
            vim.cmd("exe \"normal \\<C-S>\"")
            vim.cmd("sleep 5ms")
            vim.cmd("exe \"normal \\<C-S>\"")
        end,
    })

    -- create a new session and reuse the current prompt
    function newSessionReusePrompt()
        vim.cmd("exe \"normal \\<C-S>\"")
        vim.cmd("sleep 10ms")
        vim.cmd('normal gg0vG$"ay')
        vim.cmd("exe \"normal \\<C-S>\"")
        vim.cmd("sleep 10ms")
        vim.cmd('normal ')  -- <- this appears in nvim as ^N but means ctrl+N to type it in the editor do ctrl+v then ctrl+N
        vim.cmd("sleep 10ms")
        vim.cmd("exe \"normal \\<C-S>\"")
        vim.cmd("sleep 10ms")
        vim.cmd('normal gg0vG$x"ap')
        vim.cmd("sleep 10ms")
        vim.cmd("exe \"normal \\<C-S>\"")
    end

    ---- create a new session with the default prompt
    function newSessionDefaultPrompt()
        vim.cmd("sleep 10ms")
        vim.cmd('exe \"normal \\<C-n>\"')
        vim.cmd("sleep 10ms")
        setDefaultPrompt()
    end

    -- Choose one or the other
    --vim.api.nvim_set_keymap('n', '<A-n>', '<Cmd>lua newSessionReusePrompt()<CR>', { noremap = true, silent = true })
    -- OR
    vim.api.nvim_set_keymap('n', '<A-n>', '<Cmd>lua newSessionDefaultPrompt()<CR>', { noremap = true, silent = true })

    -- optionnal start directly a new session
    --newSessionReusePrompt()

    -- shift+enter should enter the message
    function pressEnter()
        vim.cmd("exe \"normal \\<Esc>\"")
        vim.cmd("sleep 10ms")
        vim.cmd("exe \"normal \\<CR>\"")
        print("done")
    end
    vim.api.nvim_set_keymap('i', '<S-Enter>', '<Cmd>lua pressEnter()<CR>', { noremap = true, silent = true })

end

To try :

nvim /dev/null -c "lua openChatGPT()"

My i3 shortcut:

kitty --title="windowed terminal" -- nvim /dev/null -c "lua openChatGPT()"