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.
This commit is contained in:
Jeremy Dormitzer 2024-07-21 00:49:29 -04:00
parent 70599ad053
commit 9fb96b3099

View File

@ -5,26 +5,29 @@
(use-package gptel (use-package gptel
:config :config
(setq gptel-model "llama3:latest" (defvar gptel-backend-openai (gptel-make-openai "OpenAI"
gptel-backends `((ollama . ((backend . ,(gptel-make-ollama "Ollama" :key #'gptel-api-key-from-auth-source
:stream t :stream t
:models '("llama3:latest" :models '("gpt-4o"
"mistral:latest"))) "gpt-4o-mini")))
(model . "llama3:latest"))) (defvar gptel-backend-ollama (gptel-make-ollama "Ollama"
(openai . ((backend . ,(gptel-make-openai "OpenAI" :stream t
:key #'gptel-api-key-from-auth-source :models '("llama3:latest"
:stream t "mistral:latest")))
:models '("gpt-4o"))) (setq gptel-backend gptel-backend-openai
(model . "gpt-4o")))) gptel-model "gpt-4o")
gptel-default-backend 'openai) (defun gptel-select-backend ()
(defun gptel-select-backend (backend) (interactive)
(interactive (let ((backend
(list (intern (completing-read "gptel backend: " (cl-loop
(mapcar #'car gptel-backends))))) for (name . backend) in gptel--known-backends
(let ((backend (alist-get backend gptel-backends))) nconc (cl-loop for model in (gptel-backend-models backend)
(setq gptel-backend (alist-get 'backend backend) collect (list (concat name ":" model) backend model))
gptel-model (alist-get 'model backend)))) into models-alist finally return
(gptel-select-backend gptel-default-backend) (cdr (assoc (completing-read "Backend: " models-alist nil t)
models-alist)))))
(setq gptel-backend (car backend)
gptel-model (cadr backend))))
:general :general
("C-c RET" #'gptel-send ("C-c RET" #'gptel-send
"C-c C-<return>" #'gptel-menu) "C-c C-<return>" #'gptel-menu)