### Org Mode Link Syntax for Files Source: https://github.com/karthink/gptel/blob/master/README.org Examples of how to link to local files in Org mode format for GPTel. ```org-mode [[file:/path/to/file.txt]] ``` ```org-mode [[file:/path/to/file.txt][Description]] ``` ```org-mode ``` ```org-mode [[attachment:data.txt]] ``` -------------------------------- ### Install GPTel using Straight Source: https://github.com/karthink/gptel/blob/master/README.org Use this snippet to install GPTel if you are using the Straight package manager. ```emacs-lisp (straight-use-package 'gptel) ``` -------------------------------- ### Markdown Link Syntax for Files Source: https://github.com/karthink/gptel/blob/master/README.org Examples of how to link to local files in Markdown format for GPTel. ```markdown [](file:///path/to/file.txt) ``` ```markdown [](/path/to/file.txt) ``` ```markdown [Description](file:///path/to/file.txt) ``` ```markdown [Description](/path/to/file.txt) ``` ```markdown ``` -------------------------------- ### Configure Anthropic Backend with Environment Key (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Example of setting up the Anthropic backend, specifying the API key to be retrieved from the ANTHROPIC_API_KEY environment variable. ```emacs-lisp (gptel-make-anthropic "My-Claude-backend" :key (gptel-api-key-from-environment "ANTHROPIC_API_KEY")) ``` -------------------------------- ### Configure Gemini Backend with Environment Key (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Example of setting up the Gemini backend, relying on the function to automatically detect and use the GEMINI_API_KEY environment variable. ```emacs-lisp (gptel-make-gemini "My-Gemini-backend" :key (gptel-api-key-from-environment)) ``` -------------------------------- ### Example of gptel-mode property line Source: https://github.com/karthink/gptel/wiki/Auto‐restore-gptel‐mode-in-chat-buffers.org This is an example of the property line that can be added to a file to ensure gptel-mode is enabled upon opening. ```emacs-lisp #+begin_example # -*- eval: (gptel-mode 1) -*- #+end_example ``` -------------------------------- ### Install GPTel in Doom Emacs (Package Declaration) Source: https://github.com/karthink/gptel/blob/master/README.org Add this to your packages.el file in Doom Emacs to declare GPTel as a package. ```emacs-lisp (package! gptel :recipe (:nonrecursive t)) ``` -------------------------------- ### Enable GPTel Tool Use and Add a Tool Source: https://github.com/karthink/gptel/wiki/Tools-collection Enables tool use in GPTel and demonstrates how to add a custom tool definition to `gptel-tools`. This is the basic setup required before using any custom tools. ```emacs-lisp ;; Enable tool use (setq gptel-use-tools t) ;; Add a tool to gptel-tools (add-to-list 'gptel-tools (gptel-make-tool :name "read_url" :function (lambda (url) ;; function implementation ) :description "Fetch and read the contents of a URL" :args (list '(:name "url" :type string :description "The URL to read")) :category "web")) ``` -------------------------------- ### Storing API Keys in authinfo Source: https://github.com/karthink/gptel/blob/master/README.org Example of how to store API keys for OpenAI and Anthropic in the ~/.authinfo file for secure access. The 'machine' and 'login' fields are crucial for GPTel to find the correct credentials. ```authinfo machine api.openai.com login apikey password sk-secret-openai-api-key-goes-here machine api.anthropic.com login apikey password sk-secret-anthropic-api-key-goes-here ``` -------------------------------- ### Install inline-diff Package Source: https://github.com/karthink/gptel/wiki/gptel‐rewrite-addons Install the inline-diff package using package-vc-install or a direct package manager command. This example uses the straight package manager. ```elisp (use-package inline-diff :straight (:repo "https://code.tecosaur.net/tec/inline-diff") :after gptel-rewrite) ;or use :defer ``` -------------------------------- ### Configure Local LLM Backend (llama-cpp) Source: https://github.com/karthink/gptel/blob/master/README.org Sets up a local llama-cpp backend for GPTel, enabling streaming responses. Requires specifying the host and port. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'test gptel-backend (gptel-make-openai "llama-cpp" :stream t :protocol "http" :host "localhost:8000" :models '(test))) ``` -------------------------------- ### Configure xAI Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Sets up the xAI backend for GPTel. Ensure you replace 'your-api-key' with your actual API key. Streaming is enabled by default. ```emacs-lisp (setq gptel-model 'grok-3-latest gptel-backend (gptel-make-xai "xAI" ; Any name you want :key "your-api-key" ; can be a function that returns the key :stream t)) ``` -------------------------------- ### List GPTel Variables Source: https://github.com/karthink/gptel/blob/master/README.org This Emacs Lisp code snippet retrieves a list of all variables starting with 'gptel-' that have documentation. It's useful for discovering configurable options. ```emacs-lisp (let ((all)) (mapatoms (lambda (sym) (when (and (string-match-p "^gptel-[^-]" (symbol-name sym)) (get sym 'variable-documentation)) (push sym all)))) all) ``` -------------------------------- ### Create a Kagi Backend for GPTel Source: https://github.com/karthink/gptel/wiki/Defining-custom-gptel-commands Defines a GPTel backend using the Kagi service. Replace 'YOUR_KAGI_KEY' with your actual Kagi API key. ```elisp ;; Create a kagi backend if you don't have one defined (defvar gptel--kagi (gptel-make-kagi "Kagi" :key "YOUR_KAGI_KEY")) ;or function that returns a key ``` -------------------------------- ### Integrate Kagi Summarization with Embark Source: https://github.com/karthink/gptel/wiki/Defining-custom-gptel-commands Makes the `my/kagi-summarize` function available as an Embark action for URLs. Pressing '=' after activating Embark on a URL will trigger the summarization. ```elisp ;; Make this function available to Embark (keymap-set embark-url-map "=" #'my/kagi-summarize) ``` -------------------------------- ### GPTel Tool: Read File Contents Source: https://github.com/karthink/gptel/wiki/Tools-collection Defines a tool to read and display the contents of a specified file. It supports relative paths and paths starting with '~'. ```emacs-lisp (gptel-make-tool :function (lambda (filepath) (with-temp-buffer (insert-file-contents (expand-file-name filepath)) (buffer-string))) :name "read_file" :description "Read and display the contents of a file" :args (list '(:name "filepath" :type string :description "Path to the file to read. Supports relative paths and ~.")) :category "filesystem") ``` -------------------------------- ### Change Transient Menu Suffix Key Source: https://github.com/karthink/gptel/blob/master/README.org Customize the key used to select a backend or model in GPTel's transient menu. This example changes it from '-m' to 'M'. ```emacs-lisp (transient-suffix-put 'gptel-menu (kbd "-m") :key "M") ``` -------------------------------- ### Define Custom Chat Buffer Renaming Command Source: https://github.com/karthink/gptel/wiki/Defining-custom-gptel-commands Use gptel-request to get LLM suggestions for renaming a chat buffer based on its content. This command prompts the user before renaming. ```elisp (defun gptel-rename-chat () (interactive) (unless gptel-mode (user-error "This command is intended to be used in gptel chat buffers.")) (let ((gptel-model 'gpt-4o-mini)) (gptel-request (list nil ;user "What is the chat content?" ;llm (concat "```" (if (eq major-mode 'org-mode) "org" "markdown") "\n" (buffer-substring-no-properties (point-min) (point-max)) "\n```")) ;user :system (list (format "I will provide a transcript of a chat with an LLM. \nSuggest a short and informative name for a file to store this chat in. \nUse the following guidelines:\n- be very concise, one very short sentence at most\n- no spaces, use underscores if required\n- return ONLY the title, no explanation or summary\n- append the extension .%s" (if (eq major-mode 'org-mode) "org" "md"))) :callback (lambda (resp info) (if (stringp resp) (let ((buf (plist-get info :buffer))) (when (and (buffer-live-p buf) (y-or-n-p (format "Rename buffer %s to %s? " (buffer-name buf) resp))) (with-current-buffer buf (rename-visited-file resp)))) (message "Error(%s): did not receive a response from the LLM." (plist-get info :status))))))) ``` -------------------------------- ### Define a simple preset for code explanation Source: https://github.com/karthink/gptel/blob/master/README.org Use `gptel-make-preset` to create a preset that sets only the system message for a specific task like code explanation. ```emacs-lisp (gptel-make-preset 'explain :system "Explain what this code does to a novice programmer.") ``` -------------------------------- ### Configure GitHub Copilot Business Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Registers the GitHub Copilot backend, specifically for the Business plan, by setting a custom host. You will be prompted to log in to GitHub. ```emacs-lisp (gptel-make-gh-copilot "Copilot" :host "api.business.githubcopilot.com") ``` -------------------------------- ### Define a preset for web search tasks Source: https://github.com/karthink/gptel/blob/master/README.org Create a 'websearch' preset that includes tools for web access and search, suitable for queries requiring external information. ```emacs-lisp (gptel-make-preset 'websearch :description "Haiku with basic web search capability." :backend "Claude" :model 'claude-3-5-haiku-20241022 :tools '("search_web" "read_url" "get_youtube_meta")) ``` -------------------------------- ### Register GPT4All Backend (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Registers a GPT4All backend, specifying the host, port, and available models. Requires the GPT4All application to be running locally. ```emacs-lisp (gptel-make-gpt4all "GPT4All" ;Name of your choosing :protocol "http" :host "localhost:4891" ;Where it's running :models '(mistral-7b-openorca.Q4_0.gguf)) ;Available models ``` -------------------------------- ### Get API Key from Environment Variable (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Defines a function to retrieve an API key from an environment variable. It can use a provided variable name or infer one based on the backend type (e.g., ANTHROPIC_API_KEY). ```emacs-lisp (defun gptel-api-key-from-environment (&optional var) (lambda () (getenv (or var ;provided key (thread-first ;or fall back to _API_KEY (type-of gptel-backend) (symbol-name) (substring 6) (upcase) (concat "_API_KEY")))))) ``` -------------------------------- ### Register Open WebUI with HTTPS and Self-Signed Certificates Source: https://github.com/karthink/gptel/blob/master/README.org Configure Open WebUI as a GPTel backend when using HTTPS with self-signed certificates. Use '--insecure' curl-args for such cases. ```emacs-lisp (gptel-make-openai "OpenWebUI" :host "box.local" :curl-args '("--insecure") ; needed for self-signed certs :key "KEY_FOR_ACCESSING_OPENWEBUI" :endpoint "/api/chat/completions" :stream t :models '("gemma3n:latest")) ``` -------------------------------- ### Extract YouTube Video Metadata and Transcript Source: https://github.com/karthink/gptel/wiki/Tools-collection This Emacs Lisp function uses yt-dlp to fetch a YouTube video's description and transcript. It requires yt-dlp to be installed and accessible in the system's PATH. The function saves temporary files in a video-specific directory and cleans them up afterward. ```emacs-lisp (defun my/gptel-youtube-metadata (callback url) (let* ((video-id (and (string-match (concat "^\\(?:http\\(?:s?://\\)\\)?\\(?:www\\.\\)?\\(?:youtu\\(?:\\(?:\.be\\|be\.com\)/\\)" "\\(?:watch?v=\\)?" "\\([^?&]+\\)") url) (match-string 1 url)))) (dir (file-name-concat temporary-file-directory "yt-dlp" video-id))) (if (file-directory-p dir) (delete-directory dir t)) (make-directory dir t) (let ((default-directory dir) (idx 0) (data (list :description nil :transcript nil))) (make-process :name "yt-dlp" :command `("yt-dlp" "--write-description" "--skip-download" "--output" "video" ,url) :sentinel (lambda (proc status) (cl-incf idx) (let ((default-directory dir)) (when (file-readable-p "video.description") (plist-put data :description (with-temp-buffer (insert-file-contents "video.description") (buffer-string))))) (when (= idx 2) (funcall callback (gptel--json-encode data)) (delete-directory dir t))))) (make-process :name "yt-dlp" :command `("yt-dlp" "--skip-download" "--write-auto-subs" "--sub-langs" "en,-live_chat" "--convert-subs" "srt" "--output" "video" ,url) :sentinel (lambda (proc status) (cl-incf idx) (let ((default-directory dir)) (when (file-readable-p "video.en.srt") (plist-put data :transcript (with-temp-buffer (insert-file-contents "video.en.srt") (buffer-string))))) (when (= idx 2) (funcall callback (gptel--json-encode data)) (delete-directory dir t)))))))) (gptel-make-tool :name "youtube_video_metadata" :function #'my/gptel-youtube-metadata :description "Find the description and video transcript for a youtube video. Return a JSON object containing two fields: "description": The video description added by the uploader "transcript": The video transcript in SRT format" :args '((:name "url" :description "The youtube video URL, for example \"https://www.youtube.com/watch?v=H2qJRnV8ZGA\"" :type string)) :category "web" :async t :include t) ``` -------------------------------- ### Configure Mistral AI Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Sets up GPTel to use the Mistral AI API. Specify your API key and desired model. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'mistral-small gptel-backend (gptel-make-openai "MistralLeChat" ;Any name you want :host "api.mistral.ai" :endpoint "/v1/chat/completions" :protocol "https" :key "your-api-key" ;can be a function that returns the key :models '("mistral-small"))) ``` -------------------------------- ### Configure Moonshot (Kimi) Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Registers Moonshot (Kimi) as a GPTel backend, allowing configuration of the host, API key, and models. Supports streaming for real-time responses. ```emacs-lisp (gptel-make-openai "Moonshot" :host "api.moonshot.cn" ;; or "api.moonshot.ai" for the global site :key "your-api-key" :stream t ;; optionally enable streaming :models '(kimi-latest kimi-k2-0711-preview)) ``` -------------------------------- ### GPTel Tool: Make Directory Source: https://github.com/karthink/gptel/wiki/Tools-collection Defines a tool to create a new directory. It can create the directory if it doesn't exist and reports success or failure. ```emacs-lisp (gptel-make-tool :function (lambda (parent name) (condition-case nil (progn (make-directory (expand-file-name name parent) t) (format "Directory %s created/verified in %s" name parent)) (error (format "Error creating directory %s in %s" name parent)))) :name "make_directory" :description "Create a new directory with the given name in the specified parent directory" :args (list '(:name "parent" :type string :description "The parent directory where the new directory should be created, e.g. /tmp") '(:name "name" :type string :description "The name of the new directory to create, e.g. testdir")) :category "filesystem") ``` -------------------------------- ### Define One-Shot Prompt Command Source: https://github.com/karthink/gptel/wiki/Defining-custom-gptel-commands Defines a command that takes a prompt from the minibuffer and displays the ChatGPT response in a dedicated buffer. Handles empty prompts and displays errors if the request fails. ```elisp (defvar gptel-lookup--history nil) (defun gptel-lookup (prompt) (interactive (list (read-string "Ask ChatGPT: " nil gptel-lookup--history))) (when (string= prompt "") (user-error "A prompt is required.")) (gptel-request prompt :callback (lambda (response info) (if (not response) (message "gptel-lookup failed with message: %s" (plist-get info :status)) (with-current-buffer (get-buffer-create "*gptel-lookup*") (let ((inhibit-read-only t)) (erase-buffer) (insert response)) (special-mode) (display-buffer (current-buffer) `((display-buffer-in-side-window) (side . bottom) (window-height . ,#'fit-window-to-buffer)))))))) ``` -------------------------------- ### Set Default Azure Backend (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Sets the default GPT-EL model and backend to use the configured Azure OpenAI service. Includes optional configuration parameters. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'gpt-3.5-turbo gptel-backend (gptel-make-azure "Azure-1" :protocol "https" :host "YOUR_RESOURCE_NAME.openai.azure.com" :endpoint "/openai/deployments/YOUR_DEPLOYMENT_NAME/chat/completions?api-version=2023-05-15" :stream t :key #'gptel-api-key :models '(gpt-3.5-turbo gpt-4))) ``` -------------------------------- ### Register Open WebUI Backend with GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Register Open WebUI, which provides an OpenAI-compatible API, as a GPTel backend. Configure host, protocol, API key, endpoint, and stream settings. ```emacs-lisp (gptel-make-openai "OpenWebUI" :host "localhost:3000" :protocol "http" :key "KEY_FOR_ACCESSING_OPENWEBUI" :endpoint "/api/chat/completions" :stream t :models '("gemma3n:latest")) ``` -------------------------------- ### Set Open WebUI as Default GPTel Backend Source: https://github.com/karthink/gptel/blob/master/README.org Set Open WebUI as the default GPTel backend. This involves configuring gptel-model and gptel-backend with the Open WebUI details. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model "gemma3n:latest" gptel-backend (gptel-make-openai "OpenWebUI" :host "localhost:3000" :protocol "http" :key "KEY_FOR_ACCESSING_OPENWEBUI" :endpoint "/api/chat/completions" :stream t :models '("gemma3n:latest"))) ``` -------------------------------- ### Configure PrivateGPT Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Registers a local PrivateGPT instance as a GPTel backend. Supports context and source document information. ```emacs-lisp (gptel-make-privategpt "privateGPT" ;Any name you want :protocol "http" :host "localhost:8001" :stream t :context t ;Use context provided by embeddings :sources t ;Return information about source documents :models '(private-gpt)) ``` -------------------------------- ### Configure AWS Bedrock Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Sets AWS Bedrock as the GPTel backend, specifying the model, region, and enabling streaming. Useful for users who want to leverage AWS's AI services. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'claude-sonnet-4-20250514 gptel-backend (gptel-make-bedrock "AWS" ;; optionally enable streaming :stream t ;; optionally specify the aws profile ;; :profile :region "ap-northeast-1" ;; subset of gptel--bedrock-models :models '(claude-sonnet-4-20250514) ;; Model region for cross-region inference profiles. Required for models such ;; as Claude without on-demand throughput support. One of 'apac, 'eu or 'us. ;; https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-use.html :model-region 'apac)) ``` -------------------------------- ### Enable Global Media Tracking Source: https://github.com/karthink/gptel/blob/master/README.org Set this variable globally to enable media tracking for all supported models. ```emacs-lisp gptel-track-media ``` -------------------------------- ### GPTel Tool: Create File Source: https://github.com/karthink/gptel/wiki/Tools-collection Defines a tool to create a new file with specified content in a given directory and filename. It confirms the file creation. ```emacs-lisp (gptel-make-tool :function (lambda (path filename content) (let ((full-path (expand-file-name filename path))) (with-temp-buffer (insert content) (write-file full-path)) (format "Created file %s in %s" filename path))) :name "create_file" :description "Create a new file with the specified content" :args (list '(:name "path" :type string :description "The directory where to create the file") '(:name "filename" :type string :description "The name of the file to create") '(:name "content" :type string :description "The content to write to the file")) :category "filesystem") ``` -------------------------------- ### Configure AI/ML API Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Registers the AI/ML API as a GPTel backend, supporting multiple models. Replace 'your-api-key' with your actual API key. Streaming is enabled. ```emacs-lisp ;; AI/ML API offers an OpenAI compatible API (gptel-make-openai "AI/ML API" ;Any name you want :host "api.aimlapi.com" :endpoint "/v1/chat/completions" :stream t :key "your-api-key" ;can be a function that returns the key :models '(deepseek-chat gemini-pro gpt-4o)) ``` -------------------------------- ### Set Kagi FastGPT as Default Backend Source: https://github.com/karthink/gptel/blob/master/README.org Configures Kagi's FastGPT model as the default backend for GPTel. Ensure your Kagi API key is correctly set. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'fastgpt gptel-backend (gptel-make-kagi "Kagi" :key "YOUR_KAGI_API_KEY")) ``` -------------------------------- ### Configure OpenAI OAuth Backend with Proxy (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Configures an OpenAI OAuth backend, specifying a custom host and protocol for proxying requests. ```emacs-lisp (gptel-make-openai-oauth "OpenAI-sub" :host "my.openai-proxy.tld" :protocol "http") ``` -------------------------------- ### Configure DeepSeek Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Registers the DeepSeek API for GPTel, enabling streaming responses. Requires an API key. ```emacs-lisp (gptel-make-deepseek "DeepSeek" ;Any name you want :stream t ;for streaming responses :key "your-api-key") ;can be a function that returns the key ``` -------------------------------- ### Configure AWS Bedrock Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Registers the AWS Bedrock backend for GPTel. Streaming is optional. Requires gptel-use-curl to be set to t. Ensure correct region and model are specified. ```emacs-lisp (gptel-make-bedrock "AWS" ;; optionally enable streaming :stream t :region "ap-northeast-1" ;; subset of gptel--bedrock-models :models '(claude-sonnet-4-20250514) ;; Model region for cross-region inference profiles. Required for models such ;; as Claude without on-demand throughput support. One of 'apac, 'eu or 'us. ;; https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-use.html :model-region 'apac) ``` -------------------------------- ### Register Kagi Backend Source: https://github.com/karthink/gptel/blob/master/README.org Registers the Kagi backend for GPTel, requiring a Kagi API key. This backend supports FastGPT and Universal Summarizer models but not multi-turn conversations or streaming. ```emacs-lisp (gptel-make-kagi "Kagi" :key "YOUR_KAGI_API_KEY") ``` -------------------------------- ### Register Ollama Backend with GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Use this to register a local Ollama instance as a GPTel backend. Specify the host, stream setting, and available models. ```emacs-lisp (gptel-make-ollama "Ollama" :host "localhost:11434" :stream t :models '(mistral:latest)) ``` -------------------------------- ### Register Groq Backend Source: https://github.com/karthink/gptel/blob/master/README.org Registers a Groq backend using the OpenAI compatible API. Configures host, endpoint, streaming, API key, and available models. ```emacs-lisp ;; Groq offers an OpenAI compatible API (gptel-make-openai "Groq" ;Any name you want :host "api.groq.com" :endpoint "/openai/v1/chat/completions" :stream t :key "your-api-key" ;can be a function that returns the key :models '(llama-3.3-70b-versatile llama-3.1-8b-instant llama3-70b-8192 llama3-8b-8192 mixtral-8x7b-32768 gemma-7b-it)) ``` -------------------------------- ### Register Together.ai Backend Source: https://github.com/karthink/gptel/blob/master/README.org Registers the Together.ai backend, which offers an OpenAI-compatible API. Specify the host, API key, and desired models. Streaming is enabled. ```emacs-lisp ;; Together.ai offers an OpenAI compatible API (gptel-make-openai "TogetherAI" :host "api.together.xyz" :key "your-api-key" :stream t :models '(;; has many more, check together.ai mistralai/Mixtral-8x7B-Instruct-v0.1 codellama/CodeLlama-13b-Instruct-hf codellama/CodeLlama-34b-Instruct-hf)) ``` -------------------------------- ### run_command Source: https://github.com/karthink/gptel/wiki/Tools-collection Executes a shell command and returns its output as a string. This tool requires user confirmation before execution due to its ability to run arbitrary code. ```APIDOC ## run_command ### Description Executes a shell command and returns the output as a string. IMPORTANT: This tool allows execution of arbitrary code; user confirmation will be required before any command is run. ### Arguments * **command** (string) - The complete shell command to execute. * **working_dir** (string, optional) - The directory in which to run the command. Defaults to the current directory if not specified. ### Category command ``` -------------------------------- ### Set DeepSeek as Default GPTel Backend Source: https://github.com/karthink/gptel/blob/master/README.org Configures DeepSeek-R1 as the default model and specifies 'Sambanova' as the backend for GPTel. This is an optional configuration to set a preferred backend. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'DeepSeek-R1) (setq gptel-backend (gptel-get-backend "Sambanova")) ``` -------------------------------- ### Register Llama.cpp Backend with GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Register Llama.cpp, which provides an OpenAI-compatible API, as a GPTel backend. Configure stream, protocol, host, and model names. ```emacs-lisp ; Llama.cpp offers an OpenAI compatible API (gptel-make-openai "llama-cpp" ;Any name :stream t ;Stream responses :protocol "http" :host "localhost:8000" ;Llama.cpp server location :models '(test)) ;Any names, doesn't matter for Llama ``` -------------------------------- ### Register OpenAI OAuth Backend (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Registers an OpenAI backend using OAuth. This method prompts the user to log in to OpenAI. ```emacs-lisp (gptel-make-openai-oauth "OpenAI-sub") ;Any name of your choosing ``` -------------------------------- ### Set Default OpenAI Backend (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Sets the default GPT-EL model and backend to use OpenAI OAuth. This replaces the need to select it from a menu. ```emacs-lisp (setq gptel-model 'gpt-5.4-mini gptel-backend (gptel-make-openai-oauth "OpenAI-sub")) ``` -------------------------------- ### Register Anyscale Backend Source: https://github.com/karthink/gptel/blob/master/README.org Registers the Anyscale backend, which provides an OpenAI-compatible API. Specify the host, API key, and available models. ```emacs-lisp ;; Anyscale offers an OpenAI compatible API (gptel-make-openai "Anyscale" :host "api.endpoints.anyscale.com" :key "your-api-key" :models '(;; has many more, check anyscale mistralai/Mixtral-8x7B-Instruct-v0.1)) ``` -------------------------------- ### Define a comprehensive preset for coding tasks Source: https://github.com/karthink/gptel/blob/master/README.org Create a preset named 'gpt4coding' to configure the backend, model, system prompt, and tools for coding-related tasks. ```emacs-lisp (gptel-make-preset 'gpt4coding ;preset name, a symbol :description "A preset optimized for coding tasks" ;for your reference :backend "Claude" ;gptel backend or backend name :model 'claude-3-7-sonnet-20250219.1 :system "You are an expert coding assistant. Your role is to provide high-quality code solutions, refactorings, and explanations." :tools '("read_buffer" "modify_buffer")) ;gptel tools or tool names ``` -------------------------------- ### Set Anyscale Model as Default Backend Source: https://github.com/karthink/gptel/blob/master/README.org Configures a specific Anyscale model as the default backend for GPTel. Ensure the host, API key, and model list are correctly set. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'mistralai/Mixtral-8x7B-Instruct-v0.1 gptel-backend (gptel-make-openai "Anyscale" :host "api.endpoints.anyscale.com" :key "your-api-key" :models '(;; has many more, check anyscale mistralai/Mixtral-8x7B-Instruct-v0.1))) ``` -------------------------------- ### Register GitHub Copilot Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Registers the GitHub Copilot backend with a default name. The host can be changed based on your Copilot plan. ```emacs-lisp (gptel-make-gh-copilot "Copilot") ``` -------------------------------- ### Configure Org Mode Prefixes for Branching Context Source: https://github.com/karthink/gptel/blob/master/README.org Customize prompt and response prefixes for Org mode when using branching context to ensure proper conversation flow. Requires Org 9.7+. ```emacs-lisp (setf (alist-get 'org-mode gptel-prompt-prefix-alist) "@user\n") (setf (alist-get 'org-mode gptel-response-prefix-alist) "@assistant\n") ``` -------------------------------- ### Register xAI Backend Source: https://github.com/karthink/gptel/blob/master/README.org Registers the xAI backend with GPTel. This snippet specifies the backend name, stream setting, and requires an API key, which can be a function returning the key. ```emacs-lisp (gptel-make-xai "xAI" ; Any name you want :stream t :key "your-api-key") ; can be a function that returns the key ``` -------------------------------- ### Configure GPTel in Spacemacs Source: https://github.com/karthink/gptel/blob/master/README.org Add llm-client to your dotspacemacs-configuration-layers and configure GPTel within Spacemacs. ```emacs-lisp (llm-client :variables llm-client-enable-gptel t) ``` -------------------------------- ### Register Gemini Backend with GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Register the Gemini API as a GPTel backend. The API key can be a string or a function returning the key. Streaming is enabled by default. ```emacs-lisp ; :key can be a function that returns the API key. (gptel-make-gemini "Gemini" :key "YOUR_GEMINI_API_KEY" :stream t) ``` -------------------------------- ### GPTel Tool: List Directory Contents Source: https://github.com/karthink/gptel/wiki/Tools-collection Defines a tool to list the contents of a given directory. The output is a string with each file/directory on a new line. ```emacs-lisp (gptel-make-tool :function (lambda (directory) (mapconcat #'identity (directory-files directory) "\n")) :name "list_directory" :description "List the contents of a given directory" :args (list '(:name "directory" :type string :description "The path to the directory to list")) :category "filesystem") ``` -------------------------------- ### Configure GPTel in Doom Emacs (Configuration) Source: https://github.com/karthink/gptel/blob/master/README.org Configure GPTel in your config.el file in Doom Emacs, including setting the API key. ```emacs-lisp (use-package! gptel :config (setq! gptel-api-key "your key")) ``` -------------------------------- ### Set PrivateGPT as Default GPTel Backend Source: https://github.com/karthink/gptel/blob/master/README.org Configures the local PrivateGPT instance as the default GPTel backend, including specific model and options. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'private-gpt gptel-backend (gptel-make-privategpt "privateGPT" ;Any name you want :protocol "http" :host "localhost:8001" :stream t :context t ;Use context provided by embeddings :sources t ;Return information about source documents :models '(private-gpt))) ``` -------------------------------- ### Set Together.ai Model as Default Backend Source: https://github.com/karthink/gptel/blob/master/README.org Sets a specific Together.ai model as the default backend for GPTel. Ensure the host, API key, and model list are correctly configured. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'mistralai/Mixtral-8x7B-Instruct-v0.1 gptel-backend (gptel-make-openai "TogetherAI" :host "api.together.xyz" :key "your-api-key" :stream t :models '(;; has many more, check together.ai mistralai/Mixtral-8x7B-Instruct-v0.1 codellama/CodeLlama-13b-Instruct-hf codellama/CodeLlama-34b-Instruct-hf))) ``` -------------------------------- ### Register Azure Backend (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Registers an Azure OpenAI backend, specifying host, endpoint, API version, and models. Streaming responses are enabled by default. ```emacs-lisp (gptel-make-azure "Azure-1" ;Name, whatever you'd like :protocol "https" ;Optional -- https is the default :host "YOUR_RESOURCE_NAME.openai.azure.com" :endpoint "/openai/deployments/YOUR_DEPLOYMENT_NAME/chat/completions?api-version=2023-05-15" ;or equivalent :stream t ;Enable streaming responses :key #'gptel-api-key :models '(gpt-3.5-turbo gpt-4)) ``` -------------------------------- ### run_async_command Source: https://github.com/karthink/gptel/wiki/Tools-collection Runs a shell command asynchronously and passes the output to a provided callback function. ```APIDOC ## run_async_command ### Description Run an async command. ### Arguments * **command** (string) - Command to run. * **callback** (function) - Function to call with the command output. ### Category command ``` -------------------------------- ### Run Shell Command Source: https://github.com/karthink/gptel/wiki/Tools-collection Execute a shell command and capture its output. This tool can optionally specify a working directory for the command. User confirmation is required due to the arbitrary code execution capability. ```emacs-lisp (gptel-make-tool :function (lambda (command &optional working_dir) (with-temp-message (format "Executing command: `%s`" command) (let ((default-directory (if (and working_dir (not (string= working_dir ""))) (expand-file-name working_dir) default-directory))) (shell-command-to-string command)))) :name "run_command" :description "Executes a shell command and returns the output as a string. IMPORTANT: This tool allows execution of arbitrary code; user confirmation will be required before any command is run." :args (list '(:name "command" :type string :description "The complete shell command to execute.") '(:name "working_dir" :type string :description "Optional: The directory in which to run the command. Defaults to the current directory if not specified."))) :category "command" :confirm t :include t) ``` -------------------------------- ### Set Novita AI as Default GPTel Backend Source: https://github.com/karthink/gptel/blob/master/README.org Sets 'gryphe/mythomax-l2-13b' as the default model and configures the Novita AI backend as the default for GPTel. This is an optional configuration to set a preferred backend with specific model and stream settings. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'gryphe/mythomax-l2-13b gptel-backend (gptel-make-openai "NovitaAI" :host "api.novita.ai" :endpoint "/v3/openai" :key "your-api-key" :stream t :models '(;; has many more, check https://novita.ai/llm-api mistralai/Mixtral-8x7B-Instruct-v0.1 meta-llama/llama-3-70b-instruct meta-llama/llama-3.1-70b-instruct))) ``` -------------------------------- ### Send Request with Prefix Argument Source: https://github.com/karthink/gptel/blob/master/README.org Call gptel-send with a prefix argument to access the transient menu for setting options. ```emacs-lisp gptel-send ``` -------------------------------- ### Brave Search Tool (Emacs Lisp) Source: https://github.com/karthink/gptel/wiki/Tools-collection Performs a web search using the Brave Search API. Requires a configured `brave-search-api-key` and the 'query' argument. ```emacs-lisp (defvar brave-search-api-key "" "API key for accessing the Brave Search API.") (defun brave-search-query (query) "Perform a web search using the Brave Search API with the given QUERY." (let ((url-request-method "GET") (url-request-extra-headers `(("X-Subscription-Token" . ,brave-search-api-key))) (url (format "https://api.search.brave.com/res/v1/web/search?q=%s" (url-encode-url query)))) (with-current-buffer (url-retrieve-synchronously url) (goto-char (point-min)) (when (re-search-forward "$" nil 'move) (let ((json-object-type 'hash-table)) ; Use hash-table for JSON parsing (json-parse-string (buffer-substring-no-properties (point) (point-max)))))))) (gptel-make-tool :function #'brave-search-query :name "brave_search" :description "Perform a web search using the Brave Search API" :args (list '(:name "query" :type string :description "The search query string")) :category "web") ``` -------------------------------- ### Configure Sambanova (Deepseek) Backend for GPTel Source: https://github.com/karthink/gptel/blob/master/README.org Registers Sambanova's API endpoint for accessing Deepseek-R1, which offers faster token speeds. Supports streaming responses. ```emacs-lisp (gptel-make-openai "Sambanova" ;Any name you want :host "api.sambanova.ai" :endpoint "/v1/chat/completions" :stream t ;for streaming responses :key "your-api-key" ;can be a function that returns the key :models '(DeepSeek-R1)) ``` -------------------------------- ### Define Rewrite Region Command Source: https://github.com/karthink/gptel/wiki/Defining-custom-gptel-commands Creates a command to rewrite the current region, sentence, or line using ChatGPT. Optionally prompts the user for specific rewriting instructions. Replaces the original text with the response and saves the original to the kill-ring. ```elisp (defun gptel-rewrite-and-replace (bounds &optional directive) (interactive (list (cond ((use-region-p) (cons (region-beginning) (region-end))) ((derived-mode-p 'text-mode) (list (bounds-of-thing-at-point 'sentence))) (t (cons (line-beginning-position) (line-end-position)))) (and current-prefix-arg (read-string "ChatGPT Directive: " "You are a prose editor. Rewrite my prompt more professionally.")))) (gptel-request (buffer-substring-no-properties (car bounds) (cdr bounds)) ;the prompt :system (or directive "You are a prose editor. Rewrite my prompt more professionally.") :buffer (current-buffer) :context (cons (set-marker (make-marker) (car bounds)) (set-marker (make-marker) (cdr bounds))) :callback (lambda (response info) (if (not response) (message "ChatGPT response failed with: %s" (plist-get info :status)) (let* ((bounds (plist-get info :context)) (beg (car bounds)) (end (cdr bounds)) (buf (plist-get info :buffer))) (with-current-buffer buf (save-excursion (goto-char beg) (kill-region beg end) (insert response) (set-marker beg nil) (set-marker end nil) (message "Rewrote line. Original line saved to kill-ring.")))))))) ``` -------------------------------- ### Configure Moonshot with Built-in Search Tool Request Parameters Source: https://github.com/karthink/gptel/blob/master/README.org Configures the Moonshot backend to use its built-in search tool by adding a special request parameter. This enables automatic search queries for relevant prompts. ```emacs-lisp (gptel-make-openai "Moonshot" :host "api.moonshot.cn" ;; or "api.moonshot.ai" for the global site :key "your-api-key" :stream t ;; optionally enable streaming :models '(kimi-latest kimi-k2-0711-preview) :request-params '(:tools [(:type "builtin_function" :function (:name "$web_search"))])) ``` -------------------------------- ### Set DeepSeek as Default GPTel Backend Source: https://github.com/karthink/gptel/blob/master/README.org Sets DeepSeek as the default GPTel backend, specifying the model and enabling streaming. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-model 'deepseek-reasoner gptel-backend (gptel-make-deepseek "DeepSeek" :stream t :key "your-api-key")) ``` -------------------------------- ### Set Default GPT4All Backend (Emacs Lisp) Source: https://github.com/karthink/gptel/blob/master/README.org Sets the default GPT-EL model and backend to use GPT4All. It also suggests increasing the max-tokens setting due to GPT4All's default short responses. ```emacs-lisp ;; OPTIONAL configuration (setq gptel-max-tokens 500 gptel-model 'mistral-7b-openorca.Q4_0.gguf gptel-backend (gptel-make-gpt4all "GPT4All" :protocol "http" :host "localhost:4891" :models '(mistral-7b-openorca.Q4_0.gguf))) ```