From d835351215314b37b58736bb3dd3e1e822928da5 Mon Sep 17 00:00:00 2001 From: Neale Swinnerton Date: Tue, 4 Apr 2023 10:17:24 +0100 Subject: [PATCH] fix: Incorporate @jcs090218 comments. --- README.md | 8 ++++++++ openai.el | 36 ++++++++++++++---------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 17d8c55..67bafb9 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,14 @@ You will need to set up your API key before you can use this library. (setq openai-key "[YOUR API KEY]") ``` +Alternatively you can configure a function to retrieve the key from some +external source. A function, `openai-key-auth-source` is provided to retrieve +the key from an auth-source entry under the `:host` key `api.openai.com` + +```elisp +(setq openai-key 'openai-key-auth-source) +``` + For requests that need your user identifier, ```elisp diff --git a/openai.el b/openai.el index 6be35e5..861cb4b 100644 --- a/openai.el +++ b/openai.el @@ -31,6 +31,7 @@ ;;; Code: +(require 'auth-source) (require 'cl-lib) (require 'let-alist) (require 'pcase) @@ -57,35 +58,26 @@ (when openai--show-log (apply 'message fmt args))) -(defun openai-key--auth-source () - "Retrieve the OpenAI API key from auth-source." - (let ((auth-info (auth-source-search :max 1 - :host "api.openai.com" - :require '(:user :secret)))) - (if auth-info - (funcall (plist-get (car auth-info) :secret)) - (error "OpenAI API key not found in auth-source")))) - ;; ;;; Request +;;;###autoload +(defun openai-key-auth-source () + "Retrieve the OpenAI API key from auth-source." + (if-let ((auth-info (auth-source-search :max 1 + :host "api.openai.com" + :require '(:user :secret)))) + (funcall (plist-get (car auth-info) :secret)) + (error "OpenAI API key not found in auth-source"))) -(defcustom openai-key "" - "Variable storing the openai key or a function to retrieve it. +(defvar openai-key "" + "Variable storing the openai key or a function name to retrieve it. The function should take no arguments and return a string containing the key. -A function, `openai-key--auth-source', that retrieves the key from auth-source is provided for convenience. -" - :type '(choice string function) - :set (lambda (option value) - (cond ((stringp value) - (set-default option value)) - ((functionp value) - (set-default option (symbol-name value))) - (t - (error "Invalid value for %s" option)))) - :group 'openai) +A function, `openai-key-auth-source', that retrieves the key from +auth-source is provided for convenience. +") (defvar openai-user ""