Skip to content

Commit

Permalink
fix: Incorporate @jcs090218 comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
sw1nn committed Apr 4, 2023
1 parent 452773a commit d835351
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 14 additions & 22 deletions openai.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

;;; Code:

(require 'auth-source)
(require 'cl-lib)
(require 'let-alist)
(require 'pcase)
Expand All @@ -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 ""
Expand Down

0 comments on commit d835351

Please sign in to comment.