1

I have been using this cool Emacs package called gptel.

However, it is annoying for me how lengthy the ChatGPT answers look on the screen.

When I want to improve readability, I keep executing the fill-paragraph command. I would like to have this action performed automatically for the appropriate buffer.

It is worth mentioning that the package creates a separate buffer called *ChatGPT* displaying just the questions and the answers.

I tried enabling auto-fill-mode on this buffer. But, it did not work out. Here is my config file.

How can I achieve my goal? Maybe adding a tailor-made hook?

5
  • 1
    Sorry this might not be the most appropriate place to ask. But I wanted to install the package as well. I fetched it with melpa using package-install. But then the instructions on the github page get weird.. I've created this ~/.authinfo file and put the credentials of ChatGPT there. I'm unsure if I did this right.. I also couldn't run M-x gptel-send. Since you've done it already, could you help me figure this out? Would be lovely :) Jul 28, 2023 at 12:49
  • 1
    Hi @MySQLInjection, you. can "hard code" the api-key. Look at my solution below. You can set the value for the GPT api key. Using authinfo is the correct technique concerning safety. But, if you just want to try it out, hard coding the key in the init file is fine. Jul 28, 2023 at 14:36
  • 1
    Ok I kinda got it working now. However, I get this ChatGPT error: (HTTP/2 429) You exceeded your current quota, please check your plan and billing details. You probably didn't have this, but if you know how I can troubleshoot this, let me know. Thanks for the help so far :) Jul 28, 2023 at 15:51
  • 1
    Try re-starting Emacs. And try checking your settings in open AI website. Maybe you are using more than your plan allows. Jul 29, 2023 at 0:50
  • 1
    I opened an issue on github. The owner of the repo already hinted to me, that I might not be able to use it since may because OpenAI disallowed it with all free plans. So probably I can't use the package. I'll wait for his response though. It's a bummer nevertheless. Thanks for providing the code snippet and helping me out. I appreciate it. :) Jul 29, 2023 at 7:54

2 Answers 2

1

It looks like gptel has a hook called gptel-post-response-hook, so you could do something like this:

(defun clean-gpt-buffer ()
  (fill-region (point-min) (point-max)))

(add-hook gptel-post-response-hook 'clean-gpt-buffer)

Do note that this is very much the shotgun approach and will use fill paragraph ON THE WHOLE BUFFER, so if you are using gptel in org-mode like in the demo videos I saw in the readme, this would cut long titles in two, etc.

Note that this is untested because I do not use this package.

2
  • Thanks, @trevoke. But, I am having a problem: End of file during parsing: /Users/pedro/.emacs.d/init.el To ensure normal operation, you should investigate and remove the cause of the error in your initialization file. Start Emacs with the ‘--debug-init’ option to view a complete error backtrace. Disable showing Disable logging Any idea on what could be the problem? I tried adding a quote to 'gptel-post-response-hook. But, it did not do the trick Jul 28, 2023 at 2:18
  • 1
    I missed a closing-parens. Fixed it in the answer. It looks like you got something that works for yourself anyway :)
    – Trevoke
    Jul 28, 2023 at 2:33
0

@trevoke answer helped me. But, I only managed to achieve the expected result with:

(defun clean-gpt-buffer ()
  (with-current-buffer "*ChatGPT*"
    (fill-paragraph)))

(add-hook 'gptel-post-response-hook 'clean-gpt-buffer)

Considering that I like use-package declarative approach, this is my code now:

;;; Make chatGPT buffer better to read
(defun clean-gpt-buffer ()
  (with-current-buffer "*ChatGPT*"
    (fill-paragraph)))

;;; Use chatGPT inside Emacs
(use-package gptel
 :hook ((gptel-post-response . clean-gpt-buffer)) 
 :config
 (setq gptel-api-key "omitted"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.