Spaces:
Sleeping
Sleeping
from groq import Groq | |
from typing import List, Dict | |
client = Groq() | |
async def generate_questions(career: str, qualifications: str) -> Dict: | |
# Use JSON mode to ensure we get structured data | |
system_prompt = ( | |
"You are a helpful assistant that generates a JSON object containing 5 psychological questions as an array of objects." | |
"Each object has a question string and four choices labeled A, B, C, D." | |
"The questions should assess capability for the career as per the qualification" | |
"and should be psychological not anyhting related to the career . Respond ONLY with valid JSON." | |
" The question should be understandable by the qualifications." | |
" It should always follow the json schema." | |
"{ \"$schema\": \"http://json-schema.org/draft-07/schema#\", \"$id\": \"http://example.com/schemas/questions.schema.json\", \"title\": \"Questions Schema\", \"description\": \"A schema describing a list of questions and their choices.\", \"type\": \"object\", \"properties\": { \"questions\": { \"type\": \"array\", \"description\": \"An array of questions.\", \"items\": { \"type\": \"object\", \"properties\": { \"question\": { \"type\": \"string\", \"description\": \"The text of the question.\" }, \"choices\": { \"type\": \"object\", \"description\": \"An object containing multiple choice options.\", \"properties\": { \"A\": { \"type\": \"string\", \"description\": \"Option A answer text.\" }, \"B\": { \"type\": \"string\", \"description\": \"Option B answer text.\" }, \"C\": { \"type\": \"string\", \"description\": \"Option C answer text.\" }, \"D\": { \"type\": \"string\", \"description\": \"Option D answer text.\" } }, \"required\": [\"A\", \"B\", \"C\", \"D\"] } }, \"required\": [\"question\", \"choices\"] } } }, \"required\": [\"questions\"] } " | |
) | |
response = client.chat.completions.create( | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{ | |
"role": "user", | |
"content": f"Career: {career}\nQualifications: {qualifications}\n" | |
f"Generate 5 psychological questions to determine if capable. make the question understable as per the qualification" | |
} | |
], | |
model="llama-3.3-70b-versatile", | |
temperature=0.5, | |
max_tokens=1024, | |
top_p=1, | |
stop=None, | |
stream=False, | |
response_format={"type": "json_object"} # Ensures JSON response | |
) | |
print(response.choices[0].message.content) | |
return response.choices[0].message.content | |
async def evaluate_answers(career: str, qualifications: str, answers: Dict[str, str]) -> Dict: | |
# answers is a dict mapping question_index to chosen answer, e.g. {"0": "A", "1": "C", ...} | |
system_prompt = ( | |
"You are a helpful assistant. You receive a career, qualifications, and " | |
"user answers to previously generated psychological questions. Your primary goal is to assess " | |
"the user's psychological readiness for the career based on their answers. If psychologically capable, " | |
"provide a roadmap tailored to their current qualifications and stage in life to help them achieve the career. " | |
"If they are not psychologically ready, explain the challenges they might face and suggest alternative careers " | |
"aligned with their strengths, along with motivational guidance." | |
"career options and provide motivation." | |
"in a structured JSON: { 'capable': true, 'roadmap': ['Step 1: Gain experience', 'Step 2: Apply for jobs'], }. If not capable, " | |
"suggest alternative careers: { 'capable': false, 'alternatives': [list_of_careers...] }." | |
"and also details evaluation report as statement and also provide some motivation even if capability is false" | |
"Respond ONLY with valid JSON.with following json schema" | |
'{"$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": {"capable": {"type": "boolean"}, "alternatives": {"type": "array", "items": [{"type": "string"}, {"type": "string"}, {"type": "string"}]}, "roadmap": {"type": "array", "items": [{"type": "string"}, {"type": "string"}]}, "evaluation_report": {"type": "string"}, "motivation": {"type": "string"}}, "required": ["capable", "alternatives", "roadmap", "evaluation_report", "motivation"]}' | |
) | |
user_input = { | |
"career": career, | |
"qualifications": qualifications, | |
"answers": answers | |
} | |
print(answers) | |
response = client.chat.completions.create( | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": f"evaluation should be based on answers {answers} not by qualifications {qualifications} and guide them to achive that as per the qualifations {qualifications} to achieve {career}"} | |
], | |
model="llama-3.3-70b-versatile", | |
temperature=0.5, | |
max_tokens=1024, | |
top_p=1, | |
stop=None, | |
stream=False, | |
response_format={"type": "json_object"} # Ensures JSON response | |
) | |
print(response.choices[0].message.content) | |
return response.choices[0].message.content | |