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.
;;; 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:
@ -194,6 +202,21 @@ ASSISTANT:
\"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