|
| 1 | +function! Llm() |
| 2 | + |
| 3 | + let url = "http://127.0.0.1:8080/completion" |
| 4 | + |
| 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 | + |
| 12 | + " Get the content of the current buffer |
| 13 | + let buffer_content = join(getline(1, '$'), "\n") |
| 14 | + |
| 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 | + |
| 22 | + " 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) |
| 29 | + |
| 30 | + " 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) |
| 39 | + |
| 40 | + " Extract the content field from the response |
| 41 | + let content = strpart(response, content_start, content_end - content_start) |
| 42 | + |
| 43 | + " Insert the content at the cursor position |
| 44 | + 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) |
| 56 | +endfunction |
| 57 | + |
| 58 | +command! Llm call Llm() |
0 commit comments