You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tool arguments are specified in an Elisp format that mirrors the JSON
@@ -2041,13 +2048,264 @@ This is a sentence that will be filled in later.
2041
2048
:REVIEWS: 0
2042
2049
:END:
2043
2050
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, the first few sections of this cookbook describe how to
2058
+
customize gptel's UIs beyond simply setting user options. The final
2059
+
section, [[*Building applications with ~gptel-request~]] covers the use of
2060
+
gptel's API to write specialized LLM interaction commands, alternative
2061
+
UIs or packages.
2062
+
2063
+
** Improving the chat buffer experience
2064
+
2065
+
A "chat buffer" is any text, Markdown or Org mode buffer where
2066
+
~gptel-mode~ is turned on. This is a regular Emacs buffer and is
2067
+
freely editable.
2068
+
2069
+
While gptel provides a few knobs for modifying its behavior in chat
2070
+
buffers ([[*gptel chat buffer UI]]), it makes minimal assumptions about
2071
+
the format of the chat or the structure, layout of chat buffers. In
2072
+
particular, it does not add any automatic behavior. This section
2073
+
provides recipes for some quality of life improvements to gptel chat
2074
+
buffer behavior.
2075
+
2076
+
*** Automatically persist chats to disk
2077
+
2078
+
LLM chats can be one-off, transient interactions whose value is
2079
+
limited to the moment, or perhaps to the side effects they produce via
2080
+
tool calls (such as editing code in other files). In this case there
2081
+
is little reason to keep the chat buffers around. But the
2082
+
conversations may also contain information you may want to refer to in
2083
+
the future.
2084
+
2085
+
Since they are regular Emacs buffers, they can be written to disk via
2086
+
~save-buffer~ (=C-x C-s= by default), but you will have to manually
2087
+
specify a location and a filename. With a little elisp, you can
2088
+
automate this process so the chat buffers are automatically written to
2089
+
a predtermined directory with a timestamped name when you run
2090
+
~save-buffer~:
2091
+
2092
+
#+begin_src emacs-lisp
2093
+
(require 'xdg)
2094
+
2095
+
(defvar gptel-mode-chat-directory
2096
+
(file-name-concat (xdg-data-home) "gptel-chat")
2097
+
"Directory in which to store gptel chats")
2098
+
2099
+
(defun gptel-mode-assign-filename ()
2100
+
"If this is a dissociated chat buffer, save it to a predetermined location.
2101
+
2102
+
Intended to be added to `before-save-hook' in gptel chat buffers. Use a
"Convert Org headings between BEG and END to bold text."
2272
+
(when (derived-mode-p 'org-mode)
2273
+
(save-excursion
2274
+
(goto-char beg)
2275
+
(while (re-search-forward org-heading-regexp end t)
2276
+
(forward-line 0)
2277
+
(delete-char (1+ (length (match-string 1))))
2278
+
(insert-and-inherit "*")
2279
+
(end-of-line)
2280
+
(skip-chars-backward " \t\r")
2281
+
(insert-and-inherit "*")))))
2282
+
#+end_src
2283
+
2284
+
*** TODO Response transformations using gptel's state machine
2285
+
2286
+
** Adding UI elements to gptel
2287
+
** Extending gptel's Transient menus
2288
+
** TODO Building applications with ~gptel-request~
2289
+
2290
+
The entry point to the request library is the ~gptel-request~
2291
+
function, which presents a unified way to interact with any LLM that
2292
+
gptel supports. It can be used as a building block for custom
2293
+
commands that live in your configuration, for custom workflows, or
2294
+
even to build complex packages.
2295
+
2296
+
If you intend to use ~gptel-request~ to write specialized LLM
2297
+
interaction commands or build a different UI, you can require only the
2298
+
=gptel-request= feature:
2299
+
2300
+
#+begin_src emacs-lisp
2301
+
(require 'gptel-request)
2302
+
#+end_src
2045
2303
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.
2304
+
This way you avoid loading unnecessary code, such as gptel's chat buffer UI or Transient menus.
2047
2305
2048
2306
By way of examples, this chapter describes how to do these things with ~gptel-request~.
2049
2307
2050
-
** Simple ~gptel-request~ commands
2308
+
*** Simple ~gptel-request~ commands
2051
2309
2052
2310
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:
2053
2311
@@ -2096,7 +2354,7 @@ It is not recommended to grab the buffer contents as a string, as this will caus
2096
2354
2097
2355
We can now write our first (admittedly useless) custom command:
0 commit comments