Skip to content

Commit 3622942

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

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
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: 11 additions & 18 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,7 +58,11 @@
5758
(when openai--show-log
5859
(apply 'message fmt args)))
5960

60-
(defun openai-key--auth-source ()
61+
;;
62+
;;; Request
63+
64+
;;;###autoload
65+
(defun openai-key-auth-source ()
6166
"Retrieve the OpenAI API key from auth-source."
6267
(let ((auth-info (auth-source-search :max 1
6368
:host "api.openai.com"
@@ -66,26 +71,14 @@
6671
(funcall (plist-get (car auth-info) :secret))
6772
(error "OpenAI API key not found in auth-source"))))
6873

69-
;;
70-
;;; Request
71-
72-
73-
(defcustom openai-key ""
74-
"Variable storing the openai key or a function to retrieve it.
74+
(defvar openai-key ""
75+
"Variable storing the openai key or a function name to retrieve it.
7576
7677
The function should take no arguments and return a string containing the key.
7778
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)
79+
A function, `openai-key-auth-source', that retrieves the key from
80+
auth-source is provided for convenience.
81+
")
8982

9083

9184
(defvar openai-user ""

0 commit comments

Comments
 (0)