ycRoadmapAgent / main.py
Rakshitjan's picture
Update main.py
31f24f6 verified
from fastapi import FastAPI
import json
from datetime import datetime, timedelta
from collections import defaultdict
from math import ceil
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
app = FastAPI()
# Load the roadmap (Define the roadmap as a variable in the code)
updated_roadmap = {
"schedule": [
{
"dayNumber": 1,
"date": "2025-02-09",
"subjects": [
{
"name": "Physics",
"tasks": [
{
"ChapterName": "Physics and Measurement",
"type": "Concept Understanding",
"topic": "Units of measurements, System of Units, SI Units",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 18:35:35"
},
{
"ChapterName": "Physics and Measurement",
"type": "Concept Understanding",
"topic": "Fundamental and derived units",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Physics and Measurement",
"type": "Concept Understanding",
"topic": "Least count, significant figures, Errors in measurements",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Physics and Measurement",
"type": "Concept Understanding",
"topic": "Dimensions of Physics quantities, dimensional analysis, and its applications",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
}
]
},
{
"name": "Chemistry",
"tasks": [
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Concept Understanding",
"topic": "Matter and its nature",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Concept Understanding",
"topic": "Dalton's Atomic Theory",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Concept Understanding",
"topic": "Concept of atom, molecule, element, and compound",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Concept Understanding",
"topic": "Laws of chemical combination",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
}
]
},
{
"name": "Mathematics",
"tasks": [
{
"ChapterName": "Sets, Relations and Functions",
"type": "Concept Understanding",
"topic": "Sets and their representation",
"time": "0.5 hours",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Concept Understanding",
"topic": "Union",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Concept Understanding",
"topic": "Intersection and complement of sets and their algebraic properties",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Concept Understanding",
"topic": "Power set",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
}
]
},
{
"dayNumber": 2,
"date": "2025-02-10",
"subjects": [
{
"name": "Physics",
"tasks": [
{
"ChapterName": "Physics and Measurement",
"type": "Question Practice",
"topic": "Units of measurements, System of Units, SI Units",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Physics and Measurement",
"type": "Question Practice",
"topic": "Fundamental and derived units",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Physics and Measurement",
"type": "Question Practice",
"topic": "Least count, significant figures, Errors in measurements",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-10 20:35:35"
},
{
"ChapterName": "Physics and Measurement",
"type": "Question Practice",
"topic": "Dimensions of Physics quantities, dimensional analysis, and its applications",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Chemistry",
"tasks": [
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Question Practice",
"topic": "Matter and its nature",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-10 20:35:35"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Question Practice",
"topic": "Dalton's Atomic Theory",
"time": "1 hour",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Question Practice",
"topic": "Concept of atom, molecule, element, and compound",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Question Practice",
"topic": "Laws of chemical combination",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Mathematics",
"tasks": [
{
"ChapterName": "Sets, Relations and Functions",
"type": "Question Practice",
"topic": "Sets and their representation",
"time": "0.5 hours",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Question Practice",
"topic": "Union",
"time": "0.5 hours",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Question Practice",
"topic": "Intersection and complement of sets and their algebraic properties",
"time": "0.5 hours",
"task_completed": True,
"completion_timestamp": "2025-02-09 20:35:35"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Question Practice",
"topic": "Power set",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
}
]
},
{
"dayNumber": 3,
"date": "2025-02-11",
"subjects": [
{
"name": "Physics",
"tasks": [
{
"ChapterName": "Physics and Measurement",
"type": "Revision",
"topic": "Physics and Measurement",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Chemistry",
"tasks": [
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Revision",
"topic": "Some Basic Concepts in Chemistry",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Mathematics",
"tasks": [
{
"ChapterName": "Sets, Relations and Functions",
"type": "Revision",
"topic": "Sets, Relations and Functions",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
}
]
},
{
"dayNumber": 4,
"date": "2025-02-12",
"subjects": [
{
"name": "Physics",
"tasks": [
{
"ChapterName": "Physics and Measurement",
"type": "Test",
"topic": "Physics and Measurement",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Chemistry",
"tasks": [
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Test",
"topic": "Some Basic Concepts in Chemistry",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Mathematics",
"tasks": [
{
"ChapterName": "Sets, Relations and Functions",
"type": "Test",
"topic": "Sets, Relations and Functions",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
}
]
},
{
"dayNumber": 5,
"date": "2025-02-13",
"subjects": [
{
"name": "Physics",
"tasks": [
{
"ChapterName": "Physics and Measurement",
"type": "Concept Understanding",
"topic": "Units of measurements, System of Units, SI Units",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Physics and Measurement",
"type": "Concept Understanding",
"topic": "Fundamental and derived units",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Physics and Measurement",
"type": "Concept Understanding",
"topic": "Least count, significant figures, Errors in measurements",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Physics and Measurement",
"type": "Concept Understanding",
"topic": "Dimensions of Physics quantities, dimensional analysis, and its applications",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Chemistry",
"tasks": [
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Concept Understanding",
"topic": "Atomic and molecular masses",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Concept Understanding",
"topic": "Mole concept",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Concept Understanding",
"topic": "Molar mass",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Concept Understanding",
"topic": "Percentage composition",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Mathematics",
"tasks": [
{
"ChapterName": "Sets, Relations and Functions",
"type": "Concept Understanding",
"topic": "Relation",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Concept Understanding",
"topic": "Type of relations",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Concept Understanding",
"topic": "equivalence relations",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Concept Understanding",
"topic": "Functions",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
}
]
},
{
"dayNumber": 6,
"date": "2025-02-14",
"subjects": [
{
"name": "Physics",
"tasks": [
{
"ChapterName": "Physics and Measurement",
"type": "Question Practice",
"topic": "Units of measurements, System of Units, SI Units",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Physics and Measurement",
"type": "Question Practice",
"topic": "Fundamental and derived units",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Physics and Measurement",
"type": "Question Practice",
"topic": "Least count, significant figures, Errors in measurements",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Physics and Measurement",
"type": "Question Practice",
"topic": "Dimensions of Physics quantities, dimensional analysis, and its applications",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Chemistry",
"tasks": [
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Question Practice",
"topic": "Atomic and molecular masses",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Question Practice",
"topic": "Mole concept",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Question Practice",
"topic": "Molar mass",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Question Practice",
"topic": "Percentage composition",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Mathematics",
"tasks": [
{
"ChapterName": "Sets, Relations and Functions",
"type": "Question Practice",
"topic": "Relation",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Question Practice",
"topic": "Type of relations",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Question Practice",
"topic": "equivalence relations",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
},
{
"ChapterName": "Sets, Relations and Functions",
"type": "Question Practice",
"topic": "Functions",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
}
]
},
{
"dayNumber": 7,
"date": "2025-02-15",
"subjects": [
{
"name": "Physics",
"tasks": [
{
"ChapterName": "Physics and Measurement",
"type": "Revision",
"topic": "Physics and Measurement",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Chemistry",
"tasks": [
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Revision",
"topic": "Some Basic Concepts in Chemistry",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Mathematics",
"tasks": [
{
"ChapterName": "Sets, Relations and Functions",
"type": "Revision",
"topic": "Sets, Relations and Functions",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
}
]
},
{
"dayNumber": 8,
"date": "2025-02-16",
"subjects": [
{
"name": "Physics",
"tasks": [
{
"ChapterName": "Physics and Measurement",
"type": "Test",
"topic": "Physics and Measurement",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Chemistry",
"tasks": [
{
"ChapterName": "Some Basic Concepts in Chemistry",
"type": "Test",
"topic": "Some Basic Concepts in Chemistry",
"time": "1 hour",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
},
{
"name": "Mathematics",
"tasks": [
{
"ChapterName": "Sets, Relations and Functions",
"type": "Test",
"topic": "Sets, Relations and Functions",
"time": "0.5 hours",
"task_completed": False,
"completion_timestamp": "Not Completed"
}
]
}
]
}
]
}
# Initialize the LLM model
llm = ChatOpenAI(model_name="gpt-4o-mini", temperature=0)
# Task Authentication Prompt
authenticity_prompt = ChatPromptTemplate.from_template("""
You are a Task Authenticity Verification Agent.
Review the roadmap to verify task completion authenticity based on time-series context of tasks completion status:
- Analyze the timestamps of completed tasks in chronological order.
- If a task was completed in less than 50% of the allocated time, mark it as Take Test.
- If the task is completed before the day of task mark it as take Test to validate
- If a task's completion time is inconsistent with prior tasks in the sequence, flag it for review.
- Add "task_authenticity": "Take Test to practice" or "Take test to validate": "Take test to validate" to each completed task.
- Do not remove the tasks which are not completed, Keep their task_authenticity as "task_authenticity": "".
Ordered Time-Series Context:
{context}
Input Roadmap:
{roadmap}
Output the updated roadmap in JSON format only.
""")
authenticity_chain = authenticity_prompt | llm
# Review Analysis Prompt
review_analysis_prompt = ChatPromptTemplate.from_template("""
You are a monitoring and analysis agent which gives the analysis of the user's task completion performance and roadmap.
Your analysis should include:
-> What are the task completion patterns
-> Which subjects are found interesting to the student
-> Which topics the student is ignoring
-> And many more
Input date context:
{context}
Input Roadmap:
{roadmap}
Output the analysis to me.
""")
review_analysis_chain = review_analysis_prompt | llm
def extract_surrounding_days(roadmap, target_date_str):
target_date = datetime.strptime(target_date_str, "%Y-%m-%d")
start_date = target_date - timedelta(days=2)
end_date = target_date + timedelta(days=2)
extracted_schedule = [
day for day in roadmap["schedule"]
if start_date <= datetime.strptime(day["date"], "%Y-%m-%d") <= end_date
]
return extracted_schedule
def task_completion(roadmap):
if len(roadmap) < 5:
return roadmap # Ensure we have at least 5 days for processing
incomplete_tasks_by_subject = defaultdict(list)
# Collect incomplete tasks from the previous two days
for i in range(2):
prev_day_index = i
if 0 <= prev_day_index < len(roadmap):
prev_day = roadmap[prev_day_index]
for subject in prev_day["subjects"]:
subject_name = subject["name"]
tasks = subject.get("tasks", [])
incomplete_tasks = [task for task in tasks if not task.get("task_completed", False)]
completed_tasks = [task for task in tasks if task.get("task_completed", False)]
if incomplete_tasks:
incomplete_tasks_by_subject[subject_name].extend(incomplete_tasks)
subject["tasks"] = completed_tasks
# Redistribute tasks across the next 3 days
for i in range(3):
next_day_index = i + 2
if 0 <= next_day_index < len(roadmap):
next_day = roadmap[next_day_index]
for subject in next_day["subjects"]:
subject_name = subject["name"]
if subject_name in incomplete_tasks_by_subject and incomplete_tasks_by_subject[subject_name]:
total_tasks = len(incomplete_tasks_by_subject[subject_name])
if i == 0:
tasks_to_add = ceil(total_tasks * (1 / 6))
elif i == 1:
tasks_to_add = ceil(total_tasks * (2 / 6))
else:
tasks_to_add = len(incomplete_tasks_by_subject[subject_name])
subject["tasks"].extend(incomplete_tasks_by_subject[subject_name][:tasks_to_add])
incomplete_tasks_by_subject[subject_name] = incomplete_tasks_by_subject[subject_name][tasks_to_add:]
return roadmap
def create_llm_context(roadmap, cutoff_date):
context = []
cutoff_datetime = datetime.strptime(cutoff_date, "%Y-%m-%d") - timedelta(days=1)
for day in roadmap:
task_datetime = datetime.strptime(day['date'], "%Y-%m-%d")
if task_datetime == cutoff_datetime:
for subject in day["subjects"]:
for task in subject["tasks"]:
if task.get("completion_timestamp"):
context.append((task["completion_timestamp"], task["topic"], task["type"]))
formatted_context = "\n".join([f"{ts} - {topic} ({task_type})" for ts, topic, task_type in context])
return formatted_context
def execute_chain(roadmap):
llm_context = create_llm_context(roadmap, "2025-02-11")
authenticity_roadmap = authenticity_chain.invoke({"context": llm_context, "roadmap": roadmap})
review_analysis_roadmap = review_analysis_chain.invoke({"context": "The current date is: 2025-02-11", "roadmap": authenticity_roadmap})
roadmap = task_completion(roadmap)
cleaned_text = list(authenticity_roadmap)[0][1].split("```json\n")[-1].split("\n```")[0]
authenticity_roadmap_json = json.loads("[" + cleaned_text + "]")
formatted_json = json.dumps(authenticity_roadmap_json[0], indent=4)
Agenticroadmap = formatted_json
return Agenticroadmap, list(review_analysis_roadmap)[0][1]
@app.get("/")
def run_agent():
roadmap=extract_surrounding_days(updated_roadmap,"2025-02-11")
a, b = execute_chain(roadmap)
return {"agentic_roadmap": json.loads(a), "review_analysis": b}