Commit graph

231 commits

Author SHA1 Message Date
Karthik Chikmagalur
dade9ec8e1 gptel: Add introspection commands
* gptel.el (gptel--inspect-query): Add a function to inspect the
query that will be sent.  This is useful to ensure that (only and
all of) what you expect to be sent is being sent.

* gptel-transient.el (gptel-menu): Allow the query in progress to
be inspected from `gptel-menu` as an elisp or JSON object.
2024-03-14 20:28:21 -07:00
Karthik Chikmagalur
260be9d8d4 gptel: Consolidate HTTP request process
* gptel.el (gptel-request, gptel-send, gptel--url-get-response):
Consolidate the HTTP query construction into `gptel-request`,
and use it as the single point for sending gptel queries.  This
simplifies the code, makes it easier to debug and (later) advise
or otherwise modify.  To this end, `gptel-send` reuses
`gptel-request` and no longer does its own thing.  Adjust other
HTTP request-related functions accordingly.

* gptel-curl.el (gptel-curl--get-args, gptel-curl--get-response):
Receive the full request data instead of constructing it partly in
`gptel-curl--get-args`.
2024-03-14 20:28:21 -07:00
Karthik Chikmagalur
e5f54d1d09 gptel-anthropic: Modify order of request items
* gptel-anthropic.el (gptel--request-data): Move the messages to
the end of the request data object for easier inspection.
2024-03-14 20:28:21 -07:00
Karthik Chikmagalur
12340eda46 gptel-transient: Truncate system prompt when messaging
* gptel-transient.el (gptel-system-prompt--setup): Truncate the
chosen system prompt when displaying it as a message. (#249)
2024-03-14 20:28:21 -07:00
Karthik Chikmagalur
07992f79cc gptel-anthropic: Support for the Claude haiku model
* gptel-anthropic.el (gptel-make-anthropic): Add Claude-3-haiku to
the list of supported Anthropic models.
2024-03-14 20:28:21 -07:00
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
fbb0ee29c4 gptel-org-test: Add mores tests for org conversion
* test/gptel-org-test.el: Rename file from test-gptel-org.el, and
add more tests.
2024-03-13 00:52:56 -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
d502ad8ecb test-gptel-org: Add markdown conversion tests
* test/test-gptel-org.el (test--gptel-convert-markdown->org,
test--gptel--stream-convert-markdown->org): Add tests

Also remove out-of-date demo image.
2024-03-11 00:49:05 -07:00
Cash Prokop-Weaver
3935a6dcf8
♻️: Untangle Gemini model and endpoint #212 (#213)
gptel-gemini.el (gptel-make-gemini): Decouple the Gemini model
from the API endpoint.  This is to support additional model
options in the future.
2024-03-10 21:02:42 -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
2487ada4d6 gptel-anthropic: Handle missing response chunks (trial)
gptel-anthropic.el (gptel-curl--parse-stream): Reset point
explicitly when parsing streaming responses returned by the
Anthropic API.  Try to address #233.
2024-03-07 17:03:21 -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
0d6264f268 gptel-curl: Adjust response beginning position
gptel-curl.el (gptel-curl--stream-cleanup,
gptel-curl--stream-insert-response): Don't consider
`gptel-response-prefix-string` part of the response for the
purpose of running `gptel-post-response-functions`.
2024-03-05 22:51:14 -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
87c190076e README: Clarify example configuration code
README.org (all custom backends): Clarify how to set a backend as
the gptel default.  This has been confusing users.
2024-02-28 18:28:36 -08:00
Marten Lienen
149261ee79
gptel-transient: Avoid clashes with the custom directive key (#219)
gptel-transient (gptel--system-prompt-setup): When assigning keys
to directives, avoid clashes with the system-message edit key
(`h`), and use an arbitrary unused key if no mnemonic key
assignments are possible.
2024-02-27 22:36:06 -08:00
Karthik Chikmagalur
8ba07d042c gptel: Bump version
* gptel.el: Bump version
2024-02-21 00:11:03 -08:00
r0man
43f625ecb9
gptel-openai: curl-args slot in gptel-backend (#221)
gptel-openai.el (gptel-backend, gptel-make-openai,
gptel-make-azure): Add a curl-args slot to the backend struct for
additional Curl arguments.

Usage example: This can be used to set the `--cert` and `--key`
options in a custom backend that uses mutal TLS to communicate
with an OpenAI proxy/gateway.

gptel-curl.el (gptel-curl--get-args): Add backend-specific
curl-args when creating HTTP requests.

gptel-gemini.el (gptel-make-gemini): Add a curl-args slot to the
constructor.
gptel-kagi.el (gptel-make-kagi): Ditto.
gptel-ollama.el (gptel-make-ollama): Ditto.
2024-02-20 15:21:46 -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
ef8b9093d2 gptel-gemini: Use permissive API safety settings
* gptel-gemini.el (gptel-make-gemini, gptel--request-data): The
Gemini API misclassifies harmless questions (like "What's 2+2",
see #208) as harmful.  Use the most permissive safety settings the
API offers.

Also respect the value of `:stream` used when defining Gemini
backends.
2024-02-07 19:03:45 -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
Dave Berton
d8c604b53b
README: Update with instructions for perplexity.ai (#204)
README.org: perplexity.ai provides an OpenAI-style API.  Also remove 
some extra whitespace.
2024-02-03 16:36:24 -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
6a385ffc2f README: Add FAQ entry for OpenAI quota error (#93) 2024-02-03 09:43:16 -08:00
Ihor Radchenko
95925f3571
Automatically create parent directories for gptel-crowdsourced-prompts-file (#203)
* gptel-transient.el (gptel--crowdsourced-prompts): Create containing
directory for `gptel-crowdsourced-prompts-file' if necessary.

Fixes #202.
2024-02-02 16:24:13 -08:00
Karthik Chikmagalur
a61fda4661 gptel-transient: better multi-line directive editing
* gptel-transient.el (gptel--suffix-system-message): Mark the
whole directive when editing it in the `*gptel-system*` buffer.
Previously only the first line of the directive was being marked.
2024-01-30 21:06:03 -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
89decb4201 README: Mention transient menu option saving
* README.org (FAQ): Mention the option to save transient menu
options (see #94, #192).
2024-01-21 18:48:41 -08:00
Karthik Chikmagalur
e79e386964 README: Move gptel-request to FAQ
* README.org (FAQ): Move the gptel-request description to the
FAQ (for issues like #191.)
2024-01-20 13:19:52 -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
50fb34ab7c Fix compilation warnings
* gptel-transient.el (gptel--suffix-send): Give a bit of feedback
when copying to kill ring.
2024-01-18 17:33:13 -08:00
João Távora
235134a176 Bit of feedback when copying reponse to kill ring
* gptel-transient.el (gptel--suffix-send): Tweak
2024-01-18 17:33:13 -08:00
João Távora
b3d028d074 Add .gitignore 2024-01-18 17:33:13 -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
b34e217bbf README: Mention gptel-request
* README.org:
- Mention gptel-request near the top.
- Reformat FAQ
- Add solution to #75 and #182 (Doom Emacs keybinding conflict) to
  the FAQ
2024-01-16 20:26:03 -08:00