Function: gptel--inject-prompt

gptel--inject-prompt is a byte-compiled function defined in gptel-request.el.

Signature

(gptel--inject-prompt BACKEND DATA NEW-PROMPT &optional POSITION)

Documentation

Inject NEW-PROMPT into existing prompts in query DATA.

NEW-PROMPT can be a single message or a list of messages.

If POSITION is
- nil, append NEW-PROMPT at the end of DATA
- a non-negative integer, insert it at that position in DATA.
- a negative integer, insert it there counting from the end.

- Not implemented: a list of accessors, inject it at that position.

This generic implementation handles the Anthropic, OpenAI-compatible and Ollama message formats.

Implementations

(gptel--inject-prompt BACKEND DATA NEW-PROMPT &optional POSITION) in `gptel-request.el'.

Undocumented

Source Code

;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel-request.el
;; FIXME(fsm) unify this with `gptel--inject-media', which is a mess
(cl-defgeneric gptel--inject-prompt
    (_backend data new-prompt &optional position)
  "Inject NEW-PROMPT into existing prompts in query DATA.

NEW-PROMPT can be a single message or a list of messages.

If POSITION is
- nil, append NEW-PROMPT at the end of DATA
- a non-negative integer, insert it at that position in DATA.
- a negative integer, insert it there counting from the end.

- Not implemented: a list of accessors, inject it at that position.

This generic implementation handles the Anthropic,
OpenAI-compatible and Ollama message formats."
  (when (keywordp (car-safe new-prompt)) ;Is new-prompt one or many?
    (setq new-prompt (list new-prompt)))
  (let ((prompts (plist-get data :messages)))
    (pcase position
      ('nil (plist-put data :messages (vconcat prompts new-prompt)))
      ((pred integerp)
       (when (< position 0) (setq position (+ (length prompts) position)))
       (plist-put data :messages (vconcat (substring prompts 0 position)
                                          new-prompt
                                          (substring prompts position)))))))