From 01ea2e766ff6739f13542d6907b93551569d117f Mon Sep 17 00:00:00 2001 From: jiangyinzuo Date: Fri, 16 Feb 2024 13:16:04 +0800 Subject: [PATCH] feat(vimrc): add AICommitMessage fix vim-ai configuration --- root/.vim/autoload/ai.vim | 24 ++++++++++++++++++++++++ root/.vim/ftplugin/gitcommit.vim | 1 + root/.vim/vimrc.d/ai.vim | 20 ++++++-------------- 3 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 root/.vim/autoload/ai.vim create mode 100644 root/.vim/ftplugin/gitcommit.vim diff --git a/root/.vim/autoload/ai.vim b/root/.vim/autoload/ai.vim new file mode 100644 index 0000000..4e02460 --- /dev/null +++ b/root/.vim/autoload/ai.vim @@ -0,0 +1,24 @@ +function ai#RunWithInitialPrompt(func, prompt, range, ...) range + let l:config = { + \ "options": { + \ "initial_prompt": ">>> system\n" . a:prompt, + \ }, + \} + let l:prompt = a:0 ? a:1 : '' + call call(a:func, [a:range, l:config, l:prompt]) +endfunction +function ai#GitCommitMessage() + let l:diff = system('git --no-pager diff --staged') + let l:prompt = "generate a short git commit message from the diff below, using conventional commit format:\n" . l:diff + let l:range = 0 + let l:config = { + \ "engine": "chat", + \ "options": { + \ "endpoint_url": g:openai_proxy_url, + \ "model": "gpt-4", + \ "initial_prompt": ">>> system\nyou are a code assistant", + \ "temperature": 1, + \ }, + \} + call vim_ai#AIRun(l:range, l:config, l:prompt) +endfunction diff --git a/root/.vim/ftplugin/gitcommit.vim b/root/.vim/ftplugin/gitcommit.vim new file mode 100644 index 0000000..5f9a7f6 --- /dev/null +++ b/root/.vim/ftplugin/gitcommit.vim @@ -0,0 +1 @@ +command -nargs=0 -buffer AICommitMessage call ai#GitCommitMessage() diff --git a/root/.vim/vimrc.d/ai.vim b/root/.vim/vimrc.d/ai.vim index 7d215ae..8768c93 100644 --- a/root/.vim/vimrc.d/ai.vim +++ b/root/.vim/vimrc.d/ai.vim @@ -48,9 +48,11 @@ if has('nvim') || v:version >= 900 endif if has('python3') - Plug 'madox2/vim-ai', { 'do': 'sed -i \"s/api.openai.com/api.aiproxy.io/g\" py/chat.py py/complete.py ' } + let g:openai_proxy_url = get(g:, 'openai_proxy_url', 'https://api.aiproxy.io/v1/chat/completions') + Plug 'madox2/vim-ai' let g:vim_ai_chat = { \ "options": { + \ "endpoint_url": g:openai_proxy_url, \ "model": "gpt-4", \ "max_tokens": 1000, \ "temperature": 1, @@ -66,10 +68,10 @@ if has('python3') \ "paste_mode": 1, \ }, \} - let g:vim_ai_edit = { \ "engine": "chat", \ "options": { + \ "endpoint_url": g:openai_proxy_url, \ "model": "gpt-4", \ "max_tokens": 1000, \ "temperature": 1, @@ -80,18 +82,8 @@ if has('python3') \ "paste_mode": 1, \ }, \} - let g:vim_ai_complete = g:vim_ai_edit - function AIRunWithInitialPrompt(func, prompt, range, ...) range - let l:config = { - \ "options": { - \ "initial_prompt": ">>> system\n" . a:prompt, - \ }, - \} - let l:prompt = a:0 ? a:1 : '' - call call(a:func, [a:range, l:config, l:prompt]) - endfunction - command! -range -nargs=? AITranslate ,call AIRunWithInitialPrompt(function('vim_ai#AIChatRun'), "中英互译:", , ) - command! -range -nargs=? AIPolish ,call AIRunWithInitialPrompt(function('vim_ai#AIEditRun'), "英文润色:", , ) + command! -range -nargs=? AITranslate ,call ai#RunWithInitialPrompt(function('vim_ai#AIChatRun'), "中英互译:", , ) + command! -range -nargs=? AIPolish ,call ai#RunWithInitialPrompt(function('vim_ai#AIEditRun'), "英文润色:", , ) endif