gptel: Add gptel-add-context function

This commit is contained in:
Alain M. Lafon 2024-04-21 02:45:58 +02:00 committed by Tristan Druyen
parent 8ccdc31b12
commit 3f31258e48
Signed by: tristan
SSH key fingerprint: SHA256:U7y6eMb7CQDaTHv9XoX6/BaQnPqyxxKc+Xnfcefi6rY
2 changed files with 72 additions and 1 deletions

View file

@ -626,6 +626,35 @@ gptel offers a few extra conveniences in Org mode.
- You can declare the gptel model, backend, temperature, system message and other parameters as Org properties with the command =gptel-org-set-properties=. gptel queries under the corresponding heading will always use these settings, allowing you to create mostly reproducible LLM chat notebooks, and to have simultaneous chats with different models, model settings and directives under different Org headings. - You can declare the gptel model, backend, temperature, system message and other parameters as Org properties with the command =gptel-org-set-properties=. gptel queries under the corresponding heading will always use these settings, allowing you to create mostly reproducible LLM chat notebooks, and to have simultaneous chats with different models, model settings and directives under different Org headings.
*** Optional: Add more context to your query
You can add contextual information from any Emacs buffer by utilizing
=gptel-add-context=. Each call will add another snippet including
metadata like buffer-name, LOC and major mode.
1. Select the text you want to add as context. If no text is selected,
the entire content of the current buffer will be used.
2. =gptel-add-context= adds the selected text or the whole buffer
content to the "*gptel-context*" buffer.
3. Proceed with LLM interactions using =gptel= as usual. The added
context will influence the LLM's responses, making them more
relevant and contextualized.
4. At any point, you can manually edit the "*gptel-context*" buffer to
remove stale information.
**** Practical Applications
- Enhancing code development sessions with relevant documentation or
code snippets as a reference.
- Accumulating research notes or sources while writing papers or
articles to ensure consistency in the narrative or arguments.
- Providing detailed error logs or system information during debugging
sessions to assist in generating more accurate solutions or
suggestions from the LLM.
** FAQ ** FAQ
#+html: <details><summary> #+html: <details><summary>
**** I want the window to scroll automatically as the response is inserted **** I want the window to scroll automatically as the response is inserted

View file

@ -174,6 +174,15 @@
"Use `gptel-make-openai' instead." "Use `gptel-make-openai' instead."
"0.5.0") "0.5.0")
(defcustom gptel-context-prompt-preamble
"Here, you find more context for the following user prompts. Key aspects are:
- User inputs are encapsulated within Emacs Org mode src blocks.
- Naming Convention: Each src block is identified using a structured name format '{{name-of-original-buffer}}:{{beginning-line-number}}-{{ending-line-number}}'. This scheme offers insight into the origin and scope of the code or text snippet.
- Mode Indication: The mode of the original file is included within each src block. This detail informs you about the programming language or markup format of the snippet, aiding in accurate interpretation and response."
"Instruct the llm about how to treat the additional context from *gptel-context*."
:group 'gptel
:type 'string)
(defcustom gptel-proxy "" (defcustom gptel-proxy ""
"Path to a proxy to use for gptel interactions. "Path to a proxy to use for gptel interactions.
Passed to curl via --proxy arg, for example \"proxy.yourorg.com:80\" Passed to curl via --proxy arg, for example \"proxy.yourorg.com:80\"
@ -887,7 +896,7 @@ Model parameters can be let-bound around calls to this function."
((markerp position) position) ((markerp position) position)
((integerp position) ((integerp position)
(set-marker (make-marker) position buffer)))) (set-marker (make-marker) position buffer))))
(full-prompt (full-prompt-draft
(cond (cond
((null prompt) (gptel--create-prompt start-marker)) ((null prompt) (gptel--create-prompt start-marker))
((stringp prompt) ((stringp prompt)
@ -898,6 +907,17 @@ Model parameters can be let-bound around calls to this function."
(insert prompt) (insert prompt)
(gptel--create-prompt)))) (gptel--create-prompt))))
((consp prompt) prompt))) ((consp prompt) prompt)))
(context-prompt
(when (get-buffer "*gptel-context*")
(list :role "user"
:content (format "%s\n\n%s"
gptel-context-prompt-preamble
(with-current-buffer "*gptel-context*"
(save-excursion
(buffer-substring-no-properties (point-min) (point-max))))))))
(full-prompt (if context-prompt
(append (list context-prompt) full-prompt-draft)
full-prompt-draft))
(request-data (gptel--request-data gptel-backend full-prompt)) (request-data (gptel--request-data gptel-backend full-prompt))
(info (list :data request-data (info (list :data request-data
:buffer buffer :buffer buffer
@ -1361,6 +1381,28 @@ context for the ediff session."
(goto-char (+ beg offset)) (goto-char (+ beg offset))
(pulse-momentary-highlight-region beg (+ beg (length alt-response))))) (pulse-momentary-highlight-region beg (+ beg (length alt-response)))))
;;;###autoload
(defun gptel-add-context ()
"Add selected region (or whole buffer) to *gptel-context*."
(interactive)
(let* ((context (if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(buffer-substring-no-properties (point-min) (point-max))))
(src-name (buffer-name))
(start (line-number-at-pos (region-beginning)))
(end (line-number-at-pos (region-end)))
(region-major-mode (symbol-name major-mode))
(loc (format "%s:%d-%d" src-name start end)))
(with-current-buffer (get-buffer-create "*gptel-context*")
(org-mode)
(goto-char (point-max))
(unless (bolp) (insert "\n"))
(insert (format "#+NAME: %s\n" loc))
(insert (format "#+BEGIN_SRC %s\n" region-major-mode))
(insert (format "%s\n" context))
(insert "#+END_SRC\n\n"))
(message "Context has been added to *gptel-context*.")))
(defun gptel--next-variant (&optional arg) (defun gptel--next-variant (&optional arg)
"Switch to next gptel-response at this point, if it exists." "Switch to next gptel-response at this point, if it exists."
(interactive "p") (interactive "p")