mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-12-24 10:24:35 +00:00
examples : simplify vim plugin (#2327)
Uses builtin json_encode and json_decode functions to simplify escaping Removes the need for temp files
This commit is contained in:
parent
83a00ce69b
commit
355c80f49e
@ -2,57 +2,22 @@ function! Llm()
|
|||||||
|
|
||||||
let url = "http://127.0.0.1:8080/completion"
|
let url = "http://127.0.0.1:8080/completion"
|
||||||
|
|
||||||
" Save the current cursor position
|
|
||||||
let save_cursor = getpos('.')
|
|
||||||
|
|
||||||
silent! %s/\n/\\n/g
|
|
||||||
silent! %s/\t/\\t/g
|
|
||||||
silent! %s/\\n$//
|
|
||||||
|
|
||||||
" Get the content of the current buffer
|
" Get the content of the current buffer
|
||||||
let buffer_content = join(getline(1, '$'), "\n")
|
let buffer_content = join(getline(1, '$'), "\n")
|
||||||
|
|
||||||
" Replace true newlines with "\n"
|
|
||||||
let buffer_content = substitute(buffer_content, '\n', '\\n', 'g')
|
|
||||||
|
|
||||||
" Trim leading/trailing whitespace
|
|
||||||
let buffer_content = substitute(buffer_content, '^\s\+', '', '')
|
|
||||||
let buffer_content = substitute(buffer_content, '\s\+$', '', '')
|
|
||||||
|
|
||||||
" Create the JSON payload
|
" Create the JSON payload
|
||||||
" can't escape backslash, \n gets replaced as \\n
|
let json_payload = {"temp":0.72,"top_k":100,"top_p":0.73,"repeat_penalty":1.100000023841858,"n_predict":10,"stream": v:false}
|
||||||
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}'
|
let json_payload.prompt = buffer_content
|
||||||
|
|
||||||
let prompt_tmpfile = tempname()
|
|
||||||
let response_tmpfile = tempname()
|
|
||||||
call writefile([json_payload], prompt_tmpfile)
|
|
||||||
|
|
||||||
" Define the curl command
|
" Define the curl command
|
||||||
let curl_command = 'curl -k -s -X POST -H "Content-Type: application/json" -o ' . shellescape(response_tmpfile) . ' -d @' . shellescape(prompt_tmpfile) . ' ' . url
|
let curl_command = 'curl -k -s -X POST -H "Content-Type: application/json" -d @- ' . url
|
||||||
silent execute '!'.curl_command
|
let response = system(curl_command, json_encode(json_payload))
|
||||||
|
|
||||||
let response = join(readfile(response_tmpfile), '')
|
|
||||||
let start_marker = '{"content":"'
|
|
||||||
let end_marker = '","generation_settings'
|
|
||||||
let content_start = stridx(response, start_marker) + len(start_marker)
|
|
||||||
let content_end = stridx(response, end_marker, content_start)
|
|
||||||
|
|
||||||
" Extract the content field from the response
|
" Extract the content field from the response
|
||||||
let content = strpart(response, content_start, content_end - content_start)
|
let content = json_decode(response).content
|
||||||
|
|
||||||
" Insert the content at the cursor position
|
" Insert the content at the cursor position
|
||||||
call setline(line('.'), getline('.') . content)
|
call setline(line('.'), getline('.') . content)
|
||||||
|
|
||||||
" Replace newline "\n" strings with actual newlines in the content
|
|
||||||
silent! %s/\\n/\r/g
|
|
||||||
" and tabs
|
|
||||||
silent! %s/\\t/\t/g
|
|
||||||
" and quote marks for C sources
|
|
||||||
silent! %s/\\"/\"/g
|
|
||||||
|
|
||||||
" Remove the temporary file
|
|
||||||
call delete(prompt_tmpfile)
|
|
||||||
call delete(response_tmpfile)
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
command! Llm call Llm()
|
command! Llm call Llm()
|
||||||
|
Loading…
Reference in New Issue
Block a user