[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/ellama c97c4a8096 01/11: Merge duplicate code
From: |
ELPA Syncer |
Subject: |
[elpa] externals/ellama c97c4a8096 01/11: Merge duplicate code |
Date: |
Fri, 22 Dec 2023 15:57:50 -0500 (EST) |
branch: externals/ellama
commit c97c4a809685af04ba163e803ecfbad0f0866bf3
Author: Steven Allen <steven@stebalien.com>
Commit: Steven Allen <steven@stebalien.com>
Merge duplicate code
`ellama-stream`, `ellama-stream-filter`, and `ellama-chat` were mostly
identical so I merged their implementations as much as possible.
---
ellama.el | 164 ++++++++++++++------------------------------------------------
1 file changed, 36 insertions(+), 128 deletions(-)
diff --git a/ellama.el b/ellama.el
index c4c23318f8..025b530bf4 100644
--- a/ellama.el
+++ b/ellama.el
@@ -117,6 +117,10 @@
(rx (minimal-match
(literal "```") (zero-or-more anything))))
+(defun ellama--code-filter (text)
+ "Filter code prefix/suffix from TEXT."
+ (string-trim text ellama--code-prefix ellama--code-suffix))
+
(defun ellama-setup-keymap ()
"Set up the Ellama keymap and bindings."
(interactive)
@@ -179,8 +183,16 @@ 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 (point))))
+ (filter (or (plist-get args :filter) #'identity))
+ (session (plist-get args :session))
+ (errcb (or (plist-get args :on-error) (lambda (msg) (error "Error
calling the LLM: %s" msg))))
+ (donecb (or (plist-get args :on-done) #'ignore)))
(with-current-buffer buffer
+ (if (and session ellama--chat-prompt)
+ (llm-chat-prompt-append-response
+ ellama--chat-prompt prompt)
+ (setq ellama--chat-prompt (llm-make-simple-chat-prompt prompt)))
(unwind-protect
(save-excursion
(let* ((start (make-marker))
@@ -194,7 +206,7 @@ in. Default value is (current-buffer).
(save-excursion
(goto-char start)
(delete-region start end)
- (insert text)
+ (insert (funcall filter text))
(fill-region start (point)))
(goto-char pt))
(when ellama-auto-scroll
@@ -214,69 +226,19 @@ in. Default value is (current-buffer).
(set-marker-insertion-type end t)
(spinner-start ellama-spinner-type)
(llm-chat-streaming ellama-provider
- (llm-make-simple-chat-prompt prompt)
+ ellama--chat-prompt
insert-text
(lambda (text)
(funcall insert-text text)
(with-current-buffer buffer
(undo-amalgamate-change-group
ellama--change-group)
(accept-change-group ellama--change-group)
- (spinner-stop)))
+ (spinner-stop)
+ (funcall donecb)))
(lambda (_ msg)
- (error "Error calling the LLM: %s" msg)
- (cancel-change-group
ellama--change-group)))))))))
-
-(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
- (unwind-protect
- (save-excursion
- (let* ((start (make-marker))
- (end (make-marker))
- (window (selected-window))
- (insert-text
- (lambda (text)
- ;; Erase and insert the new text between the marker cons.
- (with-current-buffer (marker-buffer start)
- (let ((pt (point)))
- (save-excursion
- (goto-char start)
- (delete-region start end)
- ;; remove prefix and suffix parts
- (insert (string-trim-right
- (string-trim-left text prefix)
- suffix))
- (fill-region start (point)))
- (goto-char pt))
- (when ellama-auto-scroll
- (select-window (get-window-with-predicate
- (lambda (_)
- (eq (current-buffer)
- (get-buffer ellama-buffer))))
- t)
- (goto-char (point-max))
- (recenter -1)
- (select-window window))))))
- (setq ellama--change-group (prepare-change-group))
- (activate-change-group ellama--change-group)
- (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
- (undo-amalgamate-change-group
ellama--change-group)
- (accept-change-group ellama--change-group)
- (spinner-stop)))
- (lambda (_ msg)
- (cancel-change-group ellama--change-group)
- (error "Error calling the LLM: %s" msg))))))))
+ (with-current-buffer buffer
+ (cancel-change-group ellama--change-group)
+ (funcall errcb msg))))))))))
;;;###autoload
(defun ellama-chat (prompt)
@@ -288,60 +250,15 @@ In BUFFER at POINT will be inserted result between PREFIX
and SUFFIX."
(funcall ellama-buffer-mode)))
(display-buffer ellama-buffer)
(with-current-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))
- (window (selected-window))
- (insert-text
- (lambda (text)
- ;; Erase and insert the new text between the marker cons.
- (with-current-buffer (marker-buffer start)
- (let ((pt (point)))
- (save-excursion
- (goto-char start)
- (delete-region start end)
- (insert text)
- (fill-region start (point)))
- (goto-char pt))
- (when ellama-auto-scroll
- (select-window (get-window-with-predicate
- (lambda (_)
- (eq (current-buffer)
- (get-buffer ellama-buffer))))
- t)
- (goto-char (point-max))
- (recenter -1)
- (select-window window))))))
- (setq ellama--change-group (prepare-change-group))
- (activate-change-group ellama--change-group)
- (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"))
- (undo-amalgamate-change-group
ellama--change-group)
- (accept-change-group ellama--change-group)
- (spinner-stop)))
- (lambda (_ msg)
- (cancel-change-group ellama--change-group)
- (error "Error calling the LLM: %s" msg)))))))
+ (ellama-stream prompt
+ :session t
+ :on-done (lambda () (save-excursion
+ (goto-char (point-max))
+ (insert "\n\n")))))))
;;;###autoload
(defalias 'ellama-ask 'ellama-chat)
@@ -502,14 +419,12 @@ In BUFFER at POINT will be inserted result between PREFIX
and SUFFIX."
(point-max)))
(text (buffer-substring-no-properties beg end)))
(kill-region beg end)
- (ellama-stream-filter
+ (ellama-stream
(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)))
+ :filter #'ellama--code-filter
+ :point beg)))
;;;###autoload
(defun ellama-enhance-code ()
@@ -523,14 +438,12 @@ In BUFFER at POINT will be inserted result between PREFIX
and SUFFIX."
(point-max)))
(text (buffer-substring-no-properties beg end)))
(kill-region beg end)
- (ellama-stream-filter
+ (ellama-stream
(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)))
+ :filter #'ellama--code-filter
+ :point beg)))
;;;###autoload
(defun ellama-complete-code ()
@@ -543,14 +456,12 @@ In BUFFER at POINT will be inserted result between PREFIX
and SUFFIX."
(region-end)
(point)))
(text (buffer-substring-no-properties beg end)))
- (ellama-stream-filter
+ (ellama-stream
(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)))
+ :filter #'ellama--code-filter
+ :point end)))
;;;###autoload
(defun ellama-add-code (description)
@@ -565,14 +476,11 @@ buffer."
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
- (ellama-stream-filter
+ (ellama-stream
(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)
- (point))))
+ :filter #'ellama--code-filter)))
;;;###autoload
- [elpa] externals/ellama updated (a0aba01fa1 -> cb0fbac035), ELPA Syncer, 2023/12/22
- [elpa] externals/ellama 0463fd4d99 02/11: Document new ellama-stream parameters, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama b1ede862bb 03/11: Fix documentation text width, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama 01bcadadf9 04/11: Fix additional lint errors, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama 2d982274cb 05/11: Pass complete response to on-done function, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama c8bf280bc9 06/11: Improve ellama-stream doc string a bit, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama c97c4a8096 01/11: Merge duplicate code,
ELPA Syncer <=
- [elpa] externals/ellama 510cc0a2ce 07/11: Merge pull request #29 from Stebalien/steb/dry, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama b2082aca99 08/11: Eliminate duplicated code, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama acb2a351dd 09/11: Fix code trimming, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama 37696f7029 10/11: Merge pull request #34 from Stebalien/steb/fix-trim, ELPA Syncer, 2023/12/22
- [elpa] externals/ellama cb0fbac035 11/11: Update version, ELPA Syncer, 2023/12/22