gptel: Make API host configurable (#67)
* Make API host configurable * Update README.org
This commit is contained in:
parent
42132d3662
commit
e6a1468bd2
3 changed files with 20 additions and 8 deletions
13
README.org
13
README.org
|
@ -15,7 +15,10 @@ https://user-images.githubusercontent.com/8607532/230516816-ae4a613a-4d01-4073-a
|
||||||
- Supports conversations and multiple independent sessions.
|
- 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.
|
- 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
|
** Installation
|
||||||
|
|
||||||
|
@ -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:
|
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
|
#+begin_src authinfo
|
||||||
machine openai.com login apikey password TOKEN
|
machine api.openai.com login apikey password TOKEN
|
||||||
#+end_src
|
#+end_src
|
||||||
- Setting it to a function that returns the key.
|
- 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.
|
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?
|
** Why another ChatGPT client?
|
||||||
|
|
||||||
Other Emacs clients for ChatGPT prescribe the format of the interaction (a comint shell, org-babel blocks, etc). I wanted:
|
Other Emacs clients for ChatGPT prescribe the format of the interaction (a comint shell, org-babel blocks, etc). I wanted:
|
||||||
|
|
|
@ -42,7 +42,7 @@
|
||||||
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* ((args
|
||||||
(list "--location" "--silent" "--compressed" "--disable"))
|
(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
|
(data (encode-coding-string
|
||||||
(json-encode (gptel--request-data prompts))
|
(json-encode (gptel--request-data prompts))
|
||||||
'utf-8))
|
'utf-8))
|
||||||
|
|
11
gptel.el
11
gptel.el
|
@ -69,6 +69,11 @@
|
||||||
(defgroup gptel nil
|
(defgroup gptel nil
|
||||||
"Interact with ChatGPT from anywhere in Emacs.")
|
"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
|
(defcustom gptel-api-key #'gptel-api-key-from-auth-source
|
||||||
"An OpenAI API key (string).
|
"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)
|
(defun gptel-api-key-from-auth-source (&optional host user)
|
||||||
"Lookup api key in the auth source.
|
"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
|
(if-let ((secret (plist-get (car (auth-source-search
|
||||||
:host (or host "openai.com")
|
:host (or host gptel-host)
|
||||||
:user (or user "apikey")))
|
:user (or user "apikey")))
|
||||||
:secret)))
|
:secret)))
|
||||||
(if (functionp secret)
|
(if (functionp secret)
|
||||||
|
@ -557,7 +562,7 @@ the response is inserted into the current buffer after point."
|
||||||
(encode-coding-string
|
(encode-coding-string
|
||||||
(json-encode (gptel--request-data (plist-get info :prompt)))
|
(json-encode (gptel--request-data (plist-get info :prompt)))
|
||||||
'utf-8)))
|
'utf-8)))
|
||||||
(url-retrieve "https://api.openai.com/v1/chat/completions"
|
(url-retrieve (format "https://%s/v1/chat/completions" gptel-host)
|
||||||
(lambda (_)
|
(lambda (_)
|
||||||
(pcase-let ((`(,response ,http-msg ,error)
|
(pcase-let ((`(,response ,http-msg ,error)
|
||||||
(gptel--url-parse-response (current-buffer))))
|
(gptel--url-parse-response (current-buffer))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue