Skip to content

Commit

Permalink
Allow for prepending (instead of appending) context to the system mes…
Browse files Browse the repository at this point in the history
…sage

... via new 'system-prepend'.

Certain use cases/LLMs may prefer to first see the context, then the system
message.
  • Loading branch information
tschwinge committed Oct 15, 2024
1 parent 3d109bc commit 0d06a15
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
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

0 comments on commit 0d06a15

Please sign in to comment.