Commit graph

139 commits

Author SHA1 Message Date
Karthik Chikmagalur
e18ceb1f84 gptel: Improve logging
* gptel.el (gptel--url-get-response): Log request headers as JSON objects.

* gptel-curl.el (gptel-curl--get-args, gptel-curl--get-response):
Log request headers as JSON objects, Curl command as a shell
command you can copy and paste into the terminal.
2024-03-14 20:28:21 -07:00
Karthik Chikmagalur
f58ad9435c gptel: Use libjansson support if available
Using the libjansson JSON parser gives us a modest boost in speed.
It's not as significant a speedup as it is for LSP clients since
our jSON payloads are smaller and less frequent -- but we might as
well use it.

* gptel.el (gptel--json-read, gptel--json-encode,
gptel--url-get-response, gptel--parse-response): Define macros to
use the libjansson-supported `json-parse-buffer` and
`json-serialize`.  Replace use of `json-encode` and `json-read`
appropriately.

* gptel-openai.el: (gptel-curl--parse-stream) : Use
`gptel--json-read` instead of `json-read`.

* gptel-ollama.el (gptel-curl--parse-stream): Use
`gptel--json-read` instead of `json-read`.

* gptel-gemini.el (gptel-curl--parse-stream): Use
`gptel--json-read` instead of `json-read`.

* gptel-curl.el (gptel-curl--get-args, gptel-curl--get-response,
gptel-curl--log-response, gptel-curl--stream-cleanup,
gptel-curl--parse-response): Use `gptel--json-read` and
`gptel--json-encode` in place of the json.el versions.

* gptel-anthropic.el (gptel-curl--parse-stream): Use
`gptel--json-read` instead of `json-read`.

* test/gptel-org-test.el: Use `gptel--json-read`.
2024-03-14 20:28:21 -07:00
Karthik Chikmagalur
9925dc91b4 gptel: Improve markdown converter
* gptel.el (gptel--stream-convert-markdown->org,
gptel--replace-source-marker): Handle backquote conversion much
better during stream-based conversion.  One-shot conversions still
require fixing.
2024-03-13 00:48:22 -07:00
Karthik Chikmagalur
3d6147830e gptel: Fix model/backend mismatch (#242)
* gptel.el (gptel--sanitize-model, gptel): Check for mismatches
between the default values of gptel-backend and gptel-model when
starting a new gptel chat.  Previously the default value of
gptel-backend was compared against the buffer-local value of
gptel-model.
2024-03-09 14:26:45 -08:00
Karthik Chikmagalur
5e9e36d854 gptel: rear-nonsticky text-property by default
* gptel.el (gptel--insert-response, gptel--restore-state): Don't
set the rear-nonsticky property on the gptel response text.
Instead, the gptel text-property is globally declared to be
nonsticky via `text-property-default-nonsticky`.

This change is since markdown-mode makes rear-nonsticky a
font-lock-managed property and we can't set it manually.  Setting
this property explicitly also makes all text properties in the
range nonsticky, which can have unintended side effects.

* gptel-curl.el (gptel-curl--stream-insert-response): Ditto.
2024-03-08 19:09:48 -08:00
Karthik Chikmagalur
b634f05fe5 gptel: Tweak markdown to org conversion
gptel.el (gptel--convert-markdown->org,
gptel--stream-convert-markdown->org): Handle the case of Markdown
bullet lists that begin with a "*", which the Gemini LLMs produce
often.  Address #238.
2024-03-07 18:24:31 -08:00
Karthik Chikmagalur
199595b0c8 gptel: Handle status HTTP 100
gptel.el (gptel--url-parse-response): Handle HTTP 100 followed by
200.  Note: this fix is brittle, it will break if 100 is followed
by an error code.

gptel-curl.el (gptel-curl--stream-filter,
gptel-curl--parse-stream): Ditto.  Address #194.
2024-03-07 10:46:30 -08:00
Karthik Chikmagalur
a32f4effe5 gptel-curl: Handle empty responses correctly
gptel-curl.el (gptel-curl--stream-cleanup): The LLM response can
be empty with HTTP status 200, for example when the API responds
with an error description instead.  Handle this case gracefully.
When `gptel-mode` is enabled, also inform the user that the
response was empty.  Fix #234.

gptel.el (gptel-post-response-functions): Documentation.  Explain
the arguments passed to each hook function in this hook when the
response fails or is empty.
2024-03-07 10:23:59 -08:00
Karthik Chikmagalur
eb088f2f21 gptel-anthropic: support Anthropic AI's Claude 3 (#229)
* gptel.el: Mention Anthropic in the package description.

* gptel-anthropic.el (gptel-anthropic, gptel-make-anthropic,
gptel--parse-response, gptel--request-data, gptel--parse-buffer,
gptel-curl--parse-stream): Add support for Anthropic AI's Claude 3
models.

* README.org: Add instructions for using Anthropic AI's Claude 3
models.
2024-03-04 11:28:57 -08:00
Karthik Chikmagalur
8ba07d042c gptel: Bump version
* gptel.el: Bump version
2024-02-21 00:11:03 -08:00
Karthik Chikmagalur
226f8f0d90 gptel: Add customizable display-action (#216)
* gptel.el (gptel-display-buffer-action, gptel): Add
`gptel-display-buffer-action` to customize the display of the
gptel buffer.

* README: Mention new option.
2024-02-10 12:04:03 -08:00
Karthik Chikmagalur
5465271541 gptel: Add gpt-4-0125-preview to model list (Fix #215)
* gptel.el (gptel--openai): Add "gpt-4-0125-preview" to the list
of ChatGPT models.  Also address byte-compiler warnings.
2024-02-09 12:15:03 -08:00
Karthik Chikmagalur
e2eccd8b08 gptel: Remove references to gptel--debug (fix #205)
* gptel.el (gptel--url-parse-response): Remove reference to
`gptel--debug`.

* gptel-curl.el (gptel-curl--sentinel): Remove reference to
`gptel--debug`.
2024-02-04 22:23:45 -08:00
Karthik Chikmagalur
bf994c0765 gptel: Add response regeneration, history and ediff
* gptel.el (gptel--attach-response-history, gptel--ediff,
gptel--next-variant, gptel--previous-variant,
gptel--mark-response):

Add `gptel--attach-response-history` -- this can be used to add
text properties to the next gptel response in the buffer.  This
is (currently) useful for tracking changes when the response
overwrites existing text.

The next three commands -- `gptel--ediff`,
`gptel--previous-variant`, `gptel--next-variant` -- provide
facilities for manipulating a gptel response at point when there
is history.  `gptel--mark-response` marks the response at point.
These are considered internal functions for now and can be
accessed from the transient menu, where they work together with
`gptel--regenerate`.

The input arguments to these commands are expected to change to
support copilot-style functionality in the near future.

* gptel-transient.el (gptel-menu, gptel--suffix-send,
gptel--regenerate):

Change the transient menu layout to be more compact (with a newly
added column.)  When overwriting the prompt with a response, save
the prompt to the gptel response's history.  Add
`gptel--regenerate` to regenerate a response.  This is accessible
from the transient menu when the point is inside response text.
2024-02-04 22:17:14 -08:00
Karthik Chikmagalur
49cfc78378 gptel: Add page boundaries, restructure files
* gptel.el: Add page boundaries and reorder file.

* gptel-transient.el: Add page boundaries.
2024-02-04 21:06:14 -08:00
Karthik Chikmagalur
af5444a2ea gptel: docstrings for multi-LLM support, bump version
* gptel.el (gptel, gptel--insert-response, gptel-temperature,
gptel-pre-response-hook, gptel-response-filter-functions,
gptel-stream, gptel--convert-org, gptel-pre-response-hook,
gptel-max-tokens, gptel-mode, gptel-request,
gptel--insert-response, gptel-set-topic, gptel--convert-org):
Replace mentions of "ChatGPT" with "LLM" or equivalent.  Update
package description and remove commented out obsolete code.

Bump version to 0.6.5.

* gptel-transient.el (gptel--crowdsourced-prompts-url,
gptel--crowdsourced-prompts, gptel-menu, gptel-system-prompt,
gptel-rewrite-menu, gptel--infix-max-tokens,
gptel--suffix-system-message): Ditto.

* gptel-ollama.el (gptel--request-data): Ditto

* gptel-kagi.el (gptel--request-data): Ditto

* gptel-curl.el (gptel-curl--stream-insert-response,
gptel-curl--stream-cleanup, gptel-curl--stream-filter): Ditto
2024-02-03 14:29:12 -08:00
Karthik Chikmagalur
2c87acddd5 gptel: Add request logging (Fix #165)
* gptel.el (gptel--debug, gptel-log-level, gptel--log-buffer-name,
gptel--url-get-response, gptel--parse-response): Optionally log
all request and response data to `gptel--log-buffer-name`, with
the log level governed by `gptel-log-level`.  Obsolete
`gptel--debug`.

* gptel-curl.el (gptel-curl--log-response, gptel-curl--get-args,
gptel-curl--get-response, gptel-curl--stream-cleanup,
gptel-curl--sentinel): Add support for logging.
2024-02-03 13:46:26 -08:00
Karthik Chikmagalur
f0e4889c36 gptel: Update OpenAI model list
* gptel.el (gptel--openai, gptel-model): Update available OpenAI
models, fix #197.
2024-01-30 20:45:37 -08:00
Karthik Chikmagalur
10f57605ce gptel: Improve Org stream conversion
* gptel.el (gptel--convert-markdown->org,
gptel--stream-convert-markdown->org): Don't touch underscores in
the source markdown.  This will turn some emphasis blocks into
underlines in Org, but we can live with that.  Fix #40.
2024-01-28 14:34:55 -08:00
Karthik Chikmagalur
c41a53f526 gptel: save/restore multi-line directives (#180)
* gptel.el (gptel--restore-state, gptel--save-state): Try to
support writing/reading multi-line directives in Org and other
modes correctly when saving the buffer.  The support is
preliminary and works as follows:
- org-mode: Replace newlines with "\n" before writing the relevant
  org property.
- other modes: escape newlines with print-escape-newlines when
updating local vars.

Neither of these is a good fix, but brings support for multi-line
directives up from completely broken to works-via-hack.  They are
subject to change in the future and might break some chat files. :(
2024-01-26 17:52:43 -08:00
Karthik Chikmagalur
d2f56c62a0 gptel-transient: Allow redirection to any buffer
* gptel.el (gptel-request): Update docstring to clarify what
BUFFER and POSITION do.  Addresses #191.

* gptel-transient.el (gptel-menu, gptel--suffix-send): Replace
"new session" and "existing session" redirection options with
"gptel session" and "any buffer", allowing for more flexibility
when redirecting.  "gptel session" can be an existing or new
session.  Fix bug where the prompt was generated from the contents
of the destination buffer instead of the current buffer when
redirecting to a gptel session.  Add comments demarcating blocks
in `gptel--suffix-send`.
2024-01-22 17:17:57 -08:00
Karthik Chikmagalur
8a25058eed gptel-openai: default :header key to simplify config
* gptel.el (gptel--openai): Don't specify header.

* gptel-openai.el (gptel-make-openai): Use a key-aware lambda for
the header argument.  This should make it easier to define new
OpenAI-style API backends (see #177, #184)

* README.org: Update with instructions for together.ai and
Anyscale, both of which provide OpenAI-style APIs.  Clean up the
config blocks for the other backends.
2024-01-19 14:45:36 -08:00
Karthik Chikmagalur
d0c685e501 gptel: checkdoc linting and indentation rules
* gptel.el (gptel-use-header-line, gptel--parse-buffer):
Docstrings.

* gptel-transient.el (gptel--transient-read-variable): Docstring.

* gptel-openai.el (gptel-make-openai, gptel-make-azure): Add
indent declaration.

* gptel-ollama.el (gptel-make-ollama): Add indent declaration.

* gptel-kagi.el (gptel-make-kagi): Add indent declaration.

* gptel-gemini.el (gptel-make-gemini): Add indent declaration.
2024-01-19 14:19:22 -08:00
João Távora
7b19cdf117 Sanitize gpt-model value just in time
* gptel-transient.el (gptel-menu): Sanitize model if it's not in
the backend.

* gptel.el (gptel--sanitize-model): New helper.

* gptel.el (gptel-send): Also sanitize model in non-prefixed gptel-send.
2024-01-18 17:42:12 -08:00
João Távora
1fcb4606a2 Fix compilation warning in gptel-openai.el
* gptel-openai.el (cl-lib): Require it.

* gptel.el (compat): Leniently require compat so gptel.el can be
compiled standalone.  This will expose other compiler errors that
are easily visible with M-x flymake.
2024-01-18 17:32:08 -08:00
Karthik Chikmagalur
92a8c0bdac gptel: letrec expansion error in Emacs 27.2
* gptel.el (gptel--stream-convert-markdown->org): (Bug #183) Set
variables to nil explicitly in the bindings section of letrec.
Implicit nil letrec-bindings appears to cause an issue with Emacs
27.2.
2024-01-17 09:45:59 -08:00
Karthik Chikmagalur
3fb064a763 gptel: Better handling of read-only bufs
* gptel.el (gptel--insert-response): Turn on visual-line-mode in
the response buffer that is created when the gptel buffer is
read-only.

* gptel-curl.el (gptel-curl--stream-insert-response): Ditto.
2024-01-15 17:29:42 -08:00
Karthik Chikmagalur
1752f1d589 gptel-kagi: Add support for the Kagi summarizer
* gptel-kagi.el (gptel--request-data, gptel--parse-buffer,
gptel-make-kagi): Add support for the Kagi summarizer.  If there
is a url at point (or at the end of the provided prompt), it is
used as the summarizer input.  Otherwise the behavior is
unchanged.

* README (Kagi): Mention summarizer support.

* gptel.el: Mention summarizer support.
2024-01-15 17:29:42 -08:00
Karthik Chikmagalur
c6a07043af gptel-kagi: Add support for Kagi FastGPT
* gptel.el: Bump version and update package description.

* gptel-kagi.el (gptel--parse-response, gptel--request-data,
gptel--parse-buffer, gptel-make-kagi): Add new file and support
for the Kagi FastGPT LLM API.  Streaming and setting model
parameters (temperature, max tokesn) are not supported by the API.
A Kagi backend can be added with `gptel-make-kagi`.

* README.org: Update with instructions for Kagi.
2024-01-12 23:17:21 -08:00
Karthik Chikmagalur
612aea3456 gptel: Make gptel-post-response-* easier to use
* gptel.el (gptel-end-of-response, gptel-post-response-hook,
gptel-post-response-functions, gptel--insert-response,
gptel-response-filter-functions):
Rename gptel-post-response-hook -> gptel-post-response-functions
The new abnormal hook now calls its functions with the start and
end positions of the response, to make it easier to act on the
response.

* gptel-curl.el (gptel-curl--stream-cleanup): Corresponding changes.

* README.org: Mention breaking change.
2024-01-12 23:04:40 -08:00
Karthik Chikmagalur
8ec233d79c gptel: Name gptel buffer according to backend
* gptel.el (gptel-default-session, gptel): Name the gptel buffer
according to the default backend.  Delete the variable
`gptel-default-session`.  Fix #174.

* gptel-openai.el (gptel-make-openai): Don't specify a key by
default. Fix #170.
2024-01-12 22:27:20 -08:00
Karthik Chikmagalur
febeada960 gptel: Make gptel-backend customizable
* gptel.el (gptel-backend): Turn `gptel-backend` into a defcustom
so it can be used with setopt.  Fix #167.

* gptel-openai.el (gptel-make-openai): Improve docstring.
2024-01-05 20:55:52 -08:00
Karthik Chikmagalur
d5b10c3d6d gptel: gptel-model can be an arbitrary string
* gptel.el (gptel-model): Allow gptel-model to be an arbitrary
string in the customize interface so it can be set with setopt
etc. (See #152.)
2024-01-02 14:47:19 -08:00
Karthik Chikmagalur
1e31f550de gptel: Declare compat as explicit dependency
* gptel.el (gptel--always, gptel--button-buttonize): Currently
gptel depends on the Compat library transitively via transient.el.
Declare it as an explicit dependency so we can get rid of special
case definitions and simplify.  This also enables us to use Emacs
28 and 29 conveniences freely in the code.
2023-12-29 13:06:25 -08:00
Karthik Chikmagalur
60cb406567 gptel: Improve documentation of gptel-send
* gptel.el (gptel-send): Update the docstring to describe the
behavior accurately.
2023-12-27 12:34:47 -08:00
Karthik Chikmagalur
32dd463bd6 README: Mention YouTube demo
* gptel.el: Change blurb

* README.org: Mention YouTube demo
2023-12-27 08:35:06 -08:00
Karthik Chikmagalur
9126bed43f gptel: Set window when doing auto-scrolling
* gptel.el (gptel-auto-scroll):  After calling `gptel-send`, the
window focus could have changed as the response is received.  Set
the window correctly when running `gptel-auto-scroll` to ensure
the correct buffer is scrolled.
2023-12-27 08:35:06 -08:00
Karthik Chikmagalur
8d3e08faa8 gptel: Don't use called-interactively-p
* gptel.el (gptel): Switch from the fragile
`called-interactively-p` to using the interactive form to check if
`gptel` is called interactively.
2023-12-22 16:49:04 -08:00
Karthik Chikmagalur
2e92c0303c gptel: gptel-backend-url can accept functions
* gptel.el (gptel--url-get-response): If the backend-url is a
function, call it to find the full url to query.

* gptel-gemini.el: Gemini uses different urls for
streaming/oneshot responses.  Set the backend-url to a function to
account for the value of gptel-stream.  This is also safer than
before as the API key is not stored as part of a static url string
in memory. Fix #153.

* gptel-curl.el (gptel-curl--get-args): If the backend-url is a
function, call it to find the full url to query.
2023-12-22 16:25:32 -08:00
Karthik Chikmagalur
4d01dddf7d gptel, gptel-curl: Address checkdoc warnings
* gptel.el (gptel--url-parse-response, gptel-max-tokens,
gptel-use-header-line): Address checkdoc warnings.

* gptel-curl.el (gptel-curl--parse-response, gptel-abort):
Address checkdoc warnings.

* gptel-gemini (gptel-make-gemini): Address checkdoc warnings.
2023-12-22 16:23:50 -08:00
Karthik Chikmagalur
7271d0e408 gptel: Try to save/restore gptel-backend in files
* gptel.el (gptel--save-state, gptel--restore-state,
gptel--backend-name, gptel--restore-backend): Try to save and
restore the gptel backend when persisting chat sessions in files.
The local variable `gptel--backend-name` holds the backend name in
the file across Emacs sessions.  The function
`gptel--restore-backend` tries to set this backend and messages
the user if this is not possible.
2023-12-22 16:23:50 -08:00
Karthik Chikmagalur
ce75072f9d gptel: Bump version 2023-12-21 18:11:48 -08:00
Karthik Chikmagalur
8973498378 gptel: Add minimal status indicator via mode-line-process
* gptel.el (gptel-update-destination, gptel-use-header-line,
gptel--update-status, gptel-mode): Improve status messaging when not
using the header-line.  When the user option
`gptel-use-header-line` (renamed from `gptel-update-destination`)
is set to nil, we use `mode-line-process` to report on in-progress
requests, and show the active LLM (model) otherwise.  Error
messages are sent to the echo area.  Close #9.

* README.org: Change `gptel-update-destination` to
`gptel-use-header-line` and tweak description.
2023-12-21 18:00:17 -08:00
Mark Stuart
4775ade6e0 gptel: add custom gptel-update-destination
README: Mention `gptel-update-destination` in README.

gptel.el (gptel-update-destination, gptel--update-status,
gptel-send, gptel--insert-response): New option
`gptel-update-destination` to control how gptel's status messages
are shown.  `gptel--update-status` replaces
`gptel--update-header-line`.  Replace calls to this function
elsewhere in gptel.el.

gptel-curl.el (gptel-abort, gptel-curl--stream-cleanup,
gptel-curl--stream-insert-response): Use `gptel--update-status` in
place of `gptel--update-header-line`.

gptel-transient.el (gptel--suffix-send): Use
`gptel--update-status` in place of `gptel--update-header-line`.
2023-12-21 17:48:36 -08:00
Karthik Chikmagalur
a202911009 gptel: Add post-stream hook, scroll commands
* gptel.el (gptel-auto-scroll, gptel-end-of-response,
gptel-post-response-hook, gptel-post-stream-hook): Add
`gptel-post-stream-hook` that runs after each text insertion when
streaming responses.  This can be used to, for instance,
auto-scroll the window as the response continues below the
viewport.  The utility function `gptel-auto-scroll` does this.
Provide a utility command `gptel-end-of-response`, which moves the
cursor to the end of the response when it is in or before it.

* gptel-curl.el (gptel-curl--stream-insert-response): Run
`gptel-post-stream-hook` where required.

* README: Add FAQ, simplify structure, mention the new hooks and
scrolling/navigation options.
2023-12-21 16:09:57 -08:00
Karthik Chikmagalur
38095eaed5 gptel: Fix prompt collection bug + linting
* gptel.el: Update package description.

* gptel-gemini.el(gptel--request-data, gptel--parse-buffer): Add
model temperature to request correctly.

* gptel-ollama.el(gptel--parse-buffer): Ensure that newlines are
trimmed correctly even when `gptel-prompt-prefix-string` and
`gptel-response-prefix-string` are absent.  Fix formatting and
linter warnings.

* gptel-openai.el(gptel--parse-buffer): Ditto.
2023-12-20 15:40:56 -08:00
Karthik Chikmagalur
e105a52541 gptel: Update docstrings for prompt/response prefixes
README: Mention `gptel-response-prefix-alist`

gptel.el (gptel-prompt-prefix-alist, gptel-response-prefix-alist):
Improve docstring.
2023-12-15 09:42:37 -08:00
daedsidog
644e341244
Add multiline prefixes & AI response prefixes (#142)
gptel: Add customizable prompt/response prefixes

gptel.el (gptel-prompt-prefix-alist, gptel-response-prefix-alist,
gptel-prompt-prefix-string, gptel-response-prefix-string,
gptel--url-get-response): Add customizable response prefixes (per
major-mode) in `gptel-response-prefix-alist`.

Rename `gptel-prompt-string` -> `gptel-prompt-prefix-string`

The function `gptel-response-prefix-string` returns the prefix
string for the response in the current major-mode.

gptel-openai.el, gptel-ollama.el (gptel--parse-buffer): Remove the
prompt and response prefixes when creating prompt strings to send
to the LLM API.

gptel-curl.el (gptel-curl--stream-cleanup,
gptel-curl--stream-insert-response): Insert the response prefix
for the current major-mode before inserting the LLM API response.
2023-12-15 09:30:16 -08:00
Karim Aziiev
d5949ef428
gptel-curl: handle large Curl payloads with a temp file (#137)
gptel-curl.el (gptel-curl--get-args,
gptel-curl-file-size-threshold): Use temporary file for curl data.
Ensure curl uses a temporary file for binary data to prevent
issues with large payloads and special characters:

- Add a new defcustom `gptel-curl-file-size-threshold` to
determine when to use a temporary file for passing data to Curl.

- Use `--data-binary` with a temp file for data larger than the
specified threshold, improving handling of large data payloads in
GPTel queries.

- Reliably clean up temporary files created for Curl requests
exceeding the size threshold.  Add a function to
`gptel-post-response-hook` to delete the file post-Curl execution
and remove itself from the hook, preventing temporary file
accumulation.
2023-12-14 20:22:53 -08:00
Moritz
3e361323d5
Update available OpenAI GPT models to match API (#146)
gptel-transient.el (gptel--infix-model):
gptel.el (gptel-model, gptel--openai): Update gpt-4 models.
2023-12-07 18:21:01 -08:00