From 51c36a256dc9f3db7d7416749f61696eecd84853 Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Tue, 17 Sep 2024 21:28:02 +0200 Subject: [PATCH] added a mechanism to easily add definitions to the ellama context --- .../module-ellama-capture-definitions.el | 42 +++++++++++++++++++ emacs/module/module-ellama-capture.el | 3 ++ emacs/module/module-llm.el | 12 +++--- 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 emacs/module/module-ellama-capture-definitions.el diff --git a/emacs/module/module-ellama-capture-definitions.el b/emacs/module/module-ellama-capture-definitions.el new file mode 100644 index 0000000..55d82fb --- /dev/null +++ b/emacs/module/module-ellama-capture-definitions.el @@ -0,0 +1,42 @@ +;;; module-ellama-capture-definition.el --- Ellama capture definitions +;;; +;;; Commentary: +;;; +;;; This file provides functions to interactively add definitions to +;;; the Ellama context. This can be useful, for example, when dealing +;;; with custom data formats, such as denote notes, where the training +;;; data is unlikely to contain information about the format. +;;; +;;; Code: + +(require 'ellama) + +(defun ellama-capture-add-definition () + "Interactively choose and add a definition to the Ellama context." + (interactive) + (let* ((funcs (apropos-internal "^ellama-capture-add-.*-definition$")) + (choices (mapcar (lambda (func) + (string-match "ellama-capture-add-\\(.*\\)-definition" (symbol-name func)) + (match-string 1 (symbol-name func))) + funcs)) + (choice (completing-read "Choose definition to add: " choices nil t))) + (if (fboundp (intern (format "ellama-capture-add-%s-definition" choice))) + (funcall (intern (format "ellama-capture-add-%s-definition" choice))) + (message "No function found for %s" choice)))) + +(defun ellama-capture-add-filetag-definition () + "Add the filetag definition to the Ellama context." + (interactive) + (let ((filetag-definition + "Filetags: Used for note categorization and organization. +Syntax: '# +filetags: :tag1:tag2:tag3:' +- Each tag is enclosed in colons +- Multiple tags can be specified +- Placed at the beginning of a note file +Example: '# +filetags: :work:project:urgent:' +Purpose: Enable efficient filtering and searching of notes.")) + (ellama-context-add-text filetag-definition) + (message "Filetag definition added to Ellama context"))) + +(provide 'module-ellama-capture-definitions) +;;; module-ellama-capture-definitions.el ends here. diff --git a/emacs/module/module-ellama-capture.el b/emacs/module/module-ellama-capture.el index 1b043e6..f6a0a07 100644 --- a/emacs/module/module-ellama-capture.el +++ b/emacs/module/module-ellama-capture.el @@ -26,6 +26,7 @@ (local-set-key (kbd "C-c C-k") 'ellama-capture-abort) (local-set-key (kbd "C-c f") 'ellama-context-add-file) (local-set-key (kbd "C-c i") 'ellama-context-add-info-node) + (local-set-key (kbd "C-c d") 'ellama-capture-add-definition) (setq header-line-format (substitute-command-keys "C-c C-c: send, C-c C-k: abort, C-c f: add file, C-c i: add info node"))) @@ -51,6 +52,8 @@ (quit-window t) (message "Ellama input aborted")) +(require 'module-ellama-capture-definitions) + (global-set-key (kbd "C-x A SPC") #'ellama-capture) (provide 'module-ellama-capture) diff --git a/emacs/module/module-llm.el b/emacs/module/module-llm.el index 7bdbc63..440fc39 100644 --- a/emacs/module/module-llm.el +++ b/emacs/module/module-llm.el @@ -26,17 +26,19 @@ (with-eval-after-load 'ellama (ellama-setup-keymap)) -;; FIXME: ellama-context-add-file does not accept arguments. may -;; explain why this doesn't work as expected. (defun ellama-context-add-marked-files () "Add all marked files in Dired to Ellama context." (interactive) - (let ((marked-files (dired-get-marked-files))) + (let ((marked-files (dired-get-marked-files)) + (added-count 0)) (if (null marked-files) (message "No files marked") (dolist (file marked-files) - (ignore-errors (ellama-context-add-file file))) - (message "Added %d file(s) to Ellama context" (length marked-files))))) + (let ((default-directory (file-name-directory file))) + (let ((element (ellama-context-element-file :name file))) + (ellama-context-element-add element) + (cl-incf added-count)))) + (message "Added %d file(s) to Ellama context" added-count)))) (with-eval-after-load 'dired (define-key dired-mode-map (kbd "C-c e") #'ellama-context-add-marked-files))