From 24baa54ac1ff3d4156a2360deb1473af04a9b1a2 Mon Sep 17 00:00:00 2001 From: whoreson <139810751+whoreson@users.noreply.github.com> Date: Sat, 22 Jul 2023 12:34:51 +0200 Subject: examples : basic VIM plugin VIM plugin for server exe --- examples/llm.vim | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 examples/llm.vim (limited to 'examples/llm.vim') diff --git a/examples/llm.vim b/examples/llm.vim new file mode 100644 index 0000000..16e308c --- /dev/null +++ b/examples/llm.vim @@ -0,0 +1,58 @@ +function! Llm() + + 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 + 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 + " can't escape backslash, \n gets replaced as \\n + 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 prompt_tmpfile = tempname() + let response_tmpfile = tempname() + call writefile([json_payload], prompt_tmpfile) + + " 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 + silent execute '!'.curl_command + + 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 + let content = strpart(response, content_start, content_end - content_start) + + " Insert the content at the cursor position + 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 + +command! Llm call Llm() -- cgit v1.2.3 From 355c80f49e32b0b15c0a457f3bad380e57f5b9ac Mon Sep 17 00:00:00 2001 From: AustinMroz Date: Sun, 23 Jul 2023 06:16:48 -0500 Subject: examples : simplify vim plugin (#2327) Uses builtin json_encode and json_decode functions to simplify escaping Removes the need for temp files --- examples/llm.vim | 45 +++++---------------------------------------- 1 file changed, 5 insertions(+), 40 deletions(-) (limited to 'examples/llm.vim') diff --git a/examples/llm.vim b/examples/llm.vim index 16e308c..efecad0 100644 --- a/examples/llm.vim +++ b/examples/llm.vim @@ -2,57 +2,22 @@ function! Llm() 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 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 - " can't escape backslash, \n gets replaced as \\n - 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 prompt_tmpfile = tempname() - let response_tmpfile = tempname() - call writefile([json_payload], prompt_tmpfile) + 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 = buffer_content " 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 - silent execute '!'.curl_command - - 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) + let curl_command = 'curl -k -s -X POST -H "Content-Type: application/json" -d @- ' . url + let response = system(curl_command, json_encode(json_payload)) " 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 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 command! Llm call Llm() -- cgit v1.2.3 From 2d7baaf50f3277e65cf71071f61ea34823d14c30 Mon Sep 17 00:00:00 2001 From: AustinMroz Date: Tue, 8 Aug 2023 06:44:48 -0500 Subject: vim : streaming and more (#2495) * Update Vim plugin * Remove getbufoneline usage, Add input bind example. getbufoneline() appears to be a recently added function and has been replaced with getbufline for compatibility. An additional example that explains how to add a keybind that works in insert mode was added. --- examples/llm.vim | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 examples/llm.vim (limited to 'examples/llm.vim') diff --git a/examples/llm.vim b/examples/llm.vim deleted file mode 100644 index efecad0..0000000 --- a/examples/llm.vim +++ /dev/null @@ -1,23 +0,0 @@ -function! Llm() - - let url = "http://127.0.0.1:8080/completion" - - " Get the content of the current buffer - let buffer_content = join(getline(1, '$'), "\n") - - " Create the JSON payload - 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 = buffer_content - - " Define the curl command - let curl_command = 'curl -k -s -X POST -H "Content-Type: application/json" -d @- ' . url - let response = system(curl_command, json_encode(json_payload)) - - " Extract the content field from the response - let content = json_decode(response).content - - " Insert the content at the cursor position - call setline(line('.'), getline('.') . content) -endfunction - -command! Llm call Llm() -- cgit v1.2.3 From e7f94d6fdc83b41ba449b4b8c80821673dd12ffc Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Tue, 8 Aug 2023 15:05:30 +0300 Subject: vim : bring back simple llm.vim example --- examples/llm.vim | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 examples/llm.vim (limited to 'examples/llm.vim') diff --git a/examples/llm.vim b/examples/llm.vim new file mode 100644 index 0000000..473e007 --- /dev/null +++ b/examples/llm.vim @@ -0,0 +1,25 @@ +" Basic plugin example + +function! Llm() + + let url = "http://127.0.0.1:8080/completion" + + " Get the content of the current buffer + let buffer_content = join(getline(1, '$'), "\n") + + " Create the JSON payload + 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 = buffer_content + + " Define the curl command + let curl_command = 'curl -k -s -X POST -H "Content-Type: application/json" -d @- ' . url + let response = system(curl_command, json_encode(json_payload)) + + " Extract the content field from the response + let content = json_decode(response).content + + " Insert the content at the cursor position + call setline(line('.'), getline('.') . content) +endfunction + +command! Llm call Llm() -- cgit v1.2.3 From 7ed8d1fe7f8cbe6a6763e6b46759795ac8d21e12 Mon Sep 17 00:00:00 2001 From: chaihahaha Date: Tue, 8 Aug 2023 20:07:02 +0800 Subject: llm.vim : multiline autocompletion, get rid of "^@" (#2543) --- examples/llm.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'examples/llm.vim') diff --git a/examples/llm.vim b/examples/llm.vim index 473e007..594a285 100644 --- a/examples/llm.vim +++ b/examples/llm.vim @@ -18,8 +18,10 @@ function! Llm() " Extract the content field from the response let content = json_decode(response).content + let split_newlines = split(content, '\n', 1) + " Insert the content at the cursor position - call setline(line('.'), getline('.') . content) + call setline(line('.'), [ getline('.') . split_newlines[0] ] + split_newlines[1:]) endfunction command! Llm call Llm() -- cgit v1.2.3