このプラグインは、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
ヒント:カスタムロール{役割}は、上記のコマンドに渡すことができます。たとえば: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
各ファイルのコンテンツは==> {path} <==
で区切られたファイルを使用して、追加のuser
ロールメッセージに追加されます。ここで、パスはファイルへのパスです。グローブはglob.gob
を介して拡張され、現在の作業ディレクトリへの相対パス( getcwd()
によって決定されます)は、絶対パスに解決されます。
サポートされているチャットの役割は>>> system
、 >>> user
、 >>> include
、および<<< 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
で実行できます。
その他の例については、ロールexample.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のカスタムAPISセクションにリストされているクールなプロジェクトをいくつかご覧ください。
let g: vim_ai_chat = {
" options " : {
" endpoint_url " : " http://localhost:8000/v1/chat/completions " ,
" enable_auth " : 0 ,
},
}
gpt-4o
などのチャットモデルを構成することができます:AI
および:AIEdit
コマンドで使用します。これらのモデルは安価ですが、現在、人間のようなテキストや解説で応答するため、コードの編集/完了には適していません。
ユースケースに応じて、優れた初期プロンプトは、チャットモデルに目的の方法で応答するように指示するのに役立ちます。
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にテキストを送信します。したがって、機密情報を含むファイルに使用しないでください。
MITライセンス