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:
Karthik Chikmagalur 2023-04-09 03:29:48 -07:00
parent acf12ee6e3
commit 8a9ae56e77
2 changed files with 18 additions and 4 deletions

View file

@ -165,7 +165,8 @@ See `gptel--url-get-response' for details."
(unless tracking-marker
(gptel--update-header-line " Typing..." 'success)
(goto-char start-marker)
(insert "\n\n")
(unless (plist-get info :in-place)
(insert "\n\n"))
(setq tracking-marker (set-marker (make-marker) (point)))
(set-marker-insertion-type tracking-marker t)
(plist-put info :tracking-marker tracking-marker))

View file

@ -282,7 +282,8 @@ By default, \"openai.com\" is used as HOST and \"apikey\" as USER."
(cl-defun gptel-request
(&optional prompt &key callback
(buffer (current-buffer))
position context
position context (stream nil)
(in-place nil)
(system gptel--system-message))
"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
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."
(let* ((gptel-stream nil)
(let* ((gptel-stream stream)
(start-marker
(cond
((null position)
@ -366,6 +377,7 @@ Model parameters can be let-bound around calls to this function."
:buffer buffer
:position start-marker)))
(when context (plist-put info :context context))
(when in-place (plist-put info :in-place in-place))
(funcall
(if gptel-use-curl
#'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)
(message "Querying ChatGPT... done.")
(goto-char start-marker)
(unless (bobp) (insert "\n\n"))
(unless (or (bobp) (plist-get info :in-place))
(insert "\n\n"))
(let ((p (point)))
(insert response)
(pulse-momentary-highlight-region p (point)))