karthink / gptel

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

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

Open aindilis opened 7 months ago

aindilis commented 7 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 7 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 6 months ago

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

aindilis commented 3 weeks ago

Hi @karthink,

Thank you for showing me how to do this!

It works well, however, when I ran it, I had problems with it not putting text annotations in the correct spot.

However, I am reopening as well because I realized a few more desirable features:

1) watermark using enriched text text properties (and other optional/configurable methods) 2) have the option for users and/or fact-checking programs like FEVER to annotate suspected or known hallucinations, or other LLM related metadata 3) allow the text properties to store or link to other metadata/properties/parameters, such as the LLM that was used, the prompt text, temperature, etc.

If you wish I can attempt to implement this in Elisp with Claude's help.

Best, Andrew