Skip to content
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

Allow for prepending (instead of appending) context to the system message #414

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
10 changes: 5 additions & 5 deletions gptel-context.el
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ ADVANCE controls the overlay boundary behavior."

MESSAGE is usually either the system message or the user prompt.
The accumulated context from CONTEXTS is appended or prepended to
it, respectively."
;; Append context before/after system message.
it, as requested."
(let ((context-string (gptel-context--string contexts)))
(if (> (length context-string) 0)
(pcase-exhaustive gptel-use-context
('system (concat message "\n\n" context-string))
('user (concat context-string "\n\n" message))
('nil message))
('system (concat message "\n\n" context-string))
('system-prepend (concat context-string "\n\n" message))
('user (concat context-string "\n\n" message))
('nil message))
message)))

(defun gptel-context--collect-media (&optional contexts)
Expand Down
7 changes: 5 additions & 2 deletions gptel-rewrite.el
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,11 @@ the changed regions. BUF is the (current) buffer."
(let* ((prompt (buffer-substring-no-properties
(region-beginning) (region-end)))
(gptel--system-message (or rewrite-message gptel--rewrite-message))
;; always send context with system message
(gptel-use-context (and gptel-use-context 'system)))
(gptel-use-context (or
;; Never send context with user prompt; instead,
;; send with with system message (append).
(and (eq gptel-use-context 'user) 'system)
gptel-use-context)))
(deactivate-mark)
(gptel-request prompt
:context
Expand Down
14 changes: 8 additions & 6 deletions gptel-transient.el
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,16 @@ value of `gptel-use-context', set from here."
:format " %k %d %v"
:set-value #'gptel--set-with-scope
:display-nil "No"
:display-map '((nil . "No")
(system . "with system message")
(user . "with user prompt"))
:display-map '((nil . "No")
(system . "with system message (append)")
(system-prepend . "with system message (prepend)")
(user . "with user prompt"))
:key "-i"
:reader (lambda (prompt &rest _)
(let* ((choices '(("No" . nil)
("with system message" . system)
("with user prompt" . user)))
(let* ((choices '(("No" . nil)
("with system message (append)" . system)
("with system message (prepend)" . system-prepend)
("with user prompt" . user)))
(destination (completing-read prompt choices nil t)))
(cdr (assoc destination choices)))))

Expand Down
13 changes: 8 additions & 5 deletions gptel.el
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,15 @@ included in the request.

Currently supported options are:

nil - Do not use the context.
system - Include the context with the system message.
user - Include the context with the user prompt."
nil - Do not use the context.
system - Include the context with the system message (append).
system-prepend - Include the context with the system message (prepend).
user - Include the context with the user prompt."
:group 'gptel
:type '(choice
(const :tag "Don't include context" nil)
(const :tag "With system message" system)
(const :tag "With system message (append)" system)
(const :tag "With system message (prepend)" system-prepend)
(const :tag "With user prompt" user)))

(defvar-local gptel--old-header-line nil)
Expand Down Expand Up @@ -1148,7 +1150,8 @@ Model parameters can be let-bound around calls to this function."
(let* ((gptel--system-message
;Add context chunks to system message if required
(if (and gptel-context--alist
(eq gptel-use-context 'system))
(or (eq gptel-use-context 'system)
(eq gptel-use-context 'system-prepend)))
(gptel-context--wrap system)
system))
(gptel-stream stream)
Expand Down