summaryrefslogtreecommitdiff
path: root/doc/vim-ai.txt
blob: 03c7e4d6c7422b185148ffb4f77af49674cada27 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
*vim-ai.txt*  Complete text using OpenAI API.

Author:  Martin Bielik <https://madox2.poriadne.sk/>
License: see https://github.com/madox2/vim-ai/blob/main/LICENSE

INTRODUCTION                                    *vim-ai*

This plugin can be used to generate code, edit text, brainstorm ideas,
translate, etc.

COMMANDS                                        *vim-ai-commands*

To set-up key bindings and examples see:
https://github.com/madox2/vim-ai

                                                *:AI*

:AI {prompt}                        complete the prompt
<selection> :AI                     complete the selection
<selection> :AI {instruction}       complete the selection using the instruction

Options: >
  let s:initial_complete_prompt =<< trim END
  >>> system

  You are a general assistant.
  Answer shortly, consisely and only what you are asked.
  Do not provide any explanantion or comments if not requested.
  If you answer in a code, do not wrap it in markdown code block.
  END

  let g:vim_ai_complete_default = {
  \  "engine": "chat",
  \  "options": {
  \    "model": "gpt-4o",
  \    "endpoint_url": "https://api.openai.com/v1/chat/completions",
  \    "max_tokens": 0,
  \    "max_completion_tokens": 0,
  \    "temperature": 0.1,
  \    "request_timeout": 20,
  \    "stream": 1,
  \    "enable_auth": 1,
  \    "token_file_path": "",
  \    "selection_boundary": "#####",
  \    "initial_prompt": s:initial_complete_prompt,
  \  },
  \  "ui": {
  \    "paste_mode": 1,
  \  },
  \}

Check OpenAI docs for more information:
https://platform.openai.com/docs/api-reference/completions

                                                *:AIEdit*

<selection>? :AIEdit                edit the current line or the selection
<selection>? :AIEdit {instruction}  edit the current line or the selection using
                                    the instruction

Options: >
  let s:initial_complete_prompt =<< trim END
  >>> system

  You are a general assistant.
  Answer shortly, consisely and only what you are asked.
  Do not provide any explanantion or comments if not requested.
  If you answer in a code, do not wrap it in markdown code block.
  END

  let g:vim_ai_edit_default = {
  \  "engine": "chat",
  \  "options": {
  \    "model": "gpt-4o",
  \    "endpoint_url": "https://api.openai.com/v1/chat/completions",
  \    "max_tokens": 0,
  \    "max_completion_tokens": 0,
  \    "temperature": 0.1,
  \    "request_timeout": 20,
  \    "stream": 1,
  \    "enable_auth": 1,
  \    "token_file_path": "",
  \    "selection_boundary": "#####",
  \    "initial_prompt": s:initial_complete_prompt,
  \  },
  \  "ui": {
  \    "paste_mode": 1,
  \  },
  \}

Check OpenAI docs for more information:
https://platform.openai.com/docs/api-reference/completions

                                                *:AIChat*

:AIChat                             continue or start a new conversation.
<selection>? :AIChat {instruction}? start a new conversation given the selection,
                                    the instruction or both

Options: >
  let s:initial_chat_prompt =<< trim END
  >>> system

  You are a general assistant.
  If you attach a code block add syntax type after ``` to enable syntax highlighting.
  END

  let g:vim_ai_chat = {
  \  "options": {
  \    "model": "gpt-4o",
  \    "max_tokens": 0,
  \    "max_completion_tokens": 0,
  \    "endpoint_url": "https://api.openai.com/v1/chat/completions",
  \    "temperature": 1,
  \    "request_timeout": 20,
  \    "stream": 1,
  \    "enable_auth": 1,
  \    "token_file_path": "",
  \    "selection_boundary": "",
  \    "initial_prompt": s:initial_chat_prompt,
  \  },
  \  "ui": {
  \    "code_syntax_enabled": 1,
  \    "populate_options": 0,
  \    "open_chat_command": "preset_below",
  \    "scratch_buffer_keep_open": 0,
  \    "paste_mode": 1,
  \  },
  \}

Check OpenAI docs for more information:
https://platform.openai.com/docs/api-reference/chat

INCLUDE FILES                                  *vim-ai-include*

To include files in the chat a special `include` role is used: >

  >>> user

  Generate documentation for the following files

  >>> include

  /home/user/myproject/requirements.txt
  /home/user/myproject/**/*.py

Each file's contents will be added to an additional `user` role message with
the files separated by `==> {path} <==`, where path is the path to the file.
Globbing is expanded out via `glob.gob` and relative paths to the current
working directory (as determined by `getcwd()`) will be resolved to absolute
paths.

                                                *:AINewChat*

:AINewChat {preset shortname}?      spawn a new conversation with a given open
                                    chat preset - below, tab or right.

                                                *:AIRedo*

:AIRedo                             repeat last AI command in order to re-try
                                    or get an alternative completion.

CONFIGURATION                                   *vim-ai-config*

To customize the default configuration, initialize the config variable with
a selection of options: >

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

Or modify options directly during the vim session: >

  let g:vim_ai_chat['options']['model'] = 'gpt-4'
  let g:vim_ai_chat['options']['temperature'] = 0.2

You can also customize the options in the chat header: >

  [chat-options]
  model=gpt-4
  temperature=0.2

  >>> user

  generate a paragraph of lorem ipsum
  ...

ROLES

Roles are defined in the `.ini` file: >

    let g:vim_ai_roles_config_file = '/path/to/my/roles.ini'

Example of a role: >

    [grammar]
    prompt = fix spelling and grammar
    config.options.temperature = 0.4

Now you can select text and run it with command `:AIEdit /grammar`.
See roles-example.ini for more examples.

The roles in g:vim_ai_roles_config_file are converted to a Vim dictionary whose
labels are the names of the roles. Optionally, roles can be added by setting
g:vim_ai_roles_config_function to the name of a Vimscript function returning a
dictionary of the same format as g:vim_ai_roles_config_file.

KEY BINDINGS

Examples how configure key bindings and customize commands: >

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

  " edit text with custom context
  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>

  " redo last AI command
  nnoremap <leader>r :AIRedo<CR>

CUSTOM COMMANDS

To create custom commands, call `AIRun`, `AIEditRun` and `AIChatRun` functions: >

  " custom command suggesting git commit message, takes no arguments
  function! AIPromptCommitMessageFn()
    let l:diff = system('git diff --staged')
    let l:prompt = "generate a short commit message from the diff below:\n" . l:diff
    let l:config = {
    \  "engine": "chat",
    \  "options": {
    \    "model": "gpt-4o",
    \    "initial_prompt": ">>> system\nyou are a code assistant",
    \    "temperature": 1,
    \  },
    \}
    call vim_ai#AIRun(l:config, l:prompt)
  endfunction
  command! AIPromptCommitMessage call AIPromptCommitMessageFn(range)

  " custom command that provides a code review for selected code block
  function! AIPromptCodeReviewFn() range
    let l:prompt = "programming syntax is " . &filetype . ", review the code below"
    let l:config = {
    \  "options": {
    \    "initial_prompt": ">>> system\nyou are a clean code expert",
    \  },
    \}
    '<,'>call vim_ai#AIChatRun(a:range, l:config, l:prompt)
  endfunction
  command! -range=0 AIPromptCodeReview <line1>,<line2>call AIPromptCodeReviewFn(<count>)

ABOUT                                           *vim-ai-about*

Contributions are welcome on GitHub:

https://github.com/madox2/vim-ai