From f32fbd670b60795494fbca15526fd53a630a9eec Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Mon, 12 Aug 2024 12:34:36 -0400 Subject: [PATCH] Add optional instruction input for custom outlines Modified aimenu function to accept an optional instruction argument, allowing users to provide specific instructions for generating the outline. Updated related logic to handle the new argument and adjusted the system message accordingly. --- emacs/.emacs.d/packages/aimenu/aimenu.el | 75 ++++++++++++++++-------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/emacs/.emacs.d/packages/aimenu/aimenu.el b/emacs/.emacs.d/packages/aimenu/aimenu.el index f58eb4b..498d0b4 100644 --- a/emacs/.emacs.d/packages/aimenu/aimenu.el +++ b/emacs/.emacs.d/packages/aimenu/aimenu.el @@ -24,8 +24,6 @@ ;; This package provides an imenu-like outline interface for any buffer, powered by an LLM. ;;; Code: - - (require 'json) (require 'gptel) @@ -104,19 +102,29 @@ OUTLINE is the parsed JSON response." (funcall-interactively #'goto-line line-number))) ;;;###autoload -(defun aimenu () - "Generate an outline of the current buffer using an LLM." - (interactive) - (let* ((buffer-contents (aimenu-get-buffer-with-line-numbers (current-buffer))) - (prompt-hash (aimenu-hash-string buffer-contents)) +(defun aimenu (arg) + "Generate an outline of the current buffer using an LLM. + +If ARG is non-nil, prompt for an instruction for generating the outline." + (interactive "P") + (let* ((instruction (if arg + (read-string "Instruction: ") + nil)) + (buffer-contents (aimenu-get-buffer-with-line-numbers (current-buffer))) + (prompt (if instruction + (concat buffer-contents "\n\nInstruction: " instruction) + buffer-contents)) + (prompt-hash (aimenu-hash-string prompt)) (cached-response (gethash prompt-hash aimenu-outline-cache)) (gptel-backend aimenu-gptel-backend) (gptel-model aimenu-gptel-model)) (if cached-response (aimenu-handle-outline-response cached-response) (gptel-request - buffer-contents - :system "Create an outline of the buffer. Return a JSON object where the keys are the outline headers and the values are the line numbers that correspond to each outline header. Reply only in valid JSON without any code fences or additional text. + prompt + :system "Create an outline of the buffer. The user may provide an instruction on how to generate the outline. If no instruction is provided, generate the outline based on the overall structure of the buffer. + +Return a JSON object where the keys are the outline headers and the values are the line numbers that correspond to each outline header. Reply only in valid JSON without any code fences or additional text. Here are some examples of your task: @@ -136,12 +144,12 @@ USER: ASSISTANT: { - \"Outline header 1\": 1, - \"Subheader 1.1\": 3, - \"Subheader 1.2\": 5, - \"Outline header 2\": 7, - \"Subheader 2.1\": 9, - \"Outline header 3\": 11 +\"Outline header 1\": 1, +\"Subheader 1.1\": 3, +\"Subheader 1.2\": 5, +\"Outline header 2\": 7, +\"Subheader 2.1\": 9, +\"Outline header 3\": 11 } USER: @@ -156,10 +164,10 @@ USER: ASSISTANT: { - \"Main Header\": 1, - \"Subheader 1\": 3, - \"Subheader 2\": 5, - \"Sub-subheader 2.1\": 7 +\"Main Header\": 1, +\"Subheader 1\": 3, +\"Subheader 2\": 5, +\"Sub-subheader 2.1\": 7 } USER: @@ -173,8 +181,8 @@ USER: ASSISTANT: { - \"main_function\": 1, - \"helper_function\": 5 +\"main_function\": 1, +\"helper_function\": 5 } USER: @@ -190,10 +198,25 @@ USER: ASSISTANT: { - \"main_section\": 1, - \"sub_section1\": 4, - \"sub_section2\": 6, - \"sub_sub_section\": 8 +\"main_section\": 1, +\"sub_section1\": 4, +\"sub_section2\": 6, +\"sub_sub_section\": 8 +} + +USER: +1: SELECT * FROM users; +2: -- This query fetches all columns from the users table +3: INSERT INTO users (name, email) +4: VALUES ('John Doe', 'john@example.com'); +5: -- This query adds a new user to the users table + +Instruction: lines with comments + +ASSISTANT: +{ +\"-- This query fetches all columns from the users table\": 2, +\"-- This query adds a new user to the users table\": 5} }" :callback (lambda (response info) (if response @@ -204,7 +227,7 @@ ASSISTANT: response-json (error "Invalid response format: %s" stripped-response))) (buffer (plist-get info :buffer))) - (puthash (aimenu-hash-string buffer-contents) + (puthash (aimenu-hash-string prompt) outline aimenu-outline-cache) (with-current-buffer buffer