Add gptel-api-key-from-auth-source (Fix #13)
This commit is contained in:
parent
87d9090b7a
commit
6f951ed690
2 changed files with 22 additions and 16 deletions
|
@ -46,15 +46,9 @@ PROMPTS is the data to send, TOKEN is a unique identifier."
|
||||||
(data (encode-coding-string
|
(data (encode-coding-string
|
||||||
(json-encode (gptel--request-data prompts))
|
(json-encode (gptel--request-data prompts))
|
||||||
'utf-8))
|
'utf-8))
|
||||||
(api-key
|
|
||||||
(cond
|
|
||||||
((stringp gptel-api-key) gptel-api-key)
|
|
||||||
((functionp gptel-api-key) (funcall gptel-api-key))
|
|
||||||
(t (setq gptel-api-key (read-passwd "OpenAI API key: ")))))
|
|
||||||
(headers
|
(headers
|
||||||
`(("Content-Type" . "application/json")
|
`(("Content-Type" . "application/json")
|
||||||
("Authorization" . ,(concat "Bearer " api-key)))))
|
("Authorization" . ,(concat "Bearer " (gptel--api-key))))))
|
||||||
|
|
||||||
(push (format "-X%s" "POST") args)
|
(push (format "-X%s" "POST") args)
|
||||||
(push (format "-w(%s . %%{size_header})" token) args)
|
(push (format "-w(%s . %%{size_header})" token) args)
|
||||||
;; (push (format "--keepalive-time %s" 240) args)
|
;; (push (format "--keepalive-time %s" 240) args)
|
||||||
|
|
30
gptel.el
30
gptel.el
|
@ -58,7 +58,7 @@
|
||||||
(require 'map)
|
(require 'map)
|
||||||
(require 'text-property-search)
|
(require 'text-property-search)
|
||||||
|
|
||||||
(defcustom gptel-api-key nil
|
(defcustom gptel-api-key #'gptel-api-key-from-auth-source
|
||||||
"An OpenAI API key (string).
|
"An OpenAI API key (string).
|
||||||
|
|
||||||
Can also be a function of no arguments that returns an API
|
Can also be a function of no arguments that returns an API
|
||||||
|
@ -66,7 +66,7 @@ key (more secure)."
|
||||||
:group 'gptel
|
:group 'gptel
|
||||||
:type '(choice
|
:type '(choice
|
||||||
(string :tag "API key")
|
(string :tag "API key")
|
||||||
(function :tag "Function that retuns the API key")))
|
(function :tag "Function that returns the API key")))
|
||||||
|
|
||||||
(defcustom gptel-playback nil
|
(defcustom gptel-playback nil
|
||||||
"Whether responses from ChatGPT be played back in chunks.
|
"Whether responses from ChatGPT be played back in chunks.
|
||||||
|
@ -117,6 +117,23 @@ return the transformed string."
|
||||||
(defvar-local gptel--temperature 1.0)
|
(defvar-local gptel--temperature 1.0)
|
||||||
(defvar-local gptel--num-messages-to-send nil)
|
(defvar-local gptel--num-messages-to-send nil)
|
||||||
|
|
||||||
|
(defun gptel-api-key-from-auth-source (&optional host user)
|
||||||
|
"Lookup api key in the auth source.
|
||||||
|
By default, \"openai.com\" is used as HOST and \"apikey\" as USER."
|
||||||
|
(if-let ((secret (plist-get (car (auth-source-search
|
||||||
|
:host (or host "openai.com")
|
||||||
|
:user (or user "apikey")))
|
||||||
|
:secret)))
|
||||||
|
(if (functionp secret) (funcall secret) secret)
|
||||||
|
(user-error "No `gptel-api-key' found in the auth source")))
|
||||||
|
|
||||||
|
(defun gptel--api-key ()
|
||||||
|
"Get api key from `gptel-api-key'."
|
||||||
|
(pcase gptel-api-key
|
||||||
|
((pred stringp) gptel-api-key)
|
||||||
|
((pred functionp) (funcall gptel-api-key))
|
||||||
|
(_ (error "`gptel-api-key' is not set"))))
|
||||||
|
|
||||||
(defun gptel--update-header-line (msg face)
|
(defun gptel--update-header-line (msg face)
|
||||||
"Update header line with status MSG in FACE."
|
"Update header line with status MSG in FACE."
|
||||||
(and header-line-format
|
(and header-line-format
|
||||||
|
@ -290,15 +307,10 @@ INFO is a plist with the following keys:
|
||||||
- :insert-marker (marker at which to insert the response)."
|
- :insert-marker (marker at which to insert the response)."
|
||||||
(let* ((inhibit-message t)
|
(let* ((inhibit-message t)
|
||||||
(message-log-max nil)
|
(message-log-max nil)
|
||||||
(api-key
|
|
||||||
(cond
|
|
||||||
((stringp gptel-api-key) gptel-api-key)
|
|
||||||
((functionp gptel-api-key) (funcall gptel-api-key))
|
|
||||||
(t (setq gptel-api-key (read-passwd "OpenAI API key: ")))))
|
|
||||||
(url-request-method "POST")
|
(url-request-method "POST")
|
||||||
(url-request-extra-headers
|
(url-request-extra-headers
|
||||||
`(("Content-Type" . "application/json")
|
`(("Content-Type" . "application/json")
|
||||||
("Authorization" . ,(concat "Bearer " api-key))))
|
("Authorization" . ,(concat "Bearer " (gptel--api-key)))))
|
||||||
(url-request-data
|
(url-request-data
|
||||||
(encode-coding-string
|
(encode-coding-string
|
||||||
(json-encode (gptel--request-data (plist-get info :prompt)))
|
(json-encode (gptel--request-data (plist-get info :prompt)))
|
||||||
|
@ -426,7 +438,7 @@ Begin at START-PT."
|
||||||
(progn
|
(progn
|
||||||
(goto-char pt)
|
(goto-char pt)
|
||||||
(insert
|
(insert
|
||||||
(cl-subseq
|
(seq-subseq
|
||||||
content-str idx
|
content-str idx
|
||||||
(min content-length (+ idx 16))))
|
(min content-length (+ idx 16))))
|
||||||
(setq idx (+ idx 16)))
|
(setq idx (+ idx 16)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue