Spaces:
Runtime error
Runtime error
Update agents.py
Browse files
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("
|
| 7 |
|
| 8 |
-
def call_glm(
|
| 9 |
headers = {"Authorization": f"Bearer {GLM_API_KEY}"}
|
| 10 |
payload = {
|
| 11 |
"model": "glm-4.5",
|
| 12 |
-
"messages": [
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 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,
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
class ForecastAgent:
|
| 37 |
def forecast(self, level, subject):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
Predict 3 high-probability topics for {level} {subject}
|
| 41 |
-
Return JSON: {{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
"""
|
| 43 |
try:
|
| 44 |
-
response = call_glm(
|
| 45 |
return json.loads(response)
|
| 46 |
-
except Exception:
|
| 47 |
-
return {"error": "Forecast
|
| 48 |
|
| 49 |
class CoachAgent:
|
| 50 |
def coach(self, analysis, level, subject):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
Return JSON: {{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
"""
|
| 57 |
try:
|
| 58 |
-
response = call_glm(
|
| 59 |
return json.loads(response)
|
| 60 |
-
except Exception:
|
| 61 |
-
return {"error": "Coach
|
|
|
|
|
|
| 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 |
+
|