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.
This commit is contained in:
Jeremy Dormitzer 2024-08-12 12:34:36 -04:00
parent 73897be017
commit f32fbd670b

View File

@ -24,8 +24,6 @@
;; This package provides an imenu-like outline interface for any buffer, powered by an LLM. ;; This package provides an imenu-like outline interface for any buffer, powered by an LLM.
;;; Code: ;;; Code:
(require 'json) (require 'json)
(require 'gptel) (require 'gptel)
@ -104,19 +102,29 @@ OUTLINE is the parsed JSON response."
(funcall-interactively #'goto-line line-number))) (funcall-interactively #'goto-line line-number)))
;;;###autoload ;;;###autoload
(defun aimenu () (defun aimenu (arg)
"Generate an outline of the current buffer using an LLM." "Generate an outline of the current buffer using an LLM.
(interactive)
(let* ((buffer-contents (aimenu-get-buffer-with-line-numbers (current-buffer))) If ARG is non-nil, prompt for an instruction for generating the outline."
(prompt-hash (aimenu-hash-string buffer-contents)) (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)) (cached-response (gethash prompt-hash aimenu-outline-cache))
(gptel-backend aimenu-gptel-backend) (gptel-backend aimenu-gptel-backend)
(gptel-model aimenu-gptel-model)) (gptel-model aimenu-gptel-model))
(if cached-response (if cached-response
(aimenu-handle-outline-response cached-response) (aimenu-handle-outline-response cached-response)
(gptel-request (gptel-request
buffer-contents prompt
: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. :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: Here are some examples of your task:
@ -136,12 +144,12 @@ USER:
ASSISTANT: ASSISTANT:
{ {
\"Outline header 1\": 1, \"Outline header 1\": 1,
\"Subheader 1.1\": 3, \"Subheader 1.1\": 3,
\"Subheader 1.2\": 5, \"Subheader 1.2\": 5,
\"Outline header 2\": 7, \"Outline header 2\": 7,
\"Subheader 2.1\": 9, \"Subheader 2.1\": 9,
\"Outline header 3\": 11 \"Outline header 3\": 11
} }
USER: USER:
@ -156,10 +164,10 @@ USER:
ASSISTANT: ASSISTANT:
{ {
\"Main Header\": 1, \"Main Header\": 1,
\"Subheader 1\": 3, \"Subheader 1\": 3,
\"Subheader 2\": 5, \"Subheader 2\": 5,
\"Sub-subheader 2.1\": 7 \"Sub-subheader 2.1\": 7
} }
USER: USER:
@ -173,8 +181,8 @@ USER:
ASSISTANT: ASSISTANT:
{ {
\"main_function\": 1, \"main_function\": 1,
\"helper_function\": 5 \"helper_function\": 5
} }
USER: USER:
@ -190,10 +198,25 @@ USER:
ASSISTANT: ASSISTANT:
{ {
\"main_section\": 1, \"main_section\": 1,
\"sub_section1\": 4, \"sub_section1\": 4,
\"sub_section2\": 6, \"sub_section2\": 6,
\"sub_sub_section\": 8 \"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) :callback (lambda (response info)
(if response (if response
@ -204,7 +227,7 @@ ASSISTANT:
response-json response-json
(error "Invalid response format: %s" stripped-response))) (error "Invalid response format: %s" stripped-response)))
(buffer (plist-get info :buffer))) (buffer (plist-get info :buffer)))
(puthash (aimenu-hash-string buffer-contents) (puthash (aimenu-hash-string prompt)
outline outline
aimenu-outline-cache) aimenu-outline-cache)
(with-current-buffer buffer (with-current-buffer buffer