[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/gptel 6f951ed690 037/273: Add gptel-api-key-from-auth-sour
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/gptel 6f951ed690 037/273: Add gptel-api-key-from-auth-source (Fix #13) |
Date: |
Wed, 1 May 2024 10:01:33 -0400 (EDT) |
branch: elpa/gptel
commit 6f951ed690d9dc611b982a3574eb9f80a68499f8
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: karthink <karthikchikmagalur@gmail.com>
Add gptel-api-key-from-auth-source (Fix #13)
---
gptel-curl.el | 8 +-------
gptel.el | 30 +++++++++++++++++++++---------
2 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/gptel-curl.el b/gptel-curl.el
index 43a4b5c578..d304275684 100644
--- a/gptel-curl.el
+++ b/gptel-curl.el
@@ -46,15 +46,9 @@ PROMPTS is the data to send, TOKEN is a unique identifier."
(data (encode-coding-string
(json-encode (gptel--request-data prompts))
'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
`(("Content-Type" . "application/json")
- ("Authorization" . ,(concat "Bearer " api-key)))))
-
+ ("Authorization" . ,(concat "Bearer " (gptel--api-key))))))
(push (format "-X%s" "POST") args)
(push (format "-w(%s . %%{size_header})" token) args)
;; (push (format "--keepalive-time %s" 240) args)
diff --git a/gptel.el b/gptel.el
index e2ffb4faf3..de7203bfed 100644
--- a/gptel.el
+++ b/gptel.el
@@ -58,7 +58,7 @@
(require 'map)
(require 'text-property-search)
-(defcustom gptel-api-key nil
+(defcustom gptel-api-key #'gptel-api-key-from-auth-source
"An OpenAI API key (string).
Can also be a function of no arguments that returns an API
@@ -66,7 +66,7 @@ key (more secure)."
:group 'gptel
:type '(choice
(string :tag "API key")
- (function :tag "Function that retuns the API key")))
+ (function :tag "Function that returns the API key")))
(defcustom gptel-playback nil
"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--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)
"Update header line with status MSG in FACE."
(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)."
(let* ((inhibit-message t)
(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-extra-headers
`(("Content-Type" . "application/json")
- ("Authorization" . ,(concat "Bearer " api-key))))
+ ("Authorization" . ,(concat "Bearer " (gptel--api-key)))))
(url-request-data
(encode-coding-string
(json-encode (gptel--request-data (plist-get info :prompt)))
@@ -426,7 +438,7 @@ Begin at START-PT."
(progn
(goto-char pt)
(insert
- (cl-subseq
+ (seq-subseq
content-str idx
(min content-length (+ idx 16))))
(setq idx (+ idx 16)))
- [nongnu] elpa/gptel deeb606409 003/273: Update license., (continued)
- [nongnu] elpa/gptel deeb606409 003/273: Update license., ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 88995a6436 007/273: gptel-curl: Add curl module and playback feature., ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel cf6999ac12 002/273: Fix byte-compile warnings, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel cd6d90b24d 026/273: gptel-transient: Improve "send in existing/new session" option, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 8fca5bc762 019/273: gptel: Add org-mode support and update README, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 8a6ef565f0 033/273: gptel-transient: Remove unused lexical vars, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 86bf0c9f74 004/273: gptel: Avoid logging url-retrieve messages, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel de70a066d7 017/273: gptel: Pulse inserted text, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 65e6d73372 013/273: gptel: Include more API parameters, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 0d26b34526 029/273: gptel: Add a debug flag, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 6f951ed690 037/273: Add gptel-api-key-from-auth-source (Fix #13),
ELPA Syncer <=
- [nongnu] elpa/gptel 4f3ca23454 040/273: gptel: Update commentary and README, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 9f8a984729 038/273: gptel: Change how api-key is read, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 9b3db255e8 049/273: gptel: Turn API parameters into defcustoms, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel a5fe30312f 005/273: gptel: Fix spacing issues in response, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 3c10147a72 009/273: gptel: Tweak README, minor linting, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 77d1010fbc 010/273: gptel-curl: Add package version, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 03113afd50 008/273: gptel: Rename internal functions, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel b212c24c4a 018/273: gptel: tweak prompt, rename url functions, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 172059060a 012/273: gptel-curl: Autoload gptel-curl-get-response, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel c8f87f5554 030/273: Update README with transient menu details, ELPA Syncer, 2024/05/01