forked from s-kostyaev/ellama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ellama.el
425 lines (380 loc) · 13.5 KB
/
ellama.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
;;; ellama.el --- Tool for interacting with LLMs -*- lexical-binding: t -*-
;; Copyright (C) 2023 Sergey Kostyaev
;; Author: Sergey Kostyaev <sskostyaev@gmail.com>
;; URL: http://github.com/s-kostyaev/ellama
;; Keywords: help local tools
;; Package-Requires: ((emacs "28.1") (llm "0.5.2") (spinner "1.7.4"))
;; Version: 0.1.0
;; Created: 8th Oct 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:
;;
;; Ellama is a tool for interacting with large language models from Emacs.
;; It allows you to ask questions and receive responses from the
;; LLMs. Ellama can perform various tasks such as translation, code
;; review, summarization, enhancing grammar/spelling or wording and
;; more through the Emacs interface. Ellama natively supports streaming
;; output, making it effortless to use with your preferred text editor.
;;
;;; Code:
(require 'json)
(require 'llm)
(require 'llm-ollama)
(require 'spinner)
(defgroup ellama nil
"Tool for interacting with LLMs for Emacs."
:group 'ellama)
(defcustom ellama-buffer "*ellama*" "Default ellama buffer."
:group 'ellama
:type 'string)
(defcustom ellama-user-nick "User" "User nick in logs."
:group 'ellama
:type 'string)
(defcustom ellama-assistant-nick "Ellama" "Assistant nick in logs."
:group 'ellama
:type 'string)
(defcustom ellama-buffer-mode 'markdown-mode "Major mode for ellama logs."
:group 'ellama
:type 'function)
(defcustom ellama-language "English" "Language for ellama translation."
:group 'ellama
:type 'string)
(defcustom ellama-provider
(make-llm-ollama
:chat-model "zephyr" :embedding-model "zephyr")
"Backend LLM provider."
:group 'ellama
:type '(sexp :validate 'cl-struct-p))
(defcustom ellama-spinner-type 'progress-bar "Spinner type for ellama."
:group 'ellama
:type 'symbol)
(defvar-local ellama--chat-prompt nil)
(defvar ellama--code-prefix
(rx (minimal-match
(zero-or-more anything) (literal "```") (zero-or-more anything) line-end)))
(defvar ellama--code-suffix
(rx (minimal-match
(literal "```") (zero-or-more anything))))
(defun ellama-stream (prompt &rest args)
"Query ellama for PROMPT.
ARGS contains keys for fine control.
:buffer BUFFER -- BUFFER is the buffer (or `buffer-name') to insert ellama reply
in. Default value is (current-buffer).
:point POINT -- POINT is the point in buffer to insert ellama reaply at."
(let* ((buffer (or (plist-get args :buffer) (current-buffer)))
(point (or (plist-get args :point)
(with-current-buffer buffer (point)))))
(with-current-buffer buffer
(save-excursion
(let* ((start (make-marker))
(end (make-marker))
(insert-text
(lambda (text)
;; Erase and insert the new text between the marker cons.
(with-current-buffer (marker-buffer start)
(save-excursion
(goto-char start)
(delete-region start end)
(insert text))))))
(set-marker start point)
(set-marker end point)
(set-marker-insertion-type start nil)
(set-marker-insertion-type end t)
(spinner-start ellama-spinner-type)
(llm-chat-streaming ellama-provider
(llm-make-simple-chat-prompt prompt)
insert-text
(lambda (text)
(funcall insert-text text)
(with-current-buffer buffer
(spinner-stop)))
(lambda (_ msg) (error "Error calling the LLM: %s" msg))))))))
(defun ellama-stream-filter (prompt prefix suffix buffer point)
"Query ellama for PROMPT with filtering.
In BUFFER at POINT will be inserted result between PREFIX and SUFFIX."
(with-current-buffer buffer
(save-excursion
(let* ((start (make-marker))
(end (make-marker))
(insert-text (lambda (text)
;; Erase and insert the new text between the marker cons.
(with-current-buffer (marker-buffer start)
(save-excursion
(goto-char start)
(delete-region start end)
;; remove prefix and suffix parts
(insert (string-trim-right
(string-trim-left text prefix)
suffix)))))))
(set-marker start point)
(set-marker end point)
(set-marker-insertion-type start nil)
(set-marker-insertion-type end t)
(spinner-start ellama-spinner-type)
(llm-chat-streaming ellama-provider
(llm-make-simple-chat-prompt prompt)
insert-text
(lambda (text)
(funcall insert-text text)
(with-current-buffer buffer
(spinner-stop)))
(lambda (_ msg) (error "Error calling the LLM: %s" msg)))))))
;;;###autoload
(defun ellama-chat (prompt)
"Send PROMPT to ellama chat with conversation history."
(interactive "sAsk ellama: ")
(while (not (buffer-live-p (get-buffer ellama-buffer)))
(get-buffer-create ellama-buffer)
(with-current-buffer ellama-buffer
(funcall ellama-buffer-mode)))
(with-current-buffer ellama-buffer
(display-buffer ellama-buffer)
(if ellama--chat-prompt
(llm-chat-prompt-append-response
ellama--chat-prompt prompt)
(setq ellama--chat-prompt (llm-make-simple-chat-prompt prompt)))
(save-excursion
(goto-char (point-max))
(insert "## " ellama-user-nick ":\n" prompt "\n\n"
"## " ellama-assistant-nick ":\n")
(let* ((start (make-marker))
(end (make-marker))
(point (point-max))
(insert-text
(lambda (text)
;; Erase and insert the new text between the marker cons.
(with-current-buffer (marker-buffer start)
(save-excursion
(goto-char start)
(delete-region start end)
(insert text))))))
(set-marker start point)
(set-marker end point)
(set-marker-insertion-type start nil)
(set-marker-insertion-type end t)
(spinner-start ellama-spinner-type)
(llm-chat-streaming ellama-provider
ellama--chat-prompt
insert-text
(lambda (text)
(funcall insert-text text)
(with-current-buffer ellama-buffer
(save-excursion
(goto-char (point-max))
(insert "\n\n"))
(spinner-stop)))
(lambda (_ msg) (error "Error calling the LLM: %s" msg)))))))
;;;###autoload
(defalias 'ellama-ask 'ellama-chat)
;;;###autoload
(defun ellama-ask-about ()
"Ask ellama about selected region or current buffer."
(interactive)
(let ((input (read-string "Ask ellama about this text: "))
(text (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(buffer-substring-no-properties (point-min) (point-max)))))
(ellama-chat (format "Text:\n%s\nRegarding this text, %s" text input))))
(defun ellama-instant (prompt)
"Prompt ellama for PROMPT to reply instantly."
(let ((buffer (get-buffer-create (make-temp-name ellama-buffer))))
(display-buffer buffer)
(ellama-stream prompt :buffer buffer (point-min))))
;;;###autoload
(defun ellama-translate ()
"Ask ellama to translate selected region or word at point."
(interactive)
(if (region-active-p)
(ellama-instant
(format "Translate the following text to %s:\n%s"
ellama-language
(buffer-substring-no-properties (region-beginning) (region-end))))
(ellama-instant
(format "Translate %s to %s" (thing-at-point 'word) ellama-language))))
;;;###autoload
(defun ellama-define-word ()
"Find definition of current word."
(interactive)
(ellama-instant (format "Define %s" (thing-at-point 'word))))
;;;###autoload
(defun ellama-summarize ()
"Summarize selected region or current buffer."
(interactive)
(let ((text (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(buffer-substring-no-properties (point-min) (point-max)))))
(ellama-instant (format "Text:\n%s\nSummarize it." text))))
;;;###autoload
(defun ellama-code-review ()
"Review code in selected region or current buffer."
(interactive)
(let ((text (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(buffer-substring-no-properties (point-min) (point-max)))))
(ellama-instant (format "Review the following code and make concise suggestions:\n```\n%s\n```" text))))
;;;###autoload
(defun ellama-change (change)
"Change selected text or text in current buffer according to provided CHANGE."
(interactive "sWhat needs to be changed: ")
(let* ((beg (if (region-active-p)
(region-beginning)
(point-min)))
(end (if (region-active-p)
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
(kill-region beg end)
(ellama-stream
(format
"Change the following text, %s, just output the final text without additional quotes around it:\n%s"
change text)
:point beg)))
;;;###autoload
(defun ellama-enhance-grammar-spelling ()
"Enhance the grammar and spelling in the currently selected region or buffer."
(interactive)
(ellama-change "improve grammar and spelling"))
;;;###autoload
(defun ellama-enhance-wording ()
"Enhance the wording in the currently selected region or buffer."
(interactive)
(ellama-change "use better wording"))
;;;###autoload
(defun ellama-make-concise ()
"Make the text of the currently selected region or buffer concise and simple."
(interactive)
(ellama-change "make it as simple and concise as possible"))
;;;###autoload
(defun ellama-change-code (change)
"Change selected code or code in current buffer according to provided CHANGE."
(interactive "sWhat needs to be changed in this code: ")
(let* ((beg (if (region-active-p)
(region-beginning)
(point-min)))
(end (if (region-active-p)
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
(kill-region beg end)
(ellama-stream-filter
(format
"Regarding the following code, %s, only ouput the result code in format ```language\n...\n```:\n```\n%s\n```"
change text)
ellama--code-prefix
ellama--code-suffix
(current-buffer)
beg)))
;;;###autoload
(defun ellama-enhance-code ()
"Change selected code or code in current buffer according to provided CHANGE."
(interactive)
(let* ((beg (if (region-active-p)
(region-beginning)
(point-min)))
(end (if (region-active-p)
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
(kill-region beg end)
(ellama-stream-filter
(format
"Enhance the following code, only ouput the result code in format ```language\n...\n```:\n```\n%s\n```"
text)
ellama--code-prefix
ellama--code-suffix
(current-buffer)
beg)))
;;;###autoload
(defun ellama-complete-code ()
"Change selected code or code in current buffer according to provided CHANGE."
(interactive)
(let* ((beg (if (region-active-p)
(region-beginning)
(point-min)))
(end (if (region-active-p)
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
(ellama-stream-filter
(format
"Continue the following code, only write new code in format ```language\n...\n```:\n```\n%s\n```"
text)
ellama--code-prefix
ellama--code-suffix
(current-buffer)
end)))
;;;###autoload
(defun ellama-add-code (description)
"Add new code according to DESCRIPTION.
Code will be generated with provided context from selected region or current
buffer."
(interactive "sDescribe the code to be generated: ")
(let* ((beg (if (region-active-p)
(region-beginning)
(point-min)))
(end (if (region-active-p)
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
(ellama-stream-filter
(format
"Context: \n```\n%s\n```\nBased on this context, %s, only ouput the result in format ```\n...\n```"
text description)
ellama--code-prefix
ellama--code-suffix
(current-buffer)
end)))
;;;###autoload
(defun ellama-render (needed-format)
"Render selected text or text in current buffer as NEEDED-FORMAT."
(interactive)
(let* ((beg (if (region-active-p)
(region-beginning)
(point-min)))
(end (if (region-active-p)
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
(kill-region beg end)
(ellama-stream
(format
"Render the following text as a %s:\n%s"
needed-format text)
:point beg)))
;;;###autoload
(defun ellama-make-list ()
"Create markdown list from active region or current buffer."
(interactive)
(ellama-render "markdown list"))
;;;###autoload
(defun ellama-make-table ()
"Create markdown table from active region or current buffer."
(interactive)
(ellama-render "markdown table"))
(defun ellama-summarize-webpage (url)
"Summarize webpage fetched from URL."
(interactive "sEnter URL you want to summarize: ")
(let ((buffer-name (url-retrieve-synchronously url t)))
;; (display-buffer buffer-name)
(with-current-buffer buffer-name
(goto-char (point-min))
(search-forward "<!DOCTYPE")
(beginning-of-line)
(kill-region (point-min) (point))
(shr-insert-document (libxml-parse-html-region (point-min) (point-max)))
(goto-char (point-min))
(search-forward "<!DOCTYPE")
(beginning-of-line)
(kill-region (point) (point-max))
(ellama-summarize))))
(provide 'ellama)
;;; ellama.el ends here.