unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Modify any clipboard by using AI and Large Language Models with Emacs
@ 2024-12-07  3:24 Jean Louis
  0 siblings, 0 replies; only message in thread
From: Jean Louis @ 2024-12-07  3:24 UTC (permalink / raw)
  To: Help GNU Emacs; +Cc: Sacha Chua

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



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-12-07  3:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-07  3:24 Modify any clipboard by using AI and Large Language Models with Emacs Jean Louis

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).