Benny-Tang commited on
Commit
257d15f
·
verified ·
1 Parent(s): 1df25bb

Update agents.py

Browse files
Files changed (1) hide show
  1. agents.py +30 -51
agents.py CHANGED
@@ -2,74 +2,53 @@ import os
2
  import random
3
  from zhipuai import ZhipuAI
4
 
5
- # Initialize ZhipuAI
6
- client = ZhipuAI(api_key=os.getenv("ZHIPUAI_API_KEY", "demo-key"))
7
 
8
  class AnalyzerAgent:
9
- """Grades answers and provides analysis."""
10
-
11
- def analyze(self, answers, questions):
12
- correct = 0
13
- total = len(questions)
14
- for q in questions:
15
- qid = str(q.get("id"))
16
- if qid in answers and answers[qid] == q.get("answer"):
17
- correct += 1
18
- return f"Score: {correct}/{total}"
19
-
20
- def get_detailed_feedback(self, answers, questions):
21
- feedback = []
22
- for q in questions:
23
- qid = str(q.get("id"))
24
- if qid not in answers:
25
- feedback.append(f"Q{qid}: Not attempted")
26
- elif answers[qid] == q.get("answer"):
27
- feedback.append(f"Q{qid}: Correct ✅")
28
  else:
29
- feedback.append(f"Q{qid}: Incorrect ❌ (Correct: {q.get('answer')})")
30
- return "\n".join(feedback)
31
-
32
 
33
  class CoachAgent:
34
- """Provides motivational coaching based on analysis."""
35
-
36
- def provide_guidance(self, analysis_text):
37
- prompt = (
38
- "You are a supportive SPM exam coach. "
39
- "Given the analysis of a student's answers, "
40
- "provide encouragement, study tips, and focus areas.\n\n"
41
- f"Analysis:\n{analysis_text}"
42
- )
43
- resp = client.chat.completions.create(
44
  model="glm-4",
45
  messages=[{"role": "user", "content": prompt}]
46
  )
47
  return resp.choices[0].message["content"]
48
 
49
-
50
  class PredictiveAgent:
51
- """Generates AI-predicted practice questions."""
 
 
52
 
53
- def generate_predicted_questions(self, subject, count=5):
54
  prompt = (
55
- f"Generate {count} multiple-choice practice questions for SPM {subject}. "
56
- "Each question should include: text, four choices (A-D), and correct answer."
57
  )
58
- resp = client.chat.completions.create(
59
  model="glm-4",
60
  messages=[{"role": "user", "content": prompt}]
61
  )
62
- # Parse output into simple mock questions (demo version)
63
- questions = []
64
- for i in range(count):
65
- questions.append({
66
- "id": 900000 + i,
67
- "text": f"Practice predicted question {i+1} on {subject}",
68
- "choices": ["A", "B", "C", "D"],
69
- "answer": random.choice(["A", "B", "C", "D"]),
70
- "source": "predicted"
71
- })
72
- return questions
73
 
74
 
75
 
 
2
  import random
3
  from zhipuai import ZhipuAI
4
 
5
+ # Load API key
6
+ ZHIPU_API_KEY = os.getenv("zhipuai_api_key")
7
 
8
  class AnalyzerAgent:
9
+ """Analyzes student answers against scheme."""
10
+ def analyze(self, student_answers, correct_answers):
11
+ score = 0
12
+ analysis = []
13
+ for i, (ans, correct) in enumerate(zip(student_answers, correct_answers)):
14
+ if ans == correct:
15
+ score += 1
16
+ analysis.append(f"Q{i+1}: ✅ Correct")
 
 
 
 
 
 
 
 
 
 
 
17
  else:
18
+ analysis.append(f"Q{i+1}: ❌ Wrong (Correct: {correct})")
19
+ return score, analysis
 
20
 
21
  class CoachAgent:
22
+ """Provides AI feedback for essays and weak areas."""
23
+ def __init__(self):
24
+ self.client = ZhipuAI(api_key=ZHIPU_API_KEY)
25
+
26
+ def coach(self, essay_text):
27
+ if not essay_text.strip():
28
+ return "⚠️ No essay text provided."
29
+ prompt = f"Provide constructive feedback and improvement tips for this essay:\n\n{essay_text}"
30
+ resp = self.client.chat.completions.create(
 
31
  model="glm-4",
32
  messages=[{"role": "user", "content": prompt}]
33
  )
34
  return resp.choices[0].message["content"]
35
 
 
36
  class PredictiveAgent:
37
+ """Forecasts likely SPM 2025–2026 questions based on past papers."""
38
+ def __init__(self):
39
+ self.client = ZhipuAI(api_key=ZHIPU_API_KEY)
40
 
41
+ def predict(self, subject, year_range="2018-2024"):
42
  prompt = (
43
+ f"Analyze Malaysian SPM {subject} past papers from {year_range}.\n"
44
+ f"Identify recurring patterns and predict 5 high-probability questions for upcoming exams."
45
  )
46
+ resp = self.client.chat.completions.create(
47
  model="glm-4",
48
  messages=[{"role": "user", "content": prompt}]
49
  )
50
+ return resp.choices[0].message["content"]
51
+
 
 
 
 
 
 
 
 
 
52
 
53
 
54