karthink / gptel

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

Watermarking / inline annotation of LLM generated content vs prompt content #295

Closed aindilis closed 1 month ago

aindilis commented 2 months ago

Hi,

I was wondering whether some feature such as the following could be implemented:

1) Inserting perhaps XML or Org-mode tags indicating the beginning and ending of LLM generated content.

E.g.

Please list all apartment finding sites.

Certainly! Here is a list of apartment sites:

craigslist.org
...

Would become

<llm-prompt>Please list all apartment finding sites.</llm-prompt>

<llm-content model="claude-3-opus">
Certainly! Here is a list of apartment sites:

craigslist.org
...
</llm-content>

2) Making such context sensitive, so it would use the appropriate tags for the mode, e.g. Org-mode, or comment them out:

(in hypothetical file: apartments.el)

;; <llm-prompt>Please list all apartment finding sites.</llm-prompt>

;; <llm-content model="claude-3-opus">
;; Certainly! Here is a list of apartment sites:
;; 
;; craigslist.org
;; ...
;; </llm-content>

Thanks!

Andrew

karthink commented 2 months ago

You can use gptel-prompt-prefix-alist and gptel-response-prefix-alist to add text before a response/prompt. This is intended for use in gptel chat buffers. See #103 for an example.

A more general solution is to use gptel-pre-response-hook and gptel-post-response-functions to add this stuff. Here is an example:

(defun gptel-tag-response (beg end)
  (save-excursion
    (goto-char end)
    ;; ending tag
    (insert "</llm-content>\n\n<llm-prompt>")
    (goto-char beg)
    (skip-chars-backward " \n\r\t")
    ;; end of prompt tag
    (insert "</llm-prompt>")
    (skip-chars-forward " \n\r\t")
    ;; beginning of response tag
    (insert (format "<llm-content model=\"%s\">\n"
                    gptel-model))))

(add-hook 'gptel-post-response-functions #'gptel-tag-response)

Modify as needed.

karthink commented 1 month ago

Closing this now, please reopen if the above solution doesn't work.