3v324v23 commited on
Commit
d507933
·
1 Parent(s): c622774

Condense prompt, add implicit sympathy instructions, and update tests

Browse files
Files changed (2) hide show
  1. backend/main.py +11 -14
  2. backend/test_sentiment.py +23 -29
backend/main.py CHANGED
@@ -128,24 +128,21 @@ def run_flow_b(message: str, api_key: str, history: Optional[List[ChatMessage]]
128
  )
129
 
130
  custom_system = (
131
- "Socratic tutor: guide with clear, substantial hints (no tiny nudges) to solve faster. "
132
- "Confidence is not mastery—continue Socratic hints unless they are close. "
133
- "Only when close to the solution, give the final answer & ask: 'Do you want to learn something else?'"
134
  )
135
 
136
  tone_instruction = (
137
- "JSON format: {\"s\": \"confusion|frustration|confused_but_engaged|confused_and_frustrated|starting_to_get_bored|confident_and_engaged|neutral\", \"r\": \"string\"}\n"
 
138
  "Rules:\n"
139
- "- No robotic templates (e.g. 'I understand', 'it can be frustrating').\n"
140
- "- NEVER use the phrase 'if you' anywhere in your response (e.g. do not say 'if you think', 'if you were', etc.). Instead, frame instructions or scenarios directly (e.g., say 'think about', 'imagine', 'when looking at', or 'sometimes').\n"
141
- "- Only ask one question at a time to avoid overwhelming the user.\n"
142
- "- Close to answer: give final answer directly, ask: 'Do you want to learn something else?'\n"
143
- "- Not close (including confident_and_engaged): Socratic guidance (substantial hint + question).\n"
144
- "Replies by state:\n"
145
- " * confusion: hint + question.\n"
146
- " * frustration: simplify step + question.\n"
147
- " * starting_to_get_bored: puzzle/analogy + question.\n"
148
- " * confident_and_engaged / neutral: guide with hint + question."
149
  )
150
 
151
  messages = [SystemMessage(content=f"{custom_system}\n\n{tone_instruction}")]
 
128
  )
129
 
130
  custom_system = (
131
+ "Socratic tutor: guide with clear, substantial hints. Continue unless close. "
132
+ "If close: give answer & ask: 'Do you want to learn something else?'"
 
133
  )
134
 
135
  tone_instruction = (
136
+ "JSON: {\"s\":\"sentiment\",\"r\":\"reply\"}\n"
137
+ "s values: confusion|frustration|confused_but_engaged|confused_and_frustrated|starting_to_get_bored|confident_and_engaged|neutral\n"
138
  "Rules:\n"
139
+ "- Sympathize with s implicitly (tone/style); never name or mention the sentiment/emotion itself.\n"
140
+ "- NEVER use 'if you' (use direct phrasing: 'think about', 'imagine').\n"
141
+ "- Ask 1 question max.\n"
142
+ "Responses:\n"
143
+ "- frustration: simplify + question.\n"
144
+ "- starting_to_get_bored: puzzle/analogy + question.\n"
145
+ "- other: hint + question."
 
 
 
146
  )
147
 
148
  messages = [SystemMessage(content=f"{custom_system}\n\n{tone_instruction}")]
backend/test_sentiment.py CHANGED
@@ -1,40 +1,34 @@
1
  import os
2
  import sys
 
3
 
4
  # Add current directory to path for imports
5
  sys.path.append(os.path.dirname(os.path.abspath(__file__)))
6
 
7
- from main import map_distilroberta_emotions
8
 
9
- def test_mapping():
10
- print("Testing DistilRoBERTa emotion mapping logic...")
11
 
12
- # 1. Frustration tests
13
- anger_test = [{"label": "anger", "score": 0.8}, {"label": "neutral", "score": 0.2}]
14
- assert map_distilroberta_emotions(anger_test) == "frustration", f"Expected frustration, got {map_distilroberta_emotions(anger_test)}"
15
 
16
- disgust_test = [{"label": "disgust", "score": 0.6}]
17
- assert map_distilroberta_emotions(disgust_test) == "frustration", f"Expected frustration, got {map_distilroberta_emotions(disgust_test)}"
18
-
19
- # 2. Confusion tests
20
- sadness_test = [{"label": "sadness", "score": 0.9}]
21
- assert map_distilroberta_emotions(sadness_test) == "confusion", f"Expected confusion, got {map_distilroberta_emotions(sadness_test)}"
22
-
23
- surprise_test = [{"label": "surprise", "score": 0.55}]
24
- assert map_distilroberta_emotions(surprise_test) == "confusion", f"Expected confusion, got {map_distilroberta_emotions(surprise_test)}"
25
-
26
- fear_test = [{"label": "fear", "score": 0.7}]
27
- assert map_distilroberta_emotions(fear_test) == "confusion", f"Expected confusion, got {map_distilroberta_emotions(fear_test)}"
28
-
29
- # 3. Confidence tests
30
- joy_test = [{"label": "joy", "score": 0.95}]
31
- assert map_distilroberta_emotions(joy_test) == "confidence", f"Expected confidence, got {map_distilroberta_emotions(joy_test)}"
32
-
33
- # 4. Boredom tests
34
- neutral_test = [{"label": "neutral", "score": 0.9}]
35
- assert map_distilroberta_emotions(neutral_test) == "boredom", f"Expected boredom, got {map_distilroberta_emotions(neutral_test)}"
36
-
37
- print("All DistilRoBERTa mapping assertions passed successfully!")
38
 
39
  if __name__ == "__main__":
40
- test_mapping()
 
1
  import os
2
  import sys
3
+ from unittest.mock import MagicMock, patch
4
 
5
  # Add current directory to path for imports
6
  sys.path.append(os.path.dirname(os.path.abspath(__file__)))
7
 
8
+ from main import run_flow_b
9
 
10
+ def test_run_flow_b_parsing():
11
+ print("Testing run_flow_b parsing and extraction logic...")
12
 
13
+ mock_response = MagicMock()
14
+ mock_response.content = '{"s": "frustration", "r": "Let\'s simplify this. What do you get when you add the numbers first?"}'
 
15
 
16
+ with patch("main.ChatGoogleGenerativeAI") as MockLLM:
17
+ mock_llm_instance = MagicMock()
18
+ mock_llm_instance.invoke.return_value = mock_response
19
+ MockLLM.return_value = mock_llm_instance
20
+
21
+ state, reply, context, est_in, est_out = run_flow_b(
22
+ message="I am stuck on this step, it is too hard!",
23
+ api_key="fake-api-key"
24
+ )
25
+
26
+ assert state == "frustration", f"Expected frustration, got {state}"
27
+ assert "simplify" in reply, f"Expected reply to contain simplification, got {reply}"
28
+ assert est_in > 0
29
+ assert est_out > 0
30
+
31
+ print("Mocked run_flow_b test passed!")
 
 
 
 
 
 
32
 
33
  if __name__ == "__main__":
34
+ test_run_flow_b_parsing()