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
Copy file name to clipboardExpand all lines: doc/manual.org
+60-6Lines changed: 60 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1411,7 +1411,7 @@ internally.
1411
1411
functionality independent of that offered by gptel. Below is a
1412
1412
schematic and the full documentation of ~gptel-request~. You may
1413
1413
prefer to learn from examples and modify them to suit your needs
1414
-
instead, in which case see [[*Extending gptel]].
1414
+
instead, in which case see [[#gptel-cookbook][the gptel cookbook]].
1415
1415
1416
1416
#+begin_example
1417
1417
╭────────────────────────────╮ GPTEL-REQUEST
@@ -1636,9 +1636,6 @@ Note:
1636
1636
may be used to rerun or continue the request at a later time. See
1637
1637
[[*gptel's finite state machine]].
1638
1638
1639
-
~gptel-request~ presents a versatile API, and its uses and arguments
1640
-
are specified in greater detail in the sections that follow.
1641
-
1642
1639
** TODO Prompt transformations
1643
1640
:PROPERTIES:
1644
1641
:CUSTOM_ID: gptel-prompt-transform-functions
@@ -2029,12 +2026,69 @@ machine, although this requires exercising some care around the
2029
2026
transitions that gptel imposes during its network handling.
2030
2027
2031
2028
This is a sentence that will be filled in later.
2032
-
* TODO Extending gptel
2029
+
* TODO gptel cookbook
2030
+
:PROPERTIES:
2031
+
:CUSTOM_ID: gptel-cookbook
2032
+
:LAST_REVIEW: [2025-08-26 Tue]
2033
+
:NEXT_REVIEW: [2025-08-26 Tue]
2034
+
:REVIEWS: 0
2035
+
:END:
2033
2036
2034
-
This section provides recipes for...
2037
+
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.
2038
+
2039
+
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.
2040
+
2041
+
By way of examples, this chapter describes how to do these things with ~gptel-request~.
2035
2042
2036
2043
** Simple ~gptel-request~ commands
2037
2044
2045
+
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:
2046
+
2047
+
#+begin_src emacs-lisp
2048
+
(gptel-request "my prompt" ;the prompt to send to ChatGPT
2049
+
;; The below keys are all optional
2050
+
:buffer some-buffer-or-name ;defaults to (current-buffer)
2051
+
:system "Chat directive here" ;defaults to gptel--system-message
2052
+
:position some-pt ;defaults to (point)
2053
+
:context (list "any other info") ;will be available to the callback
2054
+
:callback (lambda (response info) ...)) ;called with the response and an info plist
2055
+
;defaults to inserting the response at :position
2056
+
#+end_src
2057
+
2058
+
For the full documentation of available keywords, see [[*The ~gptel-request~ API]].
2059
+
2060
+
*The prompt*: changing what is sent
2061
+
2062
+
gptel-request sends the string passed to it as the user prompt for a gptel query. The following will send the string passed to it and insert the response where point is when you execute it:
2063
+
2064
+
#+begin_src emacs-lisp
2065
+
(gptel-request "What is the capital of Belgium?")
2066
+
#+end_src
2067
+
2068
+
If the prompt is nil, it sends the region (if it is active), or the contents of the buffer up to point instead:
2069
+
2070
+
#+begin_src emacs-lisp
2071
+
(gptel-request nil)
2072
+
#+end_src
2073
+
2074
+
If you want to send the whole buffer, you can move simply move the cursor to the end:
2075
+
2076
+
#+begin_src emacs-lisp
2077
+
(save-excursion
2078
+
(goto-char (point-max))
2079
+
(gptel-request nil))
2080
+
#+end_src
2081
+
2082
+
It is not recommended to grab the buffer contents as a string, as this will cause twice as much memory to be used:
2083
+
2084
+
#+begin_src emacs-lisp
2085
+
(gptel-request (buffer-string)) ;NOT recommended
2086
+
#+end_src
2087
+
2088
+
----
2089
+
2090
+
We can now write our first (admittedly useless) custom command:
0 commit comments