gptel-openai: curl-args slot in gptel-backend (#221)

gptel-openai.el (gptel-backend, gptel-make-openai,
gptel-make-azure): Add a curl-args slot to the backend struct for
additional Curl arguments.

Usage example: This can be used to set the `--cert` and `--key`
options in a custom backend that uses mutal TLS to communicate
with an OpenAI proxy/gateway.

gptel-curl.el (gptel-curl--get-args): Add backend-specific
curl-args when creating HTTP requests.

gptel-gemini.el (gptel-make-gemini): Add a curl-args slot to the
constructor.
gptel-kagi.el (gptel-make-kagi): Ditto.
gptel-ollama.el (gptel-make-ollama): Ditto.
This commit is contained in:
r0man 2024-02-21 00:21:46 +01:00 committed by GitHub
parent 226f8f0d90
commit 43f625ecb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 6 deletions

View file

@ -66,6 +66,7 @@ PROMPTS is the data to send, TOKEN is a unique identifier."
(gptel--log data "request body")) (gptel--log data "request body"))
(append (append
gptel-curl--common-args gptel-curl--common-args
(gptel-backend-curl-args gptel-backend)
(list (format "-w(%s . %%{size_header})" token)) (list (format "-w(%s . %%{size_header})" token))
(if (length< data gptel-curl-file-size-threshold) (if (length< data gptel-curl-file-size-threshold)
(list (format "-d%s" data)) (list (format "-d%s" data))

View file

@ -111,7 +111,7 @@
;;;###autoload ;;;###autoload
(cl-defun gptel-make-gemini (cl-defun gptel-make-gemini
(name &key header key (stream nil) (name &key curl-args header key (stream nil)
(host "generativelanguage.googleapis.com") (host "generativelanguage.googleapis.com")
(protocol "https") (protocol "https")
(models '("gemini-pro")) (models '("gemini-pro"))
@ -121,6 +121,8 @@
Keyword arguments: Keyword arguments:
CURL-ARGS (optional) is a list of additional Curl arguments.
HOST (optional) is the API host, defaults to HOST (optional) is the API host, defaults to
\"generativelanguage.googleapis.com\". \"generativelanguage.googleapis.com\".
@ -145,6 +147,7 @@ KEY (optional) is a variable whose value is the API key, or
function that returns the key." function that returns the key."
(declare (indent 1)) (declare (indent 1))
(let ((backend (gptel--make-gemini (let ((backend (gptel--make-gemini
:curl-args curl-args
:name name :name name
:host host :host host
:header header :header header

View file

@ -120,7 +120,7 @@
;;;###autoload ;;;###autoload
(cl-defun gptel-make-kagi (cl-defun gptel-make-kagi
(name &key stream key (name &key curl-args stream key
(host "kagi.com") (host "kagi.com")
(header (lambda () `(("Authorization" . ,(concat "Bot " (gptel--get-api-key)))))) (header (lambda () `(("Authorization" . ,(concat "Bot " (gptel--get-api-key))))))
(models '("fastgpt" (models '("fastgpt"
@ -132,6 +132,8 @@
Keyword arguments: Keyword arguments:
CURL-ARGS (optional) is a list of additional Curl arguments.
HOST is the Kagi host (with port), defaults to \"kagi.com\". HOST is the Kagi host (with port), defaults to \"kagi.com\".
MODELS is a list of available Kagi models: only fastgpt is supported. MODELS is a list of available Kagi models: only fastgpt is supported.
@ -159,6 +161,7 @@ Example:
(declare (indent 1)) (declare (indent 1))
stream ;Silence byte-compiler stream ;Silence byte-compiler
(let ((backend (gptel--make-kagi (let ((backend (gptel--make-kagi
:curl-args curl-args
:name name :name name
:host host :host host
:header header :header header

View file

@ -101,7 +101,7 @@ Ollama models.")
;;;###autoload ;;;###autoload
(cl-defun gptel-make-ollama (cl-defun gptel-make-ollama
(name &key header key models stream (name &key curl-args header key models stream
(host "localhost:11434") (host "localhost:11434")
(protocol "http") (protocol "http")
(endpoint "/api/generate")) (endpoint "/api/generate"))
@ -109,6 +109,8 @@ Ollama models.")
Keyword arguments: Keyword arguments:
CURL-ARGS (optional) is a list of additional Curl arguments.
HOST is where Ollama runs (with port), defaults to localhost:11434 HOST is where Ollama runs (with port), defaults to localhost:11434
MODELS is a list of available model names. MODELS is a list of available model names.
@ -140,6 +142,7 @@ Example:
:stream t)" :stream t)"
(declare (indent 1)) (declare (indent 1))
(let ((backend (gptel--make-ollama (let ((backend (gptel--make-ollama
:curl-args curl-args
:name name :name name
:host host :host host
:header header :header header

View file

@ -49,7 +49,7 @@
(gptel-backend (:constructor gptel--make-backend) (gptel-backend (:constructor gptel--make-backend)
(:copier gptel--copy-backend)) (:copier gptel--copy-backend))
name host header protocol stream name host header protocol stream
endpoint key models url) endpoint key models url curl-args)
;;; OpenAI (ChatGPT) ;;; OpenAI (ChatGPT)
(cl-defstruct (gptel-openai (:constructor gptel--make-openai) (cl-defstruct (gptel-openai (:constructor gptel--make-openai)
@ -115,7 +115,7 @@
;;;###autoload ;;;###autoload
(cl-defun gptel-make-openai (cl-defun gptel-make-openai
(name &key models stream key (name &key curl-args models stream key
(header (header
(lambda () (when-let (key (gptel--get-api-key)) (lambda () (when-let (key (gptel--get-api-key))
`(("Authorization" . ,(concat "Bearer " key)))))) `(("Authorization" . ,(concat "Bearer " key))))))
@ -126,6 +126,8 @@
Keyword arguments: Keyword arguments:
CURL-ARGS (optional) is a list of additional Curl arguments.
HOST (optional) is the API host, typically \"api.openai.com\". HOST (optional) is the API host, typically \"api.openai.com\".
MODELS is a list of available model names. MODELS is a list of available model names.
@ -147,6 +149,7 @@ KEY (optional) is a variable whose value is the API key, or
function that returns the key." function that returns the key."
(declare (indent 1)) (declare (indent 1))
(let ((backend (gptel--make-openai (let ((backend (gptel--make-openai
:curl-args curl-args
:name name :name name
:host host :host host
:header header :header header
@ -166,7 +169,7 @@ function that returns the key."
;;; Azure ;;; Azure
;;;###autoload ;;;###autoload
(cl-defun gptel-make-azure (cl-defun gptel-make-azure
(name &key host (name &key curl-args host
(protocol "https") (protocol "https")
(header (lambda () `(("api-key" . ,(gptel--get-api-key))))) (header (lambda () `(("api-key" . ,(gptel--get-api-key)))))
(key 'gptel-api-key) (key 'gptel-api-key)
@ -175,6 +178,8 @@ function that returns the key."
Keyword arguments: Keyword arguments:
CURL-ARGS (optional) is a list of additional Curl arguments.
HOST is the API host. HOST is the API host.
MODELS is a list of available model names. MODELS is a list of available model names.
@ -207,6 +212,7 @@ Example:
:models \\='(\"gpt-3.5-turbo\" \"gpt-4\"))" :models \\='(\"gpt-3.5-turbo\" \"gpt-4\"))"
(declare (indent 1)) (declare (indent 1))
(let ((backend (gptel--make-openai (let ((backend (gptel--make-openai
:curl-args curl-args
:name name :name name
:host host :host host
:header header :header header
@ -230,6 +236,8 @@ Example:
Keyword arguments: Keyword arguments:
CURL-ARGS (optional) is a list of additional Curl arguments.
HOST is where GPT4All runs (with port), typically localhost:8491 HOST is where GPT4All runs (with port), typically localhost:8491
MODELS is a list of available model names. MODELS is a list of available model names.