Skip to content

Commit 355c80f

Browse files
authored
examples : simplify vim plugin (#2327)
Uses builtin json_encode and json_decode functions to simplify escaping Removes the need for temp files
1 parent 83a00ce commit 355c80f

File tree

1 file changed

+5
-40
lines changed

1 file changed

+5
-40
lines changed

examples/llm.vim

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,22 @@ function! Llm()
22

33
let url = "http://127.0.0.1:8080/completion"
44

5-
" Save the current cursor position
6-
let save_cursor = getpos('.')
7-
8-
silent! %s/\n/\\n/g
9-
silent! %s/\t/\\t/g
10-
silent! %s/\\n$//
11-
125
" Get the content of the current buffer
136
let buffer_content = join(getline(1, '$'), "\n")
147

15-
" Replace true newlines with "\n"
16-
let buffer_content = substitute(buffer_content, '\n', '\\n', 'g')
17-
18-
" Trim leading/trailing whitespace
19-
let buffer_content = substitute(buffer_content, '^\s\+', '', '')
20-
let buffer_content = substitute(buffer_content, '\s\+$', '', '')
21-
228
" Create the JSON payload
23-
" can't escape backslash, \n gets replaced as \\n
24-
let json_payload = '{"prompt":"' . escape(buffer_content, '"/') . '","temp":0.72,"top_k":100,"top_p":0.73,"repeat_penalty":1.100000023841858,"n_predict":10,"stream":false}'
25-
26-
let prompt_tmpfile = tempname()
27-
let response_tmpfile = tempname()
28-
call writefile([json_payload], prompt_tmpfile)
9+
let json_payload = {"temp":0.72,"top_k":100,"top_p":0.73,"repeat_penalty":1.100000023841858,"n_predict":10,"stream": v:false}
10+
let json_payload.prompt = buffer_content
2911

3012
" Define the curl command
31-
let curl_command = 'curl -k -s -X POST -H "Content-Type: application/json" -o ' . shellescape(response_tmpfile) . ' -d @' . shellescape(prompt_tmpfile) . ' ' . url
32-
silent execute '!'.curl_command
33-
34-
let response = join(readfile(response_tmpfile), '')
35-
let start_marker = '{"content":"'
36-
let end_marker = '","generation_settings'
37-
let content_start = stridx(response, start_marker) + len(start_marker)
38-
let content_end = stridx(response, end_marker, content_start)
13+
let curl_command = 'curl -k -s -X POST -H "Content-Type: application/json" -d @- ' . url
14+
let response = system(curl_command, json_encode(json_payload))
3915

4016
" Extract the content field from the response
41-
let content = strpart(response, content_start, content_end - content_start)
17+
let content = json_decode(response).content
4218

4319
" Insert the content at the cursor position
4420
call setline(line('.'), getline('.') . content)
45-
46-
" Replace newline "\n" strings with actual newlines in the content
47-
silent! %s/\\n/\r/g
48-
" and tabs
49-
silent! %s/\\t/\t/g
50-
" and quote marks for C sources
51-
silent! %s/\\"/\"/g
52-
53-
" Remove the temporary file
54-
call delete(prompt_tmpfile)
55-
call delete(response_tmpfile)
5621
endfunction
5722

5823
command! Llm call Llm()

0 commit comments

Comments
 (0)