gptel: Add streaming, in-place options to gptel-request
* gptel.el (gptel--insert-response, gptel-request): - Add an in-place key to gptel-request. When true, the default callbacks will not delimit the API responses with newlines. - Add a strea option to gptel-request. Only works with the default filter/stream-insert callback, so it's marked as for internal use for now. * gptel-curl.el (gptel-curl--stream-insert-response): Ditto.
This commit is contained in:
parent
acf12ee6e3
commit
8a9ae56e77
2 changed files with 18 additions and 4 deletions
|
@ -165,7 +165,8 @@ See `gptel--url-get-response' for details."
|
||||||
(unless tracking-marker
|
(unless tracking-marker
|
||||||
(gptel--update-header-line " Typing..." 'success)
|
(gptel--update-header-line " Typing..." 'success)
|
||||||
(goto-char start-marker)
|
(goto-char start-marker)
|
||||||
(insert "\n\n")
|
(unless (plist-get info :in-place)
|
||||||
|
(insert "\n\n"))
|
||||||
(setq tracking-marker (set-marker (make-marker) (point)))
|
(setq tracking-marker (set-marker (make-marker) (point)))
|
||||||
(set-marker-insertion-type tracking-marker t)
|
(set-marker-insertion-type tracking-marker t)
|
||||||
(plist-put info :tracking-marker tracking-marker))
|
(plist-put info :tracking-marker tracking-marker))
|
||||||
|
|
19
gptel.el
19
gptel.el
|
@ -282,7 +282,8 @@ By default, \"openai.com\" is used as HOST and \"apikey\" as USER."
|
||||||
(cl-defun gptel-request
|
(cl-defun gptel-request
|
||||||
(&optional prompt &key callback
|
(&optional prompt &key callback
|
||||||
(buffer (current-buffer))
|
(buffer (current-buffer))
|
||||||
position context
|
position context (stream nil)
|
||||||
|
(in-place nil)
|
||||||
(system gptel--system-message))
|
(system gptel--system-message))
|
||||||
"Request a response from ChatGPT for PROMPT.
|
"Request a response from ChatGPT for PROMPT.
|
||||||
|
|
||||||
|
@ -344,8 +345,18 @@ SYSTEM is the system message (chat directive) sent to ChatGPT. If
|
||||||
omitted, the value of `gptel--system-message' for the current
|
omitted, the value of `gptel--system-message' for the current
|
||||||
buffer is used.
|
buffer is used.
|
||||||
|
|
||||||
|
The following keywords are mainly for internal use:
|
||||||
|
|
||||||
|
IN-PLACE is a boolean used by the default callback when inserting
|
||||||
|
the response to determine if delimiters are needed between the
|
||||||
|
prompt and the response.
|
||||||
|
|
||||||
|
STREAM is a boolean that determines if the response should be
|
||||||
|
streamed, as in `gptel-stream'. Do not set this if you are
|
||||||
|
specifying a custom CALLBACK!
|
||||||
|
|
||||||
Model parameters can be let-bound around calls to this function."
|
Model parameters can be let-bound around calls to this function."
|
||||||
(let* ((gptel-stream nil)
|
(let* ((gptel-stream stream)
|
||||||
(start-marker
|
(start-marker
|
||||||
(cond
|
(cond
|
||||||
((null position)
|
((null position)
|
||||||
|
@ -366,6 +377,7 @@ Model parameters can be let-bound around calls to this function."
|
||||||
:buffer buffer
|
:buffer buffer
|
||||||
:position start-marker)))
|
:position start-marker)))
|
||||||
(when context (plist-put info :context context))
|
(when context (plist-put info :context context))
|
||||||
|
(when in-place (plist-put info :in-place in-place))
|
||||||
(funcall
|
(funcall
|
||||||
(if gptel-use-curl
|
(if gptel-use-curl
|
||||||
#'gptel-curl-get-response #'gptel--url-get-response)
|
#'gptel-curl-get-response #'gptel--url-get-response)
|
||||||
|
@ -416,7 +428,8 @@ See `gptel--url-get-response' for details."
|
||||||
(put-text-property 0 (length response) 'gptel 'response response)
|
(put-text-property 0 (length response) 'gptel 'response response)
|
||||||
(message "Querying ChatGPT... done.")
|
(message "Querying ChatGPT... done.")
|
||||||
(goto-char start-marker)
|
(goto-char start-marker)
|
||||||
(unless (bobp) (insert "\n\n"))
|
(unless (or (bobp) (plist-get info :in-place))
|
||||||
|
(insert "\n\n"))
|
||||||
(let ((p (point)))
|
(let ((p (point)))
|
||||||
(insert response)
|
(insert response)
|
||||||
(pulse-momentary-highlight-region p (point)))
|
(pulse-momentary-highlight-region p (point)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue