karthink / gptel

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

How to colorize `gptel` response persistent #73

Closed tshu-w closed 12 months ago

tshu-w commented 1 year ago

I'm using gptel to refine my writing. Sometimes I modify and resend responses to OpenAI to get new replies. I want to add a face to the response text so that I can realize clear text properties when resending the modified response. Otherwise, gptel will divide it into multiple prompts. I tried the following code but it didn't work. Any suggestions would be greatly appreciated.

  (add-to-list 'gptel-response-filter-functions
               (defun gptel--colorize-response (content buffer)
                 (put-text-property 0 (length content) 'face 'font-lock-warning-face content)
                 content))
karthink commented 1 year ago

If I understand correctly, you want to classify a response message from ChatGPT as a user prompt.

For the purpose of (repeatedly) rewriting text, you could just use the rewrite feature built into gptel. Select the text to be rewritten, then gptel-menu -> r -> R. This should work no matter the source of the text selected (i.e. ChatGPT or you). Is there some reason you don't want to do it this way?

tshu-w commented 1 year ago

If I understand correctly, you want to classify a response message from ChatGPT as a user prompt.

Yes, this is what I need, or gptel consider all content as 'user' with an option, regardless of its property.

I am not using the default rewrite because I have many different directives that implement "rewriting", such as translation, paraphrasing, abbreviation, etc.

PalaceChan commented 1 year ago

(maybe misunderstanding but if you universal prefix arg before the rewrite can't you edit the prompt to select on of these many rewrite directives?)

tshu-w commented 1 year ago

maybe misunderstanding but if you universal prefix arg before the rewrite can't you edit the prompt to select on of these many rewrite directives?

I didn't realize before that the rewrite directive can also be modified, but it's not possible to select "gptel-directives" and must be manually entered, which makes it difficult to switch directives frequently.

Screenshot 2023-06-12 at 21 52 47
karthink commented 1 year ago

Yes, the UI for selecting rewrite directives needs to be better. I'll try to improve it soon.

On Mon, Jun 12, 2023, 6:55 AM Tianshu Wang @.***> wrote:

maybe misunderstanding but if you universal prefix arg before the rewrite can't you edit the prompt to select on of these many rewrite directives?

I didn't realize before that the rewrite directive can also be modified, but it's not possible to select "gptel-directives" and must be manually entered, which makes it difficult to switch directives frequently. [image: Screenshot 2023-06-12 at 21 52 47] https://user-images.githubusercontent.com/13161779/245165265-d763f69c-fd19-4321-8807-3b7195b85a82.png

— Reply to this email directly, view it on GitHub https://github.com/karthink/gptel/issues/73#issuecomment-1587390655, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACBVOLBRK7NGVIJUIU5JAXDXK4NUHANCNFSM6AAAAAAZC3VJCU . You are receiving this because you commented.Message ID: @.***>

karthink commented 1 year ago

but it's not possible to select "gptel-directives" and must be manually entered, which makes it difficult to switch directives frequently.

I took a look at this, and realized the history of rewrite directives is already saved. You can access all previously used directives with M-n and M-p in the minibuffer prompt. If you use savehist-mode, it is persisted across Emacs sessions.

tshu-w commented 1 year ago

Putting aside the original intention, I still want to know if there is a way to set up the face of the reply, so as to better distinguish between the original content and the reply.

karthink commented 12 months ago

You could try something like this:

(defun gptel--color-response ()
  (let ((prop))
    (save-excursion
      (setq prop (text-property-search-forward 'gptel 'response t))
      (put-text-property (prop-match-beginning prop)
                         (prop-match-end prop)
                         'font-lock-face
                         'warning))))

(add-hook 'gptel-post-response-hook #'gptel--color-response t)

Replace the warning face with whatever you need.