2023-03-08 00:52:48 -08:00
|
|
|
;;; gptel-curl.el --- Curl support for GPTel -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2023 Karthik Chikmagalur
|
|
|
|
|
|
|
|
;; Author: Karthik Chikmagalur;; <karthikchikmagalur@gmail.com>
|
|
|
|
;; Keywords: convenience
|
2023-03-08 19:17:14 -08:00
|
|
|
|
|
|
|
;; SPDX-License-Identifier: GPL-3.0-or-later
|
2023-03-08 00:52:48 -08:00
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Curl support for GPTel. Utility functions.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2023-03-08 19:17:14 -08:00
|
|
|
(require 'gptel)
|
|
|
|
|
2023-03-08 00:52:48 -08:00
|
|
|
(eval-when-compile
|
|
|
|
(require 'subr-x))
|
|
|
|
(require 'map)
|
|
|
|
(require 'json)
|
|
|
|
|
2023-03-08 19:23:17 -08:00
|
|
|
(defvar gptel-curl--process-alist nil
|
2023-03-08 00:52:48 -08:00
|
|
|
"Alist of active GPTel curl requests.")
|
|
|
|
|
2023-03-08 19:23:17 -08:00
|
|
|
(defun gptel-curl--get-args (prompts token)
|
2023-03-08 00:52:48 -08:00
|
|
|
"Produce list of arguments for calling Curl.
|
|
|
|
|
|
|
|
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")
|
|
|
|
(data (encode-coding-string
|
2023-03-08 19:20:00 -08:00
|
|
|
(json-encode (gptel--request-data prompts))
|
2023-03-08 00:52:48 -08:00
|
|
|
'utf-8))
|
|
|
|
(headers
|
|
|
|
`(("Content-Type" . "application/json")
|
2023-03-18 08:41:25 +01:00
|
|
|
("Authorization" . ,(concat "Bearer " (gptel--api-key))))))
|
2023-03-08 00:52:48 -08:00
|
|
|
(push (format "-X%s" "POST") args)
|
|
|
|
(push (format "-w(%s . %%{size_header})" token) args)
|
|
|
|
;; (push (format "--keepalive-time %s" 240) args)
|
|
|
|
(push (format "-m%s" 60) args)
|
|
|
|
(push "-D-" args)
|
|
|
|
(pcase-dolist (`(,key . ,val) headers)
|
|
|
|
(push (format "-H%s: %s" key val) args))
|
|
|
|
(push (format "-d%s" data) args)
|
|
|
|
(nreverse (cons url args))))
|
|
|
|
|
2023-04-04 22:19:37 -07:00
|
|
|
;;TODO: The :transformer argument here is an alternate implementation of
|
|
|
|
;;`gptel-response-filter-functions'. The two need to be unified.
|
2023-03-08 19:17:14 -08:00
|
|
|
;;;###autoload
|
2023-03-31 19:37:25 -07:00
|
|
|
(defun gptel-curl-get-response (info &optional callback)
|
2023-03-17 23:46:30 -07:00
|
|
|
"Retrieve response to prompt in INFO.
|
|
|
|
|
|
|
|
INFO is a plist with the following keys:
|
|
|
|
- :prompt (the prompt being sent)
|
|
|
|
- :gptel-buffer (the gptel buffer)
|
2023-04-06 16:18:37 -07:00
|
|
|
- :start-marker (marker at which to insert the response).
|
2023-03-31 19:37:25 -07:00
|
|
|
|
|
|
|
Call CALLBACK with the response and INFO afterwards. If omitted
|
|
|
|
the response is inserted into the current buffer after point."
|
2023-03-08 00:52:48 -08:00
|
|
|
(with-current-buffer (generate-new-buffer "*gptel-curl*")
|
|
|
|
(let* ((token (md5 (format "%s%s%s%s"
|
|
|
|
(random) (emacs-pid) (user-full-name)
|
|
|
|
(recent-keys))))
|
2023-03-17 23:46:30 -07:00
|
|
|
(args (gptel-curl--get-args (plist-get info :prompt) token))
|
2023-03-08 00:52:48 -08:00
|
|
|
(process (apply #'start-process "gptel-curl" (current-buffer)
|
2023-03-17 23:46:30 -07:00
|
|
|
"curl" args)))
|
|
|
|
(set-process-query-on-exit-flag process nil)
|
|
|
|
(setf (alist-get process gptel-curl--process-alist)
|
2023-03-31 19:37:25 -07:00
|
|
|
(nconc (list :token token
|
2023-04-01 02:36:10 -07:00
|
|
|
:callback (or callback
|
2023-04-06 16:35:51 -07:00
|
|
|
(if gptel-stream
|
|
|
|
#'gptel-curl--stream-insert-response
|
2023-04-04 22:19:37 -07:00
|
|
|
#'gptel--insert-response))
|
|
|
|
:transformer (when (or (eq gptel-default-mode 'org-mode)
|
|
|
|
(eq (buffer-local-value
|
|
|
|
'major-mode
|
|
|
|
(plist-get info :gptel-buffer))
|
|
|
|
'org-mode))
|
2023-04-06 16:35:51 -07:00
|
|
|
(gptel--stream-convert-markdown->org)))
|
2023-03-31 19:37:25 -07:00
|
|
|
info))
|
2023-04-06 16:35:51 -07:00
|
|
|
(if gptel-stream
|
|
|
|
(progn (set-process-sentinel process #'gptel-curl--stream-cleanup)
|
|
|
|
(set-process-filter process #'gptel-curl--stream-filter))
|
2023-04-01 02:36:10 -07:00
|
|
|
(set-process-sentinel process #'gptel-curl--sentinel)))))
|
|
|
|
|
2023-04-06 16:35:51 -07:00
|
|
|
(defun gptel-curl--stream-cleanup (process status)
|
2023-04-01 02:36:10 -07:00
|
|
|
"Process sentinel for GPTel curl requests.
|
|
|
|
|
|
|
|
PROCESS and STATUS are process parameters."
|
|
|
|
(let ((proc-buf (process-buffer process)))
|
|
|
|
(when gptel--debug
|
|
|
|
(with-current-buffer proc-buf
|
|
|
|
(clone-buffer "*gptel-error*" 'show)))
|
|
|
|
(let* ((info (alist-get process gptel-curl--process-alist))
|
|
|
|
(gptel-buffer (plist-get info :gptel-buffer))
|
|
|
|
(tracking-marker (plist-get info :tracking-marker))
|
2023-04-06 16:18:37 -07:00
|
|
|
(start-marker (plist-get info :start-marker)))
|
2023-04-01 02:36:10 -07:00
|
|
|
(pulse-momentary-highlight-region (+ start-marker 2) tracking-marker)
|
2023-04-06 16:18:37 -07:00
|
|
|
(with-current-buffer gptel-buffer
|
|
|
|
(when (equal (plist-get info :http-status) "200")
|
2023-04-01 02:36:10 -07:00
|
|
|
(gptel--update-header-line " Ready" 'success)
|
|
|
|
(when gptel-mode
|
|
|
|
(save-excursion (goto-char tracking-marker)
|
2023-04-06 16:18:37 -07:00
|
|
|
(insert "\n\n" (gptel-prompt-string)))))
|
|
|
|
(run-hooks 'gptel-post-response-hook)))
|
2023-04-01 02:36:10 -07:00
|
|
|
(setf (alist-get process gptel-curl--process-alist nil 'remove) nil)
|
|
|
|
(kill-buffer proc-buf)))
|
|
|
|
|
2023-04-06 16:18:37 -07:00
|
|
|
(defun gptel-curl--stream-insert-response (response info)
|
2023-04-01 02:36:10 -07:00
|
|
|
"Insert streaming RESPONSE from ChatGPT into the gptel buffer.
|
|
|
|
|
|
|
|
INFO is a mutable plist containing information relevant to this buffer.
|
|
|
|
See `gptel--url-get-response' for details."
|
|
|
|
(let ((content-str (plist-get response :content))
|
|
|
|
(status-str (plist-get response :status))
|
|
|
|
(gptel-buffer (plist-get info :gptel-buffer))
|
2023-04-06 16:18:37 -07:00
|
|
|
(start-marker (plist-get info :start-marker))
|
2023-04-04 22:19:37 -07:00
|
|
|
(tracking-marker (plist-get info :tracking-marker))
|
|
|
|
(transformer (plist-get info :transformer)))
|
2023-04-01 02:36:10 -07:00
|
|
|
(if content-str
|
|
|
|
(with-current-buffer gptel-buffer
|
|
|
|
(save-excursion
|
|
|
|
(unless tracking-marker
|
|
|
|
(gptel--update-header-line " Typing..." 'success)
|
|
|
|
(goto-char start-marker)
|
|
|
|
(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))
|
|
|
|
|
2023-04-04 22:19:37 -07:00
|
|
|
(when transformer
|
|
|
|
(setq content-str (funcall transformer content-str)))
|
|
|
|
|
2023-04-01 02:36:10 -07:00
|
|
|
(put-text-property 0 (length content-str) 'gptel 'response content-str)
|
|
|
|
(goto-char tracking-marker)
|
|
|
|
(insert content-str)))
|
|
|
|
(gptel--update-header-line
|
|
|
|
(format " Response Error: %s" status-str) 'error))))
|
|
|
|
|
2023-04-06 16:35:51 -07:00
|
|
|
(defun gptel-curl--stream-filter (process output)
|
2023-04-01 02:36:10 -07:00
|
|
|
(let* ((content-strs)
|
|
|
|
(proc-info (alist-get process gptel-curl--process-alist)))
|
|
|
|
(with-current-buffer (process-buffer process)
|
|
|
|
;; Insert output
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (process-mark process))
|
|
|
|
(insert output)
|
|
|
|
(set-marker (process-mark process) (point)))
|
|
|
|
|
|
|
|
;; Find HTTP status
|
|
|
|
(unless (plist-get proc-info :http-status)
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(when-let* (((not (= (line-end-position) (point-max))))
|
|
|
|
(http-msg (buffer-substring (line-beginning-position)
|
|
|
|
(line-end-position)))
|
|
|
|
(http-status
|
|
|
|
(save-match-data
|
|
|
|
(and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
|
|
|
|
(match-string 1 http-msg)))))
|
|
|
|
(plist-put proc-info :http-status http-status)
|
|
|
|
(plist-put proc-info :http-msg http-msg)
|
|
|
|
(unless (equal http-status "200")
|
|
|
|
(message "%s" (concat (string-trim http-msg) ": Could not parse HTTP response."))))))
|
|
|
|
|
|
|
|
(when-let ((http-msg (plist-get proc-info :http-msg))
|
|
|
|
(http-status (plist-get proc-info :http-status)))
|
|
|
|
;; Find data chunk(s) and run callback
|
|
|
|
(funcall (or (plist-get proc-info :callback)
|
2023-04-06 16:35:51 -07:00
|
|
|
#'gptel-curl--stream-insert-response)
|
2023-04-01 02:36:10 -07:00
|
|
|
(if (equal http-status "200")
|
|
|
|
(let* ((json-object-type 'plist)
|
|
|
|
(response) (content-str))
|
|
|
|
(condition-case nil
|
|
|
|
(while (re-search-forward "^data:" nil t)
|
|
|
|
(save-match-data
|
|
|
|
(unless (looking-at " *\\[DONE\\]")
|
|
|
|
(when-let* ((response (json-read))
|
|
|
|
(delta (map-nested-elt
|
|
|
|
response '(:choices 0 :delta)))
|
|
|
|
(content (plist-get delta :content)))
|
|
|
|
(push content content-strs)))))
|
|
|
|
(error
|
|
|
|
(goto-char (match-beginning 0))))
|
|
|
|
(list :content (apply #'concat (nreverse content-strs)) :status http-msg))
|
|
|
|
(list :content nil :status http-msg))
|
|
|
|
proc-info)))))
|
2023-03-08 00:52:48 -08:00
|
|
|
|
2023-03-08 19:23:17 -08:00
|
|
|
(defun gptel-curl--sentinel (process status)
|
2023-03-08 00:52:48 -08:00
|
|
|
"Process sentinel for GPTel curl requests.
|
|
|
|
|
|
|
|
PROCESS and STATUS are process parameters."
|
|
|
|
(let ((proc-buf (process-buffer process)))
|
2023-03-12 15:40:39 -07:00
|
|
|
(when gptel--debug
|
|
|
|
(with-current-buffer proc-buf
|
|
|
|
(clone-buffer "*gptel-error*" 'show)))
|
2023-03-18 00:51:49 -07:00
|
|
|
(if-let* (((eq (process-status process) 'exit))
|
2023-03-08 19:23:17 -08:00
|
|
|
(proc-info (alist-get process gptel-curl--process-alist))
|
2023-03-08 00:52:48 -08:00
|
|
|
(proc-token (plist-get proc-info :token))
|
2023-03-31 19:37:25 -07:00
|
|
|
(proc-callback (plist-get proc-info :callback))
|
2023-03-17 23:46:30 -07:00
|
|
|
(response (gptel-curl--parse-response proc-buf proc-token)))
|
2023-03-31 19:37:25 -07:00
|
|
|
(funcall proc-callback response proc-info)
|
2023-03-08 00:52:48 -08:00
|
|
|
;; Failed
|
2023-03-31 19:37:25 -07:00
|
|
|
(funcall proc-callback (list :content nil :status status) proc-info))
|
2023-03-17 23:46:30 -07:00
|
|
|
(setf (alist-get process gptel-curl--process-alist nil 'remove) nil)
|
2023-03-08 00:52:48 -08:00
|
|
|
(kill-buffer proc-buf)))
|
|
|
|
|
2023-03-08 19:23:17 -08:00
|
|
|
(defun gptel-curl--parse-response (buf token)
|
2023-03-08 00:52:48 -08:00
|
|
|
"Parse the buffer BUF with curl's response.
|
|
|
|
|
|
|
|
TOKEN is used to disambiguate multiple requests in a single
|
|
|
|
buffer."
|
|
|
|
(with-current-buffer buf
|
|
|
|
(progn
|
|
|
|
(goto-char (point-max))
|
|
|
|
(search-backward token)
|
|
|
|
(backward-char)
|
|
|
|
(pcase-let* ((`(,_ . ,header-size) (read (current-buffer))))
|
|
|
|
;; (if (search-backward token nil t)
|
|
|
|
;; (search-forward ")" nil t)
|
|
|
|
;; (goto-char (point-min)))
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
|
|
|
(if-let* ((http-msg (buffer-substring (line-beginning-position)
|
|
|
|
(line-end-position)))
|
|
|
|
(http-status
|
|
|
|
(save-match-data
|
|
|
|
(and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
|
|
|
|
(match-string 1 http-msg))))
|
|
|
|
(json-object-type 'plist)
|
|
|
|
(response (progn (goto-char header-size)
|
2023-03-22 18:34:18 -07:00
|
|
|
(condition-case nil
|
|
|
|
(json-read)
|
|
|
|
(json-readtable-error 'json-read-error)))))
|
2023-03-08 00:52:48 -08:00
|
|
|
(cond
|
2023-03-22 18:34:18 -07:00
|
|
|
((equal http-status "200")
|
|
|
|
(list :content
|
|
|
|
(string-trim
|
|
|
|
(map-nested-elt response '(:choices 0 :message :content)))
|
|
|
|
:status http-msg))
|
|
|
|
((plist-get response :error)
|
|
|
|
(let* ((error-plist (plist-get response :error))
|
|
|
|
(error-msg (plist-get error-plist :message))
|
|
|
|
(error-type (plist-get error-plist :type)))
|
|
|
|
(message "ChatGPT error: %s" error-msg)
|
|
|
|
(list :content nil :status (concat http-msg ": " error-type))))
|
|
|
|
((eq response 'json-read-error)
|
|
|
|
(message "ChatGPT error: Malformed JSON in response.")
|
|
|
|
(list :content nil :status (concat http-msg ": Malformed JSON in response.")))
|
|
|
|
(t (message "ChatGPT error: Could not parse HTTP response.")
|
|
|
|
(list :content nil :status (concat http-msg ": Could not parse HTTP response."))))
|
|
|
|
(message "ChatGPT error: Could not parse HTTP response.")
|
|
|
|
(list :content nil
|
|
|
|
:status (concat http-msg ": Could not parse HTTP response.")))))))
|
2023-03-08 00:52:48 -08:00
|
|
|
|
|
|
|
(provide 'gptel-curl)
|
|
|
|
;;; gptel-curl.el ends here
|