From 9fb96b309916eea33f221b09e40b4d714441efa5 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sun, 21 Jul 2024 00:49:29 -0400 Subject: [PATCH] Refactor gptel backend selection and configuration Reorganized backend definitions and selection process for gptel, introducing `gptel-backend-openai` and `gptel-backend-ollama` variables. Simplified backend selection logic and improved the `gptel-select-backend` function to dynamically prompt for models. --- emacs/.emacs.d/config/init-ai.el | 43 +++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/emacs/.emacs.d/config/init-ai.el b/emacs/.emacs.d/config/init-ai.el index 1a4f2f9..246f7d9 100644 --- a/emacs/.emacs.d/config/init-ai.el +++ b/emacs/.emacs.d/config/init-ai.el @@ -5,26 +5,29 @@ (use-package gptel :config - (setq gptel-model "llama3:latest" - gptel-backends `((ollama . ((backend . ,(gptel-make-ollama "Ollama" - :stream t - :models '("llama3:latest" - "mistral:latest"))) - (model . "llama3:latest"))) - (openai . ((backend . ,(gptel-make-openai "OpenAI" - :key #'gptel-api-key-from-auth-source - :stream t - :models '("gpt-4o"))) - (model . "gpt-4o")))) - gptel-default-backend 'openai) - (defun gptel-select-backend (backend) - (interactive - (list (intern (completing-read "gptel backend: " - (mapcar #'car gptel-backends))))) - (let ((backend (alist-get backend gptel-backends))) - (setq gptel-backend (alist-get 'backend backend) - gptel-model (alist-get 'model backend)))) - (gptel-select-backend gptel-default-backend) + (defvar gptel-backend-openai (gptel-make-openai "OpenAI" + :key #'gptel-api-key-from-auth-source + :stream t + :models '("gpt-4o" + "gpt-4o-mini"))) + (defvar gptel-backend-ollama (gptel-make-ollama "Ollama" + :stream t + :models '("llama3:latest" + "mistral:latest"))) + (setq gptel-backend gptel-backend-openai + gptel-model "gpt-4o") + (defun gptel-select-backend () + (interactive) + (let ((backend + (cl-loop + for (name . backend) in gptel--known-backends + nconc (cl-loop for model in (gptel-backend-models backend) + collect (list (concat name ":" model) backend model)) + into models-alist finally return + (cdr (assoc (completing-read "Backend: " models-alist nil t) + models-alist))))) + (setq gptel-backend (car backend) + gptel-model (cadr backend)))) :general ("C-c RET" #'gptel-send "C-c C-" #'gptel-menu)