Skip to content

Commit

Permalink
Close #24 - Add context-aware prompting
Browse files Browse the repository at this point in the history
Add initial context-aware prompt editing by writing simple scripts.
  • Loading branch information
w0rp authored and Angelchev committed Jul 26, 2023
1 parent 03d8140 commit 2c02408
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 1 deletion.
18 changes: 18 additions & 0 deletions autoload/neural.vim
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ function! neural#Cleanup() abort
endif
endfunction

" Pre-process input for LLMs based on custom code in Neural.
function! neural#PreProcess(buffer, input) abort
" Skip pre-processing if disabled.
if !g:neural.pre_process.enabled
return
endif

for l:split_type in split(&filetype, '\.')
try
let l:func_name = 'neural#pre_process#' . l:split_type . '#Process'
call function(l:func_name)(a:buffer, a:input)
catch /E117/
endtry
endfor
endfunction

function! neural#Prompt(prompt) abort
" Reload the Neural config on a prompt request if needed.
call neural#config#Load()
Expand Down Expand Up @@ -240,6 +256,8 @@ function! neural#Run(prompt, options) abort
let l:moving_line -= 1
endif

call neural#PreProcess(l:buffer, l:input)

let l:script_exe = s:GetScriptExecutable(l:source)
let l:command = neural#Escape(l:script_exe)
\ . ' ' . neural#Escape(l:source.script)
Expand Down
3 changes: 3 additions & 0 deletions autoload/neural/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ let s:last_dictionary = get(s:, 'last_dictionary', {})

let s:defaults = {
\ 'selected': 'openai',
\ 'pre_process': {
\ 'enabled': v:true,
\ },
\ 'ui': {
\ 'prompt_enabled': v:true,
\ 'prompt_icon': '🗲',
Expand Down
10 changes: 10 additions & 0 deletions autoload/neural/pre_process/go.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
" Author: w0rp <[email protected]>
" Description: Pre-processing rules for Go files.

function! neural#pre_process#go#Process(buffer, input) abort
let l:has_package = search('^package ', 'wnc') != 0

let a:input.prompt = 'Write golang code. '
\ . (l:has_package ? 'Do not write package main or main func. ' : '')
\ . a:input.prompt
endfunction
7 changes: 7 additions & 0 deletions autoload/neural/pre_process/markdown.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
" Author: w0rp <[email protected]>
" Description: Pre-processing rules for markdown files.

function! neural#pre_process#markdown#Process(buffer, input) abort
let a:input.prompt = 'Write text in a markdown file. '
\ . a:input.prompt
endfunction
11 changes: 11 additions & 0 deletions doc/neural.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ g:neural.selected *g:neural.selected*
2. `'chatgpt'` - ChatGPT


g:neural.pre_process.enabled *g:neural.pre_process.enabled*
*vim.g.neural.pre_process.enabled*
Type: |Boolean|
Default: `v:true`

If `v:true`, pre-process prompts before sending them to language models.

Neural edits the text you send automatically by default to improve the
quality of prompts to produce better results for each filetype.


g:neural.ui.echo_enabled *g:neural.ui.echo_enabled*
*vim.g.neural.ui.echo_enabled*
Type: |Boolean|
Expand Down
2 changes: 1 addition & 1 deletion run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ git_version=$(git describe --always --tags)
DOCKER_RUN_IMAGE="$image:$image_tag"
export DOCKER_RUN_IMAGE

tests='test/vim/*.vader'
tests='test/vim/*.vader test/vim/*/*.vader'
# These flags are forwarded to the script for running Vader tests.
verbose_flag=''
quiet_flag=''
Expand Down
20 changes: 20 additions & 0 deletions test/vim/pre_process/test_disable_pre_process.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Before:
Save g:neural

unlet! g:neural

let g:input = {'prompt': 'Do something.'}
call neural#config#Load()

After:
unlet! g:input

Restore

Given go(An empty Go file):
Execute(It should be possible to disable pre-processing):
let g:neural.pre_process.enabled = 0

call neural#PreProcess(bufnr(''), g:input)

AssertEqual 'Do something.', g:input.prompt
19 changes: 19 additions & 0 deletions test/vim/pre_process/test_go.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Before:
let g:input = {'prompt': 'Do something.'}
call neural#config#Load()

After:
unlet! g:input

Given go(An empty Go file):
Execute(Basic Go prompt editing should be done):
call neural#PreProcess(bufnr(''), g:input)

AssertEqual 'Write golang code. Do something.', g:input.prompt

Given go(A go file with a package declaration):
package anything
Execute(The prescence of a package should edit the prompt to not add a main function):
call neural#PreProcess(bufnr(''), g:input)

AssertEqual 'Write golang code. Do not write package main or main func. Do something.', g:input.prompt
12 changes: 12 additions & 0 deletions test/vim/pre_process/test_markdown.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Before:
let g:input = {'prompt': 'Do something.'}
call neural#config#Load()

After:
unlet! g:input

Given markdown(An empty Markdown file):
Execute(Basic Markdown prompt editing should be done):
call neural#PreProcess(bufnr(''), g:input)

AssertEqual 'Write text in a markdown file. Do something.', g:input.prompt
2 changes: 2 additions & 0 deletions test/vim/test_neural.vader
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ After:

runtime autoload/neural/job.vim

Restore

Given text (A file with two lines and a blank line in the middle):
First line

Expand Down

0 comments on commit 2c02408

Please sign in to comment.