Past chats for students, shorter tutor, feeling buttons
Browse files- app.py +19 -1
- static/index.html +0 -0
- tutor.py +55 -3
app.py
CHANGED
|
@@ -421,6 +421,23 @@ def tutor_budget(student_id: str) -> dict:
|
|
| 421 |
return {"usedToday": round(used, 2), "cap": DAILY_CAP_USD, "left": round(max(0.0, DAILY_CAP_USD - used), 2)}
|
| 422 |
|
| 423 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
@router.post("/api/tutor/message")
|
| 425 |
def tutor_message(body: TutorMsg) -> dict:
|
| 426 |
if body.sessionId not in STORE.chats:
|
|
@@ -431,7 +448,8 @@ def tutor_message(body: TutorMsg) -> dict:
|
|
| 431 |
# ---------------------------------------------------------------- feedback
|
| 432 |
FEEDBACK_TAGS = {"wrong answer", "unclear", "too hard", "too easy", "typo", "off topic", "helpful", "great question", "other",
|
| 433 |
"confusing", "hard to find", "looks wrong", "too slow", "bug", "idea", "love it",
|
| 434 |
-
"in syllabus", "not taught yet", "not in syllabus", "just right"
|
|
|
|
| 435 |
|
| 436 |
|
| 437 |
class FeedbackIn(BaseModel):
|
|
|
|
| 421 |
return {"usedToday": round(used, 2), "cap": DAILY_CAP_USD, "left": round(max(0.0, DAILY_CAP_USD - used), 2)}
|
| 422 |
|
| 423 |
|
| 424 |
+
@router.get("/api/tutor/sessions/{student_id}")
|
| 425 |
+
def tutor_sessions(student_id: str, chapterId: str | None = None) -> list:
|
| 426 |
+
if student_id not in STORE.students:
|
| 427 |
+
raise HTTPException(404, "Pick your name first.")
|
| 428 |
+
return TUTOR.sessions_for(student_id, chapterId) if TUTOR_ON else []
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
@router.get("/api/tutor/session/{sid}")
|
| 432 |
+
def tutor_open(sid: str, studentId: str) -> dict:
|
| 433 |
+
if not TUTOR_ON:
|
| 434 |
+
raise HTTPException(503, "The tutor isn't switched on yet.")
|
| 435 |
+
try:
|
| 436 |
+
return TUTOR.open_session(sid, studentId)
|
| 437 |
+
except KeyError as e:
|
| 438 |
+
raise HTTPException(404, "That chat wasn't found.") from e
|
| 439 |
+
|
| 440 |
+
|
| 441 |
@router.post("/api/tutor/message")
|
| 442 |
def tutor_message(body: TutorMsg) -> dict:
|
| 443 |
if body.sessionId not in STORE.chats:
|
|
|
|
| 448 |
# ---------------------------------------------------------------- feedback
|
| 449 |
FEEDBACK_TAGS = {"wrong answer", "unclear", "too hard", "too easy", "typo", "off topic", "helpful", "great question", "other",
|
| 450 |
"confusing", "hard to find", "looks wrong", "too slow", "bug", "idea", "love it",
|
| 451 |
+
"in syllabus", "not taught yet", "not in syllabus", "just right",
|
| 452 |
+
"feeling lost", "feeling repetitive", "got insight", "gaining confidence"}
|
| 453 |
|
| 454 |
|
| 455 |
class FeedbackIn(BaseModel):
|
static/index.html
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tutor.py
CHANGED
|
@@ -121,7 +121,7 @@ STEPS = {
|
|
| 121 |
# Learning in parts ("chunks", syllabus/igcse/quizzes/CHUNK_MODEL.md). No turn limit in any of these.
|
| 122 |
"c_predict": ("Predict", "low", "If this is part 1, the opening takes two exchanges. First message: state the topic's big question as an everyday puzzle in one or two plain-English sentences, and ask the student for their own guess at the WHOLE answer; then stop and wait. Second message (after they reply, even 'I don't know'): react warmly in one sentence; give our short intuitive answer to the whole puzzle in plain words (what is going on, no definitions or technical terms), building on their guess where you can; then question that answer as a curious student would ('but how do we know…?', 'who says…?', 'why would…?'), and show that each doubt is one of the smaller questions from the route given above, numbered and in order, in your own plain words. Then say we will settle them one at a time, starting with number 1. If this is a later part: in one or two sentences, recall the big question, say which numbered smaller question we have just answered and what it gave us, and why this next one is needed. Then ask this part's driving question (below), in your own words, as ONE question for the student to try before any teaching. Accept any honest attempt, including 'I don't know', warmly in one or two sentences, then call finish_step and go straight on to the next step in the same reply. (On part 1, only call finish_step after the student has answered this driving question, not after their guess at the big question.)"),
|
| 123 |
"c_explain": ("Explain", "medium", "Teach ONLY this part's ideas (listed below), from first principles, starting from the driving question and what the student just said. One idea at a time, in this order: (a) what actually happens, in plain words, tied to something the student has seen; (b) the problem that creates, the reason we need a word or a tool for it ('to say exactly which way it bounces we need a fixed line to measure from'); (c) only then the term's name and meaning. For a living system, (b) is why the organism or system needs it. Use no technical term outside this part's new terms and words the student already knows. Keep each message short (under about 150 words) and end it with a quick question that checks they are following. If they say it doesn't make sense, rebuild from a simpler starting point and an everyday example; never just repeat. There is no limit on exchanges: take as many as they need. When they have followed every idea in this part, call finish_step."),
|
| 124 |
-
"c_check": ("Check", "low", "Make sure every idea in this part has landed. Ask ONE question at a time, each on an idea not yet answered right (listed below as 'still to check')
|
| 125 |
}
|
| 126 |
CHUNK_STEPS = ("c_predict", "c_explain", "c_check")
|
| 127 |
|
|
@@ -360,7 +360,10 @@ How you teach
|
|
| 360 |
- Big question first, intuitive answer next, smaller questions from questioning that answer. Every topic answers one big question or everyday puzzle (e.g. "how does a mirror let you see yourself?", "why does a straw look bent in water?"). Open a topic in this order: (1) state the puzzle in plain English; (2) ask the student for their own guess at the WHOLE answer and wait; (3) react warmly, then give our short, intuitive answer to the whole puzzle in plain words, building on their guess where you can (a story of what is going on, not definitions); (4) then question that answer the way a curious student would ("but how do we know light bounces like that?", "who says it comes back at the same angle?", "why would your eyes be fooled?"), and show that each of these doubts is one smaller question, numbered in order, so the smaller questions grow out of the answer rather than being announced as a list; (5) then start on smaller question 1. As you move along, say where you are on the route and why the next small question is needed for the big one.
|
| 361 |
- Phenomenon before jargon. First describe what actually happens, in plain words and with something the student has seen. Only when they have the picture, show the problem a technical word or tool solves ("to say exactly which way the light bounces we need something fixed to measure from"), and only then give its name and definition. Never introduce a term just because it comes next in the syllabus.
|
| 362 |
- When they are stuck, use the hint ladder: (1) name the idea, (2) name the first step, (3) show the first step, (4) a worked example with DIFFERENT numbers, then they redo the original. Never give the final answer first.
|
| 363 |
-
-
|
|
|
|
|
|
|
|
|
|
| 364 |
- When a wrong answer matches a listed misconception, name it kindly, show a quick case where the wrong belief gives an absurd result, then ask a fresh question on the same idea.
|
| 365 |
- Ground every explanation in the chapter text below (you may cite paragraph ids like [P5] for yourself, but explain in plain words). If something is outside this chapter, say so briefly and offer to come back to the chapter.
|
| 366 |
- Check every number with the calculate tool before saying an answer is right or wrong. Never do arithmetic in your head.
|
|
@@ -493,6 +496,12 @@ def step_note(sess: dict, st: dict) -> str:
|
|
| 493 |
done_before = [c for c in parts[:k - 1]]
|
| 494 |
if done_before:
|
| 495 |
extra += f"\nEarlier parts already learned this session: {', '.join((chunk_of(sess, c) or {}).get('title', c) for c in done_before)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
if st["name"] == "c_check":
|
| 497 |
left = still_to_check(sess, st["chunk"])
|
| 498 |
extra += "\nStill to check: " + (", ".join(left) if left else "none: call finish_step")
|
|
@@ -814,7 +823,8 @@ class Tutor:
|
|
| 814 |
"started": iso(now()), "updated": iso(now()), "messages": [], "transcript": [], "attempts": [],
|
| 815 |
"flags": [], "tools": [], "usage": {"input": 0, "output": 0, "cache_read": 0, "cache_write": 0},
|
| 816 |
"confidence": None, "studentMessages": 0, "effort": SESSION_EFFORT,
|
| 817 |
-
"scope": session_scope(CHAPTERS[chapter_id], topic, steps)
|
|
|
|
| 818 |
opener = {"learn": f"I'd like to learn: {topic['title'] if topic else ''}.", "ask": f"I have a question about {topic['title'] if topic else 'this topic'}.",
|
| 819 |
"fix": "Help me fix my quiz mistakes.", "review": "Let's do my review."}[kind]
|
| 820 |
sess["messages"] = [{"role": "user", "content": opener}]
|
|
@@ -827,6 +837,48 @@ class Tutor:
|
|
| 827 |
self.store.queue_write(f"chats/{sid}.json", sess)
|
| 828 |
return {**self._public(sess), "reply": reply, "meta": _meta(sess)}
|
| 829 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 830 |
def message(self, sid: str, text: str, confidence: str | None) -> dict:
|
| 831 |
sess = self.store.chats[sid]
|
| 832 |
if spent_today(self.store, sess["studentId"]) >= DAILY_CAP_USD:
|
|
|
|
| 121 |
# Learning in parts ("chunks", syllabus/igcse/quizzes/CHUNK_MODEL.md). No turn limit in any of these.
|
| 122 |
"c_predict": ("Predict", "low", "If this is part 1, the opening takes two exchanges. First message: state the topic's big question as an everyday puzzle in one or two plain-English sentences, and ask the student for their own guess at the WHOLE answer; then stop and wait. Second message (after they reply, even 'I don't know'): react warmly in one sentence; give our short intuitive answer to the whole puzzle in plain words (what is going on, no definitions or technical terms), building on their guess where you can; then question that answer as a curious student would ('but how do we know…?', 'who says…?', 'why would…?'), and show that each doubt is one of the smaller questions from the route given above, numbered and in order, in your own plain words. Then say we will settle them one at a time, starting with number 1. If this is a later part: in one or two sentences, recall the big question, say which numbered smaller question we have just answered and what it gave us, and why this next one is needed. Then ask this part's driving question (below), in your own words, as ONE question for the student to try before any teaching. Accept any honest attempt, including 'I don't know', warmly in one or two sentences, then call finish_step and go straight on to the next step in the same reply. (On part 1, only call finish_step after the student has answered this driving question, not after their guess at the big question.)"),
|
| 123 |
"c_explain": ("Explain", "medium", "Teach ONLY this part's ideas (listed below), from first principles, starting from the driving question and what the student just said. One idea at a time, in this order: (a) what actually happens, in plain words, tied to something the student has seen; (b) the problem that creates, the reason we need a word or a tool for it ('to say exactly which way it bounces we need a fixed line to measure from'); (c) only then the term's name and meaning. For a living system, (b) is why the organism or system needs it. Use no technical term outside this part's new terms and words the student already knows. Keep each message short (under about 150 words) and end it with a quick question that checks they are following. If they say it doesn't make sense, rebuild from a simpler starting point and an everyday example; never just repeat. There is no limit on exchanges: take as many as they need. When they have followed every idea in this part, call finish_step."),
|
| 124 |
+
"c_check": ("Check", "low", "Make sure every idea in this part has landed, in as few questions as possible (usually two to four). Ask ONE question at a time, each on an idea not yet answered right (listed below as 'still to check'); never ask about an idea already answered right. Combine two of this part's ideas in one question where you can. After each answer call record_attempt with the atom ids it tested (level 1 or 2) and give one line of feedback. If it was wrong, re-explain that idea a fresh way in two to four lines, then ask a new question on it. When every idea has been answered right, call finish_step."),
|
| 125 |
}
|
| 126 |
CHUNK_STEPS = ("c_predict", "c_explain", "c_check")
|
| 127 |
|
|
|
|
| 360 |
- Big question first, intuitive answer next, smaller questions from questioning that answer. Every topic answers one big question or everyday puzzle (e.g. "how does a mirror let you see yourself?", "why does a straw look bent in water?"). Open a topic in this order: (1) state the puzzle in plain English; (2) ask the student for their own guess at the WHOLE answer and wait; (3) react warmly, then give our short, intuitive answer to the whole puzzle in plain words, building on their guess where you can (a story of what is going on, not definitions); (4) then question that answer the way a curious student would ("but how do we know light bounces like that?", "who says it comes back at the same angle?", "why would your eyes be fooled?"), and show that each of these doubts is one smaller question, numbered in order, so the smaller questions grow out of the answer rather than being announced as a list; (5) then start on smaller question 1. As you move along, say where you are on the route and why the next small question is needed for the big one.
|
| 361 |
- Phenomenon before jargon. First describe what actually happens, in plain words and with something the student has seen. Only when they have the picture, show the problem a technical word or tool solves ("to say exactly which way the light bounces we need something fixed to measure from"), and only then give its name and definition. Never introduce a term just because it comes next in the syllabus.
|
| 362 |
- When they are stuck, use the hint ladder: (1) name the idea, (2) name the first step, (3) show the first step, (4) a worked example with DIFFERENT numbers, then they redo the original. Never give the final answer first.
|
| 363 |
+
- Asking why: if the question itself asked for a reason, a right answer needs a reason. Otherwise a right recall or one-step answer counts as right; do not hold back credit to demand a reason. Ask "why?" at most ONCE per question; if the reason is thin or missing, give the one-line reason yourself, count the answer, and move on.
|
| 364 |
+
- Stay on the route. Teach and test only the ideas listed for the current part. No side trips, extra traps, extensions, "one more thing" or bonus questions beyond those ideas, however interesting. If a side point is truly needed to follow the part, give it in one sentence and carry on.
|
| 365 |
+
- Read the student. Short or tired replies ("IDK", "Because.", "just tell me", one-word answers) or annoyance ("I already told you") mean stop probing: give the answer plainly in a few lines, check it with ONE easy question, and move on. If they show they already know something (they say so, or answer it fully), skip ahead to what is still left; never make them repeat it.
|
| 366 |
+
- Feeling buttons. A message starting [feeling lost] means the student pressed "Feeling lost": stop, say in one line where we are and what we are trying to find out, then rebuild the current idea from a simpler starting point with an everyday example, in short steps, and ask one easy question. A message starting [feeling repetitive] means "Feeling repetitive": briefly acknowledge it, do not re-ask anything already answered right, and move straight on to the next idea not yet covered (or finish the step if none is left).
|
| 367 |
- When a wrong answer matches a listed misconception, name it kindly, show a quick case where the wrong belief gives an absurd result, then ask a fresh question on the same idea.
|
| 368 |
- Ground every explanation in the chapter text below (you may cite paragraph ids like [P5] for yourself, but explain in plain words). If something is outside this chapter, say so briefly and offer to come back to the chapter.
|
| 369 |
- Check every number with the calculate tool before saying an answer is right or wrong. Never do arithmetic in your head.
|
|
|
|
| 496 |
done_before = [c for c in parts[:k - 1]]
|
| 497 |
if done_before:
|
| 498 |
extra += f"\nEarlier parts already learned this session: {', '.join((chunk_of(sess, c) or {}).get('title', c) for c in done_before)}"
|
| 499 |
+
prior = sess.get("prior")
|
| 500 |
+
if prior and k == 1 and st["name"] == "c_predict":
|
| 501 |
+
done = f" Parts already learnt: {', '.join(prior['learnt'])}." if prior.get("learnt") else ""
|
| 502 |
+
extra += (f"\nThis student has already started this topic in {prior['sessions']} earlier session(s), last on {prior['last']}.{done} "
|
| 503 |
+
"They have heard the big question and our intuitive answer. Do NOT ask for their guess at the big question again "
|
| 504 |
+
"and do NOT repeat the intuitive answer: treat this as a later part (recall the big question and where we are in one or two sentences).")
|
| 505 |
if st["name"] == "c_check":
|
| 506 |
left = still_to_check(sess, st["chunk"])
|
| 507 |
extra += "\nStill to check: " + (", ".join(left) if left else "none: call finish_step")
|
|
|
|
| 823 |
"started": iso(now()), "updated": iso(now()), "messages": [], "transcript": [], "attempts": [],
|
| 824 |
"flags": [], "tools": [], "usage": {"input": 0, "output": 0, "cache_read": 0, "cache_write": 0},
|
| 825 |
"confidence": None, "studentMessages": 0, "effort": SESSION_EFFORT,
|
| 826 |
+
"scope": session_scope(CHAPTERS[chapter_id], topic, steps),
|
| 827 |
+
"prior": self._prior_on_topic(student_id, chapter_id, topic_id) if kind == "learn" else None}
|
| 828 |
opener = {"learn": f"I'd like to learn: {topic['title'] if topic else ''}.", "ask": f"I have a question about {topic['title'] if topic else 'this topic'}.",
|
| 829 |
"fix": "Help me fix my quiz mistakes.", "review": "Let's do my review."}[kind]
|
| 830 |
sess["messages"] = [{"role": "user", "content": opener}]
|
|
|
|
| 837 |
self.store.queue_write(f"chats/{sid}.json", sess)
|
| 838 |
return {**self._public(sess), "reply": reply, "meta": _meta(sess)}
|
| 839 |
|
| 840 |
+
def _prior_on_topic(self, student_id: str, chapter_id: str, topic_id: str | None) -> dict | None:
|
| 841 |
+
"""What the student already did on this topic in earlier Learn sessions, so a new one doesn't start over."""
|
| 842 |
+
if not topic_id:
|
| 843 |
+
return None
|
| 844 |
+
earlier = [s for s in self.store.chats.values()
|
| 845 |
+
if s["studentId"] == student_id and s.get("chapterId") == chapter_id and s.get("topicId") == topic_id
|
| 846 |
+
and s.get("kind") == "learn" and sum(1 for m in s["transcript"] if m["role"] == "student") >= 2]
|
| 847 |
+
if not earlier:
|
| 848 |
+
return None
|
| 849 |
+
t = topic_of(chapter_id, topic_id) or {}
|
| 850 |
+
prog = self.store.progress.get(student_id, {})
|
| 851 |
+
learnt = [c["title"] for c in t.get("chunks", []) if chunk_status(prog, chapter_id, c["id"]) == "learned"]
|
| 852 |
+
return {"sessions": len(earlier), "last": max(s["updated"] for s in earlier)[:10], "learnt": learnt}
|
| 853 |
+
|
| 854 |
+
def sessions_for(self, student_id: str, chapter_id: str | None = None) -> list[dict]:
|
| 855 |
+
"""The student's own past chats (newest first) for the 'Your tutor chats' list."""
|
| 856 |
+
out = []
|
| 857 |
+
for s in self.store.chats.values():
|
| 858 |
+
if s["studentId"] != student_id or (chapter_id and s.get("chapterId") != chapter_id):
|
| 859 |
+
continue
|
| 860 |
+
n_student = sum(1 for m in s["transcript"] if m["role"] == "student")
|
| 861 |
+
if n_student < 2: # opened and left straight away: nothing to go back to
|
| 862 |
+
continue
|
| 863 |
+
t = topic_of(s["chapterId"], s.get("topicId")) if s.get("topicId") else None
|
| 864 |
+
cur = current_step(s)
|
| 865 |
+
ch = chunk_of(s, cur["chunk"]) if cur and cur.get("chunk") else None
|
| 866 |
+
prog = self.store.progress.get(student_id, {})
|
| 867 |
+
out.append({"id": s["id"], "chapterId": s["chapterId"], "topicId": s.get("topicId"), "kind": s["kind"],
|
| 868 |
+
"title": (t or {}).get("title") or s.get("goal", ""), "started": s["started"], "updated": s["updated"],
|
| 869 |
+
"messages": len(s["transcript"]), "complete": bool(s.get("complete")),
|
| 870 |
+
"atPart": ch.get("title") if ch else None, "atChunk": ch.get("id") if ch else None,
|
| 871 |
+
"atPartLearnt": bool(ch) and chunk_status(prog, s["chapterId"], ch["id"]) == "learned"})
|
| 872 |
+
return sorted(out, key=lambda x: x["updated"], reverse=True)
|
| 873 |
+
|
| 874 |
+
def open_session(self, sid: str, student_id: str) -> dict:
|
| 875 |
+
sess = self.store.chats.get(sid)
|
| 876 |
+
if not sess or sess["studentId"] != student_id:
|
| 877 |
+
raise KeyError(sid)
|
| 878 |
+
msgs = [{"who": "tutor" if m["role"] == "tutor" else "me", "text": m["text"], "conf": m.get("confidence")}
|
| 879 |
+
for m in sess["transcript"]]
|
| 880 |
+
return {**self._public(sess), "chapterId": sess["chapterId"], "topicId": sess.get("topicId"), "msgs": msgs}
|
| 881 |
+
|
| 882 |
def message(self, sid: str, text: str, confidence: str | None) -> dict:
|
| 883 |
sess = self.store.chats[sid]
|
| 884 |
if spent_today(self.store, sess["studentId"]) >= DAILY_CAP_USD:
|