* gptel.el (gptel--playback, gptel--insert-response):
`gptel--insert-response' no longer handles stream playback. Run
`gptel-post-response-hook' after inserting the response from
ChatGPT. Delete the `gptel--playback' function since it never did
anything -- it was a placebo.
* gptel.el (gptel--url-get-response, gptel--insert-response,
gptel-send): Rename the :insert-marker keyword in the async info
plist to :start-marker.
* gptel-curl.el (gptel--insert-response-stream,
gptel-curl--cleanup-stream, gptel-curl-get-response): Ditto.
* gptel.el (gptel--convert-playback-markdown->org,
gptel-post-response-hook): Add a hook that runs after the response
is received. This will allow for custom actions like moving the
cursor to the next prompt. The markdown->org stream converter now
cleans up after itself using this hook.
* gptel.el (gptel--convert-playback-markdown->org): New converter
for markdown->org that works on text chunks while maintaining the
parse state until the text stream is finished.
* gptel-curl.el (gptel--insert-response-stream,
gptel-curl-get-response): When using `gptel-playback' and
requesting ChatGPT's responses in org-mode, run the above
converter on the received response. This works by storing the
converter and associated state as a closure in the async info
plist that is supplied along with the response, and running it
repeatedly on each chunk of text in the response stream before it
is inserted into the buffer.
FIXME: Note that `gptel-response-filter-functions' is currently
ignored if using `gptel-stream'.
* gptel.el (gptel--request-data): Request a streaming message if
`gptel-stream' is non-nil.
* gptel-curl.el (gptel-curl-get-response,
gptel-curl--cleanup-stream, gptel-curl--filter): Add a process
filter and sentinel for Curl to stream ChatGPT's response into
Emacs in real-time.
* gptel.el (gptel--url-get-response,
gptel-api-key-from-auth-source): `gptel--url-get-response' accepts
a callback argument that can be used to do something besides
inserting the response into the current buffer.
* gptel-curl.el (gptel-curl--sentinel, gptel-curl-get-response):
`gptel-curl--sentinel' now accepts a callback argument that can be
used to do something besides inserting the response into the
current buffer.
These changes are in preparation for more specific functionality,
like showing the response as a message, or replacing the prompt
with the response etc.
This changes response parsing so that the response body is decoded as
UTF-8 and then parsed as JSON, rather than the other way around.
This fixes the handling of responses that contain Unicode characters
which are encoded with multiple bytes in UTF-8, such as emojis.
If the user provides the OpenAPI key via a function, as is the case by
default if the user puts credentials in an auth-sources resource like
.authinfo or .authinfo.gpg, then it is necessary to encode the
function's returned value into utf-8 before passing it onward to build
the HTTP request. This commit ensures that happen.
Why is this necessary, given that the API key contains only
alphanumeric characters and therefore should be byte-for-byte the same
in utf-8 as in us-acii? I don't know. It may because emacs's
url-http.el library concats many strings together, and they all need
to be identically encoded before they can be combined correctly.
Whatever the reason, this fix works and allows you to send prompts
which include Unicode characters that require multibyte encodings in UTF-8
* gptel.el (gptel-send): Set the "Waiting..." state after sending
the http request -- this is less misleading if there's an error in
the http request functions (`gptel--url-get-response' or
`gptel-curl-get-response').
* gptel.el (gptel--request-data, gptel--system-message-alist,
gptel--model, gptel--temperature, gptel--max-tokens): Rename API
parameters and turn them into customizable variables. They are
still buffer-local.
Rename:
`gptel--system-message-alist' to `gptel-directives'
`gptel--max-tokens' to `gptel-max-tokens'
`gptel--model' to `gptel-model'
`gptel--temperature' to `gptel-temperature'
* gptel-transient.el (gptel-system-prompt,
gptel--infix-max-tokens, gptel--infix-model,
gptel--infix-temperature): Accommodating changes when setting the
renamed parameters.
* gptel.el (gptel-prompt-prefix-alist, gptel--playback, gptel,
gptel--insert-response, gptel-prompt-string): The prompt prefix
string is chosen automatically from the new variable
`gptel-prompt-prefix-alist', which maps major modes to the prefix
string to insert.
* gptel.el (gptel--url-parse-response): Produce better error
messages when using `url-retrieve'. This includes JSON parsing
failures and insufficient quota messages.
* gptel-curl.el (gptel-curl--parse-response): Produce better error
messages when using curl. This includes JSON parsing failures
and insufficient quota messages.
* gptel.el (gptel-send, gptel--insert-response,
gptel--url-get-response): Remove aio dependency, turn aio-defuns
into regular functions. This requires splitting `gptel-send' into
"before response" and "after response" functions, but the ability
to debug the code fully is worth the inconvenience. The new "after
response" function is `gptel--insert-response'.
* gptel-curl.el (gptel-curl--sentinel, gptel-curl-get-response):
Turn aio-defuns into regular functions.
gptel.el (gptel-send, gptel--url-parse-response): Handle insertion
better when region is active. Decode utf-8 encoded response when
using url-retrieve (instead of curl).
gptel.el (gptel--debug, gptel--url-parse-response): Add a debug
flag that shows the http response. Fix json parsing error.
gptel-curl.el (gptel-curl--sentinel): Ditto.
gptel-transient.el (gptel-send-menu, gptel--suffix-send-existing,
gptel--suffix-send-new, gptel--infix-model): Make menu keybindings
more uniform and improve descriptions. Suffixes to send queries in
existing/new sessions will now automatically send them instead of
just yanking the text into these sessions.
gptel.el (gptel--url-get-response): When `gptel-send' is called
directly, the API key is assumed to exist. Ensure that it is read.
gptel-curl.el (gptel-curl--get-args): Ditto.
gptel.el (gptel--old-header-line, gptel-mode,
gptel--transform-response): Make `gptel-mode' set up the header-line
with status, and restore the old header line when it's turned off.
Improve documentation and linting in other places. Add todos.
gptel.el (gptel--update-header-line, gptel-send, gptel--playback,
gptel--create-prompt): Add helper function to update the header-line,
and only call when `gptel-mode' is on. Use markers to track where the
response should be inserted, and insert it after (point) by default.
gptel.el (gptel): When calling `gptel' with a selected region, insert it
into a new session.
gptel-transient.el (gptel--suffix-send-existing,
gptel--suffix-send-new): Transient suffixes available when a region is
selected. These send the text as a prompt in a existing or new gptel
session.
gptel-transient (gptel-send-menu): Add new transient-prefix
`gptel-send-menu' for setting API options for the buffer
and (optionally) sending queries. Various infix options are supported.
2023-03-09: Some transient menus are generated dynamically, which
requires a more recent version of transient than the latest tagged
release. As a result, the dynamic generation code is commented out for
now.
gptel.el (gptel-response-filter-functions, gptel-send,
gptel--create-prompt, gptel--transform-response, gptel--convert-org,
gptel--convert-markdown->org): Add support for org-mode by transforming
the response manually. (Note: Asking ChatGPT to format its results in
org-mode markup produces inconsistent results.)
Additionally, the abnormal hook `gptel-resposne-filter-functions' is
added for arbitrary transformations of the response. Its implementation
seems needlessly complex, and in the future we should change it to
use `run-hook-wrapped' with a local accumulator.
gptel.el (gptel--system-message-alist, gptel--url-get-response,
gptel--url-parse-response): Tweak default programming prompt. Rename
`gptel--get-response' to the more specific `gptel--url-parse-response'.
Likewise `gptel--parse-response' -> `gptel--url-parse-response'.
gptel.el (gptel--system-message, gptel--system-message-alist,
gptel--model, gptel--temperature, gptel--max-tokens,
gptel--request-data): Add new buffer-local variables to hold API
parameters. Generating the full request data plist is now done in a
separate function, `gptel--request-data'.
gptel-curl.el (gptel-curl-get-response): Rename from `gptel--curl-get-response'
and autoload it to ease its use in `gptel-send'. Remove Version header
identifying gptel-curl as a separate package and make it require `gptel' instead.
gptel.el (gptel--prompt-markers, gptel-send, gptel--create-prompt,
gptel--numberize): Switch from using markers to text-properties to
distinguish queries from responses. The former method was very brittle.
Remove `gptel--prompt-markers', add the function `gptel--create-prompt'
and the variable `gptel--num-messages-to-send'. This variable limits the
context of the conversation that is sent with each request.
Conditionally solves #2.
gptel.el (gptel-use-curl, gptel-parse-response, gptel--playback,
gptel-send, gptel-playback): New user options `gptel-playback',
`gptel-use-curl`. The former controls whether the response is played
back in chunks, which is done by the function `gptel--playback'. The
response returned by `gptel-get-response' and `gptel--curl-get-response'
is now a plist with the content and status.
gptel-curl.el (gptel--curl-get-args, gptel--curl-get-response,
gptel--curl-sentinel): Add support for curl when available. Set it to
the default. `url-retrieve' is full of fangs that multibyte you.