Skip to content

Commit d835351

Browse files
committed
fix: Incorporate @jcs090218 comments.
1 parent 452773a commit d835351

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ You will need to set up your API key before you can use this library.
4040
(setq openai-key "[YOUR API KEY]")
4141
```
4242

43+
Alternatively you can configure a function to retrieve the key from some
44+
external source. A function, `openai-key-auth-source` is provided to retrieve
45+
the key from an auth-source entry under the `:host` key `api.openai.com`
46+
47+
```elisp
48+
(setq openai-key 'openai-key-auth-source)
49+
```
50+
4351
For requests that need your user identifier,
4452

4553
```elisp

openai.el

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
;;; Code:
3333

34+
(require 'auth-source)
3435
(require 'cl-lib)
3536
(require 'let-alist)
3637
(require 'pcase)
@@ -57,35 +58,26 @@
5758
(when openai--show-log
5859
(apply 'message fmt args)))
5960

60-
(defun openai-key--auth-source ()
61-
"Retrieve the OpenAI API key from auth-source."
62-
(let ((auth-info (auth-source-search :max 1
63-
:host "api.openai.com"
64-
:require '(:user :secret))))
65-
(if auth-info
66-
(funcall (plist-get (car auth-info) :secret))
67-
(error "OpenAI API key not found in auth-source"))))
68-
6961
;;
7062
;;; Request
7163

64+
;;;###autoload
65+
(defun openai-key-auth-source ()
66+
"Retrieve the OpenAI API key from auth-source."
67+
(if-let ((auth-info (auth-source-search :max 1
68+
:host "api.openai.com"
69+
:require '(:user :secret))))
70+
(funcall (plist-get (car auth-info) :secret))
71+
(error "OpenAI API key not found in auth-source")))
7272

73-
(defcustom openai-key ""
74-
"Variable storing the openai key or a function to retrieve it.
73+
(defvar openai-key ""
74+
"Variable storing the openai key or a function name to retrieve it.
7575
7676
The function should take no arguments and return a string containing the key.
7777
78-
A function, `openai-key--auth-source', that retrieves the key from auth-source is provided for convenience.
79-
"
80-
:type '(choice string function)
81-
:set (lambda (option value)
82-
(cond ((stringp value)
83-
(set-default option value))
84-
((functionp value)
85-
(set-default option (symbol-name value)))
86-
(t
87-
(error "Invalid value for %s" option))))
88-
:group 'openai)
78+
A function, `openai-key-auth-source', that retrieves the key from
79+
auth-source is provided for convenience.
80+
")
8981

9082

9183
(defvar openai-user ""

0 commit comments

Comments
 (0)