From: Jean Louis <bugs@gnu.support>
To: Help GNU Emacs <help-gnu-emacs@gnu.org>
Cc: Sacha Chua <sacha@sachachua.com>
Subject: Modify any clipboard by using AI and Large Language Models with Emacs
Date: Sat, 07 Dec 2024 06:24:02 +0300 [thread overview]
Message-ID: <f9848babec92861ce93a4e42862a805a.support1@rcdrun.com> (raw)
Here is useful method to modify any clipboard by using Window Manager
shortcut, bringing clipboard to Emacs, modifying it by using LLM or
Large Language Model and popular artificial intelligence, then
returning back to the initial window and inserting the clipboard.
# GNU Emacs: Modify any clipboard by using AI and Large Language Models
This method enables users to modify clipboard content from any
application running under a window manager on GNU/Linux systems. When
a clipboard change occurs, the data is sent to GNU Emacs, which
processes it using the built-in AI and large language models
(LLMs). After processing, the script returns focus to the original
application and inserts the modified clipboard content.
https://gnu.support/gnu-emacs/GNU-Emacs-Modify-any-clipboard-by-using-AI-and-Large-Language-Models-92627.html
## Setup
### The file `rcd-ai-clipboard-modify.sh` ###
The file `rcd-ai-clipboard-modify.sh` has to be placed in the
executable $PATH, and you have to have `xdotool` installed with the
Emacs running as server:
Here is the file:
<pre>
#!/usr/bin/bash
PREV_FOCUS=$(xdotool getwindowfocus)
xdotool key --clearmodifiers ctrl+a
sleep 0.5
xdotool key --clearmodifiers ctrl+c
emacsclient -e "(rcd-ai-clipboard-modify)"
xdotool windowactivate --sync $PREV_FOCUS
xdotool key --clearmodifiers ctrl+a
xdotool key --clearmodifiers ctrl+v
</pre>
### The function `rcd-ai-clipboard-modify`
Your function `rcd-ai-clipboard-modify` is here:
<pre>
(defun rcd-ai-clipboard-modify ()
"Modify clipboard by using AI."
(interactive)
(select-frame-set-input-focus (selected-frame))
(with-temp-buffer
(clipboard-yank)
(mark-whole-buffer)
(rcd-ai)
(gui--set-last-clipboard-selection (buffer-string))))
<pre>
Your function `rcd-ai` can look like following, and you may need to
rename it. You may download appropriate **llamafile** from:
<https://github.com/Mozilla-Ocho/llamafile/> and run it.
<pre>
(defun rcd-llamafile (prompt memory model)
"Return answer by using llamafile."
(let ((model)
(buffer (let ((url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/json")
("Authorization" . "Bearer no-key")))
(url-request-data
(json-encode
`((model . "LLaMA_CPP")
(messages . [
((role . "system")
(content . "You are my AI assistant."))
((role . "user")
(content . ,prompt))
])))))
(url-retrieve-synchronously
"http://localhost:8080/v1/chat/completions"))))
(when buffer
(let* ((json-response (setq my-json (rcd-parse-http-json-string (buffer-to-string buffer))))
(content (cdr (assoc 'content (cdr (assoc 'message (aref (cdr (assq 'choices my-json)) 0))))))
(content (string-replace "</s>" "\n" content)))
content))))
(defun rcd-parse-http-json-string (string)
"Parses a JSON string preceded by HTTP headers.
Returns the parsed JSON object."
(let ((json-start-index (string-match "\{" string)))
(when json-start-index
(json-read-from-string (substring string json-start-index)))))
</pre>
### Window Manager settings
In this keys we place following in the `~/.icewm/key` file as I am
using IceWM, your settings for corresponding Window Manager should be
same:
<pre>
key F2 /home/data1/protected/bin/rcd/rcd-ai-clipboard.sh
</pre>
## Video demonstration and explanation
1. Let us say you are using Mousepad editor, or Xournal++, so you may
mark the text with Ctrl-A usually, and then invoke the Window
Manager shortcut. In this example it is F2.
2. By pressing F2 the clipboard is then recorded. Your Emacs must also
synchronize clipboard by its settings.
3. Function asks user for LLM prompt, you could modify the prompt.
4. The clipboard is recorded by Emacs.
5. The program `xdotool` transfers focus to previous program, and
inserts clipboard.
GNU Emacs: Modify any clipboard by using AI and Large Language Models
This method enables users to modify clipboard content from any application running under a window manager on GNU/Linux systems. When a clipboard change occurs, the data is sent to GNU Emacs, which processes it using the built-in AI and large language models (LLMs). After processing, the script returns focus to the original application and inserts the modified clipboard content.
https://gnu.support/gnu-emacs/GNU-Emacs-Modify-any-clipboard-by-using-AI-and-Large-Language-Models-92627.html
Jean Louis
reply other threads:[~2024-12-07 3:24 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f9848babec92861ce93a4e42862a805a.support1@rcdrun.com \
--to=bugs@gnu.support \
--cc=help-gnu-emacs@gnu.org \
--cc=sacha@sachachua.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).