-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathollama.el
112 lines (93 loc) · 3.23 KB
/
ollama.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
;;; ollama.el --- ollama client for Emacs
;; Copyright (C) 2023 ZHOU Feng
;; Author: ZHOU Feng <[email protected]>
;; URL: http://github.com/zweifisch/ollama
;; Keywords: ollama llama2
;; Version: 0.0.1
;; Created: 6th Aug 2023
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; ollama client for Emacs
;;
;;; Code:
(require 'json)
(require 'cl-lib)
(require 'url)
(defgroup ollama nil
"Ollama client for Emacs."
:group 'ollama)
(defcustom ollama:endpoint "http://localhost:11434/api/generate"
"Ollama http service endpoint."
:group 'ollama
:type 'string)
(defcustom ollama:model "llama2-uncensored"
"Ollama model."
:group 'ollama
:type 'string)
(defcustom ollama:language "Chinese"
"Language to translate to."
:group 'ollama
:type 'string)
(defun ollama-fetch (url prompt model)
(let* ((url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/json")))
(url-request-data
(encode-coding-string
(json-encode `((model . ,model) (prompt . ,prompt)))
'utf-8)))
(with-current-buffer (url-retrieve-synchronously url)
(goto-char url-http-end-of-headers)
(decode-coding-string
(buffer-substring-no-properties
(point)
(point-max))
'utf-8))))
(defun ollama-get-response-from-line (line)
(cdr
(assoc 'response
(json-read-from-string line))))
(defun ollama-prompt (url prompt model)
(mapconcat 'ollama-get-response-from-line
(cl-remove-if #'(lambda (str) (string= str ""))
(split-string (ollama-fetch url prompt model) "\n")) ""))
;;;###autoload
(defun ollama-prompt-line ()
"Prompt with current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (thing-at-point 'line) ollama:model))))
;;;###autoload
(defun ollama-define-word ()
"Find definition of current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "define %s" (thing-at-point 'word)) ollama:model))))
;;;###autoload
(defun ollama-translate-word ()
"Translate current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "translate \"%s\" to %s" (thing-at-point 'word) ollama:language) ollama:model))))
;;;###autoload
(defun ollama-summarize-region ()
"Summarize marked text."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "summarize \"\"\"%s\"\"\"" (buffer-substring (region-beginning) (region-end))) ollama:model))))
(provide 'ollama)
;;; ollama.el ends here