Hari-Prasath-M91 commited on
Commit
6b4993e
·
1 Parent(s): aab4b2b

Agent-4 Update

Browse files
Files changed (1) hide show
  1. app.py +304 -3
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import copy
4
  import sqlite3
5
  import operator
 
6
  from math import ceil
7
  from fastapi import FastAPI, Query
8
  from contextlib import asynccontextmanager
@@ -25,7 +26,8 @@ session_state = {
25
  "report_data": None,
26
  "final_report": None,
27
  "dependencies": None,
28
- "updated_roadmap": None
 
29
  }
30
 
31
 
@@ -500,11 +502,32 @@ def shift_the_roadmap(roadmap, max_hours_per_day, ratio=(80, 20), dependencies=N
500
  return roadmap, extra_day_tasks
501
 
502
  def update_roadmap(current_roadmap, current_dayNumber, max_hours_per_day, dependencies, no_of_revision_days = 2):
503
- if current_dayNumber == 1:
504
  return current_roadmap
505
  current_roadmap = copy.deepcopy(current_roadmap)
506
  day_index = current_dayNumber-2
507
  test_index = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
 
509
  # Check if a test exists in any specified day
510
  for day in current_roadmap['schedule']:
@@ -1193,6 +1216,140 @@ def answer_user_query(prompt):
1193
  data = fetch_data_from_sql(query)
1194
  return generate_nl_from_sql_output(prompt, data)
1195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1196
 
1197
  @app.get("/", response_class=HTMLResponse)
1198
  def root():
@@ -1469,4 +1626,148 @@ def agent3(query: str = Query(..., description="User's message to the chatbot"))
1469
  return {"chat_response": {
1470
  "role": "assistant",
1471
  "message": f"Sorry, I encountered an error: {e}"
1472
- }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import copy
4
  import sqlite3
5
  import operator
6
+ import pandas as pd
7
  from math import ceil
8
  from fastapi import FastAPI, Query
9
  from contextlib import asynccontextmanager
 
26
  "report_data": None,
27
  "final_report": None,
28
  "dependencies": None,
29
+ "updated_roadmap": None,
30
+ "chapter_analysis": None
31
  }
32
 
33
 
 
502
  return roadmap, extra_day_tasks
503
 
504
  def update_roadmap(current_roadmap, current_dayNumber, max_hours_per_day, dependencies, no_of_revision_days = 2):
505
+ if current_dayNumber <= 1 or current_dayNumber > len(current_roadmap):
506
  return current_roadmap
507
  current_roadmap = copy.deepcopy(current_roadmap)
508
  day_index = current_dayNumber-2
509
  test_index = None
510
+
511
+ if "supplementary_tasks" in current_roadmap[day_index]:
512
+ current_roadmap[day_index+1]['supplementary_tasks'] = []
513
+ for subject in current_roadmap[day_index]["supplementary_tasks"]:
514
+ subject_name = subject["name"]
515
+ tasks = subject["tasks"]
516
+
517
+ # Separate completed and incomplete tasks
518
+ incomplete_tasks = [task for task in tasks if task['task_completed'] == False]
519
+ completed_tasks = [task for task in tasks if task['task_completed'] == True]
520
+
521
+ for task in incomplete_tasks:
522
+ task['rescheduled'] += 1
523
+ # Move incomplete tasks to next day
524
+ if incomplete_tasks:
525
+ current_roadmap[day_index+1]['supplementary_tasks'].append({
526
+ "name":subject_name,
527
+ "tasks":incomplete_tasks
528
+ })
529
+ # Keep only completed tasks in the previous day
530
+ subject["tasks"] = completed_tasks
531
 
532
  # Check if a test exists in any specified day
533
  for day in current_roadmap['schedule']:
 
1216
  data = fetch_data_from_sql(query)
1217
  return generate_nl_from_sql_output(prompt, data)
1218
 
1219
+ ## Agent 4 - Analysis and Accountability Agent
1220
+ class Plan(BaseModel):
1221
+ result: Literal["More Tasks", "Revise Chapter", "Strong Chapter"] = Field(
1222
+ ..., description="""In general what is needed to be done to make the student perform
1223
+ better in this concept the next time?"""
1224
+ )
1225
+ tasks: List[Literal["Concept Understanding", "Question Practice", "Revision", "Test", "No Tasks"]] = Field(
1226
+ ..., description="""If more tasks are needed to be scheduled for the student, what kind
1227
+ of tasks must be scheduled for the student to perform better the
1228
+ next time?"""
1229
+ )
1230
+
1231
+ class Analysis(BaseModel):
1232
+ correct_answers: int = Field(
1233
+ ..., description="Count and find out the number of 'Correct?' keys with True as the value"
1234
+ )
1235
+ why_correct: str = Field(
1236
+ ..., description="""Give a deep analysis on which area must the student be strong in, in order to answer
1237
+ the questions which were answered correctly"""
1238
+ )
1239
+ wrong_answers: int = Field(
1240
+ ..., description="ount and find out the number of 'Correct?' keys with False as the value"
1241
+ )
1242
+ why_wrong: str = Field(
1243
+ ..., description="""Give a deep analysis on which area should the student be weak in, to answer
1244
+ those questions incorrectly"""
1245
+ )
1246
+ action: Plan = Field(
1247
+ ..., description="""Deeply analyze the difficulty levels of the answered questions
1248
+ along with the change in the ELO scores, as the student gives right and
1249
+ wrong answers. With this analysis, determine what would the student
1250
+ require more of to perform better the next time."""
1251
+ )
1252
+
1253
+ analyzer = llm.with_structured_output(Analysis)
1254
+
1255
+ def get_llm_response(data):
1256
+ response = analyzer.invoke(
1257
+ [
1258
+ SystemMessage(
1259
+ content=f"""You are a smart AI mentor who is an expert in making sure that the students prepare
1260
+ perfectly for their JEE examinaton. In order to make sure of this, you are asking a set of 5
1261
+ questions to the student from the list of subtopics, which the student claims to have completed.
1262
+
1263
+ Now, once the student answers all those 5 questions. You will be given with all the details you need
1264
+ to analyze how good the student has performed. The details which will be given to you are:
1265
+
1266
+ 1). The Subject from which the question was asked. Which will be
1267
+ either Physics, Chemistry, or Maths
1268
+ 2). The Chapter from which the question was asked from.
1269
+ 3). The tag of the Question - It tells the kind of work the student
1270
+ needs to do, to answer that question. Which will be either
1271
+ Concept Understanding, Question Practice, Revision or Test.
1272
+ 4). The Complexity level of the question. Which will be either
1273
+ Easy, Hard or Medium.
1274
+ 5). The Questions which were asked to the student.
1275
+ 6). The 4 options which were given to the student.
1276
+ 7). The Option that was selected by the student.
1277
+ 8). The Correct Option.
1278
+ 9). Whether the student has answered the question correctly or not.
1279
+ 10). The ELO score of the student after answering that question.
1280
+
1281
+ Your job is to strictly follow the given output structure the whole time you would
1282
+ be producing the analysis. You will not deviate in any manner from the given output
1283
+ json structure, if you do so then the whole application will crash.
1284
+
1285
+ The complete data Required to analyze the student's performance is given to you here:
1286
+ {data}
1287
+ """
1288
+ ),
1289
+ HumanMessage(
1290
+ content=f"""Perform the analysis with absolute accuracy and make no mistake.
1291
+ The analysis will be used to understand whether the student actually knows something
1292
+ in that subtopic of the subject, or he has just marked it as completed just for the
1293
+ sake of it. Make sure your output is perfectly formatted with the given structure"""
1294
+ ),
1295
+ ]
1296
+ )
1297
+ return response
1298
+
1299
+
1300
+ def ag4_update_roadmap(test_data, roadmap, chapter_analysis, current_dayNumber):
1301
+ data_dict = test_data.to_dict(orient="records")
1302
+ response = get_llm_response(
1303
+ json.dumps(data_dict, indent=4)
1304
+ ).model_dump()
1305
+
1306
+ subject = data_dict[0]["Subject"]
1307
+ chapter = data_dict[0]["Chapter"]
1308
+
1309
+ analysis = {
1310
+ "subject": subject,
1311
+ "chapter": chapter,
1312
+ "positives": response['why_correct'],
1313
+ "negatives": response['why_wrong'],
1314
+ }
1315
+ chapter_analysis.append(analysis)
1316
+
1317
+ if response['action']['tasks'][0] != "No Tasks":
1318
+ tasks = {
1319
+ "Physics": [],
1320
+ "Chemistry": [],
1321
+ "Maths": []
1322
+ }
1323
+
1324
+ for task in response['action']['tasks']:
1325
+ tasks[subject].append({
1326
+ "ChapterName": chapter,
1327
+ "type": task,
1328
+ "rescheduled": 0,
1329
+ "completed": False,
1330
+ "completion_timestamp": None
1331
+ })
1332
+
1333
+ day = roadmap['schedule'][current_dayNumber]
1334
+ if "supplementary_tasks" not in day:
1335
+ day["supplementary_tasks"] = [{
1336
+ "name":"Physics",
1337
+ "tasks":[]
1338
+ },{
1339
+ "name":"Chemistry",
1340
+ "tasks":[]
1341
+ },{
1342
+ "name":"Maths",
1343
+ "tasks":[]
1344
+ }]
1345
+ for sub in day["supplementary_tasks"]:
1346
+ sub_name = sub["name"]
1347
+ for task in tasks[sub_name]:
1348
+ if task not in sub["tasks"]:
1349
+ sub["tasks"].append(task)
1350
+
1351
+ session_state['updated_roadmap'] = roadmap
1352
+ session_state['chapter_analysis'] = chapter_analysis
1353
 
1354
  @app.get("/", response_class=HTMLResponse)
1355
  def root():
 
1626
  return {"chat_response": {
1627
  "role": "assistant",
1628
  "message": f"Sorry, I encountered an error: {e}"
1629
+ }}
1630
+
1631
+ # --- AGENT 4: Accountability and Analysis Agent
1632
+ df = pd.DataFrame([
1633
+ {
1634
+ "Subject": "Physics",
1635
+ "Chapter": "Vectors",
1636
+ "Question Tag": "Concept Understanding",
1637
+ "Question Complexity": "Easy",
1638
+ "Question": "The vector projection of a vector on y-axis is",
1639
+ "Options Given": "(a) 5 (b) 4 (c) 3 (d) Zero",
1640
+ "Correct Option": "d",
1641
+ "Selected Option": "d",
1642
+ "Correct?": True,
1643
+ "ELO Score": 1200
1644
+ },
1645
+ {
1646
+ "Subject": "Physics",
1647
+ "Chapter": "Vectors",
1648
+ "Question Tag": "Concept Understanding",
1649
+ "Question Complexity": "Easy",
1650
+ "Question": "Position of a particle in a rectangular coordinate system is (3, 2, 5). Then its position vector will be",
1651
+ "Options Given": "(a) 3i + 2j + 5k (b) 2i + 3j + 5k (c) 5i + 2j + 3k (d) None of these",
1652
+ "Correct Option": "a",
1653
+ "Selected Option": "b",
1654
+ "Correct?": False,
1655
+ "ELO Score": 1170
1656
+ },
1657
+ {
1658
+ "Subject": "Physics",
1659
+ "Chapter": "Vectors",
1660
+ "Question Tag": "Question Practice",
1661
+ "Question Complexity": "Easy",
1662
+ "Question": "If a particle moves from point P (2,3,5) to point Q (3,4,5), its displacement vector is",
1663
+ "Options Given": "(a) i + j (b) i + j + k (c) 2i + 2j (d) None of these",
1664
+ "Correct Option": "a",
1665
+ "Selected Option": "a",
1666
+ "Correct?": True,
1667
+ "ELO Score": 1185
1668
+ },
1669
+ {
1670
+ "Subject": "Physics",
1671
+ "Chapter": "Vectors",
1672
+ "Question Tag": "Concept Understanding",
1673
+ "Question Complexity": "Medium",
1674
+ "Question": "A particle moving eastwards with 5 m/s. In 10 s the velocity changes to 5 m/s northwards. The average acceleration is",
1675
+ "Options Given": "(a) 1/2 m/s² NE (b) 1/2 m/s² North (c) 1/2 m/s² NW (d) Zero",
1676
+ "Correct Option": "a",
1677
+ "Selected Option": "a",
1678
+ "Correct?": True,
1679
+ "ELO Score": 1225
1680
+ },
1681
+ {
1682
+ "Subject": "Physics",
1683
+ "Chapter": "Vectors",
1684
+ "Question Tag": "Question Practice",
1685
+ "Question Complexity": "Medium",
1686
+ "Question": "A river is flowing west to east with 5 m/min. A man can swim in still water at 10 m/min. Which direction should he swim to reach the south bank in shortest path?",
1687
+ "Options Given": "(a) 30° E of S (b) 60° E of N (c) South (d) 30° W of N",
1688
+ "Correct Option": "c",
1689
+ "Selected Option": "a",
1690
+ "Correct?": False,
1691
+ "ELO Score": 1190
1692
+ },
1693
+ {
1694
+ "Subject": "Physics",
1695
+ "Chapter": "Vectors",
1696
+ "Question Tag": "Revision",
1697
+ "Question Complexity": "Medium",
1698
+ "Question": "Find a vector perpendicular to A = 2i + 3j in the same plane.",
1699
+ "Options Given": "(a) -3i + 2j (b) 3i - 2j (c) i + j (d) None",
1700
+ "Correct Option": "a",
1701
+ "Selected Option": "a",
1702
+ "Correct?": True,
1703
+ "ELO Score": 1230
1704
+ },
1705
+ {
1706
+ "Subject": "Physics",
1707
+ "Chapter": "Vectors",
1708
+ "Question Tag": "Revision",
1709
+ "Question Complexity": "Medium",
1710
+ "Question": "Two vectors A and B have same magnitude. If their resultant is perpendicular to A, what is the angle between A and B?",
1711
+ "Options Given": "(a) 60° (b) 120° (c) 135° (d) None",
1712
+ "Correct Option": "b",
1713
+ "Selected Option": "b",
1714
+ "Correct?": True,
1715
+ "ELO Score": 1260
1716
+ },
1717
+ {
1718
+ "Subject": "Physics",
1719
+ "Chapter": "Vectors",
1720
+ "Question Tag": "Test",
1721
+ "Question Complexity": "Hard",
1722
+ "Question": "A man walks 500 m and turns by 60° five times. What is displacement after 5th turn?",
1723
+ "Options Given": "(a) 500 m (b) 1000 m (c) 500√3 m (d) None",
1724
+ "Correct Option": "a",
1725
+ "Selected Option": "c",
1726
+ "Correct?": False,
1727
+ "ELO Score": 1210
1728
+ },
1729
+ {
1730
+ "Subject": "Physics",
1731
+ "Chapter": "Vectors",
1732
+ "Question Tag": "Test",
1733
+ "Question Complexity": "Hard",
1734
+ "Question": "Rain is falling vertically at 3 m/s and a man moves north at 4 m/s. Direction to hold umbrella?",
1735
+ "Options Given": "(a) 37° N of vertical (b) 37° S of vertical (c) 53° N of vertical (d) 53° S of vertical",
1736
+ "Correct Option": "c",
1737
+ "Selected Option": "c",
1738
+ "Correct?": True,
1739
+ "ELO Score": 1265
1740
+ },
1741
+ {
1742
+ "Subject": "Physics",
1743
+ "Chapter": "Vectors",
1744
+ "Question Tag": "Test",
1745
+ "Question Complexity": "Hard",
1746
+ "Question": "Which set of forces cannot be in equilibrium?",
1747
+ "Options Given": "(a) 10N,10N,5N (b) 5N,7N,9N (c) 8N,4N,13N (d) 9N,6N,5N",
1748
+ "Correct Option": "c",
1749
+ "Selected Option": "c",
1750
+ "Correct?": True,
1751
+ "ELO Score": 1310
1752
+ }
1753
+ ])
1754
+ df.sort_values(by=['Correct?'], ascending=False, inplace=True)
1755
+
1756
+ @app.get("/agent4")
1757
+ def agent4(
1758
+ df: pd.DataFrame = Query(df, description="Dataframe of the Results of the students test"),
1759
+ current_dayNumber: int = Query(2, description="Today's day number for scheduling tasks"),
1760
+ ):
1761
+ """
1762
+ Agent 4 - Accountability and Analysis Agent.
1763
+ """
1764
+ if session_state['updated_roadmap']:
1765
+ before = session_state['updated_roadmap']
1766
+ else:
1767
+ before = session_state['data']
1768
+ ag4_update_roadmap(df, before, session_state["task_analysis"], current_dayNumber)
1769
+
1770
+ return {
1771
+ "original_roadmap": before,
1772
+ "updated_roadmap": session_state["updated_roadmap"]
1773
+ }