From ed63a1ea0fcada37295241c9afae30c222fbb377 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 25 Jul 2024 00:10:48 -0400 Subject: [PATCH] Add gptel-org function Implemented a gptel-org function to allow interacting with GPTel within an Org-Mode buffer, setting the appropriate system message for context. Updated init-org.el to load Emacs-Lisp and Python Babel languages and disabled org-confirm-babel-evaluate for smoother code execution. --- emacs/.emacs.d/config/init-ai.el | 27 +++++++++++++++++++++++++++ emacs/.emacs.d/config/init-org.el | 5 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/emacs/.emacs.d/config/init-ai.el b/emacs/.emacs.d/config/init-ai.el index 9950f72..5e6edfa 100644 --- a/emacs/.emacs.d/config/init-ai.el +++ b/emacs/.emacs.d/config/init-ai.el @@ -30,11 +30,38 @@ (setq gptel-backend (car backend) gptel-model (cadr backend)))) (add-to-list 'gptel-directives '(shell-command . "You are a command line helper. Generate shell commands that do what is requested, without any additional description or explanation. Reply in plain text with no Markdown or other syntax. Reply with the command only.")) + (defun gptel-org (buffer &optional _ initial interactivep) + (interactive (let* ((backend (default-value 'gptel-backend)) + (backend-name + (format "*%s*" (gptel-backend-name backend)))) + (list (read-buffer "Create or choose gptel buffer: " + (generate-new-buffer-name backend-name) nil ; DEFAULT and REQUIRE-MATCH + (lambda (b) ; PREDICATE + (let ((buffer (get-buffer (or (car-safe b) b)))) + (and + (with-current-buffer buffer (eq major-mode 'org-mode)) + (buffer-local-value 'gptel-mode buffer))))) + (condition-case nil + (gptel--get-api-key + (gptel-backend-key backend)) + ((error user-error) + (setq gptel-api-key + (read-passwd + (format "%s API key: " backend-name))))) + (and (use-region-p) + (buffer-substring (region-beginning) + (region-end))) + t))) + (let ((gptel-default-mode #'org-mode)) + (gptel buffer nil initial interactivep)) + (with-current-buffer buffer + (setq-local gptel--system-message "You are a large language model living in an Emacs Org-Mode buffer and a helpful assistant. You may evaluate code when necessary by outputting an Org-Mode source block. I will execute the source block and display the results in the buffer. Respond concisely."))) :general ("C-c RET" #'gptel-send "C-c C-" #'gptel-menu) (ai-map "g" #'gptel + "o" #'gptel-org "s" #'gptel-send "m" #'gptel-menu "b" #'gptel-select-backend diff --git a/emacs/.emacs.d/config/init-org.el b/emacs/.emacs.d/config/init-org.el index b90b9ad..2bd4d45 100644 --- a/emacs/.emacs.d/config/init-org.el +++ b/emacs/.emacs.d/config/init-org.el @@ -37,7 +37,9 @@ DATE should be in the format \"YYYY-MM-DD\". If called interactively, default to (org-find-daily-note (format-time-string "%Y-%m-%d"))) (org-babel-do-load-languages 'org-babel-load-languages - '((plantuml . t))) + '((emacs-lisp . t) + (plantuml . t) + (python . t))) :custom (org-modules '(ol-doi ol-w3m @@ -88,6 +90,7 @@ DATE should be in the format \"YYYY-MM-DD\". If called interactively, default to ("\\.x?html?\\'" . default))) (org-plantuml-exec-mode 'jar) (org-plantuml-jar-path (expand-file-name "~/plantuml/plantuml.jar")) + (org-confirm-babel-evaluate nil) :general (leader-map "oa" #'org-agenda) (leader-map "oc" #'org-capture)