johnor / nvim-magic

:genie: Pluggable framework for using AI code assistance in Neovim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FORK

This is a fork from the original repo. It seemed to have stopped development. So I maintain this now.

Things I have added:

  • Chat
  • Support for chat based models (gpt-3.5-turbo, gpt-4)

Environment variables

This uses the OPEN_API_KEY from the enviroment. It is supposed to be set to your OpenAI key.

Default model

The default model used is gpt-3.5-turbo. Change that in this file, if you want:

lua/nvim-magic-openai/init.lua

nvim-magic

ci

A pluggable framework for integrating AI code assistance into Neovim. The goals are to make using AI code assistance unobtrusive, and to make it easy to create and share new flows that use AI code assistance. Go to quickstart for how to install. It currently works with OpenAI Codex.

Features

  • Completion
  • Docstring
  • Alteration
  • Chat

When using chat, the window you focus on when you start the chat will be selected for the rest of the session. Until nvim is closed or until you manually reset the chat (mcc). If text is selected in the visual, this information is passed along the question but hidden from the convo-window. See the demo gif below.

Chat (<Leader>mcc)

Example of some chatting

Completion (<Leader>mcs)

Example of Python script being generated from a docstring

Generating a docstring (<Leader>mds)

Example of Python function having a docstring generated

Asking for an alteration (<Leader>mas)

Example of Python function being altered

Quickstart

Prerequisites

  • latest stable version of Neovim (nightly may work as well)
  • curl
  • OpenAI API key

Installation

-- using packer.nvim
use({
	'jameshiew/nvim-magic',
	config = function()
		require('nvim-magic').setup()
	end,
	requires = {
		'nvim-lua/plenary.nvim',
		'MunifTanjim/nui.nvim'
	}
})

See docs/config.md if you want to override the default configuration e.g. to turn off the default keymaps, or use a different OpenAI engine than the default one (davinci-codex). Your OpenAI account might not have access to davinci-codex if it is not in the OpenAI Codex private beta (as of 2022-02-02).

Your API key should be made available to your Neovim session in an environment variable OPENAI_API_KEY. See docs/openai.md for more details. Note that API calls may be charged for by OpenAI depending on the engine used.

 export OPENAI_API_KEY='your-api-key-here'

Keymaps

These flows have keymaps set by default for visual mode selections (though you can disable this by passing use_default_keymap = false in the setup config).

You can map your own key sequences to the predefined <Plug>s if you don't want to use the default keymaps.

<Plug> default keymap mode action
<Plug>nvim-magic-append-completion <Leader>mcs visual Fetch and append completion
<Plug>nvim-magic-suggest-alteration <Leader>mss visual Ask for an alteration to the selected text
<Plug>nvim-magic-suggest-docstring <Leader>mds visual Generate a docstring
<Plug>nvim-magic-suggest-chat <Leader>mcc visual Chat, ask questions. Keep the window.
<Plug>nvim-magic-suggest-chat-reset <Leader>mcr visual Chat, reset history. You can start all over.

About

:genie: Pluggable framework for using AI code assistance in Neovim

License:MIT License


Languages

Language:Lua 98.0%Language:Mustache 2.0%