From 0d06a15e94f735e11b133d1a47ef69177ac379f0 Mon Sep 17 00:00:00 2001 From: Thomas Schwinge Date: Thu, 10 Oct 2024 22:16:15 +0200 Subject: [PATCH] Allow for prepending (instead of appending) context to the system message ... via new 'system-prepend'. Certain use cases/LLMs may prefer to first see the context, then the system message. --- gptel-context.el | 10 +++++----- gptel-rewrite.el | 7 +++++-- gptel-transient.el | 14 ++++++++------ gptel.el | 13 ++++++++----- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/gptel-context.el b/gptel-context.el index a31c315..0541a9e 100644 --- a/gptel-context.el +++ b/gptel-context.el @@ -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) diff --git a/gptel-rewrite.el b/gptel-rewrite.el index 68c22c0..0f767c2 100644 --- a/gptel-rewrite.el +++ b/gptel-rewrite.el @@ -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 diff --git a/gptel-transient.el b/gptel-transient.el index dfe61d4..a567074 100644 --- a/gptel-transient.el +++ b/gptel-transient.el @@ -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))))) diff --git a/gptel.el b/gptel.el index e62bb3e..0acdbd8 100644 --- a/gptel.el +++ b/gptel.el @@ -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) @@ -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)