Skip to content

gptel: Support prefixing dynamic system messages with lambda #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gptel-transient.el
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ Or in an extended conversation:
gptel--read-with-prefix-help)
:reader (lambda (prompt initial history)
(let* ((directive
(car-safe (gptel--parse-directive gptel--system-message 'raw)))
(car-safe (gptel--parse-directive gptel--system-message 'raw t)))
(cycle-prefix (lambda () (interactive)
(gptel--read-with-prefix directive)))
(minibuffer-local-map
Expand Down
16 changes: 13 additions & 3 deletions gptel.el
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ replaced with REPLACEMENT."
from-template width)))
(t "")))

(defun gptel--parse-directive (directive &optional raw)
(defun gptel--parse-directive (directive &optional raw lambda-prefix)
"Parse DIRECTIVE into a backend-appropriate form.

DIRECTIVE is a gptel directive: it can be a string, a list or a
Expand All @@ -1083,11 +1083,21 @@ and a template consisting of alternating user/LLM
records (a list of strings or nil).

If RAW is non-nil, the user/LLM records are not processed and are
returned as a list of strings."
returned as a list of strings.

If LAMBDA-PREFIX is non-nil, prefix dynamic system message with a
lambda sign (λ) and the documentation string, if any."
(and directive
(cl-etypecase directive
(string (list directive))
(function (gptel--parse-directive (funcall directive) raw))
(function (let ((x (gptel--parse-directive (funcall directive) raw)))
(if lambda-prefix
(cons (concat "[λ"
(and-let* ((doc (documentation directive)))
(concat ": " (substring doc nil
(string-match-p "\n" doc))))
"] " (car-safe x)) (cdr x))
x)))
(cons (if raw directive
(cons (car directive)
(gptel--parse-list
Expand Down