gptel: Add gptel-add-context
function
This commit is contained in:
parent
8ccdc31b12
commit
3f31258e48
2 changed files with 72 additions and 1 deletions
29
README.org
29
README.org
|
@ -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.
|
||||
|
||||
*** 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
|
||||
#+html: <details><summary>
|
||||
**** I want the window to scroll automatically as the response is inserted
|
||||
|
|
44
gptel.el
44
gptel.el
|
@ -174,6 +174,15 @@
|
|||
"Use `gptel-make-openai' instead."
|
||||
"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 ""
|
||||
"Path to a proxy to use for gptel interactions.
|
||||
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)
|
||||
((integerp position)
|
||||
(set-marker (make-marker) position buffer))))
|
||||
(full-prompt
|
||||
(full-prompt-draft
|
||||
(cond
|
||||
((null prompt) (gptel--create-prompt start-marker))
|
||||
((stringp prompt)
|
||||
|
@ -898,6 +907,17 @@ Model parameters can be let-bound around calls to this function."
|
|||
(insert prompt)
|
||||
(gptel--create-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))
|
||||
(info (list :data request-data
|
||||
:buffer buffer
|
||||
|
@ -1361,6 +1381,28 @@ context for the ediff session."
|
|||
(goto-char (+ beg offset))
|
||||
(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)
|
||||
"Switch to next gptel-response at this point, if it exists."
|
||||
(interactive "p")
|
||||
|
|
Loading…
Add table
Reference in a new issue