summaryrefslogtreecommitdiff
path: root/doc/vim-ai.txt
blob: fa044f92e2bd651c6699bec181d6c5b721969c54 (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
*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                                 complete the text on the current line
:AI {prompt}                        complete the prompt
<selection> :AI                     complete the selection
<selection> :AI {instruction}       complete the selection using the instruction

Options: >
  let g:vim_ai_complete = {
  \  "engine": "complete",
  \  "options": {
  \    "model": "text-davinci-003",
  \    "max_tokens": 1000,
  \    "temperature": 0.1,
  \    "request_timeout": 20,
  \    "base_url": "https://api.openai.com",
  \    "enable_auth": 1,
  \    "selection_boundary": "#####",
  \  },
  \  "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 g:vim_ai_edit = {
  \  "engine": "complete",
  \  "options": {
  \    "model": "text-davinci-003",
  \    "max_tokens": 1000,
  \    "temperature": 0.1,
  \    "request_timeout": 20,
  \    "base_url": "https://api.openai.com",
  \    "enable_auth": 1,
  \    "selection_boundary": "#####",
  \  },
  \  "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-3.5-turbo",
  \    "max_tokens": 1000,
  \    "temperature": 1,
  \    "request_timeout": 20,
  \    "base_url": "https://api.openai.com",
  \    "enable_auth": 1,
  \    "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

                                                *: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
  ...

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:range = 0
    let l:config = {
    \  "engine": "chat",
    \  "options": {
    \    "model": "gpt-3.5-turbo",
    \    "initial_prompt": ">>> system\nyou are a code assistant",
    \    "temperature": 1,
    \  },
    \}
    call vim_ai#AIRun(l:range, l:config, l:prompt)
  endfunction
  command! AIPromptCommitMessage call AIPromptCommitMessageFn()

  " custom command that provides a code review for selected code block
  function! AIPromptCodeReviewFn(range) 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 AIPromptCodeReview <line1>,<line2>call AIPromptCodeReviewFn(<range>)

ABOUT                                           *vim-ai-about*

Contributions are welcome on GitHub:

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