github-actions[bot] commited on
Commit
4211c75
·
1 Parent(s): 24ac5c8

deploy: backend from 72e77ac

Browse files
Files changed (2) hide show
  1. main.py +1 -29
  2. tests/test_model_reply_passthrough.py +86 -0
main.py CHANGED
@@ -1422,33 +1422,6 @@ def _ensure_ai_available() -> None:
1422
  # ─── Chat Helpers (Claude via InferenceClient) ────────────────
1423
 
1424
 
1425
- def _strip_repetition(text: str, min_chunk: int = 40) -> str:
1426
- """Remove repeated blocks from model output (a common issue with smaller LLMs)."""
1427
- lines = text.split("\n")
1428
- seen_blocks: list[str] = []
1429
- result_lines: list[str] = []
1430
- i = 0
1431
- while i < len(lines):
1432
- # Try to match a block of 2-4 lines that repeats
1433
- matched = False
1434
- for blen in (4, 3, 2):
1435
- if i + blen > len(lines):
1436
- continue
1437
- block = "\n".join(lines[i : i + blen]).strip()
1438
- if len(block) < min_chunk:
1439
- continue
1440
- if block in seen_blocks:
1441
- # Skip this repeated block
1442
- i += blen
1443
- matched = True
1444
- break
1445
- seen_blocks.append(block)
1446
- if not matched:
1447
- result_lines.append(lines[i])
1448
- i += 1
1449
- return "\n".join(result_lines).strip()
1450
-
1451
-
1452
  def call_hf_chat(
1453
  messages: List[Dict[str, str]],
1454
  *,
@@ -1464,8 +1437,7 @@ def call_hf_chat(
1464
  max_new_tokens=max_tokens,
1465
  timeout_sec=timeout,
1466
  )
1467
- text = get_inference_client().generate_from_messages(req)
1468
- return _strip_repetition(text)
1469
 
1470
 
1471
  def call_hf_chat_stream(
 
1422
  # ─── Chat Helpers (Claude via InferenceClient) ────────────────
1423
 
1424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1425
  def call_hf_chat(
1426
  messages: List[Dict[str, str]],
1427
  *,
 
1437
  max_new_tokens=max_tokens,
1438
  timeout_sec=timeout,
1439
  )
1440
+ return get_inference_client().generate_from_messages(req).strip()
 
1441
 
1442
 
1443
  def call_hf_chat_stream(
tests/test_model_reply_passthrough.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Model replies reach their callers whole.
2
+
3
+ call_hf_chat used to pass every non-streaming reply through a repetition stripper written for small models that
4
+ looped. It took ordinary Markdown paragraphs for repeats and deleted them: a tutor reply came back as its last
5
+ paragraph, a first-turn chat came back empty, and pretty-printed quiz JSON stopped parsing.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import os
11
+ import sys
12
+
13
+ import pytest
14
+
15
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
16
+
17
+ import main
18
+
19
+ TUTOR_PARAGRAPHS = [
20
+ "Great question! Finding the domain of a rational function comes down to one idea.",
21
+ "**Step 1:** Look at the denominator, because division by zero is undefined for every real number.",
22
+ "**Step 2:** Set the denominator equal to zero and solve, so you know which x-values to exclude.",
23
+ "What value of x makes x - 2 equal to zero?",
24
+ "Once you find that value, think about what it means for the domain. Give it a try and tell me what you get!",
25
+ ]
26
+ TUTOR_REPLY = "\n\n".join(TUTOR_PARAGRAPHS)
27
+ QUIZ_REPLY = json.dumps(
28
+ [
29
+ {
30
+ "questionType": "multiple_choice",
31
+ "question": f"What is the domain of f(x) = 1/(x - {number})?",
32
+ "options": [f"A) x != {number}", "B) all real numbers", "C) x > 0", "D) x < 0"],
33
+ "correctAnswer": "A",
34
+ "bloomLevel": "understand",
35
+ "difficulty": "medium",
36
+ "topic": "Domain and Range of Functions",
37
+ "points": 1,
38
+ "explanation": f"Division by zero is undefined, so x cannot equal {number}.",
39
+ }
40
+ for number in range(1, 11)
41
+ ],
42
+ indent=2,
43
+ )
44
+
45
+
46
+ class ReplyingInferenceClient:
47
+ """The inference client's contract: a request in, the model's reply text out."""
48
+
49
+ def __init__(self, reply: str) -> None:
50
+ self.reply = reply
51
+
52
+ def generate_from_messages(self, _request: object) -> str:
53
+ return self.reply
54
+
55
+
56
+ def serve_model_reply(monkeypatch: pytest.MonkeyPatch, reply: str) -> None:
57
+ client = ReplyingInferenceClient(reply)
58
+ monkeypatch.setattr(main, "get_inference_client", lambda: client)
59
+
60
+
61
+ @pytest.mark.parametrize("reply", [TUTOR_REPLY, QUIZ_REPLY], ids=["markdown-tutor-reply", "pretty-printed-quiz"])
62
+ def test_call_hf_chat_returns_the_reply_unchanged(monkeypatch: pytest.MonkeyPatch, reply: str) -> None:
63
+ serve_model_reply(monkeypatch, reply)
64
+
65
+ assert main.call_hf_chat([{"role": "user", "content": "Help me"}], task_type="chat") == reply
66
+
67
+
68
+ @pytest.mark.asyncio
69
+ async def test_a_first_turn_chat_reply_reaches_the_student_whole(monkeypatch: pytest.MonkeyPatch) -> None:
70
+ monkeypatch.setattr(main, "HAS_MEMORY_SERVICE", False)
71
+ monkeypatch.setattr(main, "HAS_FIREBASE_ADMIN", False)
72
+ monkeypatch.setattr(main, "retrieve_curriculum_context", lambda **_: [])
73
+ monkeypatch.setattr(main, "retrieve_chat_context", lambda _message: "")
74
+
75
+ async def route_intent(_message: str) -> dict[str, object]:
76
+ return {"choice": "conceptual_confusion", "confidence": 0.91}
77
+
78
+ monkeypatch.setattr(main, "route_student_intent", route_intent)
79
+ serve_model_reply(monkeypatch, TUTOR_REPLY)
80
+
81
+ response = await main.chat_tutor(
82
+ main.ChatRequest(message="How do I find the domain of f(x) = (x + 1)/(x - 2)?")
83
+ )
84
+
85
+ for paragraph in TUTOR_PARAGRAPHS:
86
+ assert paragraph in response.response