-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.lisp
More file actions
executable file
·60 lines (50 loc) · 2.5 KB
/
debug.lisp
File metadata and controls
executable file
·60 lines (50 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
;;; -*- Lisp -*-
;;; Add a LLM debugger hook for Common Lisp. This intercepts errors
;;; just before they get handed to the debugger, and sends the error
;;; and the backtrace to the LLM for analysis.
;;; SLIME and SLY both install their own debugger hooks, and smash the
;;; hook on each interaction, so simply setting *DEBUGGER-HOOK* is not
;;; going to work. We provide a macro, CALL-WITH-LLM-DEBUGGER-HOOK,
;;; that binds the hook around a thunk.
;;; This is basically a proof of concept. It would be better to
;;; integrate this into SLIME or SLY.
(in-package "GEMINI")
(defparameter +general-debug-system-instructions+
(str:join
#\newline
`("You are a Common Lisp expert with many years experience."
"You are particularly adept at debugging Common Lisp errors."
"You will be given an error message and a backtrace."
"You may use tools to introspect the Common Lisp environment to aid in your debugging.")))
(defun debug-system-instruction ()
(content
:parts (list (part +general-debug-system-instructions+))))
(defun debug-prompt (error-message backtrace)
(format nil "An error has been signalled by Common Lisp.
**Error Message**: ~a~%
**Backtrace**: ~a~%
Please provide debugging assistance, attempt to diagnose the error, and suggest a fix."
error-message backtrace))
(defun invoke-llm-debugger (&key error-message backtrace)
(let ((*system-instruction* (debug-system-instruction))
(*include-thoughts* t))
(invoke-gemini (debug-prompt error-message backtrace))))
(defun call-with-llm-debugger-hook (thunk)
"Creates a hook that can be used as a debugger hook for LLM-based debugging."
(let* ((old-debugger-hook *debugger-hook*)
(*debugger-hook*
(lambda (condition &optional prior-debugger-hook)
(let ((error-message (format nil "~a" condition))
(backtrace (with-output-to-string (s)
(trivial-backtrace:print-backtrace condition :output s :verbose t))))
(let ((response (invoke-llm-debugger
:error-message error-message
:backtrace backtrace)))
(format t "~&LLM Debugger Response: ~a~%" response)
(when old-debugger-hook
(funcall old-debugger-hook condition prior-debugger-hook)))))))
(funcall thunk)))
(defmacro with-llm-debugger (&body body)
"Execute BODY with the LLM debugger hook installed."
`(CALL-WITH-LLM-DEBUGGER-HOOK
(LAMBDA () ,@body)))