[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/ellama 983770a461 38/53: second step to use llm library
From: |
ELPA Syncer |
Subject: |
[elpa] externals/ellama 983770a461 38/53: second step to use llm library for LLM calls - add code filtering |
Date: |
Sun, 17 Dec 2023 18:57:59 -0500 (EST) |
branch: externals/ellama
commit 983770a461ee04f8e34f6a9be1c920221e73c004
Author: Sergey Kostyaev <s.kostyaev@omp.ru>
Commit: Sergey Kostyaev <kostyaev.sergey2@wb.ru>
second step to use llm library for LLM calls - add code filtering
---
ellama.el | 114 ++++++++++++++++++++++++++++++++++----------------------------
1 file changed, 62 insertions(+), 52 deletions(-)
diff --git a/ellama.el b/ellama.el
index ae60ed7b4a..83f572f743 100644
--- a/ellama.el
+++ b/ellama.el
@@ -1,4 +1,4 @@
-;;; ellama.el --- Ollama client for calling local LLMs
+;;; ellama.el --- Ollama client for calling local LLMs -*- lexical-binding: t
-*-
;; Copyright (C) 2023 Sergey Kostyaev
@@ -84,7 +84,7 @@
:chat-model "zephyr" :embedding-model "zephyr")
"Backend LLM provider."
:group 'ellama
- :type 'cl-struct)
+ :type '(sexp :validate 'cl-struct-p))
(defvar-local ellama-context nil "Context that contains ellama conversation
memory.")
@@ -92,15 +92,40 @@
(defvar-local ellama--request nil)
-(defvar-local ellama--extract nil)
-
-(defvar-local ellama--prefix-regexp nil)
-
-(defvar-local ellama--suffix-regexp nil)
-
-(defvar-local ellama--extraction-state 'before)
-
-(defvar-local ellama--line 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-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)
+ (llm-chat-streaming ellama-provider
+ (llm-make-simple-chat-prompt prompt)
+ insert-text
+ insert-text
+ (lambda (_ msg) (error "Error calling the LLM: %s"
msg)))))))
(defun ellama--filter (proc string)
"Filter function for ellama curl process.
@@ -122,27 +147,11 @@ Filter PROC output STRING."
(when-let ((data
(json-parse-string s :object-type 'plist)))
(when-let ((context (plist-get data :context)))
- (setq ellama-context context)
- (setq ellama--extract nil)
- (setq ellama--extraction-state 'before))
+ (setq ellama-context context))
(when-let ((response (plist-get data :response)))
(goto-char (process-mark proc))
- (if ellama--extract
- (progn
- (setq ellama--line (concat ellama--line
response))
- (when (string-suffix-p "\n" ellama--line)
- (pcase ellama--extraction-state
- ('before
- (when (string-match ellama--prefix-regexp
ellama--line)
- (setq ellama--extraction-state
'progress)))
- ('progress
- (if (string-match ellama--suffix-regexp
ellama--line)
- (setq ellama--extraction-state 'after)
- (insert ellama--line)))
- (_ nil))
- (setq ellama--line nil)))
- (insert response)
- (set-marker (process-mark proc) (point))))))
+ (insert response)
+ (set-marker (process-mark proc) (point)))))
(split-string string "\n" t))
(setq ellama--unprocessed-data nil)
(set-marker (process-mark proc) (point))
@@ -150,13 +159,6 @@ Filter PROC output STRING."
(error (setq ellama--unprocessed-data
(car (last (split-string string "\n" t))))))))))
-(defun ellama-setup-extraction (prefix-regexp suffix-regexp)
- "Setup text extraction from ellama response.
-Generation returns only text between PREFIX-REGEXP and SUFFIX-REGEXP."
- (setq ellama--extract t)
- (setq ellama--prefix-regexp prefix-regexp)
- (setq ellama--suffix-regexp suffix-regexp))
-
(defun ellama-query (prompt &rest args)
"Query ellama for PROMPT.
@@ -358,12 +360,14 @@ default. Default value is `ellama-template'."
(point-max)))
(text (buffer-substring-no-properties beg end)))
(kill-region beg end)
- (ellama-setup-extraction "```.*" "```")
- (ellama-query
+ (ellama-stream-filter
(format
- "Regarding the following code, %s, only ouput the result in format
```\n...\n```:\n```\n%s\n```"
+ "Regarding the following code, %s, only ouput the result code in format
```language\n...\n```:\n```\n%s\n```"
change text)
- :buffer (current-buffer))))
+ ellama--code-prefix
+ ellama--code-suffix
+ (current-buffer)
+ beg)))
;;;###autoload
(defun ellama-enhance-code ()
@@ -377,12 +381,14 @@ default. Default value is `ellama-template'."
(point-max)))
(text (buffer-substring-no-properties beg end)))
(kill-region beg end)
- (ellama-setup-extraction "```.*" "```")
- (ellama-query
+ (ellama-stream-filter
(format
- "Enhance the following code, only ouput the result in format
```\n...\n```:\n```\n%s\n```"
+ "Enhance the following code, only ouput the result code in format
```language\n...\n```:\n```\n%s\n```"
text)
- :buffer (current-buffer))))
+ ellama--code-prefix
+ ellama--code-suffix
+ (current-buffer)
+ beg)))
;;;###autoload
(defun ellama-complete-code ()
@@ -395,12 +401,14 @@ default. Default value is `ellama-template'."
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
- (ellama-setup-extraction "```.*" "```")
- (ellama-query
+ (ellama-stream-filter
(format
- "Continue the following code, only ouput the result in format
```\n...\n```:\n```\n%s\n```"
+ "Continue the following code, only write new code in format
```language\n...\n```:\n```\n%s\n```"
text)
- :buffer (current-buffer))))
+ ellama--code-prefix
+ ellama--code-suffix
+ (current-buffer)
+ end)))
;;;###autoload
(defun ellama-add-code (description)
@@ -415,12 +423,14 @@ buffer."
(region-end)
(point-max)))
(text (buffer-substring-no-properties beg end)))
- (ellama-setup-extraction "```.*" "```")
- (ellama-query
+ (ellama-stream-filter
(format
"Context: \n```\n%s\n```\nBased on this context, %s, only ouput the
result in format ```\n...\n```"
text description)
- :buffer (current-buffer))))
+ ellama--code-prefix
+ ellama--code-suffix
+ (current-buffer)
+ end)))
;;;###autoload
- [elpa] externals/ellama b8f3dada5a 40/53: use spinner with llm library, (continued)
- [elpa] externals/ellama b8f3dada5a 40/53: use spinner with llm library, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 42db1e0ba0 42/53: update readme, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 2700be1cf9 44/53: upate llm requirement to fix #8, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 702042c0bf 45/53: Update llm library to fix #9, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 11105e4169 50/53: Update llm dependency, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 2666f37537 01/53: Initial commit, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 14b5b2a351 13/53: add ellama-ask gif, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 6e0b054030 20/53: use defvar-local instead of defvar + make-local-variable, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama c3d90408dd 22/53: rephrase ellama summary in documentation, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 6b0108b8d6 36/53: fix conversation memory (fixes #5), ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 983770a461 38/53: second step to use llm library for LLM calls - add code filtering,
ELPA Syncer <=
- [elpa] externals/ellama 8ba8897578 48/53: Add ellama-ask-selection and ellama-ask-line commands, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama ab02e1721a 46/53: Review fixes, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 0a4c357e21 52/53: Move to GNU Elpa, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama b8ef883fb8 49/53: Update readme, ELPA Syncer, 2023/12/17
- [elpa] externals/ellama 11f601db8e 53/53: Add elpaignore, ELPA Syncer, 2023/12/17