[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/gptel 75059630a0 1/2: gptel: Linting and utility functions
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/gptel 75059630a0 1/2: gptel: Linting and utility functions |
Date: |
Thu, 26 Sep 2024 04:00:34 -0400 (EDT) |
branch: elpa/gptel
commit 75059630a0fb7396650f4f44583dca9553471468
Author: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
gptel: Linting and utility functions
* gptel.el (gptel--inspect-query, gptel--strip-mode-suffix,
gptel-prompt-prefix-string, gptel-response-prefix-string,
gptel--numberize, gptel--to-number gptel-track-response): Add
utility functions `gptel--to-string' and `gptel--intern'. Rename
`gptel--numberize' to `gptel--to-number'. Linting.
* gptel-org.el (gptel-org-set-properties,
gptel-org--entry-properties, gptel-org--send-with-props,
gptel-org-set-topic, gptel--num-messages-to-send): Linting.
---
gptel-org.el | 26 ++++++++++++++++++--------
gptel.el | 33 ++++++++++++++++++++++++++++-----
2 files changed, 46 insertions(+), 13 deletions(-)
diff --git a/gptel-org.el b/gptel-org.el
index 3102e3152f..3f57cc709c 100644
--- a/gptel-org.el
+++ b/gptel-org.el
@@ -30,7 +30,15 @@
(declare-function org-element-begin "org-element")
;; Functions used for saving/restoring gptel state in Org buffers
+(defvar gptel--num-messages-to-send)
(defvar org-entry-property-inherited-from)
+(defvar gptel-backend)
+(defvar gptel--known-backends)
+(declare-function gptel--to-string "gptel")
+(declare-function gptel--intern "gptel")
+(declare-function gptel--get-buffer-bounds "gptel")
+(declare-function gptel-backend-name "gptel")
+(declare-function gptel--parse-buffer "gptel")
(declare-function org-entry-get "org")
(declare-function org-entry-put "org")
(declare-function org-with-wide-buffer "org-macs")
@@ -122,7 +130,7 @@ This makes it feasible to have multiple conversation
branches."
(marker-position org-entry-property-inherited-from)))
(defun gptel-org-set-topic (topic)
- "Set a topic and limit this conversation to the current heading.
+ "Set a TOPIC and limit this conversation to the current heading.
This limits the context sent to the LLM to the text between the
current heading and the cursor position."
@@ -130,7 +138,7 @@ current heading and the cursor position."
(list
(progn
(or (derived-mode-p 'org-mode)
- (user-error "Support for multiple topics per buffer is only
implemented for `org-mode'."))
+ (user-error "Support for multiple topics per buffer is only
implemented for `org-mode'"))
(completing-read "Set topic as: "
(org-property-values "GPTEL_TOPIC")
nil nil (downcase
@@ -212,7 +220,9 @@ value of `gptel-org-branching-context', which see."
If in an Org buffer under a heading containing a stored gptel
configuration, use that for requests instead. This includes the
system message, model and provider (backend), among other
-parameters."
+parameters.
+
+ARGS are the original function call arguments."
(if (derived-mode-p 'org-mode)
(pcase-let ((`(,gptel--system-message ,gptel-backend ,gptel-model
,gptel-temperature ,gptel-max-tokens)
@@ -234,7 +244,7 @@ parameters."
;;; Saving and restoring state
(defun gptel-org--entry-properties (&optional pt)
- "Find gptel configuration properties stored in the current heading."
+ "Find gptel configuration properties stored at PT."
(pcase-let
((`(,system ,backend ,model ,temperature ,tokens ,num)
(mapcar
@@ -248,9 +258,9 @@ parameters."
(setq backend (alist-get backend gptel--known-backends
nil nil #'equal)))
(when temperature
- (setq temperature (gptel--numberize temperature)))
- (when tokens (setq tokens (gptel--numberize tokens)))
- (when num (setq num (gptel--numberize num)))
+ (setq temperature (gptel--to-number temperature)))
+ (when tokens (setq tokens (gptel--to-number tokens)))
+ (when num (setq num (gptel--to-number num)))
(list system backend model temperature tokens num)))
(defun gptel-org--restore-state ()
@@ -300,7 +310,7 @@ non-nil (default), display a message afterwards."
(org-entry-put pt "GPTEL_NUM_MESSAGES_TO_SEND"
(number-to-string gptel--num-messages-to-send)))
(org-entry-put pt "GPTEL_SYSTEM"
- (string-replace "\n" "\\n" gptel--system-message))
+ (string-replace "\n" "\\n" gptel--system-message))
(when gptel-max-tokens
(org-entry-put
pt "GPTEL_MAX_TOKENS" (number-to-string gptel-max-tokens)))
diff --git a/gptel.el b/gptel.el
index cc76814c1b..7b8ccf6e5e 100644
--- a/gptel.el
+++ b/gptel.el
@@ -519,7 +519,7 @@ distinction is necessary for back-and-forth conversation
with an
LLM.
In regular Emacs buffers you can turn this behavior off by
-setting `gptel-track-response' to `nil'. All text, including
+setting `gptel-track-response' to nil. All text, including
past LLM responses, is then treated as user input when sending
queries.
@@ -593,13 +593,26 @@ and \"apikey\" as USER."
(error "`gptel-api-key' is not valid")))
(t (error "`gptel-api-key' is not valid")))))
-(defsubst gptel--numberize (val)
+(defsubst gptel--to-number (val)
"Ensure VAL is a number."
(cond
((numberp val) val)
((stringp val) (string-to-number val))
((error "%S cannot be converted to a number" val))))
+(defsubst gptel--to-string (s)
+ "Convert S to a string, if possible."
+ (cl-etypecase s
+ (symbol (symbol-name s))
+ (string s)
+ (number (number-to-string s))))
+
+(defsubst gptel--intern (s)
+ "Intern S, if possible."
+ (cl-etypecase s
+ (symbol s)
+ (string (intern s))))
+
(defun gptel-auto-scroll ()
"Scroll window if LLM response continues below viewport.
@@ -647,11 +660,21 @@ Note: This will move the cursor."
,(macroexp-progn body)))
(defun gptel-prompt-prefix-string ()
+ "Prefix before user prompts in `gptel-mode'."
(or (alist-get major-mode gptel-prompt-prefix-alist) ""))
(defun gptel-response-prefix-string ()
+ "Prefix before LLM responses in `gptel-mode'."
(or (alist-get major-mode gptel-response-prefix-alist) ""))
+(defsubst gptel--trim-prefixes (s)
+ "Remove prompt/response prefixes from string S."
+ (string-trim s
+ (format "[\t\r\n ]*\\(?:%s\\)?[\t\r\n ]*"
+ (regexp-quote (gptel-prompt-prefix-string)))
+ (format "[\t\r\n ]*\\(?:%s\\)?[\t\r\n ]*"
+ (regexp-quote (gptel-response-prefix-string)))))
+
(defvar-local gptel--backend-name nil
"Store to persist backend name across Emacs sessions.
@@ -693,9 +716,9 @@ in any way.")
(get-char-property (or pt (point)) 'gptel-history))
(defun gptel--strip-mode-suffix (mode-sym)
- "Remove the -mode suffix from MODE-NAME.
+ "Remove the -mode suffix from MODE-SYM.
-MODE-NAME is typically a major-mode symbol."
+MODE-SYM is typically a major-mode symbol."
(let ((mode-name (thread-last
(symbol-name mode-sym)
(string-remove-suffix "-mode")
@@ -1041,7 +1064,7 @@ waiting for the response."
"Show REQUEST-DATA, the full LLM query to be sent, in a buffer.
This functions as a dry run of `gptel-send'. If ARG is
-the symbol json, show the encoded JSON query instead of the lisp
+the symbol json, show the encoded JSON query instead of the Lisp
structure gptel uses."
(with-current-buffer (get-buffer-create "*gptel-query*")
(let ((standard-output (current-buffer))