Skip to content

Commit 186b83b

Browse files
committed
gptel: Expand on gptel-request API, add unfinished cookbook
1 parent d7c3cde commit 186b83b

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

doc/manual.org

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ internally.
14111411
functionality independent of that offered by gptel. Below is a
14121412
schematic and the full documentation of ~gptel-request~. You may
14131413
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]].
14151415

14161416
#+begin_example
14171417
╭────────────────────────────╮ GPTEL-REQUEST
@@ -1636,9 +1636,6 @@ Note:
16361636
may be used to rerun or continue the request at a later time. See
16371637
[[*gptel's finite state machine]].
16381638

1639-
~gptel-request~ presents a versatile API, and its uses and arguments
1640-
are specified in greater detail in the sections that follow.
1641-
16421639
** TODO Prompt transformations
16431640
:PROPERTIES:
16441641
:CUSTOM_ID: gptel-prompt-transform-functions
@@ -2029,12 +2026,69 @@ machine, although this requires exercising some care around the
20292026
transitions that gptel imposes during its network handling.
20302027

20312028
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:
20332036

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~.
20352042

20362043
** Simple ~gptel-request~ commands
20372044

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:
2091+
20382092
** Building an application
20392093

20402094
#+INCLUDE: ../README.org::*FAQ :minlevel 1

0 commit comments

Comments
 (0)