Version1 / agents /curriculum_planner.py
Prerit018's picture
Upload 26 files
6bc3db2 verified
Raw
History Blame Contribute Delete
4.09 kB
import os
from dotenv import load_dotenv
import openai
import json
import re
from .base_agent import BaseAgent
from curriculum import Curriculum, Chapter, Module
class CurriculumPlannerAgent(BaseAgent):
def __init__(self):
super().__init__("CurriculumPlannerAgent")
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
self.client = openai.OpenAI(api_key=api_key)
def process(self, topic, level):
system_prompt = (
"You are an Expert Instructional Architect. Design a detailed, academically rigorous curriculum.\n\n"
"### Rules:\n"
"1. Ensure academic depth and avoid trivial content.\n"
"2. Respect the level:\n"
"- Novice: fundamentals, 4–6 modules per chapter.\n"
"- Intermediate: problem-solving & applications, 5–7 modules per chapter.\n"
"- Advanced: theory, research, edge cases, 6–8 modules per chapter.\n"
"3. Each module must have:\n"
"- `module_name`\n"
"- `learning_objective` (with knowledge, skills, applications, examples)\n"
"4. Use progressive complexity.\n\n"
"### Example Curriculum (Novice, Topic: Python Programming)\n"
"{\n"
" \"chapters\": [\n"
" {\n"
" \"chapter_name\": \"Introduction to Python\",\n"
" \"modules\": [\n"
" {\"module_name\": \"What is Python?\", \"learning_objective\": \"Understand Python’s role as a programming language, its history, and its everyday applications.\"},\n"
" {\"module_name\": \"Setting Up Python\", \"learning_objective\": \"Learn how to install Python and write your first basic script.\"},\n"
" {\"module_name\": \"Variables and Data Types\", \"learning_objective\": \"Understand how to store data in variables and use types such as strings, numbers, and booleans.\"}\n"
" ]\n"
" },\n"
" {\n"
" \"chapter_name\": \"Control Structures\",\n"
" \"modules\": [\n"
" {\"module_name\": \"If Statements\", \"learning_objective\": \"Learn decision-making in programs with if/else statements and simple examples.\"},\n"
" {\"module_name\": \"Loops\", \"learning_objective\": \"Understand repetition using for and while loops with practical use cases.\"}\n"
" ]\n"
" }\n"
" ]\n"
"}\n\n"
"### Instructions:\n"
"- Follow the same style for the requested topic.\n"
"- Do NOT output explanations outside JSON.\n"
)
user_prompt = f"Generate a curriculum for:\nTopic: {topic}\nLevel: {level}"
response = self.client.chat.completions.create(
model="gpt-4o-mini",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
try:
curriculum_json = response.choices[0].message.content.strip()
match = re.search(r'\{.*\}', curriculum_json, re.DOTALL)
if match:
json_str = match.group(0)
else:
raise ValueError("No JSON found in the response")
chapters_obj = json.loads(json_str)
chapters_data = chapters_obj["chapters"]
chapters = []
for ch in chapters_data:
modules = [Module(m["module_name"], m["learning_objective"]) for m in ch["modules"]]
chapters.append(Chapter(ch["chapter_name"], modules))
return Curriculum(topic, chapters)
except Exception as e:
print("[CurriculumPlannerAgent] Error parsing curriculum:", e)
return Curriculum(topic, [Chapter("General Introduction", [Module("Overview")])])