gptel: curl proxy support (#69)
* gptel.el (gptel-proxy): Support a proxy when interacting with openai endpoint. In many organizations the openai api can only be accessed via proxy. This is easily supported by curl. gptel-curl.el (gptel-curl--get-args): tidy up `gptel-curl--get-args'. --------- Co-authored-by: PalaceChan <XXX>
This commit is contained in:
parent
e6a1468bd2
commit
20af9a8b99
2 changed files with 23 additions and 12 deletions
|
@ -29,6 +29,7 @@
|
||||||
(require 'gptel)
|
(require 'gptel)
|
||||||
|
|
||||||
(eval-when-compile
|
(eval-when-compile
|
||||||
|
(require 'cl-lib)
|
||||||
(require 'subr-x))
|
(require 'subr-x))
|
||||||
(require 'map)
|
(require 'map)
|
||||||
(require 'json)
|
(require 'json)
|
||||||
|
@ -40,24 +41,27 @@
|
||||||
"Produce list of arguments for calling Curl.
|
"Produce list of arguments for calling Curl.
|
||||||
|
|
||||||
PROMPTS is the data to send, TOKEN is a unique identifier."
|
PROMPTS is the data to send, TOKEN is a unique identifier."
|
||||||
(let* ((args
|
(let* ((url (format "https://%s/v1/chat/completions" gptel-host))
|
||||||
(list "--location" "--silent" "--compressed" "--disable"))
|
|
||||||
(url (format "https://%s/v1/chat/completions" gptel-host))
|
|
||||||
(data (encode-coding-string
|
(data (encode-coding-string
|
||||||
(json-encode (gptel--request-data prompts))
|
(json-encode (gptel--request-data prompts))
|
||||||
'utf-8))
|
'utf-8))
|
||||||
(headers
|
(headers
|
||||||
`(("Content-Type" . "application/json")
|
`(("Content-Type" . "application/json")
|
||||||
("Authorization" . ,(concat "Bearer " (gptel--api-key))))))
|
("Authorization" . ,(concat "Bearer " (gptel--api-key))))))
|
||||||
(push (format "-X%s" "POST") args)
|
(append
|
||||||
(push (format "-w(%s . %%{size_header})" token) args)
|
(list "--location" "--silent" "--compressed" "--disable"
|
||||||
;; (push (format "--keepalive-time %s" 240) args)
|
(format "-X%s" "POST")
|
||||||
(push (format "-m%s" 60) args)
|
(format "-w(%s . %%{size_header})" token)
|
||||||
(push "-D-" args)
|
(format "-m%s" 60)
|
||||||
(pcase-dolist (`(,key . ,val) headers)
|
"-D-"
|
||||||
(push (format "-H%s: %s" key val) args))
|
(format "-d%s" data))
|
||||||
(push (format "-d%s" data) args)
|
(when (not (string-empty-p gptel-proxy))
|
||||||
(nreverse (cons url args))))
|
(list "--proxy" gptel-proxy
|
||||||
|
"--proxy-negotiate"
|
||||||
|
"--proxy-user" ":"))
|
||||||
|
(cl-loop for (key . val) in headers
|
||||||
|
collect (format "-H%s: %s" key val))
|
||||||
|
(list url))))
|
||||||
|
|
||||||
;;TODO: The :transformer argument here is an alternate implementation of
|
;;TODO: The :transformer argument here is an alternate implementation of
|
||||||
;;`gptel-response-filter-functions'. The two need to be unified.
|
;;`gptel-response-filter-functions'. The two need to be unified.
|
||||||
|
|
7
gptel.el
7
gptel.el
|
@ -74,6 +74,13 @@
|
||||||
:group 'gptel
|
:group 'gptel
|
||||||
:type 'string)
|
:type 'string)
|
||||||
|
|
||||||
|
(defcustom gptel-proxy ""
|
||||||
|
"Path to a proxy to use for gptel interactions.
|
||||||
|
Passed to curl via --proxy arg, for example \"proxy.yourorg.com:80\"
|
||||||
|
Leave it empty if you don't use a proxy."
|
||||||
|
:group 'gptel
|
||||||
|
:type 'string)
|
||||||
|
|
||||||
(defcustom gptel-api-key #'gptel-api-key-from-auth-source
|
(defcustom gptel-api-key #'gptel-api-key-from-auth-source
|
||||||
"An OpenAI API key (string).
|
"An OpenAI API key (string).
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue