frankroeder / parrot.nvim

parrot.nvim 🦜 - the plugin that brings stochastic parrots to Neovim. This is a gp.nvim-fork focused on simplicity.
Other
219 stars 14 forks source link

Working configuration for lazyvim users #3

Closed tofeeh closed 5 months ago

tofeeh commented 6 months ago

Just in case someone is using lazyvim, here is my configuration.

return {
  {
    "frankroeder/parrot.nvim",
    -- OPTIONAL dependency
    dependencies = { "fzf-lua" },
    -- OPTIONAL condition
    cond = os.getenv("OPENAI_API_KEY") ~= nil or os.getenv("PERPLEXITY_API_KEY") ~= nil,
    config = function()
      local cmd_prefix = "Prt"
      require("parrot").setup({
        providers = {
          pplx = {
            api_key = os.getenv("PERPLEXITY_API_KEY"),
            -- OPTIONAL
            -- gpg command
            -- api_key = { "gpg", "--decrypt", vim.fn.expand("$HOME") .. "/pplx_api_key.txt.gpg"  },
            -- macOS security tool
            -- api_key = { "/usr/bin/security", "find-generic-password", "-s pplx-api-key", "-w" },
          },
        },
        cmd_prefix = cmd_prefix,
        chat_conceal_model_params = false,
        hooks = {
          Complete = function(prt, params)
            local template = [[
        I have the following code from {{filename}}:

        ```{{filetype}}
        {{selection}}
    Please finish the code above carefully and logically.
    Respond just with the snippet of code that should be inserted."
    ]]
        local agent = prt.get_command_agent()
        prt.Prompt(params, prt.Target.append, nil, agent.model, template, agent.system_prompt, agent.provider)
      end,
      Explain = function(prt, params)
        local template = [[
    Explain the following code from {{filename}}:

    ```{{filetype}}
    {{selection}}
    ```

    Use the markdown format with codeblocks.
    A brief explanation of what the code above is doing:
    ]]
        local agent = prt.get_chat_agent()
        prt.logger.info("Explaining selection with agent: " .. agent.name)
        prt.Prompt(params, prt.Target.popup, nil, agent.model, template, agent.system_prompt, agent.provider)
      end,
      FixBugs = function(prt, params)
        local template = [[
    You are an expert in {{filetype}}.
    Fix bugs in the below code from {{filename}} carefully and logically:

    ```{{filetype}}
    {{selection}}
    ```

    Fixed code:
    ]]
        local agent = prt.get_command_agent()
        prt.logger.info("Fixing bugs in selection with agent: " .. agent.name)
        prt.Prompt(params, prt.Target.popup, nil, agent.model, template, agent.system_prompt, agent.provider)
      end,
      Optimize = function(prt, params)
        local template = [[
    You are an expert in {{filetype}}.
    Optimize the following code from {{filename}}:

    ```{{filetype}}
    {{selection}}
    ```

    Optimized code:
    ]]
        local agent = prt.get_command_agent()
        prt.logger.info("Optimizing selection with agent: " .. agent.name)
        prt.Prompt(params, prt.Target.popup, nil, agent.model, template, agent.system_prompt, agent.provider)
      end,
      UnitTests = function(prt, params)
        local template = [[
    I have the following code from {{filename}}:

    ```{{filetype}}
    {{selection}}
    ```

    Please respond by writing table driven unit tests for the code above.
    ]]
        local agent = prt.get_command_agent()
        prt.logger.info("Creating unit tests for selection with agent: " .. agent.name)
        prt.Prompt(params, prt.Target.enew, nil, agent.model, template, agent.system_prompt, agent.provider)
      end,
      ProofReader = function(prt, params)
        local chat_system_prompt = [[
    I want you to act as a proofreader. I will provide you with texts and
    I would like you to review them for any spelling, grammar, or
    punctuation errors. Once you have finished reviewing the text,
    provide me with any necessary corrections or suggestions to improve the
    text. Highlight the corrections with markdown bold or italics style.
    ]]
        local agent = prt.get_chat_agent()
        prt.logger.info("Proofreading selection with agent: " .. agent.name)
        prt.cmd.ChatNew(params, agent.model, chat_system_prompt)
      end,
      Debug = function(prt, params)
        local template = [[
    I want you to act as {{filetype}} expert.
    Review the following code, carefully examine it, and report potential
    bugs and edge cases alongside solutions to resolve them.
    Keep your explanation short and to the point:

    ```{{filetype}}
    {{selection}}
    ```
    ]]
        local agent = prt.get_chat_agent()
        prt.logger.info("Debugging selection with agent: " .. agent.name)
        prt.Prompt(params, prt.Target.enew, nil, agent.model, template, agent.system_prompt, agent.provider)
      end,
    },
  })
end,
keys = {
  -- Normal and Insert mode mappings
  { "<C-g>c", "<cmd>PrtChatNew<cr>", mode = { "n", "i" }, desc = "New Chat" },
  { "<C-g>t", "<cmd>PrtChatToggle tabnew<cr>", mode = { "n", "i" }, desc = "Toggle Popup Chat" },
  { "<C-g>f", "<cmd>PrtChatFinder<cr>", mode = { "n", "i" }, desc = "Chat Finder" },
  { "<C-g>r", "<cmd>PrtRewrite<cr>", mode = { "n", "i" }, desc = "Inline Rewrite" },
  { "<C-g>a", "<cmd>PrtAppend<cr>", mode = { "n", "i" }, desc = "Append" },
  { "<C-g>o", "<cmd>PrtPrepend<cr>", mode = { "n", "i" }, desc = "Prepend" },

  -- Visual mode mappings
  { "<C-g>c", "<cmd>PrtChatNew<cr>", mode = "v", desc = "Visual Chat New" },
  { "<C-g>r", "<cmd>PrtRewrite<cr>", mode = "v", desc = "Visual Rewrite" },
  { "<C-g>a", "<cmd>PrtAppend<cr>", mode = "v", desc = "Visual Append" },
  { "<C-g>o", "<cmd>PrtPrepend<cr>", mode = "v", desc = "Visual Prepend" },
  { "<C-g>e", "<cmd>PrtEnew<cr>", mode = "v", desc = "Visual Enew" },

  -- Additional mappings
  { "<C-g>s", "<cmd>PrtStop<cr>", mode = { "n", "i", "v", "x" }, desc = "Stop" },
  { "<C-g>i", "<cmd>PrtComplete<cr>", mode = { "n", "i", "v", "x" }, desc = "Complete the visual selection" },

  -- Context and agent/provider selection mappings
  { "<C-g>x", "<cmd>PrtContext<cr>", mode = "n", desc = "Open file with custom context" },
  { "<C-g>n", "<cmd>PrtAgent<cr>", mode = "n", desc = "Select agent or show info" },
  { "<C-g>p", "<cmd>PrtProvider<cr>", mode = "n", desc = "Select provider or show info" },
},

}, }



P.S. I get rid of refactoring the keymap as it obscures readability. This should be a human-readable config file, not an efficient code.
destnguyxn2 commented 5 months ago

do you know how to config ollama for provider?

frankroeder commented 5 months ago

@destnguyxn ollama is supported out of the box! You need to install ollama and have the server running on your system. Here is the default config part: https://github.com/frankroeder/parrot.nvim/blob/986759a0f102666ae836bb7a317872324adda2ce/lua/parrot/config.lua#L22

After that, you just need to switch the provider (PrtProvider) and select your ollama agent (PrtAgent). parrot.nvim comes with a bunch of agent configurations: https://github.com/frankroeder/parrot.nvim/blob/986759a0f102666ae836bb7a317872324adda2ce/lua/parrot/agents.lua#L23 but you are free to add your own. I hope that helps until I have time to write a proper documentation.