Function: gptel

gptel is an interactive and natively compiled function defined in gptel.el.

Signature

(gptel NAME &optional KEY INITIAL INTERACTIVEP)

Documentation

Switch to or start a chat session with NAME.

Ask for KEY if required by the chat backend.

If region is active, use it as the INITIAL prompt. Returns the buffer created or switched to.

INTERACTIVEP is t when gptel is called interactively.

Key Bindings

This command is not in any keymaps.

Source Code

;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel.el
;;;###autoload
(defun gptel (name &optional _key initial interactivep)
  "Switch to or start a chat session with NAME.

Ask for KEY if required by the chat backend.

If region is active, use it as the INITIAL prompt.  Returns the
buffer created or switched to.

INTERACTIVEP is t when gptel is called interactively."
  (interactive
   (let* ((backend (default-value 'gptel-backend))
          (backend-name
           (format "*%s*" (if backend (gptel-backend-name backend) "gptel"))))
     (list (read-buffer
            "Create or choose gptel buffer: "
            backend-name nil            ; DEFAULT and REQUIRE-MATCH
            (lambda (b)                 ; PREDICATE
              ;; NOTE: buffer check is required (#450)
              (and-let* ((buf (get-buffer (or (car-safe b) b))))
                (buffer-local-value 'gptel-mode buf))))
           (condition-case nil
               (gptel--get-api-key
                (gptel-backend-key backend))
             ((error user-error)
              (setq gptel-api-key
                    (read-passwd
                     (format "%s API key: " backend-name)))))
           (and (use-region-p) (buffer-substring (region-beginning)
                                                 (region-end)))
           t)))
  (with-current-buffer (get-buffer-create name)
    (cond                               ;Set major mode
     ((eq major-mode gptel-default-mode))
     ((eq gptel-default-mode 'text-mode)
      (text-mode)
      (visual-line-mode 1))
     (t (funcall gptel-default-mode)))
    (gptel--sanitize-model :backend (default-value 'gptel-backend)
                           :model (default-value 'gptel-model)
                           :shoosh nil)
    (unless gptel-mode (gptel-mode 1))
    (goto-char (point-max))
    (skip-chars-backward "\t\r\n")
    (if (bobp) (insert (or initial (gptel-prompt-prefix-string))))
    (when interactivep
      (display-buffer (current-buffer) gptel-display-buffer-action)
      (message "Send your query with %s!"
               (substitute-command-keys "\\[gptel-send]")))
    (current-buffer)))