gptel.el (80522B)
1 ;;; gptel.el --- Interact with ChatGPT or other LLMs -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2023 Karthik Chikmagalur 4 5 ;; Author: Karthik Chikmagalur <karthik.chikmagalur@gmail.com> 6 ;; Package-Version: 20241208.141 7 ;; Package-Revision: 983a9dfbf87f 8 ;; Package-Requires: ((emacs "27.1") (transient "0.4.0") (compat "29.1.4.1")) 9 ;; Keywords: convenience 10 ;; URL: https://github.com/karthink/gptel 11 12 ;; SPDX-License-Identifier: GPL-3.0-or-later 13 14 ;; This program is free software; you can redistribute it and/or modify 15 ;; it under the terms of the GNU General Public License as published by 16 ;; the Free Software Foundation, either version 3 of the License, or 17 ;; (at your option) any later version. 18 19 ;; This program is distributed in the hope that it will be useful, 20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 ;; GNU General Public License for more details. 23 24 ;; You should have received a copy of the GNU General Public License 25 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 26 27 ;; This file is NOT part of GNU Emacs. 28 29 ;;; Commentary: 30 31 ;; gptel is a simple Large Language Model chat client, with support for multiple 32 ;; models and backends. 33 ;; 34 ;; It works in the spirit of Emacs, available at any time and in any buffer. 35 ;; 36 ;; gptel supports 37 ;; 38 ;; - The services ChatGPT, Azure, Gemini, Anthropic AI, Anyscale, Together.ai, 39 ;; Perplexity, Anyscale, OpenRouter, Groq, PrivateGPT, DeepSeek, Cerebras, 40 ;; Github Models, xAI and Kagi (FastGPT & Summarizer) 41 ;; - Local models via Ollama, Llama.cpp, Llamafiles or GPT4All 42 ;; 43 ;; Additionally, any LLM service (local or remote) that provides an 44 ;; OpenAI-compatible API is supported. 45 ;; 46 ;; Features: 47 ;; - It’s async and fast, streams responses. 48 ;; - Interact with LLMs from anywhere in Emacs (any buffer, shell, minibuffer, 49 ;; wherever) 50 ;; - LLM responses are in Markdown or Org markup. 51 ;; - Supports conversations and multiple independent sessions. 52 ;; - Supports multi-modal models (send images, documents). 53 ;; - Save chats as regular Markdown/Org/Text files and resume them later. 54 ;; - You can go back and edit your previous prompts or LLM responses when 55 ;; continuing a conversation. These will be fed back to the model. 56 ;; - Redirect prompts and responses easily 57 ;; - Rewrite, refactor or fill in regions in buffers 58 ;; - Write your own commands for custom tasks with a simple API. 59 ;; 60 ;; Requirements for ChatGPT, Azure, Gemini or Kagi: 61 ;; 62 ;; - You need an appropriate API key. Set the variable `gptel-api-key' to the 63 ;; key or to a function of no arguments that returns the key. (It tries to 64 ;; use `auth-source' by default) 65 ;; 66 ;; ChatGPT is configured out of the box. For the other sources: 67 ;; 68 ;; - For Azure: define a gptel-backend with `gptel-make-azure', which see. 69 ;; - For Gemini: define a gptel-backend with `gptel-make-gemini', which see. 70 ;; - For Anthropic (Claude): define a gptel-backend with `gptel-make-anthropic', 71 ;; which see 72 ;; - For Together.ai, Anyscale, Perplexity, Groq, OpenRouter, DeepSeek, Cerebras or 73 ;; Github Models: define a gptel-backend with `gptel-make-openai', which see. 74 ;; - For PrivateGPT: define a backend with `gptel-make-privategpt', which see. 75 ;; - For Kagi: define a gptel-backend with `gptel-make-kagi', which see. 76 ;; 77 ;; For local models using Ollama, Llama.cpp or GPT4All: 78 ;; 79 ;; - The model has to be running on an accessible address (or localhost) 80 ;; - Define a gptel-backend with `gptel-make-ollama' or `gptel-make-gpt4all', 81 ;; which see. 82 ;; - Llama.cpp or Llamafiles: Define a gptel-backend with `gptel-make-openai', 83 ;; 84 ;; Consult the package README for examples and more help with configuring 85 ;; backends. 86 ;; 87 ;; Usage: 88 ;; 89 ;; gptel can be used in any buffer or in a dedicated chat buffer. The 90 ;; interaction model is simple: Type in a query and the response will be 91 ;; inserted below. You can continue the conversation by typing below the 92 ;; response. 93 ;; 94 ;; To use this in any buffer: 95 ;; 96 ;; - Call `gptel-send' to send the buffer's text up to the cursor. Select a 97 ;; region to send only the region. 98 ;; 99 ;; - You can select previous prompts and responses to continue the conversation. 100 ;; 101 ;; - Call `gptel-send' with a prefix argument to access a menu where you can set 102 ;; your backend, model and other parameters, or to redirect the 103 ;; prompt/response. 104 ;; 105 ;; To use this in a dedicated buffer: 106 ;; 107 ;; - M-x gptel: Start a chat session 108 ;; 109 ;; - In the chat session: Press `C-c RET' (`gptel-send') to send your prompt. 110 ;; Use a prefix argument (`C-u C-c RET') to access a menu. In this menu you 111 ;; can set chat parameters like the system directives, active backend or 112 ;; model, or choose to redirect the input or output elsewhere (such as to the 113 ;; kill ring or the echo area). 114 ;; 115 ;; - You can save this buffer to a file. When opening this file, turn on 116 ;; `gptel-mode' before editing it to restore the conversation state and 117 ;; continue chatting. 118 ;; 119 ;; - To include media files with your request, you can add them to the context 120 ;; (described next), or include them as links in Org or Markdown mode chat 121 ;; buffers. Sending media is disabled by default, you can turn it on globally 122 ;; via `gptel-track-media', or locally in a chat buffer via the header line. 123 ;; 124 ;; Include more context with requests: 125 ;; 126 ;; If you want to provide the LLM with more context, you can add arbitrary 127 ;; regions, buffers or files to the query with `gptel-add'. To add text or 128 ;; media files, call `gptel-add' in Dired or use the dedicated `gptel-add-file'. 129 ;; 130 ;; You can also add context from gptel's menu instead (gptel-send with a prefix 131 ;; arg), as well as examine or modify context. 132 ;; 133 ;; When context is available, gptel will include it with each LLM query. 134 ;; 135 ;; Rewrite/refactor interface 136 ;; 137 ;; In any buffer: with a region selected, you can rewrite prose, refactor code 138 ;; or fill in the region. This is accessible via `gptel-rewrite', and also from 139 ;; the `gptel-send' menu. 140 ;; 141 ;; gptel in Org mode: 142 ;; 143 ;; gptel offers a few extra conveniences in Org mode. 144 ;; - You can limit the conversation context to an Org heading with 145 ;; `gptel-org-set-topic'. 146 ;; 147 ;; - You can have branching conversations in Org mode, where each hierarchical 148 ;; outline path through the document is a separate conversation branch. 149 ;; See the variable `gptel-org-branching-context'. 150 ;; 151 ;; - You can declare the gptel model, backend, temperature, system message and 152 ;; other parameters as Org properties with the command 153 ;; `gptel-org-set-properties'. gptel queries under the corresponding heading 154 ;; will always use these settings, allowing you to create mostly reproducible 155 ;; LLM chat notebooks. 156 ;; 157 ;; Finally, gptel offers a general purpose API for writing LLM ineractions 158 ;; that suit your workflow, see `gptel-request'. 159 160 ;;; Code: 161 (declare-function markdown-mode "markdown-mode") 162 (declare-function gptel-curl-get-response "gptel-curl") 163 (declare-function gptel-menu "gptel-transient") 164 (declare-function gptel-system-prompt "gptel-transient") 165 (declare-function pulse-momentary-highlight-region "pulse") 166 167 (declare-function ediff-make-cloned-buffer "ediff-util") 168 (declare-function ediff-regions-internal "ediff") 169 170 (declare-function gptel-org--create-prompt "gptel-org") 171 (declare-function gptel-org-set-topic "gptel-org") 172 (declare-function gptel-org--save-state "gptel-org") 173 (declare-function gptel-org--restore-state "gptel-org") 174 (declare-function gptel--stream-convert-markdown->org "gptel-org") 175 (declare-function gptel--convert-markdown->org "gptel-org") 176 (define-obsolete-function-alias 177 'gptel-set-topic 'gptel-org-set-topic "0.7.5") 178 179 (eval-when-compile 180 (require 'subr-x)) 181 (require 'cl-lib) 182 (require 'compat nil t) 183 (require 'url) 184 (require 'map) 185 (require 'text-property-search) 186 (require 'cl-generic) 187 (require 'gptel-openai) 188 189 (with-eval-after-load 'org 190 (require 'gptel-org)) 191 192 193 ;;; User options 194 195 (defgroup gptel nil 196 "Interact with LLMs from anywhere in Emacs." 197 :group 'hypermedia) 198 199 ;; (defcustom gptel-host "api.openai.com" 200 ;; "The API host queried by gptel." 201 ;; :group 'gptel 202 ;; :type 'string) 203 (make-obsolete-variable 204 'gptel-host 205 "Use `gptel-make-openai' instead." 206 "0.5.0") 207 208 (defcustom gptel-proxy "" 209 "Path to a proxy to use for gptel interactions. 210 Passed to curl via --proxy arg, for example \"proxy.yourorg.com:80\" 211 Leave it empty if you don't use a proxy." 212 :type 'string) 213 214 (defcustom gptel-api-key #'gptel-api-key-from-auth-source 215 "An API key (string) for the default LLM backend. 216 217 OpenAI by default. 218 219 Can also be a function of no arguments that returns an API 220 key (more secure) for the active backend." 221 :type '(choice 222 (string :tag "API key") 223 (function :tag "Function that returns the API key"))) 224 225 (defcustom gptel-stream t 226 "Stream responses from the LLM as they are received. 227 228 This option is ignored unless 229 - the LLM backend supports streaming, and 230 - Curl is in use (see `gptel-use-curl') 231 232 When set to nil, Emacs waits for the full response and inserts it 233 all at once. This wait is asynchronous. 234 235 \='tis a bit silly." 236 :type 'boolean) 237 (make-obsolete-variable 'gptel-playback 'gptel-stream "0.3.0") 238 239 (defcustom gptel-use-curl (and (executable-find "curl") t) 240 "Whether gptel should prefer Curl when available." 241 :type 'boolean) 242 243 (defcustom gptel-curl-file-size-threshold 130000 244 "Size threshold for using file input with Curl. 245 246 Specifies the size threshold for when to use a temporary file to pass data to 247 Curl in GPTel queries. If the size of the data to be sent exceeds this 248 threshold, the data is written to a temporary file and passed to Curl using the 249 `--data-binary' option with a file reference. Otherwise, the data is passed 250 directly as a command-line argument. 251 252 The value is an integer representing the number of bytes. 253 254 Adjusting this value may be necessary depending on the environment 255 and the typical size of the data being sent in GPTel queries. 256 A larger value may improve performance by avoiding the overhead of creating 257 temporary files for small data payloads, while a smaller value may be needed 258 if the command-line argument size is limited by the operating system." 259 :type 'natnum) 260 261 (defcustom gptel-response-filter-functions 262 (list #'gptel--convert-org) 263 "Abnormal hook for transforming the response from an LLM. 264 265 This is used to format the response in some way, such as filling 266 paragraphs, adding annotations or recording information in the 267 response like links. 268 269 Each function in this hook receives two arguments, the response 270 string to transform and the LLM interaction buffer. It 271 should return the transformed string. 272 273 NOTE: This is only used for non-streaming responses. To 274 transform streaming responses, use `gptel-post-stream-hook' and 275 `gptel-post-response-functions'." 276 :type 'hook) 277 278 (defcustom gptel-pre-response-hook nil 279 "Hook run before inserting the LLM response into the current buffer. 280 281 This hook is called in the buffer where the LLM response will be 282 inserted. 283 284 Note: this hook only runs if the request succeeds." 285 :type 'hook) 286 287 (define-obsolete-variable-alias 288 'gptel-post-response-hook 'gptel-post-response-functions 289 "0.6.0" 290 "Post-response functions are now called with two arguments: the 291 start and end buffer positions of the response.") 292 293 (defcustom gptel-post-response-functions nil 294 "Abnormal hook run after inserting the LLM response into the current buffer. 295 296 This hook is called in the buffer to which the LLM response is 297 sent, and after the full response has been inserted. Each 298 function is called with two arguments: the response beginning and 299 end positions. 300 301 Note: this hook runs even if the request fails. In this case the 302 response beginning and end positions are both the cursor position 303 at the time of the request." 304 :type 'hook) 305 306 ;; (defcustom gptel-pre-stream-insert-hook nil 307 ;; "Hook run before each insertion of the LLM's streaming response. 308 309 ;; This hook is called in the buffer from which the prompt was sent 310 ;; to the LLM, immediately before text insertion." 311 ;; :group 'gptel 312 ;; :type 'hook) 313 314 (defcustom gptel-post-stream-hook nil 315 "Hook run after each insertion of the LLM's streaming response. 316 317 This hook is called in the buffer from which the prompt was sent 318 to the LLM, and after a text insertion." 319 :type 'hook) 320 321 (defcustom gptel-save-state-hook nil 322 "Hook run before gptel saves model parameters to a file. 323 324 You can use this hook to store additional conversation state or 325 model parameters to the chat buffer, or to modify the buffer in 326 some other way." 327 :type 'hook) 328 329 (defcustom gptel-default-mode (if (fboundp 'markdown-mode) 330 'markdown-mode 331 'text-mode) 332 "The default major mode for dedicated chat buffers. 333 334 If `markdown-mode' is available, it is used. Otherwise gptel 335 defaults to `text-mode'." 336 :type 'function) 337 338 ;; TODO: Handle `prog-mode' using the `comment-start' variable 339 (defcustom gptel-prompt-prefix-alist 340 '((markdown-mode . "### ") 341 (org-mode . "*** ") 342 (text-mode . "### ")) 343 "String used as a prefix to the query being sent to the LLM. 344 345 This is meant for the user to distinguish between queries and 346 responses, and is removed from the query before it is sent. 347 348 This is an alist mapping major modes to the prefix strings. This 349 is only inserted in dedicated gptel buffers." 350 :type '(alist :key-type symbol :value-type string)) 351 352 (defcustom gptel-response-prefix-alist 353 '((markdown-mode . "") 354 (org-mode . "") 355 (text-mode . "")) 356 "String inserted before the response from the LLM. 357 358 This is meant for the user to distinguish between queries and 359 responses. 360 361 This is an alist mapping major modes to the reply prefix strings. This 362 is only inserted in dedicated gptel buffers before the AI's response." 363 :type '(alist :key-type symbol :value-type string)) 364 365 (defcustom gptel-use-header-line t 366 "Whether `gptel-mode' should use header-line for status information. 367 368 When set to nil, use the mode line for (minimal) status 369 information and the echo area for messages." 370 :type 'boolean) 371 372 (defcustom gptel-display-buffer-action '(pop-to-buffer) 373 "The action used to display gptel chat buffers. 374 375 The gptel buffer is displayed in a window using 376 377 (display-buffer BUFFER gptel-display-buffer-action) 378 379 The value of this option has the form (FUNCTION . ALIST), 380 where FUNCTION is a function or a list of functions. Each such 381 function should accept two arguments: a buffer to display and an 382 alist of the same form as ALIST. See info node `(elisp)Choosing 383 Window' for details." 384 :type display-buffer--action-custom-type) 385 386 (defcustom gptel-crowdsourced-prompts-file 387 (let ((cache-dir (or (eval-when-compile 388 (require 'xdg) 389 (xdg-cache-home)) 390 user-emacs-directory))) 391 (expand-file-name "gptel-crowdsourced-prompts.csv" cache-dir)) 392 "File used to store crowdsourced system prompts. 393 394 These are prompts cached from an online source (see 395 `gptel--crowdsourced-prompts-url'), and can be set from the 396 transient menu interface provided by `gptel-menu'." 397 :type 'file) 398 399 ;; Model and interaction parameters 400 (defcustom gptel-directives 401 '((default . "You are a large language model living in Emacs and a helpful assistant. Respond concisely.") 402 (programming . "You are a large language model and a careful programmer. Provide code and only code as output without any additional text, prompt or note.") 403 (writing . "You are a large language model and a writing assistant. Respond concisely.") 404 (chat . "You are a large language model and a conversation partner. Respond concisely.")) 405 "System prompts or directives for the LLM. 406 407 Each entry in this alist maps a symbol naming the directive to 408 the directive itself. By default, gptel uses the directive with 409 the key \\+`default'. 410 411 To set the directive for a chat session interactively call 412 `gptel-send' with a prefix argument, or call `gptel-menu'. 413 414 A \"directive\" is typically the system message (also called 415 system prompt or system instruction) sent at the beginning of 416 each request to the LLM. It is used to set general instructions, 417 expectations and the overall tone. 418 419 gptel's idea of the directive is more general. A directive in 420 `gptel-directives' can be 421 422 - A string, interpreted as the system message. 423 424 - A list of strings, whose first (possibly nil) element is 425 interpreted as the system message, and the remaining elements 426 as (possibly nil) alternating user prompts and LLM responses. 427 This can be used to template the initial part of a conversation. 428 429 - A function that returns a string or a list of strings, 430 interpreted as the above. This can be used to dynamically 431 generate a system message and/or conversation template based on 432 the current context. See the definition of 433 `gptel--rewrite-directive-default' for an example." 434 :safe #'always 435 :type '(alist :key-type symbol :value-type string)) 436 437 (defcustom gptel-max-tokens nil 438 "Max tokens per response. 439 440 This is roughly the number of words in the response. 100-300 is a 441 reasonable range for short answers, 400 or more for longer 442 responses. 443 444 To set the target token count for a chat session interactively 445 call `gptel-send' with a prefix argument." 446 :safe #'always 447 :type '(choice (natnum :tag "Specify Token count") 448 (const :tag "Default" nil))) 449 450 (defcustom gptel-model 'gpt-4o-mini 451 "GPT Model for chat. 452 453 The name of the model, as a symbol. This is the name as expected 454 by the LLM provider's API. 455 456 The current options for ChatGPT are 457 - `gpt-3.5-turbo' 458 - `gpt-3.5-turbo-16k' 459 - `gpt-4o-mini' 460 - `gpt-4' 461 - `gpt-4o' 462 - `gpt-4-turbo' 463 - `gpt-4-turbo-preview' 464 - `gpt-4-32k' 465 - `gpt-4-1106-preview' 466 467 To set the model for a chat session interactively call 468 `gptel-send' with a prefix argument." 469 :safe #'always 470 :type '(choice 471 (symbol :tag "Specify model name") 472 (const :tag "GPT 4 omni mini" gpt-4o-mini) 473 (const :tag "GPT 3.5 turbo" gpt-3.5-turbo) 474 (const :tag "GPT 3.5 turbo 16k" gpt-3.5-turbo-16k) 475 (const :tag "GPT 4" gpt-4) 476 (const :tag "GPT 4 omni" gpt-4o) 477 (const :tag "GPT 4 turbo" gpt-4-turbo) 478 (const :tag "GPT 4 turbo (preview)" gpt-4-turbo-preview) 479 (const :tag "GPT 4 32k" gpt-4-32k) 480 (const :tag "GPT 4 1106 (preview)" gpt-4-1106-preview))) 481 482 (defcustom gptel-temperature 1.0 483 "\"Temperature\" of the LLM response. 484 485 This is a number between 0.0 and 2.0 that controls the randomness 486 of the response, with 2.0 being the most random. 487 488 To set the temperature for a chat session interactively call 489 `gptel-send' with a prefix argument." 490 :safe #'always 491 :type 'number) 492 493 (defvar gptel--known-backends) 494 495 (defconst gptel--openai-models 496 '((gpt-4o 497 :description "Advanced model for complex tasks; cheaper & faster than GPT-Turbo" 498 :capabilities (media tool json url) 499 :mime-types ("image/jpeg" "image/png" "image/gif" "image/webp") 500 :context-window 128 501 :input-cost 2.50 502 :output-cost 10 503 :cutoff-date "2023-10") 504 (gpt-4o-mini 505 :description "Cheap model for fast tasks; cheaper & more capable than GPT-3.5 Turbo" 506 :capabilities (media tool json url) 507 :mime-types ("image/jpeg" "image/png" "image/gif" "image/webp") 508 :context-window 128 509 :input-cost 0.15 510 :output-cost 0.60 511 :cutoff-date "2023-10") 512 (gpt-4-turbo 513 :description "Previous high-intelligence model" 514 :capabilities (media tool url) 515 :mime-types ("image/jpeg" "image/png" "image/gif" "image/webp") 516 :context-window 128 517 :input-cost 10 518 :output-cost 30 519 :cutoff-date "2023-12") 520 ;; points to gpt-4-0613 521 (gpt-4 522 :description "GPT-4 snapshot from June 2023 with improved function calling support" 523 :context-window 8.192 524 :input-cost 30 525 :output-cost 60 526 :cutoff-date "2023-09") 527 (gpt-4-turbo-preview 528 :description "Points to gpt-4-0125-preview" 529 :context-window 128 530 :input-cost 10 531 :output-cost 30 532 :cutoff-date "2023-12") 533 (gpt-4-0125-preview 534 :description "GPT-4 Turbo preview model intended to reduce cases of “laziness”" 535 :context-window 128 536 :input-cost 10 537 :output-cost 30 538 :cutoff-date "2023-12") 539 (o1-preview 540 :description "Reasoning model designed to solve hard problems across domains" 541 :context-window 128 542 :input-cost 15 543 :output-cost 60 544 :cutoff-date "2023-10" 545 :capabilities (nosystem) 546 :request-params (:stream :json-false)) 547 (o1-mini 548 :description "Faster and cheaper reasoning model good at coding, math, and science" 549 :context-window 128 550 :input-cost 3 551 :output-cost 12 552 :cutoff-date "2023-10" 553 :capabilities (nosystem) 554 :request-params (:stream :json-false)) 555 ;; limited information available 556 (gpt-4-32k 557 :input-cost 60 558 :output-cost 120) 559 (gpt-4-1106-preview 560 :description "Preview model with improved function calling support" 561 :context-window 128 562 :input-cost 10 563 :output-cost 30 564 :cutoff-date "2023-04") 565 (gpt-3.5-turbo 566 :description "More expensive & less capable than GPT-4o-mini; use that instead" 567 :capabilities (tool) 568 :mime-types ("image/jpeg" "image/png" "image/gif" "image/webp") 569 :context-window 16.358 570 :input-cost 0.50 571 :output-cost 1.50 572 :cutoff-date "2021-09") 573 (gpt-3.5-turbo-16k 574 :description "More expensive & less capable than GPT-4o-mini; use that instead" 575 :mime-types ("image/jpeg" "image/png" "image/gif" "image/webp") 576 :context-window 16.385 577 :input-cost 3 578 :output-cost 4 579 :cutoff-date "2021-09")) 580 "List of available OpenAI models and associated properties. 581 Keys: 582 583 - `:description': a brief description of the model. 584 585 - `:capabilities': a list of capabilities supported by the model. 586 587 - `:mime-types': a list of supported MIME types for media files. 588 589 - `:context-window': the context window size, in thousands of tokens. 590 591 - `:input-cost': the input cost, in US dollars per million tokens. 592 593 - `:output-cost': the output cost, in US dollars per million tokens. 594 595 - `:cutoff-date': the knowledge cutoff date. 596 597 - `:request-params': a plist of additional request parameters to 598 include when using this model. 599 600 Information about the OpenAI models was obtained from the following 601 sources: 602 603 - <https://openai.com/pricing> 604 - <https://platform.openai.com/docs/models>") 605 606 (defvar gptel--openai 607 (gptel-make-openai 608 "ChatGPT" 609 :key 'gptel-api-key 610 :stream t 611 :models gptel--openai-models)) 612 613 (defcustom gptel-backend gptel--openai 614 "LLM backend to use. 615 616 This is the default \"backend\", an object of type 617 `gptel-backend' containing connection, authentication and model 618 information. 619 620 A backend for ChatGPT is pre-defined by gptel. Backends for 621 other LLM providers (local or remote) may be constructed using 622 one of the available backend creation functions: 623 - `gptel-make-openai' 624 - `gptel-make-azure' 625 - `gptel-make-ollama' 626 - `gptel-make-gpt4all' 627 - `gptel-make-gemini' 628 See their documentation for more information and the package 629 README for examples." 630 :safe #'always 631 :type `(choice 632 (const :tag "ChatGPT" ,gptel--openai) 633 (restricted-sexp :match-alternatives (gptel-backend-p 'nil) 634 :tag "Other backend"))) 635 636 (defvar gptel-expert-commands nil 637 "Whether experimental gptel options should be enabled. 638 639 This opens up advanced options in `gptel-menu'.") 640 641 (defvar-local gptel--bounds nil) 642 (put 'gptel--bounds 'safe-local-variable #'always) 643 644 (defvar gptel--num-messages-to-send nil) 645 (put 'gptel--num-messages-to-send 'safe-local-variable #'always) 646 647 (defcustom gptel-log-level nil 648 "Logging level for gptel. 649 650 This is one of nil or the symbols info and debug: 651 652 nil: Don't log responses 653 info: Log request and response bodies 654 debug: Log request/response bodies, headers and all other 655 connection settings. 656 657 When non-nil, information is logged to `gptel--log-buffer-name', 658 which see." 659 :type '(choice 660 (const :tag "No logging" nil) 661 (const :tag "Limited" info) 662 (const :tag "Full" debug))) 663 (make-obsolete-variable 664 'gptel--debug 'gptel-log-level "0.6.5") 665 666 (defcustom gptel-track-response t 667 "Distinguish between user messages and LLM responses. 668 669 When creating a prompt to send to the LLM, gptel distinguishes 670 between text entered by the user and past LLM responses. This 671 distinction is necessary for back-and-forth conversation with an 672 LLM. 673 674 In regular Emacs buffers you can turn this behavior off by 675 setting `gptel-track-response' to nil. All text, including 676 past LLM responses, is then treated as user input when sending 677 queries. 678 679 This variable has no effect in dedicated chat buffers (buffers 680 with `gptel-mode' enabled), where user prompts and responses are 681 always handled separately." 682 :type 'boolean) 683 684 (defcustom gptel-track-media nil 685 "Whether supported media in chat buffers should be sent. 686 687 When the active `gptel-model' supports it, gptel can send images 688 or other media from links in chat buffers to the LLM. To use 689 this, the following steps are required. 690 691 1. `gptel-track-media' (this variable) should be non-nil 692 693 2. The LLM should provide vision or document support. Currently, 694 only the OpenAI, Anthropic and Ollama APIs are supported. See 695 the documentation of `gptel-make-openai', `gptel-make-anthropic' 696 and `gptel-make-ollama' resp. for details on how to specify media 697 support for models. 698 699 3. Only \"standalone\" links in chat buffers are considered. 700 These are links on their own line with no surrounding text. 701 Further: 702 703 - In Org mode, only files or URLs of the form 704 [[/path/to/media][bracket links]] and <angle/link/path> 705 are sent. 706 707 - In Markdown mode, only files or URLS of the form 708 [bracket link](/path/to/media) and <angle/link/path> 709 are sent. 710 711 This option has no effect in non-chat buffers. To include 712 media (including images) more generally, use `gptel-add'." 713 :type 'boolean) 714 715 (defcustom gptel-use-context 'system 716 "Where in the request to inject gptel's additional context. 717 718 gptel always includes the active region or the buffer up to the 719 cursor in the request to the LLM. Additionally, you can add 720 other buffers or their regions to the context with 721 `gptel-add-context', or from gptel's menu. This data will be 722 sent with every request. 723 724 This option controls whether and where this additional context is 725 included in the request. 726 727 Currently supported options are: 728 729 nil - Do not use the context. 730 system - Include the context with the system message. 731 user - Include the context with the user prompt." 732 :group 'gptel 733 :type '(choice 734 (const :tag "Don't include context" nil) 735 (const :tag "With system message" system) 736 (const :tag "With user prompt" user))) 737 738 (defvar-local gptel--old-header-line nil) 739 740 (defvar gptel-context--alist nil 741 "List of gptel's context sources. 742 743 Each entry is of the form 744 (buffer . (overlay1 overlay2 ...)) 745 or 746 (\"path/to/file\").") 747 748 749 ;;; Utility functions 750 751 (defun gptel-api-key-from-auth-source (&optional host user) 752 "Lookup api key in the auth source. 753 By default, the LLM host for the active backend is used as HOST, 754 and \"apikey\" as USER." 755 (if-let ((secret 756 (plist-get 757 (car (auth-source-search 758 :host (or host (gptel-backend-host gptel-backend)) 759 :user (or user "apikey") 760 :require '(:secret))) 761 :secret))) 762 (if (functionp secret) 763 (encode-coding-string (funcall secret) 'utf-8) 764 secret) 765 (user-error "No `gptel-api-key' found in the auth source"))) 766 767 ;; FIXME Should we utf-8 encode the api-key here? 768 (defun gptel--get-api-key (&optional key) 769 "Get api key from KEY, or from `gptel-api-key'." 770 (when-let ((key-sym (or key (gptel-backend-key gptel-backend)))) 771 (cl-typecase key-sym 772 (function (string-trim-right (funcall key-sym) "[\n\r]+")) 773 (string (string-trim-right key-sym "[\n\r]+")) 774 (symbol (if-let ((val (symbol-value key-sym))) 775 (gptel--get-api-key val) 776 (error "`gptel-api-key' is not valid"))) 777 (t (error "`gptel-api-key' is not valid"))))) 778 779 (defsubst gptel--to-number (val) 780 "Ensure VAL is a number." 781 (cond 782 ((numberp val) val) 783 ((stringp val) (string-to-number val)) 784 ((error "%S cannot be converted to a number" val)))) 785 786 (defsubst gptel--to-string (s) 787 "Convert S to a string, if possible." 788 (cl-etypecase s 789 (symbol (symbol-name s)) 790 (string s) 791 (number (number-to-string s)))) 792 793 (defsubst gptel--intern (s) 794 "Intern S, if possible." 795 (cl-etypecase s 796 (symbol s) 797 (string (intern s)))) 798 799 (defun gptel--merge-plists (&rest plists) 800 "Merge PLISTS, altering the first one. 801 802 Later plists in the sequence take precedence over earlier ones." 803 (let (;; (rtn (copy-sequence (pop plists))) 804 (rtn (pop plists)) 805 p v ls) 806 (while plists 807 (setq ls (pop plists)) 808 (while ls 809 (setq p (pop ls) v (pop ls)) 810 (setq rtn (plist-put rtn p v)))) 811 rtn)) 812 (defun gptel-auto-scroll () 813 "Scroll window if LLM response continues below viewport. 814 815 Note: This will move the cursor." 816 (when-let ((win (get-buffer-window (current-buffer) 'visible)) 817 ((not (pos-visible-in-window-p (point) win))) 818 (scroll-error-top-bottom t)) 819 (condition-case nil 820 (with-selected-window win 821 (scroll-up-command)) 822 (error nil)))) 823 824 (defun gptel-beginning-of-response (&optional _ _ arg) 825 "Move point to the beginning of the LLM response ARG times." 826 (interactive "p") 827 ;; FIXME: Only works for arg == 1 828 (gptel-end-of-response nil nil (- (or arg 1)))) 829 830 (defun gptel-end-of-response (&optional _ _ arg) 831 "Move point to the end of the LLM response ARG times." 832 (interactive (list nil nil 833 (prefix-numeric-value current-prefix-arg))) 834 (unless arg (setq arg 1)) 835 (let ((search (if (> arg 0) 836 #'text-property-search-forward 837 #'text-property-search-backward))) 838 (dotimes (_ (abs arg)) 839 (funcall search 'gptel 'response t) 840 (if (> arg 0) 841 (when (looking-at (concat "\n\\{1,2\\}" 842 (regexp-quote 843 (gptel-prompt-prefix-string)) 844 "?")) 845 (goto-char (match-end 0))) 846 (when (looking-back (concat (regexp-quote 847 (gptel-response-prefix-string)) 848 "?") 849 (point-min)) 850 (goto-char (match-beginning 0))))))) 851 852 (defmacro gptel--at-word-end (&rest body) 853 "Execute BODY at end of the current word or punctuation." 854 `(save-excursion 855 (skip-syntax-forward "w.") 856 ,(macroexp-progn body))) 857 858 (defun gptel-prompt-prefix-string () 859 "Prefix before user prompts in `gptel-mode'." 860 (or (alist-get major-mode gptel-prompt-prefix-alist) "")) 861 862 (defun gptel-response-prefix-string () 863 "Prefix before LLM responses in `gptel-mode'." 864 (or (alist-get major-mode gptel-response-prefix-alist) "")) 865 866 (defsubst gptel--trim-prefixes (s) 867 "Remove prompt/response prefixes from string S." 868 (string-trim s 869 (format "[\t\r\n ]*\\(?:%s\\)?[\t\r\n ]*" 870 (regexp-quote (gptel-prompt-prefix-string))) 871 (format "[\t\r\n ]*\\(?:%s\\)?[\t\r\n ]*" 872 (regexp-quote (gptel-response-prefix-string))))) 873 874 (defsubst gptel--link-standalone-p (beg end) 875 "Return non-nil if positions BEG and END are isolated. 876 877 This means the extent from BEG to END is the only non-whitespace 878 content on this line." 879 (save-excursion 880 (and (= beg (progn (goto-char beg) (beginning-of-line) 881 (skip-chars-forward "\t ") 882 (point))) 883 (= end (progn (goto-char end) (end-of-line) 884 (skip-chars-backward "\t ") 885 (point)))))) 886 887 (defvar-local gptel--backend-name nil 888 "Store to persist backend name across Emacs sessions. 889 890 Note: Changing this variable does not affect gptel\\='s behavior 891 in any way.") 892 (put 'gptel--backend-name 'safe-local-variable #'always) 893 894 ;;;; Model interface 895 ;; NOTE: This interface would be simpler to implement as a defstruct. But then 896 ;; users cannot set `gptel-model' to a symbol/string directly, or we'd need 897 ;; another map from these symbols to the actual model structs. 898 899 (defsubst gptel--model-name (model) 900 "Get name of gptel MODEL." 901 (gptel--to-string model)) 902 903 (defsubst gptel--model-capabilities (model) 904 "Get MODEL capabilities." 905 (get model :capabilities)) 906 907 (defsubst gptel--model-mimes (model) 908 "Get supported mime-types for MODEL." 909 (get model :mime-types)) 910 911 (defsubst gptel--model-capable-p (cap &optional model) 912 "Return non-nil if MODEL supports capability CAP." 913 (memq cap (gptel--model-capabilities 914 (or model gptel-model)))) 915 916 ;; TODO Handle model mime specifications like "image/*" 917 (defsubst gptel--model-mime-capable-p (mime &optional model) 918 "Return non nil if MODEL can understand MIME type." 919 (car-safe (member mime (gptel--model-mimes 920 (or model gptel-model))))) 921 922 (defsubst gptel--model-request-params (model) 923 "Get model-specific request parameters for MODEL." 924 (get model :request-params)) 925 926 ;;;; File handling 927 (defun gptel--base64-encode (file) 928 "Encode FILE as a base64 string. 929 930 FILE is assumed to exist and be a regular file." 931 (with-temp-buffer 932 (insert-file-contents-literally file) 933 (base64-encode-region (point-min) (point-max) 934 :no-line-break) 935 (buffer-string))) 936 937 ;;;; Response text recognition 938 939 (defun gptel--get-buffer-bounds () 940 "Return the gptel response boundaries in the buffer as an alist." 941 (save-excursion 942 (save-restriction 943 (widen) 944 (goto-char (point-max)) 945 (let ((prop) (bounds)) 946 (while (setq prop (text-property-search-backward 947 'gptel 'response t)) 948 (push (cons (prop-match-beginning prop) 949 (prop-match-end prop)) 950 bounds)) 951 bounds)))) 952 953 (defun gptel--get-bounds () 954 "Return the gptel response boundaries around point." 955 (let (prop) 956 (save-excursion 957 (when (text-property-search-backward 958 'gptel 'response t) 959 (when (setq prop (text-property-search-forward 960 'gptel 'response t)) 961 (cons (prop-match-beginning prop) 962 (prop-match-end prop))))))) 963 964 (defun gptel--in-response-p (&optional pt) 965 "Check if position PT is inside a gptel response." 966 (get-char-property (or pt (point)) 'gptel)) 967 968 (defun gptel--at-response-history-p (&optional pt) 969 "Check if gptel response at position PT has variants." 970 (get-char-property (or pt (point)) 'gptel-history)) 971 972 (defvar gptel--mode-description-alist 973 '((js2-mode . "Javascript") 974 (sh-mode . "Shell") 975 (enh-ruby-mode . "Ruby") 976 (yaml-mode . "Yaml") 977 (yaml-ts-mode . "Yaml") 978 (rustic-mode . "Rust")) 979 "Mapping from unconventionally named major modes to languages. 980 981 This is used when generating system prompts for rewriting and 982 when including context from these major modes.") 983 984 (defun gptel--strip-mode-suffix (mode-sym) 985 "Remove the -mode suffix from MODE-SYM. 986 987 MODE-SYM is typically a major-mode symbol." 988 (or (alist-get mode-sym gptel--mode-description-alist) 989 (let ((mode-name (thread-last 990 (symbol-name mode-sym) 991 (string-remove-suffix "-mode") 992 (string-remove-suffix "-ts")))) 993 ;; NOTE: The advertised calling convention of provided-mode-derived-p 994 ;; has changed in Emacs 30, this needs to be updated eventually 995 (if (provided-mode-derived-p 996 mode-sym 'prog-mode 'text-mode 'tex-mode) 997 mode-name "")))) 998 999 ;;;; Directive handling 1000 1001 1002 (defvar gptel--system-message (alist-get 'default gptel-directives) 1003 "The system message used by gptel.") 1004 (put 'gptel--system-message 'safe-local-variable #'always) 1005 1006 (defun gptel--describe-directive (directive width &optional replacement) 1007 "Find description for DIRECTIVE, truncated to WIDTH. 1008 1009 DIRECTIVE is a gptel directive, and can be a string, a function 1010 or a list of strings. See `gptel-directives'. 1011 1012 The result is a string intended for display. Newlines are 1013 replaced with REPLACEMENT." 1014 (cl-typecase directive 1015 (string 1016 (concat 1017 (string-replace "\n" (or replacement " ") 1018 (truncate-string-to-width 1019 directive width nil nil t)))) 1020 (function 1021 (concat 1022 "λ: " 1023 (string-replace 1024 "\n" (or replacement " ") 1025 (truncate-string-to-width 1026 (or (and-let* ((doc (documentation directive))) 1027 (substring doc nil (string-match-p "\n" doc))) 1028 "[Dynamically generated; no preview available]") 1029 width nil nil t)))) 1030 (list (and-let* ((from-template (car directive))) 1031 (gptel--describe-directive 1032 from-template width))) 1033 (t ""))) 1034 1035 (defun gptel--parse-directive (directive &optional raw) 1036 "Parse DIRECTIVE into a backend-appropriate form. 1037 1038 DIRECTIVE is a gptel directive: it can be a string, a list or a 1039 function that returns either, see `gptel-directives'. 1040 1041 Return a cons cell consisting of the system message (a string) 1042 and a template consisting of alternating user/LLM 1043 records (a list of strings or nil). 1044 1045 If RAW is non-nil, the user/LLM records are not processed and are 1046 returned as a list of strings." 1047 (and directive 1048 (cl-etypecase directive 1049 (string (list directive)) 1050 (function (gptel--parse-directive (funcall directive) raw)) 1051 (cons (if raw directive 1052 (cons (car directive) 1053 (gptel--parse-list 1054 gptel-backend (cdr directive)))))))) 1055 1056 1057 1058 ;;; Logging 1059 1060 (defconst gptel--log-buffer-name "*gptel-log*" 1061 "Log buffer for gptel.") 1062 1063 (declare-function json-pretty-print "json") 1064 1065 (defun gptel--log (data &optional type no-json) 1066 "Log DATA to `gptel--log-buffer-name'. 1067 1068 TYPE is a label for data being logged. DATA is assumed to be 1069 Valid JSON unless NO-JSON is t." 1070 (with-current-buffer (get-buffer-create gptel--log-buffer-name) 1071 (let ((p (goto-char (point-max)))) 1072 (unless (bobp) (insert "\n")) 1073 (insert (format "{\"gptel\": \"%s\", " (or type "none")) 1074 (format-time-string "\"timestamp\": \"%Y-%m-%d %H:%M:%S\"}\n") 1075 data) 1076 (unless no-json (ignore-errors (json-pretty-print p (point))))))) 1077 1078 1079 ;;; Saving and restoring state 1080 1081 (defun gptel--restore-state () 1082 "Restore gptel state when turning on `gptel-mode'." 1083 (when (buffer-file-name) 1084 (if (derived-mode-p 'org-mode) 1085 (progn 1086 (require 'gptel-org) 1087 (gptel-org--restore-state)) 1088 (when gptel--bounds 1089 (mapc (pcase-lambda (`(,beg . ,end)) 1090 (put-text-property beg end 'gptel 'response)) 1091 gptel--bounds) 1092 (message "gptel chat restored.")) 1093 (when gptel--backend-name 1094 (if-let ((backend (alist-get 1095 gptel--backend-name gptel--known-backends 1096 nil nil #'equal))) 1097 (setq-local gptel-backend backend) 1098 (message 1099 (substitute-command-keys 1100 (concat 1101 "Could not activate gptel backend \"%s\"! " 1102 "Switch backends with \\[universal-argument] \\[gptel-send]" 1103 " before using gptel.")) 1104 gptel--backend-name)))))) 1105 1106 (defun gptel--save-state () 1107 "Write the gptel state to the buffer. 1108 1109 This saves chat metadata when writing the buffer to disk. To 1110 restore a chat session, turn on `gptel-mode' after opening the 1111 file." 1112 (run-hooks 'gptel-save-state-hook) 1113 (if (derived-mode-p 'org-mode) 1114 (progn 1115 (require 'gptel-org) 1116 (gptel-org--save-state)) 1117 (let ((print-escape-newlines t)) 1118 (save-excursion 1119 (save-restriction 1120 (add-file-local-variable 'gptel-model gptel-model) 1121 (add-file-local-variable 'gptel--backend-name 1122 (gptel-backend-name gptel-backend)) 1123 (unless (equal (default-value 'gptel-temperature) gptel-temperature) 1124 (add-file-local-variable 'gptel-temperature gptel-temperature)) 1125 (unless (equal (default-value 'gptel--system-message) 1126 gptel--system-message) 1127 (add-file-local-variable 1128 'gptel--system-message 1129 (car-safe (gptel--parse-directive gptel--system-message)))) 1130 (when gptel-max-tokens 1131 (add-file-local-variable 'gptel-max-tokens gptel-max-tokens)) 1132 (when (natnump gptel--num-messages-to-send) 1133 (add-file-local-variable 'gptel--num-messages-to-send 1134 gptel--num-messages-to-send)) 1135 (add-file-local-variable 'gptel--bounds (gptel--get-buffer-bounds))))))) 1136 1137 1138 ;;; Minor mode and UI 1139 1140 ;; NOTE: It's not clear that this is the best strategy: 1141 (add-to-list 'text-property-default-nonsticky '(gptel . t)) 1142 1143 ;;;###autoload 1144 (define-minor-mode gptel-mode 1145 "Minor mode for interacting with LLMs." 1146 :lighter " GPT" 1147 :keymap 1148 (let ((map (make-sparse-keymap))) 1149 (define-key map (kbd "C-c RET") #'gptel-send) 1150 map) 1151 (if gptel-mode 1152 (progn 1153 (unless (or (derived-mode-p 'org-mode 'markdown-mode) 1154 (eq major-mode 'text-mode)) 1155 (gptel-mode -1) 1156 (user-error (format "`gptel-mode' is not supported in `%s'." major-mode))) 1157 (add-hook 'before-save-hook #'gptel--save-state nil t) 1158 (gptel--restore-state) 1159 (if gptel-use-header-line 1160 (setq gptel--old-header-line header-line-format 1161 header-line-format 1162 (list '(:eval (concat (propertize " " 'display '(space :align-to 0)) 1163 (format "%s" (gptel-backend-name gptel-backend)))) 1164 (propertize " Ready" 'face 'success) 1165 '(:eval 1166 (let* ((model (gptel--model-name gptel-model)) 1167 (system 1168 (propertize 1169 (buttonize 1170 (format "[Prompt: %s]" 1171 (or (car-safe (rassoc gptel--system-message gptel-directives)) 1172 (gptel--describe-directive gptel--system-message 15))) 1173 (lambda (&rest _) (gptel-system-prompt))) 1174 'mouse-face 'highlight 1175 'help-echo "System message for session")) 1176 (context 1177 (and gptel-context--alist 1178 (cl-loop for entry in gptel-context--alist 1179 if (bufferp (car entry)) count it into bufs 1180 else count (stringp (car entry)) into files 1181 finally return 1182 (propertize 1183 (buttonize 1184 (concat "[Context: " 1185 (and (> bufs 0) (format "%d buf" bufs)) 1186 (and (> bufs 1) "s") 1187 (and (> bufs 0) (> files 0) ", ") 1188 (and (> files 0) (format "%d file" files)) 1189 (and (> files 1) "s") 1190 "]") 1191 (lambda (&rest _) 1192 (require 'gptel-context) 1193 (gptel-context--buffer-setup))) 1194 'mouse-face 'highlight 1195 'help-echo "Active gptel context")))) 1196 (toggle-track-media 1197 (lambda (&rest _) 1198 (setq-local gptel-track-media 1199 (not gptel-track-media)) 1200 (if gptel-track-media 1201 (message 1202 (concat 1203 "Sending media from included links. To include media, create " 1204 "a \"standalone\" link in a paragraph by itself, separated from surrounding text.")) 1205 (message "Ignoring image links. Only link text will be sent.")) 1206 (run-at-time 0 nil #'force-mode-line-update))) 1207 (track-media 1208 (and (gptel--model-capable-p 'media) 1209 (if gptel-track-media 1210 (propertize 1211 (buttonize "[Sending media]" toggle-track-media) 1212 'mouse-face 'highlight 1213 'help-echo 1214 "Sending media from standalone links/urls when supported.\nClick to toggle") 1215 (propertize 1216 (buttonize "[Ignoring media]" toggle-track-media) 1217 'mouse-face 'highlight 1218 'help-echo 1219 "Ignoring images from standalone links/urls.\nClick to toggle"))))) 1220 (concat 1221 (propertize 1222 " " 'display 1223 `(space :align-to (- right ,(+ 5 (length model) (length system) 1224 (length track-media) (length context))))) 1225 track-media (and context " ") context " " system " " 1226 (propertize 1227 (buttonize (concat "[" model "]") 1228 (lambda (&rest _) (gptel-menu))) 1229 'mouse-face 'highlight 1230 'help-echo "GPT model in use")))))) 1231 (setq mode-line-process 1232 '(:eval (concat " " 1233 (buttonize (gptel--model-name gptel-model) 1234 (lambda (&rest _) (gptel-menu)))))))) 1235 (remove-hook 'before-save-hook #'gptel--save-state t) 1236 (if gptel-use-header-line 1237 (setq header-line-format gptel--old-header-line 1238 gptel--old-header-line nil) 1239 (setq mode-line-process nil)))) 1240 1241 (defun gptel--update-status (&optional msg face) 1242 "Update status MSG in FACE." 1243 (when gptel-mode 1244 (if gptel-use-header-line 1245 (and (consp header-line-format) 1246 (setf (nth 1 header-line-format) 1247 (propertize msg 'face face))) 1248 (if (member msg '(" Typing..." " Waiting...")) 1249 (setq mode-line-process (propertize msg 'face face)) 1250 (setq mode-line-process 1251 '(:eval (concat " " 1252 (buttonize (gptel--model-name gptel-model) 1253 (lambda (&rest _) (gptel-menu)))))) 1254 (message (propertize msg 'face face)))) 1255 (force-mode-line-update))) 1256 1257 (declare-function gptel-context--wrap "gptel-context") 1258 1259 1260 ;;; Send queries, handle responses 1261 (cl-defun gptel-request 1262 (&optional prompt &key callback 1263 (buffer (current-buffer)) 1264 position context dry-run 1265 (stream nil) (in-place nil) 1266 (system gptel--system-message)) 1267 "Request a response from the `gptel-backend' for PROMPT. 1268 1269 The request is asynchronous, the function immediately returns 1270 with the data that was sent. 1271 1272 Note: This function is not fully self-contained. Consider 1273 let-binding the parameters `gptel-backend', `gptel-model' and 1274 `gptel-use-context' around calls to it as required. 1275 1276 If PROMPT is 1277 - a string, it is used to create a full prompt suitable for 1278 sending to the LLM. 1279 - A list of strings, it is interpreted as a conversation, i.e. a 1280 series of alternating user prompts and LLM responses. 1281 - nil but region is active, the region contents are used. 1282 - nil, the current buffer's contents up to (point) are used. 1283 Previous responses from the LLM are identified as responses. 1284 - A list of plists, it is used as is. 1285 1286 Keyword arguments: 1287 1288 CALLBACK, if supplied, is a function of two arguments, called 1289 with the RESPONSE (a string) and INFO (a plist): 1290 1291 (funcall CALLBACK RESPONSE INFO) 1292 1293 RESPONSE is 1294 1295 - A string if the request was successful 1296 - nil if there was no response or an error. 1297 - The symbol `abort' if the request was aborted, see 1298 `gptel-abort'. 1299 1300 The INFO plist has (at least) the following keys: 1301 :data - The request data included with the query 1302 :position - marker at the point the request was sent, unless 1303 POSITION is specified. 1304 :buffer - The buffer current when the request was sent, 1305 unless BUFFER is specified. 1306 :status - Short string describing the result of the request, including 1307 possible HTTP errors. 1308 1309 Example of a callback that messages the user with the response 1310 and info: 1311 1312 (lambda (response info) 1313 (if (stringp response) 1314 (let ((posn (marker-position (plist-get info :position))) 1315 (buf (buffer-name (plist-get info :buffer)))) 1316 (message \"Response for request from %S at %d: %s\" 1317 buf posn response)) 1318 (message \"gptel-request failed with message: %s\" 1319 (plist-get info :status)))) 1320 1321 Or, for just the response: 1322 1323 (lambda (response _) 1324 ;; Do something with response 1325 (message (rot13-string response))) 1326 1327 If CALLBACK is omitted, the response is inserted at the point the 1328 request was sent. 1329 1330 BUFFER and POSITION are the buffer and position (integer or 1331 marker) at which the response is inserted. If a CALLBACK is 1332 specified, no response is inserted and these arguments are 1333 ignored, but they are still available in the INFO plist passed 1334 to CALLBACK for you to use. 1335 1336 BUFFER defaults to the current buffer, and POSITION to the value 1337 of (point) or (region-end), depending on whether the region is 1338 active. 1339 1340 CONTEXT is any additional data needed for the callback to run. It 1341 is included in the INFO argument to the callback. 1342 1343 SYSTEM is the system message or extended chat directive sent to 1344 the LLM. This can be a string, a list of strings or a function 1345 that returns either; see `gptel-directives' for more 1346 information. If SYSTEM is omitted, the value of 1347 `gptel--system-message' for the current buffer is used. 1348 1349 The following keywords are mainly for internal use: 1350 1351 IN-PLACE is a boolean used by the default callback when inserting 1352 the response to determine if delimiters are needed between the 1353 prompt and the response. 1354 1355 STREAM is a boolean that determines if the response should be 1356 streamed, as in `gptel-stream'. The calling convention for 1357 streaming callbacks is slightly different: 1358 1359 (funcall CALLBACK RESPONSE INFO) 1360 1361 - CALLBACK will be called with each response text chunk (a 1362 string) as it is received. 1363 1364 - When the HTTP request ends successfully, CALLBACK will be 1365 called with a RESPONSE argument of t to indicate success. 1366 1367 - If the HTTP request throws an error, CALLBACK will be called 1368 with a RESPONSE argument of nil. You can find the error via 1369 (plist-get INFO :status). 1370 1371 - If the request is aborted, CALLBACK will be called with a 1372 RESPONSE argument of `abort'. 1373 1374 If DRY-RUN is non-nil, construct and return the full 1375 query data as usual, but do not send the request. 1376 1377 Model parameters can be let-bound around calls to this function." 1378 (declare (indent 1)) 1379 ;; TODO Remove this check in version 1.0 1380 (gptel--sanitize-model) 1381 (let* ((directive (gptel--parse-directive system)) 1382 ;; DIRECTIVE contains both the system message and the template prompts 1383 (gptel--system-message 1384 ;; Add context chunks to system message if required 1385 (if (and gptel-context--alist 1386 (eq gptel-use-context 'system) 1387 (not (gptel--model-capable-p 'nosystem))) 1388 (gptel-context--wrap (car directive)) 1389 (car directive))) 1390 (gptel-stream stream) 1391 (start-marker 1392 (cond 1393 ((null position) 1394 (if (use-region-p) 1395 (set-marker (make-marker) (region-end)) 1396 (gptel--at-word-end (point-marker)))) 1397 ((markerp position) position) 1398 ((integerp position) 1399 (set-marker (make-marker) position buffer)))) 1400 (full-prompt 1401 (nconc 1402 (cdr directive) ;prompt constructed from directive/template 1403 (cond ;prompt from buffer or explicitly supplied 1404 ((null prompt) 1405 (gptel--create-prompt start-marker)) 1406 ((stringp prompt) 1407 ;; FIXME Dear reader, welcome to Jank City: 1408 (with-temp-buffer 1409 (let ((gptel-model (buffer-local-value 'gptel-model buffer)) 1410 (gptel-backend (buffer-local-value 'gptel-backend buffer))) 1411 (insert prompt) 1412 (gptel--create-prompt)))) 1413 ((consp prompt) (gptel--parse-list gptel-backend prompt))))) 1414 (request-data (gptel--request-data gptel-backend full-prompt)) 1415 (info (list :data request-data 1416 :buffer buffer 1417 :position start-marker))) 1418 ;; This context should not be confused with the context aggregation context! 1419 (when context (plist-put info :context context)) 1420 (when in-place (plist-put info :in-place in-place)) 1421 (unless dry-run 1422 (funcall (if gptel-use-curl 1423 #'gptel-curl-get-response #'gptel--url-get-response) 1424 info callback)) 1425 request-data)) 1426 1427 (defvar gptel--request-alist nil "Alist of active gptel requests.") 1428 1429 (defun gptel-abort (buf) 1430 "Stop any active gptel process associated with buffer BUF. 1431 1432 BUF defaults to the current buffer." 1433 (interactive (list (current-buffer))) 1434 (when-let* ((proc-attrs 1435 (cl-find-if (lambda (proc-list) 1436 (eq (plist-get (cdr proc-list) :buffer) buf)) 1437 gptel--request-alist)) 1438 (proc (car proc-attrs)) 1439 (info (cdr proc-attrs))) 1440 ;; Run callback with abort signal 1441 (with-demoted-errors "Callback error: %S" 1442 (funcall (plist-get info :callback) 'abort info)) 1443 (if gptel-use-curl 1444 (progn ;Clean up Curl process 1445 (setf (alist-get proc gptel--request-alist nil 'remove) nil) 1446 (set-process-sentinel proc #'ignore) 1447 (delete-process proc) 1448 (kill-buffer (process-buffer proc))) 1449 (plist-put info :callback #'ignore) 1450 (let (kill-buffer-query-functions) 1451 (kill-buffer proc))) ;Can't stop url-retrieve process 1452 (with-current-buffer buf 1453 (when gptel-mode (gptel--update-status " Abort" 'error))) 1454 (message "Stopped gptel request in buffer %S" (buffer-name buf)))) 1455 1456 ;; TODO: Handle multiple requests(#15). (Only one request from one buffer at a time?) 1457 ;;;###autoload 1458 (defun gptel-send (&optional arg) 1459 "Submit this prompt to the current LLM backend. 1460 1461 By default, the contents of the buffer up to the cursor position 1462 are sent. If the region is active, its contents are sent 1463 instead. 1464 1465 The response from the LLM is inserted below the cursor position 1466 at the time of sending. To change this behavior or model 1467 parameters, use prefix arg ARG activate a transient menu with 1468 more options instead. 1469 1470 This command is asynchronous, you can continue to use Emacs while 1471 waiting for the response." 1472 (interactive "P") 1473 (if (and arg (require 'gptel-transient nil t)) 1474 (call-interactively #'gptel-menu) 1475 (message "Querying %s..." (gptel-backend-name gptel-backend)) 1476 (gptel--sanitize-model) 1477 (gptel-request nil :stream gptel-stream) 1478 (gptel--update-status " Waiting..." 'warning))) 1479 1480 (declare-function json-pretty-print-buffer "json") 1481 (defun gptel--inspect-query (request-data &optional arg) 1482 "Show REQUEST-DATA, the full LLM query to be sent, in a buffer. 1483 1484 This functions as a dry run of `gptel-send'. If ARG is 1485 the symbol json, show the encoded JSON query instead of the Lisp 1486 structure gptel uses." 1487 (with-current-buffer (get-buffer-create "*gptel-query*") 1488 (let ((standard-output (current-buffer)) 1489 (inhibit-read-only t)) 1490 (buffer-disable-undo) 1491 (erase-buffer) 1492 (if (eq arg 'json) 1493 (progn (fundamental-mode) 1494 (insert (gptel--json-encode request-data)) 1495 (json-pretty-print-buffer)) 1496 (lisp-data-mode) 1497 (prin1 request-data) 1498 (pp-buffer)) 1499 (goto-char (point-min)) 1500 (view-mode 1) 1501 (display-buffer (current-buffer) gptel-display-buffer-action)))) 1502 1503 (defun gptel--insert-response (response info) 1504 "Insert the LLM RESPONSE into the gptel buffer. 1505 1506 INFO is a plist containing information relevant to this buffer. 1507 See `gptel--url-get-response' for details." 1508 (let* ((status-str (plist-get info :status)) 1509 (gptel-buffer (plist-get info :buffer)) 1510 (start-marker (plist-get info :position)) 1511 response-beg response-end) 1512 ;; Handle read-only buffers 1513 (when (with-current-buffer gptel-buffer 1514 (or buffer-read-only 1515 (get-char-property start-marker 'read-only))) 1516 (message "Buffer is read only, displaying reply in buffer \"*LLM response*\"") 1517 (display-buffer 1518 (with-current-buffer (get-buffer-create "*LLM response*") 1519 (visual-line-mode 1) 1520 (goto-char (point-max)) 1521 (move-marker start-marker (point) (current-buffer)) 1522 (current-buffer)) 1523 '((display-buffer-reuse-window 1524 display-buffer-pop-up-window) 1525 (reusable-frames . visible)))) 1526 ;; Insert response and status message/error message 1527 (with-current-buffer gptel-buffer 1528 (if response 1529 (progn 1530 (setq response (gptel--transform-response 1531 response gptel-buffer)) 1532 (save-excursion 1533 (put-text-property 1534 0 (length response) 'gptel 'response response) 1535 (with-current-buffer (marker-buffer start-marker) 1536 (goto-char start-marker) 1537 (run-hooks 'gptel-pre-response-hook) 1538 (unless (or (bobp) (plist-get info :in-place)) 1539 (insert "\n\n") 1540 (when gptel-mode 1541 (insert (gptel-response-prefix-string)))) 1542 (setq response-beg (point)) ;Save response start position 1543 (insert response) 1544 (setq response-end (point)) 1545 (pulse-momentary-highlight-region response-beg response-end) 1546 (when gptel-mode (insert "\n\n" (gptel-prompt-prefix-string)))) ;Save response end position 1547 (when gptel-mode (gptel--update-status " Ready" 'success)))) 1548 (gptel--update-status 1549 (format " Response Error: %s" status-str) 'error) 1550 (message "gptel response error: (%s) %s" 1551 status-str (plist-get info :error)))) 1552 ;; Run hook in visible window to set window-point, BUG #269 1553 (if-let ((gptel-window (get-buffer-window gptel-buffer 'visible))) 1554 (with-selected-window gptel-window 1555 (run-hook-with-args 'gptel-post-response-functions response-beg response-end)) 1556 (with-current-buffer gptel-buffer 1557 (run-hook-with-args 'gptel-post-response-functions response-beg response-end))))) 1558 1559 (defun gptel--create-prompt (&optional prompt-end) 1560 "Return a full conversation prompt from the contents of this buffer. 1561 1562 If `gptel--num-messages-to-send' is set, limit to that many 1563 recent exchanges. 1564 1565 If the region is active limit the prompt to the region contents 1566 instead. 1567 1568 If `gptel-context--alist' is non-nil and the additional 1569 context needs to be included with the user prompt, add it. 1570 1571 If PROMPT-END (a marker) is provided, end the prompt contents 1572 there." 1573 (save-excursion 1574 (save-restriction 1575 (let* ((max-entries (and gptel--num-messages-to-send 1576 (* 2 gptel--num-messages-to-send))) 1577 (prompt-end (or prompt-end (point-max))) 1578 (prompts 1579 (cond 1580 ((use-region-p) 1581 ;; Narrow to region 1582 (narrow-to-region (region-beginning) (region-end)) 1583 (goto-char (point-max)) 1584 (gptel--parse-buffer gptel-backend max-entries)) 1585 ((derived-mode-p 'org-mode) 1586 (require 'gptel-org) 1587 (goto-char prompt-end) 1588 (gptel-org--create-prompt prompt-end)) 1589 (t (goto-char prompt-end) 1590 (gptel--parse-buffer gptel-backend max-entries))))) 1591 ;; NOTE: prompts is modified in place here 1592 (when gptel-context--alist 1593 ;; Inject context chunks into the last user prompt if required. 1594 ;; This is also the fallback for when `gptel-use-context' is set to 1595 ;; 'system but the model does not support system messages. 1596 (when (and gptel-use-context 1597 (or (eq gptel-use-context 'user) 1598 (gptel--model-capable-p 'nosystem)) 1599 (> (length prompts) 0)) ;FIXME context should be injected 1600 ;even when there are no prompts 1601 (gptel--wrap-user-prompt gptel-backend prompts)) 1602 ;; Inject media chunks into the first user prompt if required. Media 1603 ;; chunks are always included with the first user message, 1604 ;; irrespective of the preference in `gptel-use-context'. This is 1605 ;; because media cannot be included (in general) with system messages. 1606 (when (and gptel-use-context gptel-track-media 1607 (gptel--model-capable-p 'media)) 1608 (gptel--wrap-user-prompt gptel-backend prompts :media))) 1609 prompts)))) 1610 1611 (cl-defgeneric gptel--parse-buffer (backend max-entries) 1612 "Parse current buffer backwards from point and return a list of prompts. 1613 1614 BACKEND is the LLM backend in use. 1615 1616 MAX-ENTRIES is the number of queries/responses to include for 1617 contexbt.") 1618 1619 (cl-defgeneric gptel--parse-list (backend prompt-list) 1620 "Parse PROMPT-LIST and return a list of prompts suitable for 1621 BACKEND. 1622 1623 PROMPT-LIST is interpreted as a conversation, i.e. an alternating 1624 series of user prompts and LLM responses. The returned structure 1625 is suitable for including in the request payload. 1626 1627 BACKEND is the LLM backend in use.") 1628 1629 (cl-defgeneric gptel--parse-media-links (mode beg end) 1630 "Find media links between BEG and END. 1631 1632 MODE is the major-mode of the buffer. 1633 1634 Returns a plist where each entry is of the form 1635 (:text \"some text\") 1636 or 1637 (:media \"media uri or file path\")." 1638 (ignore mode) ;byte-compiler 1639 (list `(:text ,(buffer-substring-no-properties 1640 beg end)))) 1641 1642 (defvar markdown-regex-link-inline) 1643 (defvar markdown-regex-angle-uri) 1644 (declare-function markdown-link-at-pos "markdown-mode") 1645 (declare-function mailcap-file-name-to-mime-type "mailcap") 1646 1647 (cl-defmethod gptel--parse-media-links ((_mode (eql 'markdown-mode)) beg end) 1648 "Parse text and actionable links between BEG and END. 1649 1650 Return a list of the form 1651 ((:text \"some text\") 1652 (:media \"/path/to/media.png\" :mime \"image/png\") 1653 (:text \"More text\")) 1654 for inclusion into the user prompt for the gptel request." 1655 (require 'mailcap) ;FIXME Avoid this somehow 1656 (let ((parts) (from-pt)) 1657 (save-excursion 1658 (setq from-pt (goto-char beg)) 1659 (while (re-search-forward 1660 (concat "\\(?:" markdown-regex-link-inline "\\|" 1661 markdown-regex-angle-uri "\\)") 1662 end t) 1663 (when-let* ((link-at-pt (markdown-link-at-pos (point))) 1664 ((gptel--link-standalone-p 1665 (car link-at-pt) (cadr link-at-pt))) 1666 (path (nth 3 link-at-pt)) 1667 (path (string-remove-prefix "file://" path)) 1668 (mime (mailcap-file-name-to-mime-type path)) 1669 ((gptel--model-mime-capable-p mime))) 1670 (cond 1671 ((seq-some (lambda (p) (string-prefix-p p path)) 1672 '("https:" "http:" "ftp:")) 1673 ;; Collect text up to this image, and collect this image url 1674 (when (gptel--model-capable-p 'url) ; FIXME This is not a good place 1675 ; to check for url capability! 1676 (push (list :text (buffer-substring-no-properties from-pt (car link-at-pt))) 1677 parts) 1678 (push (list :url path :mime mime) parts) 1679 (setq from-pt (cadr link-at-pt)))) 1680 ((file-readable-p path) 1681 ;; Collect text up to this image, and collect this image 1682 (push (list :text (buffer-substring-no-properties from-pt (car link-at-pt))) 1683 parts) 1684 (push (list :media path :mime mime) parts) 1685 (setq from-pt (cadr link-at-pt))))))) 1686 (unless (= from-pt end) 1687 (push (list :text (buffer-substring-no-properties from-pt end)) parts)) 1688 (nreverse parts))) 1689 1690 (cl-defgeneric gptel--wrap-user-prompt (backend _prompts) 1691 "Wrap the last prompt in PROMPTS with gptel's context. 1692 1693 PROMPTS is a structure as returned by `gptel--parse-buffer'. 1694 Typically this is a list of plists. 1695 1696 BACKEND is the gptel backend in use." 1697 (display-warning 1698 '(gptel context) 1699 (format "Context support not implemented for backend %s, ignoring context" 1700 (gptel-backend-name backend)))) 1701 1702 (cl-defgeneric gptel--request-data (backend prompts) 1703 "Generate a plist of all data for an LLM query. 1704 1705 BACKEND is the LLM backend in use. 1706 1707 PROMPTS is the plist of previous user queries and LLM responses.") 1708 1709 ;; TODO: Use `run-hook-wrapped' with an accumulator instead to handle 1710 ;; buffer-local hooks, etc. 1711 (defun gptel--transform-response (content-str buffer) 1712 "Filter CONTENT-STR through `gptel-response-filter-functions`. 1713 1714 BUFFER is passed along with CONTENT-STR to each function in this 1715 hook." 1716 (let ((filtered-str content-str)) 1717 (dolist (filter-func gptel-response-filter-functions filtered-str) 1718 (condition-case nil 1719 (when (functionp filter-func) 1720 (setq filtered-str 1721 (funcall filter-func filtered-str buffer))) 1722 (error 1723 (display-warning '(gptel filter-functions) 1724 (format "Function %S returned an error" 1725 filter-func))))))) 1726 1727 (defun gptel--convert-org (content buffer) 1728 "Transform CONTENT according to required major-mode. 1729 1730 Currently only `org-mode' is handled. 1731 1732 BUFFER is the LLM interaction buffer." 1733 (if (with-current-buffer buffer (derived-mode-p 'org-mode)) 1734 (gptel--convert-markdown->org content) 1735 content)) 1736 1737 (defun gptel--url-get-response (info &optional callback) 1738 "Fetch response to prompt in INFO from the LLM. 1739 1740 INFO is a plist with the following keys: 1741 - :data (the data being sent) 1742 - :buffer (the gptel buffer) 1743 - :position (marker at which to insert the response). 1744 1745 Call CALLBACK with the response and INFO afterwards. If omitted 1746 the response is inserted into the current buffer after point." 1747 (let* ((inhibit-message t) 1748 (message-log-max nil) 1749 (backend gptel-backend) 1750 (url-request-method "POST") 1751 (url-request-extra-headers 1752 (append '(("Content-Type" . "application/json")) 1753 (when-let ((header (gptel-backend-header gptel-backend))) 1754 (if (functionp header) 1755 (funcall header) header)))) 1756 (url-request-data 1757 (encode-coding-string 1758 (gptel--json-encode (plist-get info :data)) 1759 'utf-8))) 1760 ;; why do these checks not occur inside of `gptel--log'? 1761 (when gptel-log-level ;logging 1762 (when (eq gptel-log-level 'debug) 1763 (gptel--log (gptel--json-encode 1764 (mapcar (lambda (pair) (cons (intern (car pair)) (cdr pair))) 1765 url-request-extra-headers)) 1766 "request headers")) 1767 (gptel--log url-request-data "request body")) 1768 (let ((proc-buf 1769 (url-retrieve (let ((backend-url (gptel-backend-url gptel-backend))) 1770 (if (functionp backend-url) 1771 (funcall backend-url) backend-url)) 1772 (lambda (_) 1773 (pcase-let ((`(,response ,http-msg ,error) 1774 (gptel--url-parse-response backend (current-buffer))) 1775 (buf (current-buffer))) 1776 (plist-put info :status http-msg) 1777 (when error (plist-put info :error error)) 1778 (with-demoted-errors "gptel callback error: %S" 1779 (funcall (or callback #'gptel--insert-response) 1780 response info)) 1781 (setf (alist-get buf gptel--request-alist nil 'remove) nil) 1782 (kill-buffer buf))) 1783 nil t nil))) 1784 (setf (alist-get proc-buf gptel--request-alist) 1785 ;; TODO: Add transformer here. NOTE: We need info to be mutated here. 1786 (nconc info (list :callback callback :backend backend)))))) 1787 1788 (cl-defgeneric gptel--parse-response (backend response proc-info) 1789 "Response extractor for LLM requests. 1790 1791 BACKEND is the LLM backend in use. 1792 1793 RESPONSE is the parsed JSON of the response, as a plist. 1794 1795 PROC-INFO is a plist with process information and other context. 1796 See `gptel-curl--get-response' for its contents.") 1797 1798 (defvar url-http-end-of-headers) 1799 (defvar url-http-response-status) 1800 (defun gptel--url-parse-response (backend response-buffer) 1801 "Parse response from BACKEND in RESPONSE-BUFFER." 1802 (when (buffer-live-p response-buffer) 1803 (with-current-buffer response-buffer 1804 (when gptel-log-level ;logging 1805 (save-excursion 1806 (goto-char url-http-end-of-headers) 1807 (when (eq gptel-log-level 'debug) 1808 (gptel--log (gptel--json-encode (buffer-substring-no-properties (point-min) (point))) 1809 "response headers")) 1810 (gptel--log (buffer-substring-no-properties (point) (point-max)) 1811 "response body"))) 1812 (if-let* ((http-msg (string-trim (buffer-substring (line-beginning-position) 1813 (line-end-position)))) 1814 (response (progn (goto-char url-http-end-of-headers) 1815 (condition-case nil 1816 (gptel--json-read) 1817 (error 'json-read-error))))) 1818 (cond 1819 ;; FIXME Handle the case where HTTP 100 is followed by HTTP (not 200) BUG #194 1820 ((or (memq url-http-response-status '(200 100)) 1821 (string-match-p "\\(?:1\\|2\\)00 OK" http-msg)) 1822 (list (string-trim (gptel--parse-response backend response 1823 `(:buffer ,response-buffer 1824 :backend ,backend))) 1825 http-msg)) 1826 ((plist-get response :error) 1827 (let* ((error-data (plist-get response :error)) 1828 (error-msg (plist-get error-data :message)) 1829 (error-type (plist-get error-data :type)) 1830 (backend-name (gptel-backend-name backend))) 1831 (if (stringp error-data) 1832 (progn 1833 (message "%s error: (%s) %s" backend-name http-msg error-data) 1834 (setq error-msg (string-trim error-data))) 1835 (when (stringp error-msg) 1836 (message "%s error: (%s) %s" backend-name http-msg (string-trim error-msg))) 1837 (when error-type 1838 (setq http-msg (concat "(" http-msg ") " (string-trim error-type))))) 1839 (list nil (concat "(" http-msg ") " (or error-msg ""))))) 1840 ((eq response 'json-read-error) 1841 (list nil (concat "(" http-msg ") Malformed JSON in response.") "json-read-error")) 1842 (t (list nil (concat "(" http-msg ") Could not parse HTTP response.") 1843 "Could not parse HTTP response."))) 1844 (list nil (concat "(" http-msg ") Could not parse HTTP response.") 1845 "Could not parse HTTP response."))))) 1846 1847 (cl-defun gptel--sanitize-model (&key (backend gptel-backend) 1848 (model gptel-model) 1849 (shoosh t)) 1850 "Check if MODEL is available in BACKEND, adjust accordingly. 1851 1852 If SHOOSH is true, don't issue a warning." 1853 (let ((available (gptel-backend-models backend))) 1854 (when (stringp model) 1855 (unless shoosh 1856 (display-warning 1857 'gptel 1858 (format "`gptel-model' expects a symbol, found string \"%s\" 1859 Resetting `gptel-model' to %s" 1860 model model))) 1861 (setq gptel-model (gptel--intern model) 1862 model gptel-model)) 1863 (unless (member model available) 1864 (let ((fallback (car available))) 1865 (unless shoosh 1866 (display-warning 1867 'gptel 1868 (format (concat "Preferred `gptel-model' \"%s\" not" 1869 "supported in \"%s\", using \"%s\" instead") 1870 model (gptel-backend-name backend) fallback))) 1871 (setq-local gptel-model fallback))))) 1872 1873 ;;;###autoload 1874 (defun gptel (name &optional _ initial interactivep) 1875 "Switch to or start a chat session with NAME. 1876 1877 Ask for API-KEY if `gptel-api-key' is unset. 1878 1879 If region is active, use it as the INITIAL prompt. Returns the 1880 buffer created or switched to. 1881 1882 INTERACTIVEP is t when gptel is called interactively." 1883 (interactive 1884 (let* ((backend (default-value 'gptel-backend)) 1885 (backend-name 1886 (format "*%s*" (gptel-backend-name backend)))) 1887 (list (read-buffer 1888 "Create or choose gptel buffer: " 1889 backend-name nil ; DEFAULT and REQUIRE-MATCH 1890 (lambda (b) ; PREDICATE 1891 ;; NOTE: buffer check is required (#450) 1892 (and-let* ((buf (get-buffer (or (car-safe b) b)))) 1893 (buffer-local-value 'gptel-mode buf)))) 1894 (condition-case nil 1895 (gptel--get-api-key 1896 (gptel-backend-key backend)) 1897 ((error user-error) 1898 (setq gptel-api-key 1899 (read-passwd 1900 (format "%s API key: " backend-name))))) 1901 (and (use-region-p) 1902 (buffer-substring (region-beginning) 1903 (region-end))) 1904 t))) 1905 (with-current-buffer (get-buffer-create name) 1906 (cond ;Set major mode 1907 ((eq major-mode gptel-default-mode)) 1908 ((eq gptel-default-mode 'text-mode) 1909 (text-mode) 1910 (visual-line-mode 1)) 1911 (t (funcall gptel-default-mode))) 1912 (gptel--sanitize-model :backend (default-value 'gptel-backend) 1913 :model (default-value 'gptel-model) 1914 :shoosh nil) 1915 (unless gptel-mode (gptel-mode 1)) 1916 (goto-char (point-max)) 1917 (skip-chars-backward "\t\r\n") 1918 (if (bobp) (insert (or initial (gptel-prompt-prefix-string)))) 1919 (when interactivep 1920 (display-buffer (current-buffer) gptel-display-buffer-action) 1921 (message "Send your query with %s!" 1922 (substitute-command-keys "\\[gptel-send]"))) 1923 (current-buffer))) 1924 1925 1926 ;;; Response tweaking commands 1927 1928 (defun gptel--attach-response-history (history &optional buf) 1929 "Attach HISTORY to the next gptel response in buffer BUF. 1930 1931 HISTORY is a list of strings typically containing text replaced 1932 by gptel. BUF is the current buffer if not specified. 1933 1934 This is used to maintain variants of prompts or responses to diff 1935 against if required." 1936 (with-current-buffer (or buf (current-buffer)) 1937 (letrec ((gptel--attach-after 1938 (lambda (b e) 1939 (put-text-property b e 'gptel-history 1940 (append (ensure-list history) 1941 (get-char-property (1- e) 'gptel-history))) 1942 (remove-hook 'gptel-post-response-functions 1943 gptel--attach-after 'local)))) 1944 (add-hook 'gptel-post-response-functions gptel--attach-after 1945 nil 'local)))) 1946 1947 (defun gptel--ediff (&optional arg bounds-func) 1948 "Ediff response at point against previous gptel responses. 1949 1950 If prefix ARG is non-nil, select the previous response to ediff 1951 against interactively. 1952 1953 If specified, use BOUNDS-FUNC to compute the bounds of the 1954 response at point. This can be used to include additional 1955 context for the ediff session." 1956 (interactive "P") 1957 (when (gptel--at-response-history-p) 1958 (pcase-let* ((`(,beg . ,end) (funcall (or bounds-func #'gptel--get-bounds))) 1959 (prev-response 1960 (if arg 1961 (completing-read "Choose response variant to diff against: " 1962 (get-char-property (point) 'gptel-history) 1963 nil t) 1964 (car-safe (get-char-property (point) 'gptel-history)))) 1965 (buffer-mode major-mode) 1966 (bufname (buffer-name)) 1967 (`(,new-buf ,new-beg ,new-end) 1968 (with-current-buffer 1969 (get-buffer-create (concat bufname "-PREVIOUS-*")) 1970 (let ((inhibit-read-only t)) 1971 (erase-buffer) 1972 (delay-mode-hooks (funcall buffer-mode)) 1973 (visual-line-mode) 1974 (insert prev-response) 1975 (goto-char (point-min)) 1976 (list (current-buffer) (point-min) (point-max)))))) 1977 (unless prev-response (user-error "gptel response is additive: no changes to ediff")) 1978 (require 'ediff) 1979 (letrec ((cwc (current-window-configuration)) 1980 (gptel--ediff-restore 1981 (lambda () 1982 (when (window-configuration-p cwc) 1983 (set-window-configuration cwc)) 1984 (kill-buffer (get-buffer (concat bufname "-PREVIOUS-*"))) 1985 (kill-buffer (get-buffer (concat bufname "-CURRENT-*"))) 1986 (remove-hook 'ediff-quit-hook gptel--ediff-restore)))) 1987 (add-hook 'ediff-quit-hook gptel--ediff-restore) 1988 (apply 1989 #'ediff-regions-internal 1990 (get-buffer (ediff-make-cloned-buffer (current-buffer) "-CURRENT-*")) 1991 beg end new-buf new-beg new-end 1992 nil 1993 (list 'ediff-regions-wordwise 'word-wise nil) 1994 ;; (if (transient-arg-value "-w" args) 1995 ;; (list 'ediff-regions-wordwise 'word-wise nil) 1996 ;; (list 'ediff-regions-linewise nil nil)) 1997 ))))) 1998 1999 (defun gptel--mark-response () 2000 "Mark gptel response at point, if any." 2001 (interactive) 2002 (unless (gptel--in-response-p) (user-error "No gptel response at point")) 2003 (pcase-let ((`(,beg . ,end) (gptel--get-bounds))) 2004 (goto-char beg) (push-mark) (goto-char end) (activate-mark))) 2005 2006 (defun gptel--previous-variant (&optional arg) 2007 "Switch to previous gptel-response at this point, if it exists." 2008 (interactive "p") 2009 (pcase-let* ((`(,beg . ,end) (gptel--get-bounds)) 2010 (history (get-char-property (point) 'gptel-history)) 2011 (alt-response (car-safe history)) 2012 (offset)) 2013 (unless (and history alt-response) 2014 (user-error "No variant responses available")) 2015 (if (> arg 0) 2016 (setq history (append (cdr history) 2017 (list (buffer-substring-no-properties beg end)))) 2018 (setq 2019 alt-response (car (last history)) 2020 history (cons (buffer-substring-no-properties beg end) 2021 (nbutlast history)))) 2022 (add-text-properties 2023 0 (length alt-response) 2024 `(gptel response gptel-history ,history) 2025 alt-response) 2026 (setq offset (min (- (point) beg) (1- (length alt-response)))) 2027 (delete-region beg end) 2028 (insert alt-response) 2029 (goto-char (+ beg offset)) 2030 (pulse-momentary-highlight-region beg (+ beg (length alt-response))))) 2031 2032 (defun gptel--next-variant (&optional arg) 2033 "Switch to next gptel-response at this point, if it exists." 2034 (interactive "p") 2035 (gptel--previous-variant (- arg))) 2036 2037 (provide 'gptel) 2038 ;;; gptel.el ends here 2039 2040 ;; Local Variables: 2041 ;; bug-reference-url-format: "https://github.com/karthink/gptel/issues/%s" 2042 ;; End: