Skip to content

Commit

Permalink
feat(vimrc): add AICommitMessage
Browse files Browse the repository at this point in the history
fix vim-ai configuration
  • Loading branch information
jiangyinzuo committed Feb 16, 2024
1 parent b743bed commit 01ea2e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
24 changes: 24 additions & 0 deletions root/.vim/autoload/ai.vim
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions root/.vim/ftplugin/gitcommit.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
command -nargs=0 -buffer AICommitMessage call ai#GitCommitMessage()
20 changes: 6 additions & 14 deletions root/.vim/vimrc.d/ai.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 <line1>,<line2>call AIRunWithInitialPrompt(function('vim_ai#AIChatRun'), "中英互译:", <range>, <f-args>)
command! -range -nargs=? AIPolish <line1>,<line2>call AIRunWithInitialPrompt(function('vim_ai#AIEditRun'), "英文润色:", <range>, <f-args>)
command! -range -nargs=? AITranslate <line1>,<line2>call ai#RunWithInitialPrompt(function('vim_ai#AIChatRun'), "中英互译:", <range>, <f-args>)
command! -range -nargs=? AIPolish <line1>,<line2>call ai#RunWithInitialPrompt(function('vim_ai#AIEditRun'), "英文润色:", <range>, <f-args>)
endif

0 comments on commit 01ea2e7

Please sign in to comment.