gptel-transient: Allow arbitrary system prompts/directives
* gptel.el (gptel-directives): Bump required transient version to 0.4.0. Remove placeholder about hard-coded directives from the docstring for `gptel-directives'. * gptel-transient.el (gptel-system-prompt, gptel-system-prompt--setup): Dynamically generate the system prompt menu in `gptel-menu'. Delete helper functions that hard-coded the list of system prompts/directives before.
This commit is contained in:
parent
09729ee718
commit
706ad703db
2 changed files with 28 additions and 59 deletions
|
@ -103,25 +103,28 @@ Or is it the other way around?"
|
||||||
|
|
||||||
|
|
||||||
;; ** Prefix for setting the system prompt.
|
;; ** Prefix for setting the system prompt.
|
||||||
|
(defun gptel-system-prompt--setup (_)
|
||||||
;; These helper functions are a temporary workaround to fix #45. Once dynamic
|
"Set up suffixes for system prompt."
|
||||||
;; transients are supported, we can do away with all this jank.
|
(transient-parse-suffixes
|
||||||
(defun gptel--system-prompt-programming ()
|
'gptel-system-prompt
|
||||||
(interactive)
|
(cl-loop for (type . prompt) in gptel-directives
|
||||||
(setq gptel--system-message
|
with taken
|
||||||
(alist-get 'programming gptel-directives)))
|
for name = (symbol-name type)
|
||||||
(defun gptel--system-prompt-default ()
|
for key =
|
||||||
(interactive)
|
(let ((idx 0) pos)
|
||||||
(setq gptel--system-message
|
(while (or (not pos) (member pos taken))
|
||||||
(alist-get 'chat gptel-directives)))
|
(setq pos (substring name idx (1+ idx)))
|
||||||
(defun gptel--system-prompt-writing ()
|
(cl-incf idx))
|
||||||
(interactive)
|
(push pos taken)
|
||||||
(setq gptel--system-message
|
pos)
|
||||||
(alist-get 'writing gptel-directives)))
|
collect (list (key-description key) (capitalize name)
|
||||||
(defun gptel--system-prompt-chat ()
|
`(lambda () (interactive)
|
||||||
(interactive)
|
(message "Directive: %s" ,prompt)
|
||||||
(setq gptel--system-message
|
(setq gptel--system-message ,prompt))
|
||||||
(alist-get 'default gptel-directives)))
|
:transient t)
|
||||||
|
into prompt-suffixes
|
||||||
|
finally return (cons (list 'gptel--suffix-system-message)
|
||||||
|
prompt-suffixes))))
|
||||||
|
|
||||||
(transient-define-prefix gptel-system-prompt ()
|
(transient-define-prefix gptel-system-prompt ()
|
||||||
"Change the system prompt to send ChatGPT.
|
"Change the system prompt to send ChatGPT.
|
||||||
|
@ -135,17 +138,12 @@ You are a poet. Reply only in verse.
|
||||||
|
|
||||||
Customize `gptel-directives' for task-specific prompts."
|
Customize `gptel-directives' for task-specific prompts."
|
||||||
[:description
|
[:description
|
||||||
(lambda () (format "Directive: %s"
|
(lambda () (format "Current directive: %s"
|
||||||
(truncate-string-to-width
|
(truncate-string-to-width
|
||||||
gptel--system-message
|
gptel--system-message 100 nil nil t)))
|
||||||
(max (- (window-width) 14) 20) nil nil t)))
|
|
||||||
:class transient-column
|
:class transient-column
|
||||||
:pad-keys t
|
:setup-children gptel-system-prompt--setup
|
||||||
(gptel--suffix-system-message)
|
:pad-keys t])
|
||||||
("p" "Programming" gptel--system-prompt-programming :transient t)
|
|
||||||
("d" "Default" gptel--system-prompt-default :transient t)
|
|
||||||
("w" "Writing" gptel--system-prompt-writing :transient t)
|
|
||||||
("c" "Chat" gptel--system-prompt-chat :transient t)])
|
|
||||||
|
|
||||||
;; ** Prefix for rewriting/refactoring
|
;; ** Prefix for rewriting/refactoring
|
||||||
|
|
||||||
|
@ -171,31 +169,6 @@ Customize `gptel-directives' for task-specific prompts."
|
||||||
(setq gptel--rewrite-message (gptel--rewrite-message)))
|
(setq gptel--rewrite-message (gptel--rewrite-message)))
|
||||||
(transient-setup 'gptel-rewrite-menu))
|
(transient-setup 'gptel-rewrite-menu))
|
||||||
|
|
||||||
;; TODO: Switch to dynamic Transient menus (below) once there's a new Transient release
|
|
||||||
;; (transient-define-prefix gptel-system-prompt ()
|
|
||||||
;; "Change the system prompt to send ChatGPT."
|
|
||||||
;; [:description (lambda () (format "Current directive: %s"
|
|
||||||
;; (truncate-string-to-width gptel--system-message 100 nil nil t)))
|
|
||||||
;; :class transient-column
|
|
||||||
;; :setup-children gptel-system-prompt--setup
|
|
||||||
;; :pad-keys t])
|
|
||||||
|
|
||||||
;; (defun gptel-system-prompt--setup (_)
|
|
||||||
;; "Set up suffixes for system prompt."
|
|
||||||
;; (transient-parse-suffixes
|
|
||||||
;; 'gptel-system-prompt
|
|
||||||
;; (cl-loop for (type . prompt) in gptel-directives
|
|
||||||
;; for name = (symbol-name type)
|
|
||||||
;; for key = (substring name 0 1)
|
|
||||||
;; collect (list (key-description key) (capitalize name)
|
|
||||||
;; `(lambda () (interactive)
|
|
||||||
;; (message "Directive: %s" ,prompt)
|
|
||||||
;; (setq gptel--system-message ,prompt))
|
|
||||||
;; :transient t)
|
|
||||||
;; into prompt-suffixes
|
|
||||||
;; finally return (cons (list 'gptel--suffix-system-message)
|
|
||||||
;; prompt-suffixes))))
|
|
||||||
|
|
||||||
;; * Transient Infixes
|
;; * Transient Infixes
|
||||||
|
|
||||||
;; ** Infixes for model parameters
|
;; ** Infixes for model parameters
|
||||||
|
|
8
gptel.el
8
gptel.el
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
;; Author: Karthik Chikmagalur
|
;; Author: Karthik Chikmagalur
|
||||||
;; Version: 0.3.5
|
;; Version: 0.3.5
|
||||||
;; Package-Requires: ((emacs "27.1") (transient "0.3.7"))
|
;; Package-Requires: ((emacs "27.1") (transient "0.4.0"))
|
||||||
;; Keywords: convenience
|
;; Keywords: convenience
|
||||||
;; URL: https://github.com/karthink/gptel
|
;; URL: https://github.com/karthink/gptel
|
||||||
|
|
||||||
|
@ -158,11 +158,7 @@ request to ChatGPT.
|
||||||
|
|
||||||
Each entry in this alist maps a symbol naming the directive to
|
Each entry in this alist maps a symbol naming the directive to
|
||||||
the string that is sent. To set the directive for a chat session
|
the string that is sent. To set the directive for a chat session
|
||||||
interactively call `gptel-send' with a prefix argument.
|
interactively call `gptel-send' with a prefix argument."
|
||||||
|
|
||||||
Note: Currently the names (default, programming, writing and
|
|
||||||
chat) are hard-coded and only their values may be customized.
|
|
||||||
This will be fixed in an upcoming release."
|
|
||||||
:group 'gptel
|
:group 'gptel
|
||||||
:type '(alist :key-type symbol :value-type string))
|
:type '(alist :key-type symbol :value-type string))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue