branch: elpa/gptel commit 6f5df772b55e231fc8271787677fbab825e80489 Author: John Wiegley <jo...@newartisans.com> Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
gptel: Add prompt-transform functions * gptel.el: (gptel-prompt-filter-hook, gptel-prompt-transform-functions): Introduce a hook to hold transformations to apply to the prompt. This will be used to hold functions for context injection, Org property stripping and so on, as well as just-in-time preset application and any RAG plugins. This hook will be passed explicitly to `gptel-request' as an argument, since `gptel-request' is supposed to be a simple function by default, with behavior that depends as little on global variables as possible. Obsolete `gptel-prompt-filter-hook', since it is superseded by `gptel-prompt-transform-functions'. This hook currently does nothing, and will be added to the `gptel-request' pipeline next. (gptel--with-buffer-copy, gptel--create-prompt-buffer): Remove gptel-prompt-filter-hook from the prompt-buffer creation pipeline. * gptel-org.el (gptel-org--create-prompt): Remove gptel-prompt-filter-hook from the prompt-buffer creation pipeline. --- gptel-org.el | 2 -- gptel.el | 37 ++++++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/gptel-org.el b/gptel-org.el index 3d32024f09..003e8af38a 100644 --- a/gptel-org.el +++ b/gptel-org.el @@ -248,7 +248,6 @@ depend on the value of `gptel-org-branching-context', which see." (gptel-org--unescape-tool-results) (gptel-org--strip-block-headers) (when gptel-org-ignore-elements (gptel-org--strip-elements)) - (save-excursion (run-hooks 'gptel-prompt-filter-hook)) (current-buffer)))) ;; Create prompt the usual way (let ((org-buf (current-buffer)) @@ -257,7 +256,6 @@ depend on the value of `gptel-org-branching-context', which see." (gptel-org--unescape-tool-results) (gptel-org--strip-block-headers) (when gptel-org-ignore-elements (gptel-org--strip-elements)) - (save-excursion (run-hooks 'gptel-prompt-filter-hook)) (current-buffer)))))) (defun gptel-org--strip-elements () diff --git a/gptel.el b/gptel.el index b492099dbd..ff09431da5 100644 --- a/gptel.el +++ b/gptel.el @@ -286,17 +286,33 @@ The default for windows comes from Microsoft documentation located here: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa" :type 'natnum) -(defcustom gptel-prompt-filter-hook nil - "Hook run to modify the buffer before sending. +(define-obsolete-variable-alias 'gptel-prompt-filter-hook + 'gptel-prompt-transform-functions "0.9.9") + +(defcustom gptel-prompt-transform-functions nil + "Handlers to augment or transform a query before sending it. This hook is called in a temporary buffer containing the text to be sent, with the cursor at the end of the prompt. You can use it to modify the buffer as required. -Example: A typical use case might be to search for occurrences of -$(cmd) and replace it with the output of the shell command cmd, -making it easy to send the output of shell commands to the LLM." - :group 'gptel +Example: A typical use case might be to search for occurrences of $(cmd) +and replace it with the output of the shell command cmd, making it easy +to send the output of shell commands to the LLM. + +Transform functions can be synchronous or asynchronous. + +Synchronous hook functions must accept zero or one argument: the INFO +plist for the current request. + +Asynchronous hook functions must accept two arguments: a callback to +call after the transformation is complete, and the INFO plist for the +current request. + +Note that while this set of handlers can certainly be set with a global +value to be applied to all queries in all buffers, it meant to be set +locally for a specific buffer, or chat topic, or only the context of a +certain task." :type 'hook) (defcustom gptel-post-request-hook nil @@ -1025,9 +1041,8 @@ If positions START and END are provided, insert that part of BUF first." (dolist (sym '( gptel-backend gptel--system-message gptel-model gptel-mode gptel-track-response gptel-track-media gptel-use-tools gptel-tools gptel-use-curl - gptel-prompt-filter-hook gptel-use-context - gptel--num-messages-to-send gptel-stream - gptel-include-reasoning + gptel-use-context gptel--num-messages-to-send + gptel-stream gptel-include-reasoning gptel-temperature gptel-max-tokens gptel-cache)) (set (make-local-variable sym) (buffer-local-value sym ,buf))) @@ -2408,13 +2423,11 @@ be used to rerun or continue the request at a later time." ((stringp prompt) (gptel--with-buffer-copy buffer nil nil (insert prompt) - (save-excursion (run-hooks 'gptel-prompt-filter-hook)) (current-buffer))) ((consp prompt) ;; (gptel--parse-list gptel-backend prompt) (gptel--with-buffer-copy buffer nil nil (gptel--parse-list-and-insert prompt) - (save-excursion (run-hooks 'gptel-prompt-filter-hook)) (current-buffer))))) (info (list :data prompt-buffer :buffer buffer @@ -2718,11 +2731,9 @@ current buffer up to point, or PROMPT-END if provided." ((use-region-p) (let ((rb (region-beginning)) (re (region-end))) (gptel--with-buffer-copy buf rb re - (save-excursion (run-hooks 'gptel-prompt-filter-hook)) (current-buffer)))) (t (unless prompt-end (setq prompt-end (point))) (gptel--with-buffer-copy buf (point-min) prompt-end - (save-excursion (run-hooks 'gptel-prompt-filter-hook)) (current-buffer)))))) ;; NOTE: prompts is modified in place here ;; (gptel--wrap-user-prompt-maybe prompts)