refactor: Extract DeepSeek API key reading into reusable functions

This commit is contained in:
lemyx (aider) 2024-12-29 09:56:55 +08:00
parent 510163d321
commit beebcb11ee

View File

@ -3,6 +3,16 @@
;;; Code: ;;; Code:
(defun deepseek-api-key-path ()
"Return the path to the DeepSeek API key file."
(expand-file-name "~/.config/llm/deepseek/api_key.txt"))
(defun read-deepseek-api-key ()
"Read and return the DeepSeek API key from the configured file."
(with-temp-buffer
(insert-file-contents (deepseek-api-key-path))
(string-trim (buffer-string))))
(use-package gptel (use-package gptel
:ensure t :ensure t
:config :config
@ -12,18 +22,14 @@
:host "api.deepseek.com" :host "api.deepseek.com"
:endpoint "/chat/completions" :endpoint "/chat/completions"
:stream t :stream t
:key (with-temp-buffer :key (read-deepseek-api-key)
(insert-file-contents "~/.config/llm/deepseek/api_key.txt")
(string-trim (buffer-string)))
:models '("deepseek-chat" "deepseek-coder")))) :models '("deepseek-chat" "deepseek-coder"))))
;; aider.el ;; aider.el
(add-to-list 'load-path "~/.emacs.d/site-lisp/aider/") (add-to-list 'load-path "~/.emacs.d/site-lisp/aider/")
(require 'aider) (require 'aider)
(setq aider-args '("--model" "deepseek/deepseek-coder")) (setq aider-args '("--model" "deepseek/deepseek-coder"))
(setenv "DEEPSEEK_API_KEY" (with-temp-buffer (setenv "DEEPSEEK_API_KEY" (read-deepseek-api-key))
(insert-file-contents "~/.config/llm/deepseek/api_key.txt")
(string-trim (buffer-string))))
(provide 'init-ai) (provide 'init-ai)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;