Skip to content

Commit

Permalink
Merge pull request #22 from madox2/empty-response-elimination
Browse files Browse the repository at this point in the history
Empty response elimination, closes #20
  • Loading branch information
madox2 authored Apr 20, 2023
2 parents cdba357 + e4c3dad commit 940fd18
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ Below are listed all available configuration options, along with their default v
```vim
" :AI
" - options: openai config (see https://platform.openai.com/docs/api-reference/completions)
" - options.request_timout: request timeout in seconds
" - options.selection_boundary: seleciton prompt wrapper (eliminates empty responses, see #20)
" - engine: complete | chat - see how to configure chat engine in the section below
let g:vim_ai_complete = {
\ "engine": "complete",
Expand All @@ -194,11 +196,14 @@ let g:vim_ai_complete = {
\ "max_tokens": 1000,
\ "temperature": 0.1,
\ "request_timeout": 20,
\ "selection_boundary": "#####",
\ },
\}
" :AIEdit
" - options: openai config (see https://platform.openai.com/docs/api-reference/completions)
" - options.request_timout: request timeout in seconds
" - options.selection_boundary: seleciton prompt wrapper
" - engine: complete | chat - see how to configure chat engine in the section below
let g:vim_ai_edit = {
\ "engine": "complete",
Expand All @@ -207,6 +212,7 @@ let g:vim_ai_edit = {
\ "max_tokens": 1000,
\ "temperature": 0.1,
\ "request_timeout": 20,
\ "selection_boundary": "#####",
\ },
\}
Expand All @@ -221,6 +227,8 @@ END
" :AIChat
" - options: openai config (see https://platform.openai.com/docs/api-reference/chat)
" - options.initial_prompt: prompt prepended to every chat request
" - options.request_timout: request timeout in seconds
" - options.selection_boundary: seleciton prompt wrapper
" - ui.populate_options: put [chat-options] to the chat header
" - ui.open_chat_command: customize how to open chat window
" - ui.scratch_buffer_keep_open: re-use scratch buffer within the vim session
Expand All @@ -230,6 +238,7 @@ let g:vim_ai_chat = {
\ "max_tokens": 1000,
\ "temperature": 1,
\ "request_timeout": 20,
\ "selection_boundary": "",
\ "initial_prompt": s:initial_chat_prompt,
\ },
\ "ui": {
Expand Down Expand Up @@ -271,6 +280,7 @@ let chat_engine_config = {
\ "max_tokens": 1000,
\ "temperature": 0.1,
\ "request_timeout": 20,
\ "selection_boundary": "",
\ "initial_prompt": initial_prompt,
\ },
\}
Expand Down
41 changes: 30 additions & 11 deletions autoload/vim_ai.vim
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,42 @@ function! vim_ai#MakeScratchWindow()
endif
endfunction

function! s:MakePrompt(is_selection, lines, instruction)
function! s:MakeSelectionPrompt(is_selection, lines, instruction, options)
let l:selection = ""
if a:instruction == ""
let l:selection = a:lines
elseif a:is_selection
let l:boundary = a:options['selection_boundary']
if l:boundary != "" && match(a:lines, l:boundary) == -1
" NOTE: surround selection with boundary (e.g. #####) in order to eliminate empty responses
let l:selection = l:boundary . "\n" . a:lines . "\n" . l:boundary
else
let l:selection = a:lines
endif
endif
return l:selection
endfunction

function! s:MakePrompt(is_selection, lines, instruction, options)
let l:lines = trim(join(a:lines, "\n"))
let l:instruction = trim(a:instruction)
let l:delimiter = l:instruction != "" && a:is_selection ? ":\n" : ""
let l:selection = a:is_selection || l:instruction == "" ? l:lines : ""
let l:selection = s:MakeSelectionPrompt(a:is_selection, l:lines, l:instruction, a:options)
return join([l:instruction, l:delimiter, l:selection], "")
endfunction

function! vim_ai#AIRun(is_selection, ...) range
let l:engine = g:vim_ai_complete['engine']
let l:options = g:vim_ai_complete['options']

let l:instruction = a:0 ? a:1 : ""
let l:lines = getline(a:firstline, a:lastline)
let l:prompt = s:MakePrompt(a:is_selection, l:lines, l:instruction)
let l:prompt = s:MakePrompt(a:is_selection, l:lines, l:instruction, l:options)

let s:last_command = "complete"
let s:last_instruction = l:instruction
let s:last_is_selection = a:is_selection

let l:engine = g:vim_ai_complete['engine']
let l:options = g:vim_ai_complete['options']
let l:cursor_on_empty_line = trim(join(l:lines, "\n")) == ""
set paste
if l:cursor_on_empty_line
Expand All @@ -76,22 +93,26 @@ function! vim_ai#AIRun(is_selection, ...) range
endfunction

function! vim_ai#AIEditRun(is_selection, ...) range
let l:engine = g:vim_ai_edit['engine']
let l:options = g:vim_ai_edit['options']

let l:instruction = a:0 ? a:1 : ""
let l:prompt = s:MakePrompt(a:is_selection, getline(a:firstline, a:lastline), l:instruction)
let l:prompt = s:MakePrompt(a:is_selection, getline(a:firstline, a:lastline), l:instruction, l:options)

let s:last_command = "edit"
let s:last_instruction = l:instruction
let s:last_is_selection = a:is_selection

let l:engine = g:vim_ai_edit['engine']
let l:options = g:vim_ai_edit['options']
set paste
execute "normal! " . a:firstline . "GV" . a:lastline . "Gc"
execute "py3file " . s:complete_py
set nopaste
endfunction

function! vim_ai#AIChatRun(is_selection, ...) range
let l:options = g:vim_ai_chat['options']
let l:ui = g:vim_ai_chat['ui']

let l:instruction = ""
let l:lines = getline(a:firstline, a:lastline)
set paste
Expand All @@ -101,7 +122,7 @@ function! vim_ai#AIChatRun(is_selection, ...) range
let l:prompt = ""
if a:0 || a:is_selection
let l:instruction = a:0 ? a:1 : ""
let l:prompt = s:MakePrompt(a:is_selection, l:lines, l:instruction)
let l:prompt = s:MakePrompt(a:is_selection, l:lines, l:instruction, l:options)
endif
execute "normal! Gi" . l:prompt
endif
Expand All @@ -110,8 +131,6 @@ function! vim_ai#AIChatRun(is_selection, ...) range
let s:last_instruction = l:instruction
let s:last_is_selection = a:is_selection

let l:options = g:vim_ai_chat['options']
let l:ui = g:vim_ai_chat['ui']
execute "py3file " . s:chat_py
set nopaste
endfunction
Expand Down
3 changes: 3 additions & 0 deletions autoload/vim_ai_config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let g:vim_ai_complete_default = {
\ "max_tokens": 1000,
\ "temperature": 0.1,
\ "request_timeout": 20,
\ "selection_boundary": "#####",
\ },
\}
let g:vim_ai_edit_default = {
Expand All @@ -14,6 +15,7 @@ let g:vim_ai_edit_default = {
\ "max_tokens": 1000,
\ "temperature": 0.1,
\ "request_timeout": 20,
\ "selection_boundary": "#####",
\ },
\}

Expand All @@ -29,6 +31,7 @@ let g:vim_ai_chat_default = {
\ "max_tokens": 1000,
\ "temperature": 1,
\ "request_timeout": 20,
\ "selection_boundary": "",
\ "initial_prompt": s:initial_chat_prompt,
\ },
\ "ui": {
Expand Down
3 changes: 3 additions & 0 deletions doc/vim-ai.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Options: >
\ "max_tokens": 1000,
\ "temperature": 0.1,
\ "request_timeout": 20,
\ "selection_boundary": "#####",
\ },
\}

Expand All @@ -48,6 +49,7 @@ Options: >
\ "max_tokens": 1000,
\ "temperature": 0.1,
\ "request_timeout": 20,
\ "selection_boundary": "#####",
\ },
\}

Expand Down Expand Up @@ -75,6 +77,7 @@ Options: >
\ "max_tokens": 1000,
\ "temperature": 1,
\ "request_timeout": 20,
\ "selection_boundary": "#####",
\ "initial_prompt": s:initial_chat_prompt,
\ },
\ "ui": {
Expand Down

0 comments on commit 940fd18

Please sign in to comment.