From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Modify any clipboard by using AI and Large Language Models with Emacs Date: Sat, 07 Dec 2024 06:24:02 +0300 Message-ID: Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17821"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Sacha Chua To: Help GNU Emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat Dec 07 04:24:52 2024 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tJlR5-0004TH-2f for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 07 Dec 2024 04:24:51 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tJlQT-0007xi-MZ; Fri, 06 Dec 2024 22:24:13 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tJlQQ-0007xQ-FW for help-gnu-emacs@gnu.org; Fri, 06 Dec 2024 22:24:10 -0500 Original-Received: from stw1.rcdrun.com ([217.170.207.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tJlQO-0005Pm-Iw for help-gnu-emacs@gnu.org; Fri, 06 Dec 2024 22:24:10 -0500 Original-Received: from localhost ([::ffff:41.75.190.82]) (AUTH: PLAIN admin, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 0000000000081FFA.000000006753BFD4.003C0272; Fri, 06 Dec 2024 20:24:04 -0700 Received-SPF: pass client-ip=217.170.207.13; envelope-from=support1@rcdrun.com; helo=stw1.rcdrun.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:148644 Archived-At: 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:
#!/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
### The function `rcd-ai-clipboard-modify` Your function `rcd-ai-clipboard-modify` is here:
(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))))

Your function `rcd-ai` can look like following, and you may need to
rename it. You may download appropriate **llamafile** from:
 and run it.

(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 "" "\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)))))
### 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:
key F2 /home/data1/protected/bin/rcd/rcd-ai-clipboard.sh
## 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