이 플러그인은 VIM 및 Neovim에 인공 지능 (AI) 기능을 추가합니다. OpenAI의 API로 구동되는 코드를 생성, 텍스트 편집 또는 GPT 모델과 대화식 대화를 할 수 있습니다.
AI 명령으로 무엇을 할 수 있는지 아이디어를 얻으려면 커뮤니티 위키의 프롬프트를 참조하십시오.
이 플러그인은 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
팁 : 사용자 정의 역할 {role}은 초기 매개 변수 /{역할}에 의해 위 명령으로 전달 될 수 있습니다 :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
역할 메시지에 추가됩니다. 여기서 경로는 파일의 경로입니다. Globbing은 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
명령으로 실행할 수 있습니다.
더 많은 예는 real-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 호환 엔드 포인트를 사용하도록 플러그인을 구성 할 수 있습니다. 커뮤니티 위키의 Custom API 섹션에 나열된 멋진 프로젝트를 참조하십시오.
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
커뮤니티 위키에 유용한 맞춤형 명령 모음을 찾을 수 있습니다.
사용자 정의 명령을 만들려면 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> )
기부금을 환영합니다! 풀 요청을 열고, 문제를보고하거나, 커뮤니티 위키에 기여하십시오.
정확성 : GPT는 첫눈에 정확하게 보이는 텍스트와 코드를 생성하는 데 능숙하지만 완전히 잘못 될 수 있습니다. 이 플러그인에서 생성 된 모든 출력을 철저히 검토하고 읽고 테스트하십시오!
프라이버시 :이 플러그인은 완료 및 편집을 생성 할 때 텍스트를 OpenAI로 보냅니다. 따라서 민감한 정보가 포함 된 파일에서 사용하지 마십시오.
MIT 라이센스