يضيف هذا البرنامج المساعد قدرات الذكاء الاصطناعي (AI) إلى VIM و Neovim. يمكنك إنشاء رمز أو تحرير النص أو إجراء محادثة تفاعلية مع نماذج GPT ، وكلها مدعومة من API من Openai.
للحصول على فكرة ما هو ممكن القيام به بأوامر الذكاء الاصطناعى ، انظر إلى المطالبات في ويكي المجتمع
يستخدم هذا البرنامج المساعد API Openai لإنشاء ردود. ستحتاج إلى إعداد حساب والحصول على مفتاح 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
، ولكن يمكنك تغييره عن طريق تعيين g:vim_ai_token_file_path
في ملف .vimrc
:
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
لاستخدام أمر الذكاء الاصطناعي ، اكتب الأمر متبوعًا بمطالبة التعليمات. يمكنك أيضًا الجمع بينه مع التحديد المرئي. فيما يلي نظرة عامة موجزة على الأوامر المتاحة:
========= 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
نصيحة: يمكن تمرير دور مخصص {rod} إلى الأوامر المذكورة أعلاه بواسطة معلمة أولية /{{} ، على سبيل المثال :AIEdit /grammar
.
نصيحة: الجمع بين الأوامر مع نطاق :help range
، على سبيل المثال لتحديد المخزن المؤقت بالكامل - :%AIE fix grammar
إذا كنت مهتمًا بمزيد من النصائح أو ترغب في رفع مستوى VIM مع المزيد من الأوامر مثل :GitCommitMessage
- مما يشير إلى رسالة الالتزام بالجيت ، تفضل بزيارة Wiki المجتمع.
?
الوثائق أدناه {instruction}
{role}
<selection>
رمز معلمة اختيارية.
: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}?
- استخدام الدور لإكمال
عندما تنتهي الذكاء الاصطناعى من الرد ، يمكنك متابعة المحادثة عن طريق إدخال وضع إدراج ، وإضافة موجه ، ثم استخدام الأمر :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} <==
، حيث يكون المسار هو المسار إلى الملف. يتم توسيع Globbing عبر glob.gob
وسيتم حل المسارات النسبية إلى دليل العمل الحالي (كما هو محدد بواسطة getcwd()
) إلى مسارات مطلقة.
أدوار الدردشة المدعومة هي >>> system
، >>> user
، >>> include
و <<< assistant
:AINewChat
:AINewChat {preset shortname}?
- ابدأ محادثة جديدة
يتم استخدام هذا الأمر عندما تحتاج إلى تفرخ دردشة جديدة بطريقة محددة أو في الموقف عندما :AIChat
عادةً ما يواصل المحادثة بدلاً من ذلك.
كمعلمة ، تضع اختصار أمر محدد للدردشة - below
، tab
أو right
. على سبيل المثال :: :AINewChat right
.
:AIRedo
:AIRedo
- كرر الأمر آخر منظمة العفو الدولية
استخدم هذا مباشرة بعد أمر AI
/ AIEdit
/ AIChat
من أجل إعادة تجهيز أو الحصول على إكمال بديل. لاحظ أن العشوائية في الاستجابات تعتمد بشكل كبير على معلمة temperature
.
في سياق هذا البرنامج المساعد ، يعني الدور تعليمات و/أو تكوين قابلة لإعادة الاستخدام. يتم تعريف الأدوار في ملف التكوين .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
.
انظر ROLES-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. شاهد بعض المشاريع الرائعة المدرجة في قسم واجهات برمجة التطبيقات المخصصة في مجتمع ويكي.
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 عند توليد الإكمال والتحريرات. لذلك ، لا تستخدمه على الملفات التي تحتوي على معلومات حساسة.
رخصة معهد ماساتشوستس للتكنولوجيا