Benny-Tang commited on
Commit
3bb7108
·
verified ·
1 Parent(s): 9f22a20

Update agents.py

Browse files
Files changed (1) hide show
  1. agents.py +58 -20
agents.py CHANGED
@@ -2,39 +2,77 @@ import os
2
  import random
3
  from zhipuai import ZhipuAI
4
 
5
- API_KEY = os.getenv("zhipuai_api_key")
6
- client = ZhipuAI(api_key=API_KEY)
7
 
8
  class AnalyzerAgent:
9
- def analyze(self, answers, correct_answers):
10
- score = 0
 
 
 
 
 
 
 
 
 
 
11
  feedback = []
12
- for qid, user_ans in answers.items():
13
- if user_ans == correct_answers.get(qid):
14
- score += 1
 
 
 
15
  else:
16
- feedback.append(f"Q{qid}: Correct was {correct_answers.get(qid)}")
17
- return score, feedback
 
18
 
19
  class CoachAgent:
20
- def give_feedback(self, feedback):
21
- if not feedback:
22
- return "Excellent! You answered all correctly."
23
- return "Review these: " + "; ".join(feedback)
24
 
25
- class PredictiveAgent:
26
- def predict_questions(self, past_questions, subject):
27
- """
28
- Use GLM4.5 to predict potential questions based on past trends.
29
- """
30
- prompt = f"Analyze these past SPM {subject} questions and predict possible future ones:\n{past_questions[:1000]}"
 
31
  resp = client.chat.completions.create(
32
- model="glm-4-5",
33
  messages=[{"role": "user", "content": prompt}]
34
  )
35
  return resp.choices[0].message["content"]
36
 
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
 
 
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
+
76
 
77
 
78