Code Monkey home page Code Monkey logo

chatgpt.nvim's Introduction

ChatGPT.nvim

GitHub Workflow Status Lua

ChatGPT is a Neovim plugin that allows you to interact with OpenAI's GPT-3 language model. With ChatGPT, you can ask questions and get answers from GPT-3 in real-time.

preview image

Installation

  • Make sure you have curl installed.
  • Set environment variable called $OPENAI_API_KEY which you can obtain here.
-- 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

{
  yank_register = "+",
  edit_with_instructions = {
    diff = false,
    keymaps = {
      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>" },
      close_normal = { "<Esc>" },
      yank_last = "<C-y>",
      yank_last_code = "<C-k>",
      scroll_up = "<C-u>",
      scroll_down = "<C-d>",
      toggle_settings = "<C-o>",
      new_session = "<C-n>",
      cycle_windows = "<Tab>",
      select_session = "<Space>",
      rename_session = "r",
      delete_session = "d",
    },
  },
  popup_layout = {
    relative = "editor",
    position = "50%",
    size = {
      height = "80%",
      width = "80%",
    },
  },
  popup_window = {
    filetype = "chatgpt",
    border = {
      highlight = "FloatBorder",
      style = "rounded",
      text = {
        top = " ChatGPT ",
      },
    },
    win_options = {
      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>",
  },
  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 = {},
  predefined_chat_gpt_prompts = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv",
}

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-002 model (GPT 3.5 fine-tuned for coding).

You can map it usig 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 cofig 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-c> [Chat] 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-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>), settigs can be modified by pressing Enter on the related config. Settings are saved across sections

"Buy Me A Coffee"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.