Skip to content

ChatGPT Neovim Plugin: Effortless Natural Language Generation with OpenAI's ChatGPT API

License

Notifications You must be signed in to change notification settings

skdziwak/ChatGPT.nvim

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ChatGPT.nvim

GitHub Workflow Status Lua

ChatGPT is a Neovim plugin that allows you to effortlessly utilize the OpenAI ChatGPT API, empowering you to generate natural language responses from OpenAI's ChatGPT directly within the editor in response to your inquiries.

preview image

Features

  • Interactive Q&A: Engage in interactive question-and-answer sessions with the powerful gpt model (ChatGPT) using an intuitive interface.
  • Persona-based Conversations: Explore various perspectives and have conversations with different personas by selecting prompts from Awesome ChatGPT Prompts.
  • Code Editing Assistance: Enhance your coding experience with an interactive editing window powered by the gpt model, offering instructions tailored for coding tasks.
  • Code Completion: Enjoy the convenience of code completion similar to GitHub Copilot, leveraging the capabilities of the gpt model to suggest code snippets and completions based on context and programming patterns.
  • Customizable Actions: Execute a range of actions utilizing the gpt model, such as grammar correction, translation, keyword generation, docstring creation, test addition, code optimization, summarization, bug fixing, code explanation, Roxygen editing, and code readability analysis. Additionally, you can define your own custom actions using a JSON file.

For a comprehensive understanding of the extension's functionality, you can watch a plugin showcase video

Installation

  • Make sure you have curl installed.
  • Get an API key from OpenAI, which you can obtain here.

The OpenAI API key can be provided in one of the following two ways:

  1. In the configuration option api_key_cmd, provide the path and arguments to an executable that returns the API key via stdout.
  2. Setting it via an environment variable called $OPENAI_API_KEY.

Custom OpenAI API host with the configuration option api_host_cmd or environment variable called $OPEN_API_HOST. It's useful if you can't access OpenAI driectly

-- Packer
use({
  "jackMort/ChatGPT.nvim",
    config = function()
      require("chatgpt").setup()
    end,
    requires = {
      "MunifTanjim/nui.nvim",
      "nvim-lua/plenary.nvim",
      "nvim-telescope/telescope.nvim"
    }
})

-- Lazy
{
  "jackMort/ChatGPT.nvim",
    event = "VeryLazy",
    config = function()
      require("chatgpt").setup()
    end,
    dependencies = {
      "MunifTanjim/nui.nvim",
      "nvim-lua/plenary.nvim",
      "nvim-telescope/telescope.nvim"
    }
}

Configuration

ChatGPT.nvim comes with the following defaults, you can override them by passing config as setup param

{
    api_key_cmd = nil,
    yank_register = "+",
    edit_with_instructions = {
      diff = false,
      keymaps = {
        close = "<C-c>",
        accept = "<C-y>",
        toggle_diff = "<C-d>",
        toggle_settings = "<C-o>",
        cycle_windows = "<Tab>",
        use_output_as_input = "<C-i>",
      },
    },
    chat = {
      welcome_message = WELCOME_MESSAGE,
      loading_text = "Loading, please wait ...",
      question_sign = "",
      answer_sign = "",
      max_line_length = 120,
      sessions_window = {
        border = {
          style = "rounded",
          text = {
            top = " Sessions ",
          },
        },
        win_options = {
          winhighlight = "Normal:Normal,FloatBorder:FloatBorder",
        },
      },
      keymaps = {
        close = { "<C-c>" },
        yank_last = "<C-y>",
        yank_last_code = "<C-k>",
        scroll_up = "<C-u>",
        scroll_down = "<C-d>",
        new_session = "<C-n>",
        cycle_windows = "<Tab>",
        cycle_modes = "<C-f>",
        select_session = "<Space>",
        rename_session = "r",
        delete_session = "d",
        draft_message = "<C-d>",
        toggle_settings = "<C-o>",
        toggle_message_role = "<C-r>",
        toggle_system_role_open = "<C-s>",
        stop_generating = "<C-x>",
      },
    },
    popup_layout = {
      default = "center",
      center = {
        width = "80%",
        height = "80%",
      },
      right = {
        width = "30%",
        width_settings_open = "50%",
      },
    },
    popup_window = {
      border = {
        highlight = "FloatBorder",
        style = "rounded",
        text = {
          top = " ChatGPT ",
        },
      },
      win_options = {
        wrap = true,
        linebreak = true,
        foldcolumn = "1",
        winhighlight = "Normal:Normal,FloatBorder:FloatBorder",
      },
      buf_options = {
        filetype = "markdown",
      },
    },
    system_window = {
      border = {
        highlight = "FloatBorder",
        style = "rounded",
        text = {
          top = " SYSTEM ",
        },
      },
      win_options = {
        wrap = true,
        linebreak = true,
        foldcolumn = "2",
        winhighlight = "Normal:Normal,FloatBorder:FloatBorder",
      },
    },
    popup_input = {
      prompt = "",
      border = {
        highlight = "FloatBorder",
        style = "rounded",
        text = {
          top_align = "center",
          top = " Prompt ",
        },
      },
      win_options = {
        winhighlight = "Normal:Normal,FloatBorder:FloatBorder",
      },
      submit = "<C-Enter>",
      submit_n = "<Enter>",
      max_visible_lines = 20
    },
    settings_window = {
      border = {
        style = "rounded",
        text = {
          top = " Settings ",
        },
      },
      win_options = {
        winhighlight = "Normal:Normal,FloatBorder:FloatBorder",
      },
    },
    openai_params = {
      model = "gpt-3.5-turbo",
      frequency_penalty = 0,
      presence_penalty = 0,
      max_tokens = 300,
      temperature = 0,
      top_p = 1,
      n = 1,
    },
    openai_edit_params = {
      model = "code-davinci-edit-001",
      temperature = 0,
      top_p = 1,
      n = 1,
    },
    actions_paths = {},
    show_quickfixes_cmd = "Trouble quickfix",
    predefined_chat_gpt_prompts = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv",
  }

Secrets Management

Providing the OpenAI API key via an environment variable is dangerous, as it leaves the API key easily readable by any process that can access the environment variables of other processes. In addition, it encourages the user to store the credential in clear-text in a configuration file.

As an alternative to providing the API key via the OPENAI_API_KEY environment variable, the user is encouraged to use the api_key_cmd configuration option. The api_key_cmd configuration option takes a string, which is executed at startup, and whose output is used as the API key.

The following configuration would use 1Passwords CLI, op, to fetch the API key from the credential field of the OpenAI entry.

require("chatgpt").setup({
    api_key_cmd = "op read op://private/OpenAI/credential --no-newline"
})

The following configuration would use GPG to decrypt a local file containing the API key

local home = vim.fn.expand("$HOME")
require("chatgpt").setup({
    api_key_cmd = "gpg --decrypt " .. home .. "/secret.txt.gpg"
})

Usage

Plugin exposes following commands:

ChatGPT

ChatGPT command which opens interactive window using the gpt-3.5-turbo model. (also known as ChatGPT)

ChatGPTActAs

ChatGPTActAs command which opens a prompt selection from Awesome ChatGPT Prompts to be used with the gpt-3.5-turbo model.

preview image

ChatGPTEditWithInstructions

ChatGPTEditWithInstructions command which opens interactive window to edit selected text or whole window using the code-davinci-edit-001 model (GPT 3.5 fine-tuned for coding).

You can map it using the Lua API, e.g. using which-key.nvim:

local chatgpt = require("chatgpt")
wk.register({
    p = {
        name = "ChatGPT",
        e = {
            function()
                chatgpt.edit_with_instructions()
            end,
            "Edit with instructions",
        },
    },
}, {
    prefix = "<leader>",
    mode = "v",
})

preview image

ChatGPTRun

ChatGPTRun [action] command which runs specific actions -- see actions.json file for a detailed list. Available actions are:

  1. grammar_correction
  2. translate
  3. keywords
  4. docstring
  5. add_tests
  6. optimize_code
  7. summarize
  8. fix_bugs
  9. explain_code
  10. roxygen_edit
  11. code_readability_analysis -- see demo

All the above actions are using gpt-3.5-turbo model.

It is possible to define custom actions with a JSON file. See actions.json for an example. The path of custom actions can be set in the config (see actions_paths field in the config example above).

An example of custom action may look like this: (# marks comments)

{
  "action_name": {
    "type": "chat", # or "completion" or "edit"
    "opts": {
      "template": "A template using possible variable: {{filetype}} (neovim filetype), {{input}} (the selected text) an {{argument}} (provided on the command line)",
      "strategy": "replace", # or "display" or "append" or "edit"
      "params": { # parameters according to the official OpenAI API
        "model": "gpt-3.5-turbo", # or any other model supported by `"type"` in the OpenAI API, use the playground for reference
        "stop": [
          "```" # a string used to stop the model
        ]
      }
    },
    "args": {
      "argument": {
          "type": "strig",
          "optional": "true",
          "default": "some value"
      }
    }
  }
}

The edit strategy consists in showing the output side by side with the iput and available for further editing requests. For now, edit strategy is implemented for chat type only.

The display strategy shows the output in a float window.

append and replace modify the text directly in the buffer.

Interactive popup

When using ChatGPT and ChatGPTEditWithInstructions, the following keybindings are available:

  • <C-Enter> [Both] to submit.
  • <C-y> [Both] to copy/yank last answer.
  • <C-o> [Both] Toggle settings window.
  • <Tab> [Both] Cycle over windows.
  • <C-f> [Chat] Cycle over modes (center, stick to right).
  • <C-c> [Both] to close chat window.
  • <C-u> [Chat] scroll up chat window.
  • <C-d> [Chat] scroll down chat window.
  • <C-k> [Chat] to copy/yank code from last answer.
  • <C-n> [Chat] Start new session.
  • <C-d> [Chat] draft message (create message without submitting it to server)
  • <C-r> [Chat] switch role (switch between user and assistant role to define a workflow)
  • <C-s> [Both] Toggle system message window.
  • <C-i> [Edit Window] use response as input.
  • <C-d> [Edit Window] view the diff between left and right panes and use diff-mode commands

When the setting window is opened (with <C-o>), settings can be modified by pressing Enter on the related config. Settings are saved across sections

Whichkey plugin mappings

Add these to your whichkey plugin mappings for convenient binds

c = {
  name = "ChatGPT",
  c = { "<cmd>ChatGPT<CR>", "ChatGPT" },
  e = { "<cmd>ChatGPTEditWithInstruction<CR>", "Edit with instruction" },
  g = { "<cmd>ChatGPTRun grammar_correction<CR>", "Grammar Correction" },
  t = { "<cmd>ChatGPTRun translate<CR>", "Translate" },
  k = { "<cmd>ChatGPTRun keywords<CR>", "Keywords" },
  d = { "<cmd>ChatGPTRun docstring<CR>", "Docstring" },
  a = { "<cmd>ChatGPTRun add_tests<CR>", "Add Tests" },
  o = { "<cmd>ChatGPTRun optimize_code<CR>", "Optimize Code" },
  s = { "<cmd>ChatGPTRun summarize<CR>", "Summarize" },
  f = { "<cmd>ChatGPTRun fix_bugs<CR>", "Fix Bugs" },
  x = { "<cmd>ChatGPTRun explain_code<CR>", "Explain Code" },
  r = { "<cmd>ChatGPTRun roxygen_edit<CR>", "Roxygen Edit" },
  l = { "<cmd>ChatGPTRun code_readability_analysis<CR>", "Code Readability Analysis" },
  },

"Buy Me A Coffee"

About

ChatGPT Neovim Plugin: Effortless Natural Language Generation with OpenAI's ChatGPT API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 100.0%