Commit graph

272 commits

Author SHA1 Message Date
Alain M. Lafon
bf02ee68a4
gptel: Add gptel-add-context function 2024-04-29 17:33:13 +02:00
Karthik Chikmagalur
4273f067e8 gptel-org: Improve stream converter
* test/gptel-org-test.el (gptel-org--test-stream-conversion): Add
a test harness to make comparing markdown to org easy.

* gptel-org.el (gptel--stream-convert-markdown->org): Handle
headings and strong chars in the converter.  Addresses #296.
2024-04-28 23:05:08 -07:00
Karthik Chikmagalur
b2985392f4 gptel: Linting for NonGNU ELPA
* gptel.el (gptel--next-variant, gptel--mark-response,
gptel--sanitize-model, gptel--url-parse-response,
gptel--at-word-end, gptel--get-api-key, gptel-log-level,
gptel-backend, gptel-temperature, gptel-model, gptel-max-tokens,
gptel-directives, gptel-crowdsourced-prompts-file,
gptel-display-buffer-action, gptel-use-header-line,
gptel-response-prefix-alist, gptel-prompt-prefix-alist,
gptel-post-stream-hook, gptel-post-response-functions,
gptel-pre-response-hook, gptel-response-filter-functions,
gptel-curl-file-size-threshold, gptel-stream, gptel-api-key,
gptel-proxy): Remove customization group "gptel" from all user
options, as this is inferred from the "gptel-" prefix.  Adjust
customization types for some options.  Minor linting and
formatting changes in multiple functions.
2024-04-28 09:16:22 -07:00
Karthik Chikmagalur
306fe3bd8c gptel-ollama: Fix parsing error (#179)
* gptel-ollama.el (gptel-curl--parse-stream): Don't throw an error
when regex-searching.
2024-04-27 20:14:17 -07:00
Karthik Chikmagalur
a2b16c43b1 gptel-org: Include org-element-lineage-map with gptel (#294)
* gptel-org.el (gptel-org--element-lineage-map): Include
`org-element-lineage-map` for compatibility with older versions of Org (pre
9.7).
2024-04-25 13:58:33 -07:00
Karthik Chikmagalur
44feb1637f gptel-transient: Update header-line in gptel--suffix-send
* gptel-transient.el (gptel--suffix-send): Update the header line
when using starting a request in `gptel--suffix-send`.  Fix #293.
2024-04-24 16:06:31 -07:00
Karthik Chikmagalur
66a63e6c82 gptel-ollama: switch to chat API
* gptel-ollama.el (gptel-curl--parse-stream,
gptel--parse-response, gptel--request-data, gptel--parse-buffer,
gptel--ollama-context, gptel--ollama-token-count,
gptel-make-ollama): Switch to Ollama's chat API from
the completions API.  This makes interacting with Ollama fully
stateless, like all the other APIs, and should help significantly
with issues like #249 and #279.  Support non-streaming responses
from Ollama in the process.

Remove `gptel--ollama-context` as it is no longer needed.

Add a `gptel--ollama-token-count` for tracking token costs. A UI
affordance for this is not implemented yet, but is planned.
2024-04-22 12:39:21 -07:00
Karthik Chikmagalur
9b094b8b1e gptel: Fix url-retrieve response parser bug
* gptel.el (gptel--url-parse-response): Fix quoted variable in
response parser.
2024-04-22 12:37:20 -07:00
Norio Suzuki
2b938114cf
gptel: Add GPT 4 Turbo (#286)
* gptel.el (gptel--openai, gptel-model): Add gpt-4-turbo model.
2024-04-20 10:35:26 -07:00
Cash Prokop-Weaver
70889ad95c
gptel-gemini: Add Gemini 1.5 (#284)
gptel-gemini.el (gptel-make-gemini): Add support for the Gemini
1.5 pro model. Closes #212.
2024-04-10 09:50:07 -07:00
Bryan Larsen
e994a443d3
README: add OpenRouter instructions (#282)
README: Add instructions for using OpenRouter.
2024-04-06 15:08:15 -07:00
Tiou Lims
b4088e3f7b
README: New pacakge based on gptel, magit-gptcommit (#281)
README: Mention magit-gptcommit.
2024-04-06 15:06:33 -07:00
Karthik Chikmagalur
7b6e3c5900 gptel: Release v0.8.5
* gptel.el: Bump version.
2024-04-04 01:13:46 -07:00
Karthik Chikmagalur
4d4b61af94 gptel-transient: More robust dry-run commands
* gptel.el (gptel--inspect-query): `gptel--inspect-query` now
takes data to display as an argument.  Reduce its function to
displaying a buffer with the data.

* gptel-transient.el (gptel-menu, gptel--suffix-send): Fold
dry-run the option into `gptel--suffix-send` and call it with a
dry-run flag instead of using an alternate pathway for dry-runs.
The "Inspect query" suffixes of `gptel-menu` now perform actual
dry-runs, avoiding issues like #276.
2024-04-04 01:13:46 -07:00
Karthik Chikmagalur
567af4d2ee gptel-org: Read config from Org properties (#141)
* gptel.el: Load gptel-org after Org is loaded.

* gptel-org.el (gptel-org--send-with-props): Advise `gptel-send`
and `gptel--suffix-send` to use gptel's local config (stored under
the current Org heading) when applicable.  Advice is a hacky way
to do it, but this is the simplest option without explicit
indirection via `derived-mode-p`-based dispatch code in many more
places in gptel.
2024-04-04 01:13:46 -07:00
Karthik Chikmagalur
f2fd2b13b0 gptel-org: Move response transform code for Org
* gptel.el (gptel--convert-markdown->org,
gptel--stream-convert-markdown->org, gptel-set-topic): Move code
for transforming responses and setting the GPTEL_TOPIC property to
gptel-org.  Add declarations for the byte-compiler.

* gptel-org.el (gptel-org-branching-context, gptel-org-set-topic,
gptel-org--restore-state, gptel--convert-markdown->org,
gptel--stream-convert-markdown->org): Add `gptel-org-set-topic` to
set the topic per heading in Org buffers.  Fix typo in
gptel-org--restore-state.  Add declarations for
the byte-compiler and page markers for the reader.
2024-04-04 01:13:46 -07:00
Karthik Chikmagalur
8dbcbbb908 gptel-org: Move session save/restore code for Org
* gptel.el (gptel--restore-backend, gptel--save-state,
gptel--restore-state): Move the Org-specific code for saving and
restoring state to gptel-org.

* gptel-org.el (gptel-org--entry-properties,
gptel-org--save-state, gptel-org--restore-state): Org-specific
code for saving and restoring state using Org properties, moved
from gptel-org.
2024-04-04 00:25:09 -07:00
Karthik Chikmagalur
2982ede17d gptel-org: Add gptel-org
* gptel.el (gptel--create-prompt): Split the Org mode logic in
`gptel--create-prompt` into gptel-org.el.

* gptel-org.el (gptel-org-branching-context,
gptel-org--create-prompt): Handle prompt creation for Org buffers
in `gptel-org--create-prompt`.  The option
`gptel-org-branching-context` limits the context in Org buffers to
the direct ancestors of the active heading.  This is useful for
conversations with branches.  gptel-org.el is intended to
eventually contain all the Org mode specific gptel code, and will
not be loaded unless gptel is called in an Org buffer.
2024-04-04 00:25:08 -07:00
Karthik Chikmagalur
5d74ec4de0 gptel: Set system message correctly in gptel-request
* gptel.el (gptel-request): let-bind `gptel--system-message`
correctly in `gptel-request`.  The Anthropic API requires the
system message to be attached to the query differently from the
others, causing the let-bindings in `gptel-request` to not work as
expected. (#276)
2024-04-03 09:34:07 -07:00
Karthik Chikmagalur
53a905dafc gptel: Show chosen system message in header-line
* gptel.el (gptel): Show chosen system message in
header-line (#274).  Remove the context indicator `[Send: ...]`,
as this needs to be reworked for the upcoming context-inclusion
features anyway.

* gptel-transient.el (gptel-system-prompt): Autoload
`gptel-system-prompt`, this is required for the gptel header-line.
2024-04-02 19:33:39 -07:00
Karthik Chikmagalur
53ee34653e gptel-openai: Typo in gptel--json-read
* gptel-openai.el (gptel--json-read): Fix typo in
`gptel--json-read` that caused macroexpansion intended to use
json.el fail.  This error was reported by @vkz.  Fix #260.
2024-04-02 11:50:30 -07:00
Karthik Chikmagalur
f24ec164cd gptel: Adjust doc-string
* gptel.el (gptel-post-response-functions): Adjust docstring for
this hook to mention that the hook runs in the response buffer,
not the prompt buffer. (#269)
2024-03-30 09:42:30 -07:00
Karthik Chikmagalur
81bb467104 gptel: Set window when running post-response hook
* gptel.el (gptel--insert-response): Moving point in a buffer does
not move window-point in a window displaying that buffer if the
window is not selected (#269).  So select the gptel response
window explicitly (if possible) when running
`gptel-post-response-functions`.

NOTE: Instead of selecting the window before running the hook, We
could run `set-window-point` in the window after running it.
Since the hook can have any kind of behavior (smooth-scrolling for
instance) we want it to play out interactively.

* gptel-curl.el (gptel--curl-stream-cleanup): Ditto.
2024-03-29 15:47:32 -07:00
Karthik Chikmagalur
26326c302e gptel-anthropic: Parsing fix
gptel-anthropic.el (gptel-curl--parse-stream): When parsing
responses from Anthropic, wait for more input when the
corresponding data chunk for the event hasn't arrived yet. (#261)
2024-03-29 14:08:32 -07:00
Karthik Chikmagalur
9a5a4a60d5 README: Mention wiki entry on saving transient flags
* README.org (FAQ): Mention wiki entry on saving transient flags.
2024-03-21 11:30:30 -07:00
Karthik Chikmagalur
12e00cbd09 gptel-transient: No pre-fill when reading from minibuffer
* gptel-transient.el (gptel--suffix-send): Don't pre-fill the
minibuffer prompt with the current line when reading the query
from the minibuffer.  (If the region is active, it is used as the
initial contents -- this behavior is unchanged.)
2024-03-21 10:40:31 -07:00
Karthik Chikmagalur
34a52aa047 gptel-anthropic: Remove debug code
* gptel-anthropic.el (gptel--parse-response): Remove left over
debugging code.
2024-03-21 10:23:16 -07:00
Karthik Chikmagalur
9eea4be5ed gptel-transient: Fix gptel-menu definition bug (#265)
gptel-transient.el (gptel-menu): Fix error in definition syntax.
Fix provided by @wrn.
2024-03-20 19:46:41 -07:00
Fredrik Bergroth
9bc54bed9c
gptel-transient: Remove "-ts" suffix from major mode (#259)
gptel-transient (gptel--rewrite-message): Remove "-ts" suffix from
major mode.
2024-03-19 18:06:48 -07:00
Karthik Chikmagalur
5d069cfca8 gptel-anthropic: Simplify stream parser
* gptel-anthropic.el (gptel-curl--parse-stream): Remove extraneous
statements and simplify the stream parser.  Addresses #261, but
the main problem is still elusive.
2024-03-19 18:01:55 -07:00
Karthik Chikmagalur
22f7043c32 gptel: Fix gptel-end-of-response
* gptel.el (gptel-end-of-response): Handle non-interactive case
where one of the function arguments can be nil.  Fix #262.
2024-03-19 17:39:13 -07:00
Karthik Chikmagalur
5dcbf40066 gptel: Make model parameters global
* gptel.el (gptel-backend, gptel-model, gptel-temperature,
gptel-max-tokens, gptel--num-messages-to-send,
gptel--system-message): Make all model/request paramters global
variables, i.e. not buffer-local by default.  This is following
the discussion in #249.

* gptel-transient.el (gptel-menu, gptel-system-prompt--setup,
gptel-system-prompt, gptel--suffix-system-message,
gptel--infix-provider, gptel--infix-temperature, gptel--switches,
gptel--set-buffer-locally, gptel--set-with-scope): and associated
transient methods: add a toggle `gptel--set-buffer-locally` to
allow model parameters to be set buffer-locally.  The function
`gptel--set-with-scope` can be used to reset a variable or set it
buffer-locally.

Reorder gptel-transient so all the custom classes, methods and
utility functions are at the top.

* README.org (all backend sections): Replace `setq-default` with
setq in the recommended configuration.
2024-03-16 20:59:48 -07:00
Karthik Chikmagalur
e3b3591d73 README: Add support for Groq (#257)
* README.org: Mention details for configuring Groq.
2024-03-16 20:27:54 -07:00
Karthik Chikmagalur
94b13e78ec gptel-transient: enable additional directive by default
* gptel-transient.el (gptel-menu, gptel--infix-add-directive,
gptel--suffix-system-message):  Enable the additional directive
option in `gptel-menu` by default, and change the wording of
documentation strings for transient options.
2024-03-16 20:17:52 -07:00
Karthik Chikmagalur
b31c9be5e0 gptel-ollama: Adjust Ollama stream parser for libjansson
* gptel-ollama.el (gptel-curl--parse-stream): libjansson and
json.el behave differently w.r.t moving point when there is a
parsing error.  Fix by explicitly handling point when there is an
error. (#255)
2024-03-16 20:17:52 -07:00
Karthik Chikmagalur
73a0cc25ba gptel-transient: Simplify model selection
* gptel-transient.el (gptel--infix-provider, gptel--infix-model):
Simplify model and backend selection into a single step.  Remove
unused and obsolete option `gptel--infix-provider`.
2024-03-16 20:17:52 -07:00
Karthik Chikmagalur
6d3e4a99f5 gptel-transient: Rename additional-directive functions
* gptel-transient.el (gptel--get-directive,
gptel--infix-add-directive, gptel-menu): Rename the
directive-related functions to be shorter/more consistent with the
rest of the code.

gptel--additional-directive     -> gptel--infix-add-directive
gptel--additional-directive-get -> gptel--get-directive
2024-03-16 20:17:52 -07:00
Karthik Chikmagalur
161c77ad7f gptel-transient: Adjust several menu options
* gptel-transient.el (gptel-menu, gptel-system-prompt--setup,
gptel-system-prompt, gptel--infix-temperature,
transient-format-value, gptel--additional-directive,
gptel--suffix-system-message): Tweak to the "additional directive"
overlay display.  `gptel-menu` changes based on feedback
from #249 (thanks to @jwr):

- Keys to set the system message are remapped from "h" to
"s" (mnemonic)
- `gptel--infix-temperature` is now hidden by default and requires
enabling `gptel-expert-commands`
- Keys to prompt from minibuf and respond in echo area are changed
to "m" and "e" respectively.
2024-03-16 20:17:51 -07:00
Karthik Chikmagalur
376fb4b423 gptel-transient: Additional directives option (#249)
* gptel.el (gptel-end-of-response, gptel-beginning-of-response,
gptel-expert-commands): Add `gptel-expert-commands` to selectively
enable experimental options in `gptel-menu`.  This should keep the
interface from overwhelming new users.  Add a command to move to
the beginning of a response.

* gptel-transient.el (gptel-menu, gptel-system-prompt,
gptel--instructions-make-overlay, gptel-option-overlaid,
transient-format-value, gptel--additional-directive,
gptel--additional-directive-get): Add a transient option to
include a (short) additional instruction/directive along with the
system message.  This makes it convenient to have an extensive
system message and specify additional, per-response tasks (such as
refactoring) on top.  Ensure that the dry run options handle this
correctly.  This option is made available when
`gptel-expert-commands` is turned on.

NOTE: WIP design.  The nomenclature for `gptel-expert-commands`
and "additional directive" is subject to change.
2024-03-16 20:17:35 -07:00
Karthik Chikmagalur
7e6b106516 gptel-transient: Change menu display
* gptel-transient.el (gptel-menu, gptel-lisp-variable,
gptel--infix-num-messages-to-send, gptel--infix-max-tokens,
gptel--infix-provider): Improve the wording and the display of the
default settings.  Define a new class `gptel-lisp-variable` to
faciliate this.
2024-03-14 20:28:21 -07:00
Karthik Chikmagalur
f529457bbe gptel: Use visual-line-mode when ediff-ing
* gptel.el (gptel--ediff): Use `visual-line-mode` when ediff-ing.
2024-03-14 20:28:21 -07:00
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