Spaces:
Running
Running
Update backend/agents/hint_agent.py
Browse files- backend/agents/hint_agent.py +44 -26
backend/agents/hint_agent.py
CHANGED
|
@@ -19,46 +19,64 @@ class HintAgent:
|
|
| 19 |
self.model = genai.GenerativeModel('gemini-1.5-flash')
|
| 20 |
|
| 21 |
def generate_hint(self,
|
|
|
|
| 22 |
level_context: Dict[str, Any],
|
| 23 |
-
|
| 24 |
-
error_message: Optional[str] = None) -> str:
|
| 25 |
"""
|
| 26 |
Generates a hint based on the level and user's current attempt.
|
| 27 |
"""
|
| 28 |
if not hasattr(self, 'model'):
|
| 29 |
return "SYSTEM ERROR: API Key missing for Hint Agent."
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
try:
|
| 52 |
response = self.model.generate_content(prompt)
|
| 53 |
return response.text.strip()
|
| 54 |
except Exception as e:
|
| 55 |
-
# Fallback Hint
|
| 56 |
-
return "Try breaking the problem down into smaller steps. Check
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
from dotenv import load_dotenv
|
| 60 |
load_dotenv()
|
| 61 |
agent = HintAgent()
|
| 62 |
-
context = {"title": "Mars Rover", "
|
| 63 |
-
|
| 64 |
-
print(agent.generate_hint(context,
|
|
|
|
| 19 |
self.model = genai.GenerativeModel('gemini-1.5-flash')
|
| 20 |
|
| 21 |
def generate_hint(self,
|
| 22 |
+
mode: str,
|
| 23 |
level_context: Dict[str, Any],
|
| 24 |
+
user_state: Dict[str, Any]) -> str:
|
|
|
|
| 25 |
"""
|
| 26 |
Generates a hint based on the level and user's current attempt.
|
| 27 |
"""
|
| 28 |
if not hasattr(self, 'model'):
|
| 29 |
return "SYSTEM ERROR: API Key missing for Hint Agent."
|
| 30 |
|
| 31 |
+
if mode == "maze":
|
| 32 |
+
prompt = f"""
|
| 33 |
+
You are a coding tutor helping a student navigate a maze in a STEM app. The student is asking for a hint.
|
| 34 |
+
|
| 35 |
+
LEVEL CONTEXT:
|
| 36 |
+
Title: {level_context.get('title')}
|
| 37 |
+
Goal: {level_context.get('goal_description') or level_context.get('problem')}
|
| 38 |
+
|
| 39 |
+
USER'S CURRENT STATE:
|
| 40 |
+
Current Position (Row, Col): {user_state.get('current_position')}
|
| 41 |
+
Target Objective they need to reach next: {user_state.get('next_objective')}
|
| 42 |
+
|
| 43 |
+
TASK:
|
| 44 |
+
Provide a helpful, encouraging hint.
|
| 45 |
+
Tell them what they should look for next or what they might have missed (e.g. "Make sure you grab the Key before heading to the Exit! Look around the top right area.").
|
| 46 |
+
Keep it under 2 sentences. DO NOT give them exact turn-by-turn answers.
|
| 47 |
+
"""
|
| 48 |
+
else:
|
| 49 |
+
prompt = f"""
|
| 50 |
+
You are a coding tutor. The student is stuck on a Blockly coding challenge.
|
| 51 |
+
|
| 52 |
+
LEVEL CONTEXT:
|
| 53 |
+
Title: {level_context.get('title')}
|
| 54 |
+
Goal: {level_context.get('goal_description') or level_context.get('problem')}
|
| 55 |
+
|
| 56 |
+
USER'S CURRENT CODE:
|
| 57 |
+
{user_state.get('user_code')}
|
| 58 |
+
|
| 59 |
+
ERROR MESSAGE (if any):
|
| 60 |
+
{user_state.get('error')}
|
| 61 |
+
|
| 62 |
+
TASK:
|
| 63 |
+
Provide a helpful, encouraging hint.
|
| 64 |
+
DO NOT give the full solution immediately unless they are very stuck.
|
| 65 |
+
If there is a syntax error or logical flaw, point it out gently.
|
| 66 |
+
Keep it under 2 sentences.
|
| 67 |
+
"""
|
| 68 |
|
| 69 |
try:
|
| 70 |
response = self.model.generate_content(prompt)
|
| 71 |
return response.text.strip()
|
| 72 |
except Exception as e:
|
| 73 |
+
# Fallback Hint
|
| 74 |
+
return "Try breaking the problem down into smaller steps. Check your logic again!"
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
from dotenv import load_dotenv
|
| 78 |
load_dotenv()
|
| 79 |
agent = HintAgent()
|
| 80 |
+
context = {"title": "Mars Rover", "problem": "Move forward 3 times"}
|
| 81 |
+
state = {"user_code": "move_forward\nturn_left", "error": "Did not reach the goal"}
|
| 82 |
+
print(agent.generate_hint("blockly", context, state))
|