Commit graph

178 commits

Author SHA1 Message Date
Karthik Chikmagalur
c11e53061c gptel-transient: New gptel-menu transient options
* gptel-transient.el (gptel-menu, gptel--suffix-send,
gptel--suffix-send-existing, gptel--suffix-send-new): Rewrite
`gptel-menu' to allow for selecting prompt sources and response
sinks independently. Sensible combinations of the following are
now possible:
- prompt from minibuffer
- Replace prompt with response
- Response to echo area
- Response to new gptel session
- Response to existing gptel session
- Response to kill-ring

The transient suffixes `gptel--suffix-send-existing' and
`gptel--suffix-send-new' are now obsolete and have been removed.
2023-04-09 13:02:56 -07:00
Karthik Chikmagalur
23332a9bc5 gptel-transient: Add outline-minor-mode support
* gptel-transient.el: Add section headers and an `outline-regexp'.
2023-04-09 13:01:46 -07:00
Karthik Chikmagalur
937c754e57 gptel-transient: Add refactor transient
* gptel-transient.el: Add `gptel-rewrite-menu' (accessible via the
main `gptel-menu') to rewrite/refactor the selected region,
including the choice to Ediff it linewise/wordwise afterwards.
2023-04-09 13:01:40 -07:00
Karthik Chikmagalur
c6abda6f0f gptel-transient: Rename gptel-send-menu -> gptel-menu
* gptel.el (gptel-send): Replace references to `gptel-send-menu'.

* gptel-transient.el: Rename `gptel-send-menu' t `gptel-menu'.
This shorter name is apropos of its increased feature set (in the
forthcoming commits).
2023-04-09 04:14:37 -07:00
Karthik Chikmagalur
8a9ae56e77 gptel: Add streaming, in-place options to gptel-request
* gptel.el (gptel--insert-response, gptel-request):
- Add an in-place key to gptel-request. When true, the default
callbacks will not delimit the API responses with newlines.
- Add a strea option to gptel-request. Only works with the default
filter/stream-insert callback, so it's marked as for internal use
for now.

* gptel-curl.el (gptel-curl--stream-insert-response): Ditto.
2023-04-09 03:29:48 -07:00
Karthik Chikmagalur
acf12ee6e3 gptel: return the gptel buffer
* gptel.el (gptel): `gptel' returns the buffer after creating or
switching to it.  This is useful for other commands that call
`gptel'.
2023-04-08 20:17:06 -07:00
Karthik Chikmagalur
44045be327 gptel: gptel-mode is local by default
* gptel.el (gptel-mode): Don't specify `:global nil`.
2023-04-08 20:17:06 -07:00
Karthik Chikmagalur
1b47235e25 README: Add section on gptel-request
* README.org (** Using it your way): New section describing
gptel-request.
2023-04-08 20:17:06 -07:00
Karthik Chikmagalur
18222818dc gptel: Add gptel-request
* gptel.el (gptel-request): `gptel-request' is a lower level API
than `gptel-send' for defining custom commands that use ChatGPT's
responses.
2023-04-08 20:17:06 -07:00
Karthik Chikmagalur
f0953d569e gptel: Simplify response API
* gptel.el (gptel--url-parse-response, gptel--url-get-response,
gptel--insert-response, gptel-send):
- Use shorter keys for passing the info plist,
- record errors in the info plist,
- separate user messaging from the callback and more.
- Make the API more functional (i.e. less imperative)

This is in preparation for adding `gptel-request', an API for
defining custom commands.

Note: The streaming filter and callback are mostly unchanged.
Streaming is not planned to be accessible via `gptel-request'.

* gptel-curl.el (gptel-curl--parse-response, gptel-curl--sentinel,
gptel-curl--stream-filter, gptel-curl--stream-insert-response,
gptel-curl--stream-cleanup, gptel-curl-get-response): Ditto.
2023-04-08 16:57:38 -07:00
Karthik Chikmagalur
36051b15d5 gptel: Unify error descriptions
* gptel.el (gptel--url-parse-response, gptel--insert-response):
Use the same error codes/descriptions across url-retrieve/Curl,
with and without streaming responses.

* gptel-curl.el (gptel-curl--parse-response,
gptel-curl--stream-filter, gptel-curl--stream-cleanup): Ditto.
2023-04-08 12:22:36 -07:00
Karthik Chikmagalur
1cd0f58552 gptel-curl: Better error handling when streaming
* gptel-curl.el (gptel-curl--stream-filter,
gptel-curl--stream-cleanup): When streaming responses, move the
error handling from the Curl process filter to the cleanup
sentinel. This simplifies the filter code a fair bit.
2023-04-08 09:57:38 -07:00
Karthik Chikmagalur
6c47c0a483 README: Add videos with streaming
* README.org (In a dedicated chat buffer): Move the any-buffer
interaction description up.
2023-04-06 17:32:35 -07:00
Karthik Chikmagalur
5a0deda7fc gptel: Turn on streaming replies by default
* gptel.el (gptel-stream): When Curl is available, stream ChatGPT
responses by default.
2023-04-06 17:14:40 -07:00
Karthik Chikmagalur
a43f5ac5fa gptel: Rename stream-related vars and defuns
* gptel.el (gptel--convert-playback-markdown->org, gptel--request-data,
gptel-playback):
- `gptel-playback' is now obsolete, use `gptel-stream' instead.
- `gptel--convert-playback-markdown->org': rename to
  `gptel--stream-convert-markdown->org'

* gptel-curl.el (gptel-curl--filter, gptel-curl-get-response):
- `gptel--insert-response-stream': rename to `gptel-curl--stream-insert-response'
- `gptel-curl--cleanup-stream': rename to `gptel-curl--stream-cleanup'
- `gptel-curl--filter': rename to `gptel-curl--stream-filter'
2023-04-06 17:14:40 -07:00
Karthik Chikmagalur
ba133267ec gptel: Run post-response-hook after inserting response
* 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.
2023-04-06 17:14:40 -07:00
Karthik Chikmagalur
2e34981aa0 gptel: Rename :insert-marker
* 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.
2023-04-06 17:14:40 -07:00
Karthik Chikmagalur
c22c59d20a gptel: Add post-response hook and fix org converter
* 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.
2023-04-06 17:14:40 -07:00
Karthik Chikmagalur
c9795fe9e8 gptel: org support for streaming WIP
* 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'.
2023-04-06 17:14:40 -07:00
Karthik Chikmagalur
d5ad620555 gptel-curl: process filter for streaming support
* 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.
2023-04-06 17:14:40 -07:00
Karthik Chikmagalur
936c27e28b gptel: Fix header-line-format update
* gptel.el (gptel--update-header-line): Only fix the
`header-line-format' when in gptel-mode.
2023-04-06 17:13:45 -07:00
Karthik Chikmagalur
d77c8f37c5 gptel: Improve header-line-format
* gptel.el (gptel-mode): Show the model and context in the header
line.
2023-04-03 14:08:03 -07:00
AlessandroW
1f03655e2d
Add Doom Emacs installation instructions (#28)
README: Add Doom Emacs installation instructions
2023-04-01 14:25:47 -07:00
Karthik Chikmagalur
f7ba368c38 gptel: More flexible callbacks
* 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.
2023-03-31 19:37:25 -07:00
algal
c2ad1c004d Decode response body as utf-8 and then parse as json
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.
2023-03-31 18:04:44 -07:00
algal
a500c76a68 Encode a lambda-provided API key as utf-8
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
2023-03-31 18:04:44 -07:00
Karthik Chikmagalur
1c07a94e18 README: Update manual install instructions 2023-03-28 12:24:08 -07:00
Rida Ayed
1ab8a57183 add installation instructions 2023-03-28 12:19:56 -07:00
Karthik Chikmagalur
1828dd3fa4 gptel: Set "waiting" state after sending the prompt
* 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').
2023-03-24 18:03:40 -07:00
Karthik Chikmagalur
9b3db255e8 gptel: Turn API parameters into defcustoms
* 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.
2023-03-24 17:51:00 -07:00
Karthik Chikmagalur
f843614f5b gptel: Set gptel-api-key if reading from minibuffer
* gptel.el (gptel): Set `gptel-api-key' if reading it from the minibuffer (by
calling M-x gptel).
2023-03-24 17:51:00 -07:00
Karthik Chikmagalur
552939b2f6 gptel: Fix free-variable error
* gptel.el (gptel--url-parse-response): Fix free-variable error.
2023-03-24 17:36:19 -07:00
Karthik Chikmagalur
5ebaf361f1 gptel: Handle the prompt prefix string automatically
* 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.
2023-03-24 16:46:22 -07:00
Akira Komamura
cd22ea836c Add autoload for gptel-send 2023-03-24 14:39:07 -07:00
Karthik Chikmagalur
048eaf9b64 README: Update description of chat parameters 2023-03-23 14:39:17 -07:00
Karthik Chikmagalur
2b2dbe2664 gptel: Fix parsing error in url-retrieve buffer
* gptel.el (gptel--url-parse-response): Fix parsing error.
2023-03-23 14:34:26 -07:00
Schroedi
23caab41cf Add gpt-4 model 2023-03-23 14:27:33 -07:00
Karthik Chikmagalur
42d53b25e5 gptel: Handle insufficient quota and other errors
* 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.
2023-03-22 19:43:02 -07:00
Karthik Chikmagalur
4f3ca23454 gptel: Update commentary and README 2023-03-19 19:58:19 -07:00
Karthik Chikmagalur
f0eba0cf4f README: Update README for MELPA 2023-03-19 17:50:51 -07:00
Karthik Chikmagalur
9f8a984729 gptel: Change how api-key is read
* gptel.el (gptel): If the function `gptel--api-key' fails to find the API key
from `gptel-api-key', prompt the user directly.
2023-03-18 12:59:25 -07:00
Daniel Mendler
6f951ed690 Add gptel-api-key-from-auth-source (Fix #13) 2023-03-18 12:35:30 -07:00
Karthik Chikmagalur
87d9090b7a gptel-curl: Fix process sentinel
* gptel-curl.el (gptel-curl--sentinel): Use `process-status' instead of trying
to match on the string passed to he process sentinel as the status.
2023-03-18 00:51:49 -07:00
Karthik Chikmagalur
051501c892 README: Change installation instructions (no aio) 2023-03-18 00:04:16 -07:00
Karthik Chikmagalur
040baad910 gptel: Remove aio dependency
* 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.
2023-03-17 23:51:26 -07:00
Karthik Chikmagalur
8a6ef565f0 gptel-transient: Remove unused lexical vars 2023-03-17 23:44:17 -07:00
Karthik Chikmagalur
30161850ad gptel-transient: Allow setting num past messages to 0
gptel-transient.el (gptel--infix-num-messages-to-send): Allow
setting the number of past messages to send to 0 for no-context
responses.
2023-03-14 02:09:53 -07:00
Karthik Chikmagalur
1ada9c9214 gptel: Handle insertion with region-active correctly
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).
2023-03-14 02:09:53 -07:00
Karthik Chikmagalur
c8f87f5554 Update README with transient menu details 2023-03-14 02:09:53 -07:00
Karthik Chikmagalur
0d26b34526 gptel: Add a debug flag
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.
2023-03-12 15:42:16 -07:00