该插件为您的VIM和Neovim添加了人工智能(AI)功能。您可以生成代码,编辑文本或与GPT模型进行交互式对话,所有这些都由OpenAI的API提供支持。
为了了解AI命令有可能做什么,请参阅社区Wiki上的提示
该插件使用OpenAI的API生成响应。您将需要设置一个帐户并获取API密钥。 API的使用不是免费的,但成本是合理的,取决于您使用的代币数量,简单地用来您发送和接收多少文本(请参阅定价)。请注意,该插件不会在幕后发送您的任何代码。您只为提示和聊天内容分享并付费。
# save api key to `~/.config/openai.token` file
echo " YOUR_OPENAI_API_KEY " > ~ /.config/openai.token
# alternatively set it as an environment variable
export OPENAI_API_KEY= " YOUR_OPENAI_API_KEY "
# or configure it with your organization id
echo " YOUR_OPENAI_API_KEY,YOUR_OPENAI_ORG_ID " > ~ /.config/openai.token
export OPENAI_API_KEY= " YOUR_OPENAI_API_KEY,YOUR_OPENAI_ORG_ID "
默认的API密钥文件位置是~/.config/openai.token
,但是您可以通过在.vimrc
文件中设置g:vim_ai_token_file_path
来更改它:
let g: vim_ai_token_file_path = ' ~/.config/openai.token '
vim-plug
Plug ' madox2/vim-ai '
使用内置的VIM软件包:help packages
# vim
mkdir -p ~ /.vim/pack/plugins/start
git clone https://github.com/madox2/vim-ai.git ~ /.vim/pack/plugins/start/vim-ai
# neovim
mkdir -p ~ /.local/share/nvim/site/pack/plugins/start
git clone https://github.com/madox2/vim-ai.git ~ /.local/share/nvim/site/pack/plugins/start/vim-ai
要使用AI命令,请键入命令,然后输入指令提示。您也可以将其与视觉选择结合在一起。这是可用命令的简要概述:
========= Basic AI commands =========
:AI complete text
:AIEdit edit text
:AIChat continue or open new chat
============= Utilities =============
:AIRedo repeat last AI command
:AINewChat open new chat
:help vim-ai
提示:随时按Ctrl-c
取消完成
提示:设置自己的键绑定或使用命令快捷方式 - :AIE
, :AIC
, :AIR
提示:例如,可以通过初始参数 /{requ}将自定义角色{角色}传递给上述命令,例如:AIEdit /grammar
。
提示:将命令与范围结合:help range
,例如选择整个缓冲区 - :%AIE fix grammar
如果您对更多提示感兴趣,或者想通过以下更多命令来升级您的VIM,例如:GitCommitMessage
建议使用GIT提交消息,请访问社区Wiki。
在下面的文档中, <selection>
表示视觉选择或任何其他范围, {instruction}
指令提示符, {role}
自定义角色和?
符号可选参数。
:AI
:AI
在当前行上完成文本
:AI {prompt}
- 完成提示
<selection> :AI
完成选择
<selection> :AI {instruction}
- 使用指令完成选择
<selection>? :AI /{role} {instruction}?
- 使用角色完成
:AIEdit
<selection>? :AIEdit
编辑当前行或选择
<selection>? :AIEdit {instruction}
- 使用指令编辑当前行或选择
<selection>? :AIEdit /{role} {instruction}?
- 使用角色进行编辑
:AIChat
:AIChat
继续或开始新的对话。
<selection>? :AIChat {instruction}?
- 鉴于选择,指令或两者兼而有之新的对话
<selection>? :AIChat /{role} {instruction}?
- 使用角色完成
当AI完成回答后,您可以通过输入插入模式,添加提示,然后使用命令:AIChat
再次继续对话。
.aichat
文件您可以编辑并将聊天对话保存到.aichat
文件中,然后稍后再还原。这使您可以创建可重复使用的自定义提示,例如:
# ./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]
要在聊天中包含文件,请使用特殊include
角色:
>>> user
Generate documentation for the following files
>>> include
/home/user/myproject/requirements.txt
/home/user/myproject/**/*.py
每个文件的内容将被添加到附加的user
角色消息中,并以==> {path} <==
分开的文件,其中路径是文件的路径。通过glob.gob
和当前工作目录的相对路径扩展(由getcwd()
确定)将被解析为绝对路径。
支持的聊天角色是>>> system
, >>> user
, >>> include
and <<< assistant
:AINewChat
:AINewChat {preset shortname}?
- 开始新对话
当您需要以特定方式或在以下情况下产生新聊天时:AIChat
使用此命令。
作为参数,您将打开的聊天命令预设快捷方式 - below
, tab
或right
。例如:AINewChat right
。
:AIRedo
:AIRedo
重复最后AI命令
在AI
/ AIEdit
/ AIChat
命令之后立即使用此操作,以重尝试或获得替代性完成。请注意,响应的随机性在很大程度上取决于temperature
参数。
在此插件的上下文中,角色意味着可重复使用的AI指令和/或配置。角色在配置.ini
文件中定义。例如,通过定义grammar
角色:
let g: vim_ai_roles_config_file = ' /path/to/my/roles.ini '
# /path/to/my/roles.ini
[grammar]
prompt = fix spelling and grammar
[grammar.options]
temperature = 0.4
现在,您可以选择文本并使用命令:AIEdit /grammar
运行。
有关更多示例,请参见角色 - 示例.ini。
该插件未设置任何键绑定。在.vimrc
中创建自己的绑定以触发AI命令,例如:
" 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>
" redo last AI command
nnoremap <leader> r :AIRedo <CR>
每个命令都配置为相应的配置变量。要自定义默认配置,请使用选项选择的配置变量初始化配置变量,例如将其放在.vimrc
文件:
let g: vim_ai_chat = {
" options " : {
" model " : " gpt-4 " ,
" temperature " : 0.2 ,
},
}
设置以上设置后,您可以在VIM会话中直接修改选项:
let g: vim_ai_chat [ ' options ' ][ ' model ' ] = ' gpt-4 '
let g: vim_ai_chat [ ' options ' ][ ' temperature ' ] = 0.2
或直接在聊天缓冲区中自定义选项:
[chat-options]
model =gpt-4
temperature =0.2
>>> user
generate a paragraph of lorem ipsum
以下列出了所有可用的配置选项以及它们的默认值。请注意,聊天模型中没有任何令牌限制。
" :AI
" - engine: complete | chat - see how to configure chat engine in the section below
" - options: openai config (see https://platform.openai.com/docs/api-reference/completions)
" - options.request_timeout: request timeout in seconds
" - options.enable_auth: enable authorization using openai key
" - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20)
" - ui.paste_mode: use paste mode (see more info in the Notes below)
let g: vim_ai_complete = {
" engine " : " complete " ,
" options " : {
" model " : " gpt-3.5-turbo-instruct " ,
" endpoint_url " : " https://api.openai.com/v1/completions " ,
" max_tokens " : 1000 ,
" temperature " : 0.1 ,
" request_timeout " : 20 ,
" enable_auth " : 1 ,
" selection_boundary " : " ##### " ,
},
" ui " : {
" paste_mode " : 1 ,
},
}
" :AIEdit
" - engine: complete | chat - see how to configure chat engine in the section below
" - options: openai config (see https://platform.openai.com/docs/api-reference/completions)
" - options.request_timeout: request timeout in seconds
" - options.enable_auth: enable authorization using openai key
" - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20)
" - ui.paste_mode: use paste mode (see more info in the Notes below)
let g: vim_ai_edit = {
" engine " : " complete " ,
" options " : {
" model " : " gpt-3.5-turbo-instruct " ,
" endpoint_url " : " https://api.openai.com/v1/completions " ,
" max_tokens " : 1000 ,
" temperature " : 0.1 ,
" request_timeout " : 20 ,
" enable_auth " : 1 ,
" selection_boundary " : " ##### " ,
},
" ui " : {
" paste_mode " : 1 ,
},
}
" This prompt instructs model to work with syntax highlighting
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
" :AIChat
" - options: openai config (see https://platform.openai.com/docs/api-reference/chat)
" - options.initial_prompt: prompt prepended to every chat request (list of lines or string)
" - options.request_timeout: request timeout in seconds
" - options.enable_auth: enable authorization using openai key
" - options.selection_boundary: selection prompt wrapper (eliminates empty responses, see #20)
" - ui.populate_options: put [chat-options] to the chat header
" - ui.open_chat_command: preset (preset_below, preset_tab, preset_right) or a custom command
" - ui.scratch_buffer_keep_open: re-use scratch buffer within the vim session
" - ui.paste_mode: use paste mode (see more info in the Notes below)
let g: vim_ai_chat = {
" options " : {
" model " : " gpt-4o " ,
" endpoint_url " : " https://api.openai.com/v1/chat/completions " ,
" max_tokens " : 0 ,
" temperature " : 1 ,
" request_timeout " : 20 ,
" 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 ,
},
}
" Notes:
" ui.paste_mode
" - if disabled code indentation will work but AI doesn't always respond with a code block
" therefore it could be messed up
" - find out more in vim's help `:help paste`
" options.max_tokens
" - note that prompt + max_tokens must be less than model's token limit, see #42, #46
" - setting max tokens to 0 will exclude it from the OpenAI API request parameters, it is
" unclear/undocumented what it exactly does, but it seems to resolve issues when the model
" hits token limit, which respond with `OpenAI: HTTPError 400`
可以配置插件以使用不同的OpenAI兼容端点。请参阅社区Wiki的自定义API部分中列出的一些很酷的项目。
let g: vim_ai_chat = {
" options " : {
" endpoint_url " : " http://localhost:8000/v1/chat/completions " ,
" enable_auth " : 0 ,
},
}
可以在:AI
和:AIEdit
命令中配置聊天模型,例如gpt-4o
。这些模型更便宜,但目前不太适合代码编辑/完成,因为它们以类似人类的文字和评论做出响应。
根据用例,良好的初始提示可以帮助指示聊天模型以所需的方式响应:
let initial_prompt = << trim END
>>> system
You are going to play a role of a completion engine with following parameter s:
Task: Provide compact code/text completion, generation, transformation or explanation
Topic: general programming and text editing
Style: Plain result without any commentary, unless commentary is necessary
Audience: Users of text editor and programmers that need to transform/generate text
END
let chat_engine_config = {
" engine " : " chat " ,
" options " : {
" model " : " gpt-4o " ,
" endpoint_url " : " https://api.openai.com/v1/chat/completions " ,
" max_tokens " : 0 ,
" temperature " : 0.1 ,
" request_timeout " : 20 ,
" selection_boundary " : " " ,
" initial_prompt " : initial_prompt,
},
}
let g: vim_ai_complete = chat_engine_config
let g: vim_ai_edit = chat_engine_config
您可能会在社区Wiki上找到有用的自定义命令集合。
要创建自定义命令,您可以调用AIRun
, AIEditRun
和AIChatRun
功能。例如:
" custom command suggesting git commit message, takes no arguments
function ! GitCommitMessageFn ()
let l: diff = system ( ' git --no-pager 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 n you are a code assistant " ,
" temperature " : 1 ,
},
}
call vim_ai#AIRun ( l: config , l: prompt )
endfunction
command ! GitCommitMessage call GitCommitMessageFn ()
" custom command that provides a code review for selected code block
function ! CodeReviewFn ( range ) range
let l: prompt = " programming syntax is " . & filetype . " , review the code below "
let l: config = {
" options " : {
" initial_prompt " : " >>> system n you are a clean code expert " ,
},
}
exe a: firstline . " , " . a: lastline . " call vim_ai#AIChatRun(a:range, l:config, l:prompt) "
endfunction
command ! - range = 0 CodeReview <line1> , <line2> call CodeReviewFn ( <count> )
欢迎捐款!请随时打开拉动请求,报告问题或为社区Wiki做出贡献。
准确性:GPT擅长生产看起来正确的文本和代码,但可能完全是错误的。确保彻底查看,阅读和测试此插件生成的所有输出!
隐私:此插件在生成完成和编辑时将文本发送到OpenAI。因此,请勿在包含敏感信息的文件上使用它。
麻省理工学院许可证