Benny-Tang commited on
Commit
f53cf63
·
verified ·
1 Parent(s): 079930c

Update agents.py

Browse files
Files changed (1) hide show
  1. agents.py +50 -34
agents.py CHANGED
@@ -2,60 +2,76 @@ import os
2
  import json
3
  import requests
4
 
 
5
  GLM_API_URL = "https://api.your-glm-provider.com/v1/chat/completions"
6
- GLM_API_KEY = os.getenv("GLM_API_KEY")
7
 
8
- def call_glm(prompt, temperature=0.3):
9
  headers = {"Authorization": f"Bearer {GLM_API_KEY}"}
10
  payload = {
11
  "model": "glm-4.5",
12
- "messages": [{"role": "system", "content": "You are an educational assistant."},
13
- {"role": "user", "content": prompt}],
14
- "temperature": temperature
 
 
15
  }
16
  response = requests.post(GLM_API_URL, headers=headers, json=payload)
17
  result = response.json()
18
  return result["choices"][0]["message"]["content"]
19
 
20
  class AnalyzerAgent:
21
- def analyze(self, answers, question_bank):
22
- # Simple deterministic analysis
23
- topic_stats = {}
24
- for qid, result in answers.items():
25
- q = next(item for item in question_bank if str(item["id"]) == qid)
26
- for topic in q.get("topics", []):
27
- if topic not in topic_stats:
28
- topic_stats[topic] = {"correct": 0, "total": 0}
29
- topic_stats[topic]["total"] += 1
30
- if result["user"] == result["correct"]:
31
- topic_stats[topic]["correct"] += 1
32
-
33
- analysis = {t: round(v["correct"]/v["total"], 2) for t,v in topic_stats.items()}
34
- return {"topic_accuracy": analysis}
 
 
35
 
36
  class ForecastAgent:
37
  def forecast(self, level, subject):
38
- prompt = f"""
39
- You are an exam forecast assistant.
40
- Predict 3 high-probability topics for {level} {subject} exam.
41
- Return JSON: {{"predicted_topics": [{{"topic": "...", "confidence": 0.0}}]}}
 
 
 
 
42
  """
43
  try:
44
- response = call_glm(prompt)
45
  return json.loads(response)
46
- except Exception:
47
- return {"error": "Forecast unavailable"}
48
 
49
  class CoachAgent:
50
  def coach(self, analysis, level, subject):
51
- prompt = f"""
52
- You are a study coach.
53
- The student's weaknesses are: {analysis}.
54
- Suggest a study plan and 3 practice questions for {level} {subject}.
55
- Return JSON: {{"tips": ["..."], "study_plan": "...", "practice_questions": [{{"text": "...", "answer": "..."}}]}}
 
 
 
 
 
 
56
  """
57
  try:
58
- response = call_glm(prompt)
59
  return json.loads(response)
60
- except Exception:
61
- return {"error": "Coach unavailable"}
 
 
2
  import json
3
  import requests
4
 
5
+ # GLM-4.5 API configuration
6
  GLM_API_URL = "https://api.your-glm-provider.com/v1/chat/completions"
7
+ GLM_API_KEY = os.getenv("ZHIPUAI_API_KEY") # Hugging Face Secret
8
 
9
+ def call_glm(system_prompt, user_prompt, temperature=0.3):
10
  headers = {"Authorization": f"Bearer {GLM_API_KEY}"}
11
  payload = {
12
  "model": "glm-4.5",
13
+ "messages": [
14
+ {"role": "system", "content": system_prompt},
15
+ {"role": "user", "content": user_prompt},
16
+ ],
17
+ "temperature": temperature,
18
  }
19
  response = requests.post(GLM_API_URL, headers=headers, json=payload)
20
  result = response.json()
21
  return result["choices"][0]["message"]["content"]
22
 
23
  class AnalyzerAgent:
24
+ def analyze(self, answers, exam_data):
25
+ system_prompt = "You are an exam analysis assistant."
26
+ user_prompt = f"""
27
+ Analyze student answers against correct answers.
28
+ Answers: {json.dumps(answers)}
29
+ Exam questions: {json.dumps(exam_data)}
30
+ Return JSON: {{
31
+ "topic_accuracy": {{"topic": 0-1}},
32
+ "weak_topics": ["..."]
33
+ }}
34
+ """
35
+ try:
36
+ response = call_glm(system_prompt, user_prompt)
37
+ return json.loads(response)
38
+ except Exception as e:
39
+ return {"error": f"Analyzer failed: {str(e)}"}
40
 
41
  class ForecastAgent:
42
  def forecast(self, level, subject):
43
+ system_prompt = "You are an exam forecast assistant."
44
+ user_prompt = f"""
45
+ Predict 3 high-probability exam topics for {level} {subject}.
46
+ Return JSON: {{
47
+ "predicted_topics": [
48
+ {{"topic": "...", "confidence": 0-1}}
49
+ ]
50
+ }}
51
  """
52
  try:
53
+ response = call_glm(system_prompt, user_prompt)
54
  return json.loads(response)
55
+ except Exception as e:
56
+ return {"error": f"Forecast failed: {str(e)}"}
57
 
58
  class CoachAgent:
59
  def coach(self, analysis, level, subject):
60
+ system_prompt = "You are a study coach for school exams."
61
+ user_prompt = f"""
62
+ Based on this analysis: {json.dumps(analysis)},
63
+ suggest a study plan and 3 practice questions for {level} {subject}.
64
+ Return JSON: {{
65
+ "tips": ["..."],
66
+ "study_plan": "...",
67
+ "practice_questions": [
68
+ {{"text": "...", "answer": "..."}}
69
+ ]
70
+ }}
71
  """
72
  try:
73
+ response = call_glm(system_prompt, user_prompt)
74
  return json.loads(response)
75
+ except Exception as e:
76
+ return {"error": f"Coach failed: {str(e)}"}
77
+