branch: externals/llm commit 0cb682aff401c0554f6faf01fad97943393f469c Merge: f409841da3 c10187a07a Author: Andrew Hyatt <ahy...@gmail.com> Commit: GitHub <nore...@github.com>
Merge pull request #39 from r0man/plz Plz --- llm-request-plz.el | 14 +++++++++++--- plz-event-source.el | 8 +++++++- plz-media-type.el | 27 ++++++++++++++++++--------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/llm-request-plz.el b/llm-request-plz.el index 8d7b9a94cd..82755484f1 100644 --- a/llm-request-plz.el +++ b/llm-request-plz.el @@ -27,18 +27,24 @@ (require 'rx) (require 'url-http) -(defcustom llm-request-plz-timeout (* 2 60) +(defcustom llm-request-plz-timeout nil "The number of seconds to wait for a response from a HTTP server. -Request timings are depending on the request. Requests that need +When set to nil, don't timeout while receiving a response. +Request timings are depending on the request. Requests that need more output may take more time, and there is other processing -besides just token generation that can take a while. Sometimes +besides just token generation that can take a while. Sometimes the LLM can get stuck, and you don't want it to take too long. This should be balanced to be good enough for hard requests but not very long so that we can end stuck requests." :type 'integer :group 'llm) +(defcustom llm-request-plz-connect-timeout 10 + "The number of seconds to wait for a connection to a HTTP server." + :type 'integer + :group 'llm) + (defun llm-request-success (status) "Return non-nil if STATUS is a successful HTTP status code." (<= 200 status 299)) @@ -59,6 +65,7 @@ TIMEOUT is the number of seconds to wait for a response." :as `(media-types ,plz-media-types) :body (when data (encode-coding-string (json-encode data) 'utf-8)) + :connect-timeout llm-request-plz-connect-timeout :headers (append headers '(("Content-Type" . "application/json"))) :timeout (or timeout llm-request-plz-timeout)))) (if (llm-request-success (plz-response-status resp)) @@ -141,6 +148,7 @@ only used by other methods in this file." plz-media-types)) :body (when data (encode-coding-string (json-encode data) 'utf-8)) + :connect-timeout llm-request-plz-connect-timeout :headers (append headers '(("Content-Type" . "application/json"))) :then (lambda (response) diff --git a/plz-event-source.el b/plz-event-source.el index b522cf0c1b..4f2cad0d23 100644 --- a/plz-event-source.el +++ b/plz-event-source.el @@ -4,6 +4,10 @@ ;; Author: r0man <ro...@burningswell.com> ;; Maintainer: r0man <ro...@burningswell.com> +;; URL: https://github.com/r0man/plz-event-source.el +;; Version: 0.1-pre +;; Package-Requires: ((emacs "26.3")) +;; Keywords: comm, network, http ;; This file is part of GNU Emacs. @@ -458,7 +462,9 @@ callbacks will always be set to nil.") (cl-defmethod plz-media-type-then ((media-type plz-media-type:text/event-stream) response) "Transform the RESPONSE into a format suitable for MEDIA-TYPE." (plz-event-source-close plz-event-source--current) - (cl-call-next-method media-type response)) + (cl-call-next-method media-type response) + (setf (plz-response-body response) nil) + response) (provide 'plz-event-source) ;;; plz-event-source.el ends here diff --git a/plz-media-type.el b/plz-media-type.el index c20855487d..4216cba7b9 100644 --- a/plz-media-type.el +++ b/plz-media-type.el @@ -204,19 +204,22 @@ HTTP responses. The media type sets the body slot of the plz-response structure to the unmodified value of the HTTP response body. It is used as the default media type processor.") -(cl-defmethod plz-media-type-else ((media-type plz-media-type:application/octet-stream) error) +(cl-defmethod plz-media-type-else + ((media-type plz-media-type:application/octet-stream) error) "Transform the ERROR into a format suitable for MEDIA-TYPE." (when-let (response (plz-error-response error)) (setf (plz-error-response error) (plz-media-type-then media-type response))) error) -(cl-defmethod plz-media-type-then ((media-type plz-media-type:application/octet-stream) response) +(cl-defmethod plz-media-type-then + ((media-type plz-media-type:application/octet-stream) response) "Transform the RESPONSE into a format suitable for MEDIA-TYPE." (ignore media-type) (setf (plz-response-body response) (buffer-string)) response) -(cl-defmethod plz-media-type-process ((media-type plz-media-type:application/octet-stream) process chunk) +(cl-defmethod plz-media-type-process + ((media-type plz-media-type:application/octet-stream) process chunk) "Process the CHUNK according to MEDIA-TYPE using PROCESS." (ignore media-type) (save-excursion @@ -267,7 +270,8 @@ accordingly.") :null-object null-object :object-type object-type)) ) -(cl-defmethod plz-media-type-then ((media-type plz-media-type:application/json) response) +(cl-defmethod plz-media-type-then + ((media-type plz-media-type:application/json) response) "Transform the RESPONSE into a format suitable for MEDIA-TYPE." (setf (plz-response-body response) (plz-media-type--parse-json-object media-type)) response) @@ -331,14 +335,16 @@ will always be set to nil.") (funcall handler (cdr result))) (setq result (plz-media-type:application/json-array--consume-next media-type)))))) -(cl-defmethod plz-media-type-process ((media-type plz-media-type:application/json-array) process chunk) +(cl-defmethod plz-media-type-process + ((media-type plz-media-type:application/json-array) process chunk) "Process the CHUNK according to MEDIA-TYPE using PROCESS." (ignore media-type) (cl-call-next-method media-type process chunk) (plz-media-type:application/json-array--parse-stream media-type) (set-marker (process-mark process) (point-max))) -(cl-defmethod plz-media-type-then ((media-type plz-media-type:application/json-array) response) +(cl-defmethod plz-media-type-then + ((media-type plz-media-type:application/json-array) response) "Transform the RESPONSE into a format suitable for MEDIA-TYPE." (ignore media-type) (plz-media-type:application/json-array--parse-stream media-type) @@ -386,12 +392,14 @@ will always be set to nil.") (funcall handler object)) (setq object (plz-media-type:application/x-ndjson--parse-line media-type)))))) -(cl-defmethod plz-media-type-process ((media-type plz-media-type:application/x-ndjson) process chunk) +(cl-defmethod plz-media-type-process + ((media-type plz-media-type:application/x-ndjson) process chunk) "Process the CHUNK according to MEDIA-TYPE using PROCESS." (cl-call-next-method media-type process chunk) (plz-media-type:application/x-ndjson--parse-stream media-type)) -(cl-defmethod plz-media-type-then ((media-type plz-media-type:application/x-ndjson) response) +(cl-defmethod plz-media-type-then + ((media-type plz-media-type:application/x-ndjson) response) "Transform the RESPONSE into a format suitable for MEDIA-TYPE." (plz-media-type:application/x-ndjson--parse-stream media-type) (setf (plz-response-body response) nil) @@ -408,7 +416,8 @@ body of the plz-response structure is set to the result of parsing the HTTP response body with the `libxml-parse-html-region' function.") -(cl-defmethod plz-media-type-then ((media-type plz-media-type:application/xml) response) +(cl-defmethod plz-media-type-then + ((media-type plz-media-type:application/xml) response) "Transform the RESPONSE into a format suitable for MEDIA-TYPE." (with-slots (array-type false-object null-object object-type) media-type (setf (plz-response-body response)