Skip to content

Commit 7a56d45

Browse files
Yahiewibrichet
andauthored
Implement debug mode (#14)
* implement debug mode flag * Update jupyter_ai_tutor/handlers.py Co-authored-by: Nicolas Brichet <32258950+brichet@users.noreply.github.com> * Update jupyter_ai_tutor/handlers.py Co-authored-by: Nicolas Brichet <32258950+brichet@users.noreply.github.com> * Update jupyter_ai_tutor/handlers.py Co-authored-by: Nicolas Brichet <32258950+brichet@users.noreply.github.com> * Update jupyter_ai_tutor/handlers.py Co-authored-by: Nicolas Brichet <32258950+brichet@users.noreply.github.com> * Update jupyter_ai_tutor/handlers.py Co-authored-by: Nicolas Brichet <32258950+brichet@users.noreply.github.com> * Refactor debug code --------- Co-authored-by: Nicolas Brichet <32258950+brichet@users.noreply.github.com>
1 parent 4f22f75 commit 7a56d45

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,6 @@ dmypy.json
129129

130130
# Yarn cache
131131
.yarn/
132+
133+
# Ignore secrets in '.env' (API keys)
134+
.env

jupyter_ai_tutor/app.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
22

33
from jupyter_server.extension.application import ExtensionApp
4-
from traitlets import Unicode
4+
from traitlets import Bool, Unicode
55

66
from .handlers import ExplainHandler
77

@@ -20,12 +20,19 @@ class JupyterAITutorApp(ExtensionApp):
2020
),
2121
).tag(config=True)
2222

23+
debug = Bool(
24+
default_value=False,
25+
help="Whether to log prompts and replies to /tmp for debugging.",
26+
).tag(config=True)
27+
2328
def initialize_settings(self):
2429
path = Path(self.agent_md) if self.agent_md else _DEFAULT_AGENT_MD
2530
self.settings["jupyter_ai_tutor.system_prompt"] = path.read_text(
2631
encoding="utf-8"
2732
).strip()
28-
self.log.info("jupyter_ai_tutor: loaded system prompt from %s", path)
33+
self.settings["jupyter_ai_tutor.debug"] = self.debug
34+
self.log.info("jupyter_ai_tutor: loaded system prompt from %s (debug=%s)", path, self.debug)
35+
2936

3037
def initialize_handlers(self):
3138
self.handlers = [

jupyter_ai_tutor/handlers.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import json
2+
import tempfile
3+
from datetime import datetime
4+
from pathlib import Path
25

36
import tornado
47
from jupyter_server.base.handlers import APIHandler
@@ -36,6 +39,28 @@ async def post(self):
3639
self.set_header("X-Accel-Buffering", "no")
3740

3841
system_prompt = self.settings.get("jupyter_ai_tutor.system_prompt", "")
42+
debug_mode = self.settings.get("jupyter_ai_tutor.debug", False)
43+
prompt_file = None
44+
answer_file = None
45+
accumulated_response = ""
46+
47+
if debug_mode:
48+
debug_dir = Path(tempfile.gettempdir()) / "jupyter-ai-tutor"
49+
try:
50+
debug_dir.mkdir(parents=True, exist_ok=True)
51+
except Exception as e:
52+
self.log.error(f"Failed to create debug directory {debug_dir}: {e}")
53+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
54+
prompt_file = debug_dir / f"{timestamp}_jupyter_tutor_prompt.txt"
55+
answer_file = debug_dir / f"{timestamp}_jupyter_tutor_answer.txt"
56+
try:
57+
with prompt_file.open("w", encoding="utf-8") as f:
58+
f.write("=== SYSTEM PROMPT ===\n\n")
59+
f.write(system_prompt)
60+
f.write("\n\n=== USER MESSAGE ===\n\n")
61+
f.write(message_body)
62+
except Exception as e:
63+
self.log.error(f"Failed to write tutor debug prompt: {e}")
3964

4065
try:
4166
from jupyter_ai_jupyternaut.jupyternaut.chat_models import ChatLiteLLM
@@ -63,10 +88,27 @@ async def post(self):
6388
)
6489
)
6590
if text:
91+
if debug_mode:
92+
accumulated_response += text
6693
self.write(f"data: {json.dumps({'text': text})}\n\n")
6794
self.flush()
6895

96+
if debug_mode and answer_file:
97+
try:
98+
with answer_file.open("w", encoding="utf-8") as f:
99+
f.write(accumulated_response)
100+
f.write("\n")
101+
except Exception as e:
102+
self.log.error(f"Failed to write tutor debug answer: {e}")
103+
69104
except StreamClosedError:
105+
if debug_mode and answer_file and accumulated_response:
106+
try:
107+
with answer_file.open("w", encoding="utf-8") as f:
108+
f.write(accumulated_response)
109+
f.write("\n")
110+
except Exception as e:
111+
pass
70112
return # Client disconnected; stop streaming
71113
except Exception as e:
72114
self.log.exception("Error during tutor LLM call")

0 commit comments

Comments
 (0)