@@ -2,57 +2,22 @@ function! Llm()
2
2
3
3
let url = " http://127.0.0.1:8080/completion"
4
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
5
" Get the content of the current buffer
13
6
let buffer_content = join (getline (1 , ' $' ), " \n " )
14
7
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
8
" 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
29
11
30
12
" 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))
39
15
40
16
" 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
42
18
43
19
" Insert the content at the cursor position
44
20
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
21
endfunction
57
22
58
23
command ! Llm call Llm ()
0 commit comments