gptel: Make API host configurable (#67)

* Make API host configurable

* Update README.org
This commit is contained in:
Tianshu Wang 2023-06-01 11:24:13 +08:00 committed by GitHub
parent 42132d3662
commit e6a1468bd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 8 deletions

View file

@ -15,8 +15,11 @@ https://user-images.githubusercontent.com/8607532/230516816-ae4a613a-4d01-4073-a
- Supports conversations and multiple independent sessions.
- You can go back and edit your previous prompts, or even ChatGPT's previous responses when continuing a conversation. These will be fed back to ChatGPT.
GPTel uses Curl if available, but falls back to =url-retrieve= to work without external dependencies.
GPTel uses Curl if available, but falls back to url-retrieve to work without external dependencies.
** Breaking Changes
1. =gptel-api-key-from-auth-source= search the API KEY using the =gptel-host=, i.e., "api.openai.com" instead of the original "openai.com". You need to update your =~/.authinfo=.
** Installation
GPTel is on MELPA. Install it with =M-x package-install⏎= =gptel=.
@ -60,9 +63,9 @@ Procure an [[https://platform.openai.com/account/api-keys][OpenAI API key]].
Optional: Set =gptel-api-key= to the key. Alternatively, you may choose a more secure method such as:
- Storing in =~/.authinfo=. By default, "openai.com" is used as HOST and "apikey" as USER.
- Storing in =~/.authinfo=. By default, "api.openai.com" is used as HOST and "apikey" as USER.
#+begin_src authinfo
machine openai.com login apikey password TOKEN
machine api.openai.com login apikey password TOKEN
#+end_src
- Setting it to a function that returns the key.
@ -114,6 +117,10 @@ If you want custom behavior, such as
GPTel provides a general =gptel-request= function that accepts a custom prompt and a callback to act on the response. You can use this to build custom workflows not supported by =gptel-send=. See the documentation of =gptel-request=, and the [[https://github.com/karthink/gptel/wiki][wiki]] for examples.
** Additional Configuration
- You can override the OpenAI API host by customizing =gptel-host=. This is useful for those who transform Azure API into OpenAI API format, utilize reverse proxy, or employ third-party proxy services for the OpenAI API.
** Why another ChatGPT client?
Other Emacs clients for ChatGPT prescribe the format of the interaction (a comint shell, org-babel blocks, etc). I wanted:

View file

@ -42,7 +42,7 @@
PROMPTS is the data to send, TOKEN is a unique identifier."
(let* ((args
(list "--location" "--silent" "--compressed" "--disable"))
(url "https://api.openai.com/v1/chat/completions")
(url (format "https://%s/v1/chat/completions" gptel-host))
(data (encode-coding-string
(json-encode (gptel--request-data prompts))
'utf-8))

View file

@ -69,6 +69,11 @@
(defgroup gptel nil
"Interact with ChatGPT from anywhere in Emacs.")
(defcustom gptel-host "api.openai.com"
"The API host queried by gptel."
:group 'gptel
:type 'string)
(defcustom gptel-api-key #'gptel-api-key-from-auth-source
"An OpenAI API key (string).
@ -214,9 +219,9 @@ To set the temperature for a chat session interactively call
(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."
By default, `gptel-host' is used as HOST and \"apikey\" as USER."
(if-let ((secret (plist-get (car (auth-source-search
:host (or host "openai.com")
:host (or host gptel-host)
:user (or user "apikey")))
:secret)))
(if (functionp secret)
@ -557,7 +562,7 @@ the response is inserted into the current buffer after point."
(encode-coding-string
(json-encode (gptel--request-data (plist-get info :prompt)))
'utf-8)))
(url-retrieve "https://api.openai.com/v1/chat/completions"
(url-retrieve (format "https://%s/v1/chat/completions" gptel-host)
(lambda (_)
(pcase-let ((`(,response ,http-msg ,error)
(gptel--url-parse-response (current-buffer))))