該插件為您的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。因此,請勿在包含敏感信息的文件上使用它。
麻省理工學院許可證