summaryrefslogtreecommitdiff
path: root/README.md
blob: 6cb5e051f07d2aeb007cbafd2912b207acbbf43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# vim-ai

Complete text and chat with GPT in Vim/Neovim using OpenAI.

## Features

- Generate text or code, answer questions
- Edit selected text in-place
- Interactive conversation with ChatGPT

![vim-ai demo](./demo.gif)

## Installation

vim-ai requires Vim/Neovim compiled with python3 support and the [openai-python](https://github.com/openai/openai-python) library.

```sh
# configure openai api key https://platform.openai.com/account/api-keys
echo "YOUR_OPENAI_API_KEY" > ~/.config/openai.token

# alternatively using environment variable
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
```

Add plugin to your `.vimrc` using `vim-plug`:

```vim
" ./install.sh script will automatically install openai-python
Plug 'madox2/vim-ai', { 'do': './install.sh' }
```

## Usage

### :AI

`:AI` - complete the text on the current line

`:AI {prompt}` - complete the prompt

`(visual selection) :AI` - complete the selection

`(visual selection) :AI {instruction}` - complete the selection using the instruction

### :AIEdit

`(visual selection)? :AIEdit` - edit the current line or the selection

`(visual selection)? :AIEdit {instruction}` - edit the current line or the selection using the instruction

### :AIChat


`:AIChat` - continue or start a new conversation.

`(visual selection)? :AIChat {instruction}?` - start a new conversation given the selection, the instruction or both

Press `Ctrl-c` to cancel completion.

#### Custom conversation prompts

You can edit and save the conversation to an `.aichat` file and restore it later.
This allows you to create re-usable custom prompts. For example:

```
# ./refactoring-prompt.aichat

>>> system

You are a Clean Code expert, I have the following code, please refactor it in a more clean and concise way so that my colleagues can maintain the code more easily. Also, explain why you want to refactor the code so that I can add the explanation to the Pull Request.

>>> user

[attach code]

```

Supported chat roles are **`>>> system`**, **`>>> user`** and **`<<< assistant`**

## Configuration

### Key bindings

Map keys in your `.vimrc` to trigger `:AI` command.

```vim
" complete text on the current line or in visual selection
nnoremap <leader>a :AI<CR>
xnoremap <leader>a :AI<CR>

" edit text with a custom prompt
xnoremap <leader>s :AIEdit fix grammar and spelling<CR>
nnoremap <leader>s :AIEdit fix grammar and spelling<CR>

" trigger chat
xnoremap <leader>c :AIChat<CR>
nnoremap <leader>c :AIChat<CR>
```

### Completion configuration

Request to the OpenAI API can be configured for each command.
To customize the default configuration, initialize the config variable with a selection of options.  For example:

```vim
let g:vim_ai_chat = {
\  "options": {
\    "model": "gpt-4",
\    "temperature": 0.2,
\  },
\}
```

Below are listed available options along with default values:

```vim
" :AI
" - https://platform.openai.com/docs/api-reference/completions
let g:vim_ai_complete = {
\  "options": {
\    "model": "text-davinci-003",
\    "max_tokens": 1000,
\    "temperature": 0.1,
\    "request_timeout": 10,
\  },
\}

" :AIEdit
" - https://platform.openai.com/docs/api-reference/completions
let g:vim_ai_edit = {
\  "options": {
\    "model": "text-davinci-003",
\    "max_tokens": 1000,
\    "temperature": 0.1,
\    "request_timeout": 10,
\  },
\}

" :AIChat
" - https://platform.openai.com/docs/api-reference/chat
let g:vim_ai_chat = {
\  "options": {
\    "model": "gpt-3.5-turbo",
\    "max_tokens": 1000,
\    "temperature": 1,
\    "request_timeout": 10,
\  },
\}
```

### Custom commands

To customize and re-use prompts it is useful to put some context in it. You can do it by prepending text to AI commands.

```vim
command! -range -nargs=? AITranslate <line1>,<line2>call AIEditRun(<range>, "Translate to English: " . <q-args>)

command! -range -nargs=? AICode <line1>,<line2>call AIRun(<range>, "Programming syntax is " . &filetype . ", " . <q-args>)

" available functions are: AIRun, AIEditRun, AIChatRun
```


## Important Disclaimer

**Accuracy**: GPT is good at producing text and code that looks correct at first glance, but may be completely wrong. Be sure to thoroughly review, read and test all output generated by this plugin!

**Privacy**: This plugin sends text to OpenAI when generating completions and edits. Therefore, do not use it on files containing sensitive information.

## License

[MIT License](https://github.com/madox2/vim-ai/blob/main/LICENSE)