karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.03k stars 111 forks source link

gptel-request wrong type argument #319

Open ppanko opened 1 month ago

ppanko commented 1 month ago

Hello, thank you for the wonderful package. I am running into an error attempting to run a function that uses gptel-request (using Windows 11 OS). Here is my config setup:

  (use-package gptel
    :config
    ;; Set the default model to use
    (setq-default gptel-model "gpt-3.5-turbo")
    ;; set API key
    (setq gptel-api-key "____________"))

  (defun gptel-rewrite-and-replace (bounds &optional directive)
    (interactive
     (list
      (cond
       ((use-region-p) (cons (region-beginning) (region-end)))
       ((derived-mode-p 'text-mode)
        (list (bounds-of-thing-at-point 'sentence)))
       (t (cons (line-beginning-position) (line-end-position))))
      (and current-prefix-arg
           (read-string "ChatGPT Directive: "
                        "You are a prose editor. Rewrite my prompt more professionally."))))
    (gptel-request
        (buffer-substring-no-properties (car bounds) (cdr bounds)) ;the prompt
      :system (or directive "You are a prose editor. Rewrite my prompt more professionally.")
      :buffer (current-buffer)
      :context (cons (set-marker (make-marker) (car bounds))
                     (set-marker (make-marker) (cdr bounds)))
      :callback
      (lambda (response info)
        (if (not response)
            (message "ChatGPT response failed with: %s" (plist-get info :status))
          (let* ((bounds (plist-get info :context))
                 (beg (car bounds))
                 (end (cdr bounds))
                 (buf (plist-get info :buffer)))
            (with-current-buffer buf
              (save-excursion
                (goto-char beg)
                (kill-region beg end)
                (insert response)
                (set-marker beg nil)
                (set-marker end nil)
                (message "Rewrote line. Original line saved to kill-ring."))))))))

The gptel-rewrite-and-replace function, which was borrowed from example 2 on the gptel wiki page, throws the following error:

error in process sentinel: gptel-curl--parse-response: Wrong type argument: stringp, nil
error in process sentinel: Wrong type argument: stringp, nil

gptel-send works just fine as does sending requests through curl directly. I can send the debug/log output if needed. Thanks in advance!

karthink commented 1 month ago

I can't reproduce this. Could you run M-x toggle-debug-on-error, produce the error and supply the backtrace here?

ppanko commented 1 month ago
Debugger entered--Lisp error: (wrong-type-argument json-value-p "HTTP/2 200 \ndate: Tue, 21 May 2024 20:09:13 GMT\ncontent-type: application/json; charset=utf-8\n...")

json-serialize("HTTP/2 200 \ndate: Tue, 21 May 2024 20:09:13 GMT\ncontent-type: application/json; charset=utf-8\n..." :null-object nil :false-object :json-false)

gptel-curl--log-response(#<buffer *gptel-curl*<3>> (:token "04a8d912aa1d4a4ecf45416b86ee5e39" :parser #f(compiled-function (backend response info) #<bytecode 0x1974b7d>) :callback (lambda (response info) (if (not response) (message "ChatGPT response failed with: %s" (plist-get info :status)) (let* ((bounds (plist-get info :context)) (beg (car bounds)) (end (cdr bounds)) (buf (plist-get info :buffer))) (save-current-buffer (set-buffer buf) (save-excursion (goto-char beg) (kill-region beg end) (insert response) (set-marker beg nil) (set-marker end nil) (message "Rewrote line. Original line saved to kill-ring."))))) :transformer nil :data (:model "gpt-3.5-turbo" :messages [(:role "system" :content "You are a prose editor. Rewrite my prompt more pro...") (:role "user" :content ";; elisp: fizzbuzz")] :stream :json-false :temperature 1.0) :buffer #<buffer *scratch*> :position #<marker at 19 in *scratch*> :context (#<marker at 1 in *scratch*> . #<marker at 19 in *scratch*>)))

gptel-curl--sentinel(#<process gptel-curl> "finished\n")

P.S. sorry -- mis-clicked on "close" initially

karthink commented 1 month ago

This error is different from the one in your previous post. It looks like there are two different errors. Please turn off logging with (setq gptel-log-level nil) and produce a backtrace for the original error.

ppanko commented 1 month ago

Correct -- here's the error from the OP:

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  string-match("\\(?:[ \11\n\15]+\\)\\'" nil nil)
  gptel-curl--parse-response((:token "dcab6e4d3b53057500fb4191cf8ddfb3" :parser #f(compiled-function (backend response info) #<bytecode 0x1974b7d>) :callback (lambda (response info) (if (not response) (message "ChatGPT response failed with: %s" (plist-get info :status)) (let* ((bounds (plist-get info :context)) (beg (car bounds)) (end (cdr bounds)) (buf (plist-get info :buffer))) (save-current-buffer (set-buffer buf) (save-excursion (goto-char beg) (kill-region beg end) (insert response) (set-marker beg nil) (set-marker end nil) (message "Rewrote line. Original line saved to kill-ring.")))))) :transformer nil :data (:model "gpt-3.5-turbo" :messages [(:role "system" :content "You are a prose editor. Rewrite my prompt more pro...") (:role "user" :content ";; elisp: fizzbuzz")] :stream :json-false :temperature 1.0) :buffer #<buffer *scratch*> :position #<marker at 19 in *scratch*> :context (#<marker at 1 in *scratch*> . #<marker at 19 in *scratch*>)))
  gptel-curl--sentinel(#<process gptel-curl> "finished\n")
karthink commented 1 month ago

That's quite odd. I'd ask you to turn on logging but that's independently broken too. I don't see anything wrong with that json-serialize call.

ppanko commented 1 month ago

Thanks for checking. Clearing the config file didn't affect either error -- gptel-send works but gptel-rewrite-and-replace does not.

Any chance either issue is related to this block? https://github.com/karthink/gptel/blob/3bce2caa5dc773d1b1fce53e8453d2e1ce534b8b/gptel-curl.el#L388-L395

karthink commented 1 month ago

No, I think it's related to string-trim further below. To analyze this I'd have to fix the logging bug first.