Skip to content

Commit 564b4c8

Browse files
committed
manual: Update
1 parent ab0f847 commit 564b4c8

File tree

1 file changed

+193
-43
lines changed

1 file changed

+193
-43
lines changed

doc/manual.org

Lines changed: 193 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,6 @@ gptel is a Large Language Model client for Emacs, with support for
6767
multiple models and backends. It works in the spirit of Emacs,
6868
available at any time and uniformly in any buffer.
6969

70-
gptel supports the following services.
71-
72-
#+html: <div align="center">
73-
#+attr_texinfo: :columns .2 .7
74-
| LLM Backend | Requires |
75-
|--------------------+----------------------------|
76-
| ChatGPT | [[https://platform.openai.com/account/api-keys][API key]] |
77-
| Anthropic (Claude) | [[https://www.anthropic.com/api][API key]] |
78-
| Gemini | [[https://makersuite.google.com/app/apikey][API key]] |
79-
| Ollama | [[https://ollama.ai/][Ollama running locally]] |
80-
| Llama.cpp | [[https://github.com/ggerganov/llama.cpp/tree/master/examples/server#quick-start][Llama.cpp running locally]] |
81-
| Llamafile | [[https://github.com/Mozilla-Ocho/llamafile#quickstart][Local Llamafile server]] |
82-
| GPT4All | [[https://gpt4all.io/index.html][GPT4All running locally]] |
83-
| Kagi FastGPT | [[https://kagi.com/settings?p=api][API key]] |
84-
| Kagi Summarizer | [[https://kagi.com/settings?p=api][API key]] |
85-
| Azure | Deployment and API key |
86-
| Groq | [[https://console.groq.com/keys][API key]] |
87-
| Perplexity | [[https://docs.perplexity.ai/docs/getting-started][API key]] |
88-
| OpenRouter | [[https://openrouter.ai/keys][API key]] |
89-
| together.ai | [[https://api.together.xyz/settings/api-keys][API key]] |
90-
| Anyscale | [[https://docs.endpoints.anyscale.com/][API key]] |
91-
| PrivateGPT | [[https://github.com/zylon-ai/private-gpt#-documentation][PrivateGPT running locally]] |
92-
| DeepSeek | [[https://platform.deepseek.com/api_keys][API key]] |
93-
| Cerebras | [[https://cloud.cerebras.ai/][API key]] |
94-
| Github Models | [[https://github.com/settings/tokens][Token]] |
95-
| Novita AI | [[https://novita.ai/model-api/product/llm-api?utm_source=github_gptel&utm_medium=github_readme&utm_campaign=link][Token]] |
96-
| xAI | [[https://console.x.ai?utm_source=github_gptel&utm_medium=github_readme&utm_campaign=link][API key]] |
97-
#+html: </div>
98-
9970
** Basic concepts
10071

10172
#+cindex: Large Language Model
@@ -218,7 +189,11 @@ some extra niceties for chat interaction.
218189

219190
** Chat persistence
220191

221-
** The rewrite interface
192+
** TODO The rewrite interface
193+
194+
** TODO gptel in Org mode
195+
196+
gptel offers a few extra conveniences in Org mode.
222197

223198
* TODO gptel's design
224199

@@ -451,14 +426,6 @@ always contains the last request as a gptel state-machine object (see
451426
[[*gptel's finite state machine][gptel's state machine]]).
452427

453428
** TODO gptel chat buffer UI
454-
455-
<<gptel-prompt-prefix-alist>>
456-
#+vindex: gptel-prompt-prefix-alist
457-
- ~gptel-prompt-prefix-alist~
458-
459-
#+vindex: gptel-response-prefix-alist
460-
- ~gptel-response-prefix-alist~
461-
462429
*** gptel in Org mode
463430

464431
#+vindex: gptel-org-branching-context
@@ -828,9 +795,12 @@ To use tools in gptel, you need
828795
currently include any tool specifications out of the box.
829796

830797
*** Obtaining tools
831-
*** Writing tools
798+
*** TODO Writing tools
832799
:PROPERTIES:
833800
:CUSTOM_ID: writing-tools
801+
:LAST_REVIEW: [2025-09-12 Fri]
802+
:NEXT_REVIEW: [2025-09-12 Fri]
803+
:REVIEWS: 0
834804
:END:
835805

836806
A gptel tool is a structure specifying an Elisp function, the format
@@ -903,6 +873,43 @@ arguments.
903873
subsequent requests in the same buffer. This is primarily useful
904874
in chat buffers.
905875

876+
For example, to not prompt for user confirmation when creating files
877+
in the current working directory but in all other directories:
878+
879+
#+begin_src emacs-lisp
880+
(gptel-make-tool
881+
:name \"create_file\"
882+
:description \"Create a new file with the specified content\"
883+
:confirm (lambda (&rest args)
884+
(cl-destructuring-bind (path filename content)
885+
args
886+
(not (my-filepath-within-current-directory-p path))))
887+
:function (lambda (path filename content)
888+
(let ((full-path (expand-file-name filename path)))
889+
(with-temp-buffer
890+
(insert content)
891+
(write-file full-path))
892+
(format \"Created file %s in %s\" filename path)))
893+
:args (list '(:name \"path\"
894+
:type string
895+
:description \"The directory where to create the file\")
896+
'(:name \"filename\"
897+
:type string
898+
:description \"The name of the file to create\")
899+
'(:name \"content\"
900+
:type string
901+
:description \"The content to write to the file\"))
902+
:category \"filesystem\")
903+
904+
(defun my-filepath-within-current-directory-p (filepath)
905+
\"Return t if FILEPATH is within the current working directory or its subdirectories.
906+
FILEPATH can be relative or absolute, and may or may not end in a slash.\"
907+
(let* ((current-dir-truename (file-truename (file-name-as-directory default-directory)))
908+
(filepath-truename (file-truename (expand-file-name filepath)))
909+
(filepath-dir-truename (file-name-as-directory (file-name-directory filepath-truename))))
910+
(string-prefix-p current-dir-truename filepath-dir-truename)))
911+
#+end_src
912+
906913
**** Specifying tool arguments
907914

908915
Tool arguments are specified in an Elisp format that mirrors the JSON
@@ -2041,13 +2048,156 @@ This is a sentence that will be filled in later.
20412048
:REVIEWS: 0
20422049
:END:
20432050

2044-
gptel is really the combination of two libraries: the request API and the =gptel= UI. gptel's opinionated UI -- the chat buffer, the transient menus, the presentation of tool calls and so on -- comprise one way of using gptel, but it is certainly not the only one.
2051+
gptel is really the combination of two libraries: the request API and
2052+
the =gptel= UI. For convenience both are made available in a single
2053+
Emacs package. gptel's opinionated UI -- the chat buffer, the
2054+
transient menus, the presentation of tool calls and so on -- comprise
2055+
one way of using gptel, but it is certainly not the only one.
2056+
2057+
Accordingly, this cookbook has two sections. [[*Extending gptel's UI]]
2058+
describes how to customize gptel's UIs beyond simply setting user
2059+
options, and [[*Building applications with ~gptel-request~]] covers the
2060+
use of gptel's API for specialized LLM interactions.
2061+
2062+
** TODO Extending gptel's UI
2063+
2064+
*** Improving the chat buffer experience
2065+
2066+
A "chat buffer" is any text, Markdown or Org mode buffer where
2067+
~gptel-mode~ is turned on. This is a regular Emacs buffer and is
2068+
freely editable.
2069+
2070+
While gptel provides a few knobs for modifying its behavior in chat
2071+
buffers ([[*gptel chat buffer UI]]), it makes minimal assumptions about
2072+
the format of the chat or the structure, layout of chat buffers. In
2073+
particular, it does not add any automatic behavior. This section
2074+
provides recipes for some quality of life improvements to gptel chat
2075+
buffer behavior.
2076+
2077+
**** Automatically persist chats to disk
2078+
2079+
LLM chats can be one-off, transient interactions whose value is
2080+
limited to the moment, or perhaps to the side effects they produce via
2081+
tool calls (such as editing code in other files). In this case there
2082+
is little reason to keep the chat buffers around. But the
2083+
conversations may also contain information you may want to refer to in
2084+
the future.
2085+
2086+
Since they are regular Emacs buffers, they can be written to disk via
2087+
~save-buffer~ (=C-x C-s= by default), but you will have to manually
2088+
specify a location and a filename. With a little elisp, you can
2089+
automate this process so the chat buffers are automatically written to
2090+
a predtermined directory with a timestamped name when you run
2091+
~save-buffer~:
2092+
2093+
#+begin_src emacs-lisp
2094+
(defvar gptel-mode-chat-directory
2095+
(file-name-concat (xdg-data-home) "gptel-chat")
2096+
"Directory in which to store gptel chats")
2097+
2098+
(defun gptel-mode-assign-filename ()
2099+
"If this is a dissociated chat buffer, save it to a predetermined location.
2100+
2101+
Intended to be added to `before-save-hook' in gptel chat buffers. Use a
2102+
prefix-arg to save manually."
2103+
(unless (or (buffer-file-name) current-prefix-arg)
2104+
(make-directory gptel-mode-chat-directory t)
2105+
(setq buffer-file-name
2106+
(file-name-concat
2107+
gptel-mode-chat-directory
2108+
(concat
2109+
(format-time-string "%Y%m%d%H%M%2S-")
2110+
(file-name-sans-extension
2111+
(replace-regexp-in-string " " "-" (buffer-name)))
2112+
(pcase major-mode
2113+
('org-mode ".org") ('markdown-mode ".md") (_ ".txt")))))
2114+
(rename-buffer (file-name-nondirectory buffer-file-name) t)))
2115+
#+end_src
2116+
2117+
Unless the chat buffer is already associated with a file,
2118+
~gptel-mode-assign-filename~ sets a suitable timestamped file name for
2119+
a buffer. We ensure that this runs in gptel chat buffers right before
2120+
~save-buffer~ is called:
2121+
2122+
#+begin_src emacs-lisp
2123+
(defun gptel-mode-auto-save-chat ()
2124+
"Enable saving chat buffers to predetermined location."
2125+
(add-hook 'before-save-hook #'gptel-mode-assign-filename
2126+
nil 'local))
2127+
2128+
(add-hook 'gptel-mode-hook #'gptel-mode-auto-save-chat)
2129+
#+end_src
2130+
2131+
Note that ~gptel-mode-assign-filename~ does not alter the value of
2132+
~default-directory~ in the buffer, and the chat buffer will continue
2133+
to have access to the same environment until it is closed and reopened
2134+
as a file from the custom location. This is important, for example,
2135+
if tool calls involving file paths are involved.
2136+
2137+
**** Resume chat sessions more robustly
2138+
2139+
gptel adds metadata to a chat buffer when saving it to a file. Among
2140+
other information, this metadata includes the model, backend and tools
2141+
in use, as well as the response boundaries. When opening this file,
2142+
~gptel-mode~ is not automatically enabled, so modifying the buffer can
2143+
cause the recorded response boundaries to go out of sync.
2144+
2145+
~gptel-mode~ can be automatically enabled on opening the file by
2146+
adding a [[info:emacs#Specifying File Variables][property-line]] at the top of the file, like
2147+
2148+
#+begin_src
2149+
# -*- eval: (gptel-mode 1) -*-
2150+
#+end_src
2151+
2152+
We can get gptel to include this line automatically as follows:
2153+
2154+
#+begin_src emacs-lisp
2155+
(defun gptel-mode-auto-enable ()
2156+
"Ensure that this file opens with `gptel-mode' enabled."
2157+
(save-excursion
2158+
(let ((enable-local-variables t)) ; Ensure we can modify local variables
2159+
(if (and (save-excursion
2160+
(goto-char (point-min))
2161+
(looking-at ".*-\\*-"))) ; If there's a -*- line
2162+
;; First remove any existing eval, then add the new one
2163+
(modify-file-local-variable-prop-line
2164+
'eval nil 'delete))
2165+
;; Always add our eval
2166+
(add-file-local-variable-prop-line
2167+
'eval '(and (fboundp 'gptel-mode) (gptel-mode 1))))))
2168+
2169+
(add-hook 'gptel-save-state-hook #'gptel-mode-auto-enable)
2170+
#+end_src
2171+
2172+
~gptel-save-state-hook~ runs just before gptel updates its metadata in
2173+
the file.
2174+
2175+
Note that this function will overwrite any other =eval= local variable
2176+
specification already present on the line.
2177+
2178+
*** Extending gptel's Transient menus
2179+
2180+
** TODO Building applications with ~gptel-request~
2181+
2182+
The entry point to the request library is the ~gptel-request~
2183+
function, which presents a unified way to interact with any LLM that
2184+
gptel supports. It can be used as a building block for custom
2185+
commands that live in your configuration, for custom workflows, or
2186+
even to build complex packages.
2187+
2188+
If you intend to use ~gptel-request~ to write specialized LLM
2189+
interaction commands or build a different UI, you can require only the
2190+
=gptel-request= feature:
2191+
2192+
#+begin_src emacs-lisp
2193+
(require 'gptel-request)
2194+
#+end_src
20452195

2046-
The entry point to the request library is the ~gptel-request~ function, which presents a unified way to interact with any LLM that gptel supports. It can be used as a building block for custom commands that live in your configuration, custom workflows, or even to build complex packages.
2196+
This way you avoid loading unnecessary code, such as gptel's chat buffer UI or Transient menus.
20472197

20482198
By way of examples, this chapter describes how to do these things with ~gptel-request~.
20492199

2050-
** Simple ~gptel-request~ commands
2200+
*** Simple ~gptel-request~ commands
20512201

20522202
gptel provides gptel-request, a lower level function, to query ChatGPT with custom behavior. It accepts a prompt to send to the the active ~gptel-model~, along with several keyword arguments that modify what will be sent and how the response will be handled:
20532203

@@ -2096,7 +2246,7 @@ It is not recommended to grab the buffer contents as a string, as this will caus
20962246

20972247
We can now write our first (admittedly useless) custom command:
20982248

2099-
** Building an application
2249+
*** Building an application
21002250

21012251
#+INCLUDE: ../README.org::*FAQ :minlevel 1
21022252
#+INCLUDE: ../README.org::*Alternatives :minlevel 1

0 commit comments

Comments
 (0)