SOCAv2 / main.py
Rakshitjan's picture
Update main.py
2d0a8d0 verified
# # main.py
# from fastapi import FastAPI, HTTPException
# from pydantic import BaseModel
# from typing import Dict
# import os
# from groq import Groq
# app = FastAPI()
# # Pydantic model for request
# class ScoreInput(BaseModel):
# score_percentages: Dict[str, float]
# time_percentages: Dict[str, float]
# # Helper functions
# def get_final_score(score_percentages: Dict[str, float], time_percentages: Dict[str, float]) -> Dict[str, float]:
# final_score = {}
# for skill in score_percentages:
# score_avg = (score_percentages[skill] + time_percentages[skill]) / 2
# final_score[skill] = score_avg
# return final_score
# def get_strengths_and_weaknesses(final_score: Dict[str, float]):
# sorted_skills = sorted(
# [(skill, score) for skill, score in final_score.items()],
# key=lambda item: item[1],
# reverse=True
# )
# num_skills = len(sorted_skills)
# if num_skills == 0:
# return [], [], []
# split1 = num_skills // 3
# split2 = 2 * (num_skills // 3)
# strengths = sorted_skills[:split1]
# opportunities = sorted_skills[split1:split2]
# challenges = sorted_skills[split2:]
# return strengths, opportunities, challenges
# # FastAPI route
# @app.post("/analyze")
# async def analyze_scores(input_data: ScoreInput):
# final_score = get_final_score(input_data.score_percentages, input_data.time_percentages)
# strengths, opportunities, challenges = get_strengths_and_weaknesses(final_score)
# # Groq API call
# api_key = os.getenv("GROQ_API_KEY")
# if not api_key:
# raise HTTPException(status_code=500, detail="Groq API key not found")
# client = Groq(api_key=api_key)
# sys_prompt = f"""You are an advanced language model trained to analyze student responses from a questionnaire on Academic, Cognitive, and Study Profile aspects related to JEE Mains preparation. Your task is to generate a personalized SCO (Strengths, Challenges, Opportunities) analysis and an Action Plan section based on the user's inputs.
# You have been provided with the strengths {strengths}, Opportunities {opportunities} and Challenges {challenges} skills of the user
# Output Structure:
# SCO Analysis:
# Strengths:
# - List the student's strengths based on their {strengths} skills
# - Let the student now how they can use these strengths in their JEE preparation and exam to improve their score.
# - Also tell them how do they improve their score more.
# Opportunities:
# - List the student's strengths based on their {opportunities} skills
# - Suggest opportunities for improvement by leveraging the student's strengths and addressing their challenges.
# - Recommend ways to enhance their score in the {opportunities} skills.
# - Also tell them if they improve in these skills what opportunities they have in improving their scores
# Challenges:
# - List the student's strengths based on their {challenges} skills
# - Guide the student that these skills are basically the core area where they are lacking
# - Tell them that if they continue not focusing upon them they might get far away from their JEE goal.
# Action Plan:
# - Provide a detailed plan to the student to improve in the {challenges} skills.
# - Recommend targeted strategies, resources, and techniques to improve their {challenges} skills.
# - Let them know if they improve these areas how it can help boost their scores and make their preparation more effective.
# - Incorporate time management, revision, and test-taking strategies specific to JEE Mains and the identified subjects/topics/subtopics.
# Things that LLM need to make sure:
# 1) Your analysis and action plan should be comprehensive, consistent, and tailored to the individual student's responses while leveraging your knowledge of the JEE Mains exam context, the mapping of subjects/topics to general cognitive traits and skills, and the ability to identify overarching trends across related subjects/topics.
# 2) Make sure you give the output that extracts the student.
# 3) Make sure you give out output in bullet points.
# 4) While entering a new line in the output use "\n" new line character.
# 5) Make the output very much JEE (Joint Entrance Examination) based and give everything with context to Physics , Chemistry and Maths JEE syllabus.
# 6) Use Italics, Bold and underline appropriately to improve the output more.
# 7) Bold text where you are taking chapter names from Physics , Chemsitry and Maths only which are in syllabus of Joint Entrance Examination.
# 8) Dont use "+" or any other special symbol whenever you want to break a line use "\n" to do it in the output.
# """
# try:
# chat_completion = client.chat.completions.create(
# messages=[
# {"role": "system", "content": sys_prompt},
# {"role": "user", "content": f"Generate the SOCA analysis based on the system prompt and {strengths}, {opportunities} and {challenges}. MAKE SURE WE STRICTLY FOLLOW THE STRUCTURE."},
# ],
# model="llama3-70b-8192",
# )
# analysis = chat_completion.choices[0].message.content
# except Exception as e:
# raise HTTPException(status_code=500, detail=f"Error calling Groq API: {str(e)}")
# return {"analysis": analysis}
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run(app, host="0.0.0.0", port=8000)
# Merger of SOCA v1 and SOCA v2
from embedchain import App
from fastapi import FastAPI, HTTPException
from mangum import Mangum
from pydantic import BaseModel
from typing import Dict
import os
from groq import Groq
app = FastAPI()
handler = Mangum(app)
#SOCA V1 CODE START
api_key1 = os.getenv('GROQ_API_KEY')
config = {
'llm': {
'provider': 'groq',
'config': {
'model':'llama3-70b-8192',
'top_p': 0.5,
'api_key': api_key1,
'stream': True
}
},
'embedder': {
'provider': 'huggingface',
'config': {
'model': 'sentence-transformers/all-mpnet-base-v2'
}
}
}
swot_bot = App.from_config(config=config)
swot_bot.add("web_page","https://www.allen.ac.in/engineering/jee-main/tips-tricks/")
# swot_bot.add("https://motion.ac.in/blog/jee-main-weightage-chapter-wise/")
swot_bot.add("https://www.allen.ac.in/engineering/jee-main/preparation-strategy/")
#swot_bot.add("https://byjus.com/jee/how-to-prepare-for-jee-at-home/")
swot_bot.add("https://www.askiitians.com/iit-jee/how-to-prepare-for-iit-jee-from-class-11.html")
# swot_bot.add("https://byjus.com/jee/complete-study-plan-to-crack-jee-main/")
#swot_bot.add("https://mystudycart.com/iit-jee-preparation")
swot_bot.add("https://engineering.careers360.com/articles/how-prepare-for-jee-main")
swot_bot.add("https://www.allenoverseas.com/blog/jee-main-2024-exam-strategies-subject-wise-preparation-tips/")
swot_bot.add("https://www.vedantu.com/jee-main/topics")
swot_bot.add("https://www.pw.live/exams/wp-content/uploads/2024/01/syllabus-for-jee-main-2024-as-on-01-november-2023-1-3.pdf")
swot_bot.add("https://www.pw.live/exams/wp-content/uploads/2024/01/syllabus-for-jee-main-2024-as-on-01-november-2023-4-8.pdf")
swot_bot.add("https://www.pw.live/exams/jee/jee-main-chemistry-syllabus/")
swot_bot.add("https://www.pw.live/topics-chemistry-class-11")
swot_bot.add("https://www.pw.live/topics-chemistry-class-12")
system_prompt = """You are an advanced language model trained to analyze student responses from a questionnaire on Academic, Cognitive, and Study Profile aspects related to JEE Mains preparation. Your task is to generate a personalized SCO (Strengths, Challenges, Opportunities) analysis and an Action Plan section based on the user's inputs.
Questionnaire Structure:
Academic Profile:
- Confidence scores in various subjects/topics and subtopics covered in JEE Mains (e.g., Physical Chemistry: Electrochemistry, Redox Reactions; Inorganic Chemistry: Transition Elements, Periodic Table, Representative Elements)
Cognitive Profile:
- Learning styles (visual, auditory, kinesthetic)
- Problem-solving abilities
- Time management skills
- Attention span and focus
Study Profile:
- Study habits (consistent/irregular, self-study/coaching)
- Average study hours per day
- Revision strategies
- Test-taking strategies
Given: You have been provided with the weightages of different topics/subjects in the JEE Mains exam and common knowledge specific to the JEE context. Additionally, you have access to a database that maps specific subjects/topics to general cognitive traits and skills required for success in those areas.
Output Structure:
SCO Analysis:
Strengths:
- List the student's strengths based on their high confidence scores, favorable cognitive abilities, and effective study habits.
- Identify general cognitive traits and skills the student excels at based on their performance in specific subjects/topics and subtopics (e.g., strong visualization skills for organic chemistry, pattern recognition abilities for algebra, etc.)
- Highlight overarching trends in the student's strengths across related subjects/topics (e.g., strong in Physical Chemistry but struggles in Inorganic Chemistry)
Challenges:
- Identify the areas where the student faces difficulties based on low confidence scores, cognitive limitations, and ineffective study habits.
- Highlight general cognitive traits and skills the student struggles with based on their performance in specific subjects/topics and subtopics.
- Identify overarching trends in the student's weaknesses across related subjects/topics.
Opportunities:
- Suggest opportunities for improvement by leveraging the student's strengths and addressing their challenges.
- Recommend ways to enhance the general cognitive traits and skills required for success in specific subjects/topics and subtopics.
Action Plan:
- Provide a detailed, subject/topic/subtopic-specific action plan tailored to the student's SCO analysis.
- Recommend targeted strategies, resources, and techniques to improve their preparation in the identified areas of weakness, including subject-specific cognitive skills and study behaviors.
- Suggest ways to enhance their strengths and capitalize on opportunities, including leveraging their strong cognitive traits and effective study habits.
- Incorporate time management, revision, and test-taking strategies specific to JEE Mains and the identified subjects/topics/subtopics.
- Address overarching trends in the student's strengths and weaknesses across related subjects/topics, and categorize this insight under appropriate headings.
|
Your analysis and action plan should be comprehensive, consistent, and tailored to the individual student's responses while leveraging your knowledge of the JEE Mains exam context, the mapping of subjects/topics to general cognitive traits and skills, and the ability to identify overarching trends across related subjects/topics."""
#SOCA v1 CODE END
# Pydantic model for request
class ScoreInput(BaseModel):
score_percentages: Dict[str, float]
time_percentages: Dict[str, float]
#SOCA v1 Inputs
confidence_scores_str: str
problem_solving_approach: str
thorough_understanding: str
feedback: str
misconception: str
time_management: str
time_division: str
mock_test_frequency: str
progress_monitoring: str
study_methods: str
study_techniques: str
# Helper functions
def get_final_score(score_percentages: Dict[str, float], time_percentages: Dict[str, float]) -> Dict[str, float]:
final_score = {}
for skill in score_percentages:
score_avg = (score_percentages[skill] + time_percentages[skill]) / 2
final_score[skill] = score_avg
return final_score
def get_strengths_and_weaknesses(final_score: Dict[str, float]):
sorted_skills = sorted(
[(skill, score) for skill, score in final_score.items()],
key=lambda item: item[1],
reverse=True
)
num_skills = len(sorted_skills)
if num_skills == 0:
return [], [], []
split1 = num_skills // 3
split2 = 2 * (num_skills // 3)
strengths = sorted_skills[:split1]
opportunities = sorted_skills[split1:split2]
challenges = sorted_skills[split2:]
return strengths, opportunities, challenges
# FastAPI route
@app.post("/analyze")
async def analyze_scores(input_data: ScoreInput):
final_score = get_final_score(input_data.score_percentages, input_data.time_percentages)
strengths, opportunities, challenges = get_strengths_and_weaknesses(final_score)
#SOCA v1 Code Output
user_response = f"""Confidence score of the student across different subjects out of 10 :{input_data.confidence_scores_str},
'When faced with complex,multi-stemp problems in JEE, how likely are you to approach problem-solving systematically, breaking down each step ?':{input_data.problem_solving_approach},
'In your JEE preparation, how likely are you to ensure thorough understanding of fundamental concepts before moving on to advanced topics ?':{input_data.thorough_understanding},
'How likely are to integrate feedback from practice tests or teachers into your JEE preparation strategy ?':{input_data.feedback},
'When encountering a misconception or misunderstanding in a JEE concept, how likely are you to identify and resolve it ?': {input_data.misconception},
'How likely are you to effectively manage time during JEE exams, especially in sections with limited time constraints?':{input_data.time_management},
'How do you divide your study time among Physics, Chemistry and Mathematics for JEE ? (Allocate Percentage)': {input_data.time_division},
'How often do you use mock tests and past question papers for JEE preparation ?': {input_data.mock_test_frequency},
'How do you monitor your progress in JEE topics or chapters?': {input_data.progress_monitoring},
'How do you adjust your study methods for difficult or new JEE topics ?': {input_data.study_methods},
'What techniques do you use to remember JEE concepts and formulas for a long time ? eg: Flashcards, Mindmap, etc.': {input_data.study_techniques}"""
outputSOCAv1 = swot_bot.query(system_prompt + user_response)
# Groq API call
api_key2 = os.getenv("GROQ_API_KEY")
if not api_key2:
raise HTTPException(status_code=500, detail="Groq API key not found")
client2 = Groq(api_key=api_key2)
client3 = Groq(api_key=os.getenv("GROQ_API_KEY"))
sys_prompt = f"""You are an advanced language model trained to analyze student responses from a questionnaire on Academic, Cognitive, and Study Profile aspects related to JEE Mains preparation. Your task is to generate a personalized SCO (Strengths, Challenges, Opportunities) analysis and an Action Plan section based on the user's inputs.
You have been provided with the strengths {strengths}, Opportunities {opportunities} and Challenges {challenges} skills of the user
Output Structure:
SCO Analysis:
Strengths:
- List the student's strengths based on their {strengths} skills
- Let the student now how they can use these strengths in their JEE preparation and exam to improve their score.
- Also tell them how do they improve their score more.
Opportunities:
- List the student's strengths based on their {opportunities} skills
- Suggest opportunities for improvement by leveraging the student's strengths and addressing their challenges.
- Recommend ways to enhance their score in the {opportunities} skills.
- Also tell them if they improve in these skills what opportunities they have in improving their scores
Challenges:
- List the student's strengths based on their {challenges} skills
- Guide the student that these skills are basically the core area where they are lacking
- Tell them that if they continue not focusing upon them they might get far away from their JEE goal.
Action Plan:
- Provide a detailed plan to the student to improve in the {challenges} skills.
- Recommend targeted strategies, resources, and techniques to improve their {challenges} skills.
- Let them know if they improve these areas how it can help boost their scores and make their preparation more effective.
- Incorporate time management, revision, and test-taking strategies specific to JEE Mains and the identified subjects/topics/subtopics.
Things that LLM need to make sure:
1) Your analysis and action plan should be comprehensive, consistent, and tailored to the individual student's responses while leveraging your knowledge of the JEE Mains exam context, the mapping of subjects/topics to general cognitive traits and skills, and the ability to identify overarching trends across related subjects/topics.
2) Make sure you give the output that extracts the student.
3) Make sure you give out output in bullet points.
4) While entering a new line in the output use "\n" new line character.
5) Make the output very much JEE (Joint Entrance Examination) based and give everything with context to Physics , Chemistry and Maths JEE syllabus.
6) Bold text where you are taking chapter names from Physics , Chemsitry and Maths only which are in syllabus of Joint Entrance Examination.
7) Dont use "+" or any other special symbol whenever you want to break a line use "\n" to do it in the output.
"""
try:
chat_completion2 = client2.chat.completions.create(
messages=[
{"role": "system", "content": sys_prompt},
{"role": "user", "content": f"Generate the SOCA analysis based on the system prompt and {strengths}, {opportunities} and {challenges}. MAKE SURE WE STRICTLY FOLLOW THE STRUCTURE."},
],
model="llama3-70b-8192",
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error calling Groq API: {str(e)}")
outputSOCAv2= chat_completion2.choices[0].message.content
system_promptMerge = f""" ROLE 1 : You are an advanced language model trained to analyze student responses from a questionnaire on Academic, Cognitive, and Study Profile aspects related to JEE Mains preparation. Your task is to generate a personalized SCO (Strengths, Challenges, Opportunities) analysis and an Action Plan section based on the user's inputs.
Questionnaire Structure:
Academic Profile:
- Confidence scores in various subjects/topics and subtopics covered in JEE Mains (e.g., Physical Chemistry: Electrochemistry, Redox Reactions; Inorganic Chemistry: Transition Elements, Periodic Table, Representative Elements)
Cognitive Profile:
- Learning styles (visual, auditory, kinesthetic)
- Problem-solving abilities
- Time management skills
- Attention span and focus
Study Profile:
- Study habits (consistent/irregular, self-study/coaching)
- Average study hours per day
- Revision strategies
- Test-taking strategies
Given: You have been provided with the weightages of different topics/subjects in the JEE Mains exam and common knowledge specific to the JEE context. Additionally, you have access to a database that maps specific subjects/topics to general cognitive traits and skills required for success in those areas.
The Analysis of Role 1 is given is {outputSOCAv1}.
ROLE 2 : You are an advanced language model trained to analyze student responses from a questionnaire on Academic, Cognitive, and Study Profile aspects related to JEE Mains preparation. Your task is to generate a personalized SCO (Strengths, Challenges, Opportunities) analysis and an Action Plan section based on the user's inputs.
You have been provided with the strengths {strengths}, Opportunities {opportunities} and Challenges {challenges} skills of the user
The Analysis of Role 2 is {outputSOCAv2}
Now you have to merge both the outputs of Role 1 and Role 2 and validate them also with each other .
You are an Expert Joint Entrance Examination teacher who has to generate the Strengths , Oppourtunities , Challenges and Action Plan of a user.
"""
OutputStructure="""SCO Analysis:
Strengths:
- List the student's strengths based on their strengths skills
- Let the student now how they can use these strengths in their JEE preparation and exam to improve their score.
- Also tell them how do they improve their score more.
Opportunities:
- List the student's strengths based on their opportunities skills
- Suggest opportunities for improvement by leveraging the student's strengths and addressing their challenges.
- Recommend ways to enhance their score in the opportunities skills.
- Also tell them if they improve in these skills what opportunities they have in improving their scores
Challenges:
- List the student's strengths based on their challenges skills
- Guide the student that these skills are basically the core area where they are lacking
- Tell them that if they continue not focusing upon them they might get far away from their JEE goal.
Action Plan:
- Provide a detailed plan to the student to improve in the challenges skills.
- Recommend targeted strategies, resources, and techniques to improve their challenges skills.
- Let them know if they improve these areas how it can help boost their scores and make their preparation more effective.
- Incorporate time management, revision, and test-taking strategies specific to JEE Mains and the identified subjects/topics/subtopics.
Things that LLM need to make sure:
1) Your analysis and action plan should be comprehensive, consistent, and tailored to the individual student's responses while leveraging your knowledge of the JEE Mains exam context, the mapping of subjects/topics to general cognitive traits and skills, and the ability to identify overarching trends across related subjects/topics.
2) Make sure you give the output that extracts the student.
3) Make sure you give out output in bullet points.
4) While entering a new line in the output use "\n" new line character.
5) Make the output very much JEE (Joint Entrance Examination) based and give everything with context to Physics , Chemistry and Maths JEE syllabus.
6) Use Italics, Bold and underline appropriately to improve the output more.
7) Bold text where you are taking chapter names from Physics , Chemsitry and Maths only which are in syllabus of Joint Entrance Examination.
8) Dont use "+" or any other special symbol whenever you want to break a line use "\n" to do it in the output."""
try:
chat_completion3 = client3.chat.completions.create(
messages=[
{"role": "system", "content": system_promptMerge+OutputStructure},
{"role": "user", "content": f"Generate the SOCA analysis based on the system prompt and {OutputStructure}"},
],
model="llama3-70b-8192",
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error calling Groq API: {str(e)}")
outputSOCAv3= chat_completion3.choices[0].message.content
return {"analysis": outputSOCAv3}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)