Spaces:
Sleeping
Sleeping
File size: 14,022 Bytes
aa8e154 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | """
modules/llm.py β Phase 1 upgrade
LLM backend: Groq | Model: llama-3.3-70b-versatile
"""
import os
from dotenv import load_dotenv
from groq import Groq
load_dotenv()
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
if not GROQ_API_KEY:
raise ValueError("GROQ_API_KEY not set in environment.")
client = Groq(api_key=GROQ_API_KEY)
# llama3-70b-8192 was decommissioned Aug 2025 β replaced with successor
MODEL = 'llama-3.3-70b-versatile'
# ββ Fixed bookend questions βββββββββββββββββββββββββββββββββββββββββββββββββββ
FIRST_QUESTION = "Tell me about yourself and walk me through your background."
LAST_QUESTION = "Where do you see yourself in 5 years, and how does this role fit into that vision?"
# ββ Role-specific system prompts ββββββββββββββββββββββββββββββββββββββββββββββ
ROLE_SYSTEM_PROMPTS = {
"sde": (
"You are a senior Software Development Engineer conducting a technical interview. "
"Focus on data structures, algorithms, system design, coding practices, and past engineering projects."
),
"data scientist": (
"You are a senior Data Scientist conducting a technical interview. "
"Focus on ML/DL concepts, model evaluation, feature engineering, statistics, and past ML projects."
),
"ml engineer": (
"You are a senior ML Engineer conducting a technical interview. "
"Focus on model deployment, MLOps, pipelines, scalability, and production ML systems."
),
"pm": (
"You are a senior Product Manager conducting a behavioral and strategy interview. "
"Focus on product thinking, prioritization, stakeholder management, and past product decisions."
),
"default": (
"You are a professional interviewer conducting a structured interview. "
"Ask thoughtful questions relevant to the candidate's background and target role."
),
}
def _get_system_prompt(job_role: str) -> str:
key = job_role.lower().strip()
for role_key in ROLE_SYSTEM_PROMPTS:
if role_key in key:
return ROLE_SYSTEM_PROMPTS[role_key]
return ROLE_SYSTEM_PROMPTS["default"]
# ββ Question Generator ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def generate_questions(name: str, job_role: str, experience: str,
skills: str, resume_text: str = '', jd_text: str = '',
difficulty: str = 'Medium',
num_questions: int = 2) -> list[str]:
"""
Generate role-specific interview questions personalised to the candidate.
Returns a list: [FIRST_QUESTION, ...generated..., LAST_QUESTION]
"""
system_prompt = _get_system_prompt(job_role)
resume_section = ""
if resume_text:
resume_section = f"\nResume Highlights:\n{resume_text[:2000]}\n"
jd_section = ""
if jd_text:
jd_section = f"\nJob Description Context:\n{jd_text[:1000]}\n"
diff_prompts = {
"Easy": "Keep questions foundational, focusing on core concepts and straightforward past experiences.",
"Medium": "Include a balance of practical implementation challenges and conceptual understanding.",
"Advance": "Focus heavily on complex edge cases, system bottlenecks, in-depth architectural decisions, and deep domain expertise."
}
diff_instruction = diff_prompts.get(difficulty, diff_prompts["Medium"])
user_prompt = f"""Generate exactly {num_questions} interview questions for the following candidate.
Return ONLY a numbered list. No explanation, no preamble, no trailing text.
Candidate Profile:
- Name: {name}
- Target Role: {job_role}
- Experience: {experience}
- Key Skills: {skills}
- Difficulty: {difficulty}
{resume_section}
{jd_section}
Rules:
- Difficulty Tuning: {diff_instruction}
- Mix technical and behavioral questions in equal proportion.
- Make each question specific to their background and the provided Job Description context β avoid generic questions.
- Each question should be a single clear sentence."""
try:
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=0.7,
)
raw = (response.choices[0].message.content or "").strip()
lines = [l.strip() for l in raw.split('\n') if l.strip()]
questions = []
for line in lines:
if line and line[0].isdigit():
q = line.split('.', 1)[-1].strip()
q = q.split(')', 1)[-1].strip()
if q:
questions.append(q)
questions = questions[:num_questions]
# Pad with fallback if parsing returned fewer than expected
fallbacks = [
f"Describe a challenging {job_role} project you've worked on.",
f"How do you stay updated with the latest trends in {job_role}?",
]
while len(questions) < num_questions:
questions.append(fallbacks[len(questions) % len(fallbacks)])
return [FIRST_QUESTION] + questions + [LAST_QUESTION]
except Exception as e:
print(f"[generate_questions] Error: {e}")
return [
FIRST_QUESTION,
f"Tell me about a challenging project you've worked on as a {job_role}.",
LAST_QUESTION,
]
# ββ Follow-up Question Generator βββββββββββββββββββββββββββββββββββββββββββββ
def generate_followup(question: str, answer: str, job_role: str) -> str | None:
"""
Given the candidate's answer, decide if a follow-up is warranted.
Returns a follow-up question string, or None if not needed.
"""
system_prompt = _get_system_prompt(job_role)
user_prompt = f"""You are interviewing a candidate for: {job_role}
Original Question: {question}
Candidate Answer: {answer}
Decide: does this answer warrant a follow-up probe?
- If the answer is vague, incomplete, or raises an interesting point worth exploring β return ONE concise follow-up question.
- If the answer is complete and sufficient β return exactly: NO_FOLLOWUP
Return ONLY the follow-up question or NO_FOLLOWUP. Nothing else."""
try:
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=0.5,
)
result = (response.choices[0].message.content or "").strip()
if result == "NO_FOLLOWUP" or not result:
return None
return result
except Exception as e:
print(f"[generate_followup] Error: {e}")
return None
# ββ Answer Evaluator ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def evaluate_answer(question: str, answer: str, job_role: str,
followup: str = '', followup_answer: str = '') -> dict:
"""
Evaluate candidate's answer. Returns a structured dict with score and feedback.
Optionally includes follow-up Q&A in evaluation context.
"""
if not answer or len(answer.strip()) < 5:
return {
"score": 0,
"score_str": "0/10",
"strength": "No answer detected.",
"improvement": "Please speak clearly into the mic.",
"raw": "Score: 0/10\nStrength: No answer detected.\nImprovement: Please speak clearly into the mic.",
}
followup_section = ""
if followup and followup_answer:
followup_section = f"\nFollow-up Question: {followup}\nFollow-up Answer: {followup_answer}\n"
system_prompt = _get_system_prompt(job_role)
user_prompt = f"""You are evaluating a candidate for the role of: {job_role}
Question: {question}
Candidate Answer: {answer}{followup_section}
Evaluate and respond ONLY in this exact format β no extra text:
Score: X/10
Strength: <one sentence about what they did well>
Improvement: <one specific, actionable suggestion>
Detail: <two sentences of deeper feedback>"""
try:
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=0.3,
)
raw = (response.choices[0].message.content or "").strip()
return _parse_evaluation(raw)
except Exception as e:
print(f"[evaluate_answer] Error: {e}")
fallback = "Score: 5/10\nStrength: Answer provided.\nImprovement: Technical issues prevented detailed evaluation.\nDetail: Please retry."
return _parse_evaluation(fallback)
def _parse_evaluation(raw: str) -> dict:
"""Parse structured evaluation text into a dict."""
result = {"raw": raw, "score": 5, "score_str": "5/10",
"strength": "", "improvement": "", "detail": ""}
for line in raw.split('\n'):
if ':' not in line:
continue
key, _, value = line.partition(':')
key = key.strip().lower()
value = value.strip()
if key == "score":
result["score_str"] = value
try:
result["score"] = int(value.split('/')[0])
except ValueError:
pass
elif key == "strength":
result["strength"] = value
elif key == "improvement":
result["improvement"] = value
elif key == "detail":
result["detail"] = value
return result
# ββ Final Summary Generator βββββββββββββββββββββββββββββββββββββββββββββββββββ
def generate_final_summary(results: list[dict], job_role: str) -> dict:
"""
Generate overall interview summary from all Q&A results.
Each result dict should have: question, answer, feedback (dict or str).
Returns a structured summary dict.
"""
def feedback_str(r):
fb = r.get('feedback', '')
if isinstance(fb, dict):
return fb.get('raw', '')
return str(fb)
all_feedback = '\n\n'.join([
f"Q: {r['question']}\nA: {r['answer']}\nFeedback: {feedback_str(r)}"
for r in results
])
# Compute average score if evaluations are structured
scores = []
for r in results:
fb = r.get('feedback', {})
if isinstance(fb, dict) and 'score' in fb:
scores.append(fb['score'])
avg_score = round(sum(scores) / len(scores), 1) if scores else None
user_prompt = f"""You evaluated a complete mock interview for the role of: {job_role}
Interview Transcript:
{all_feedback}
{'Computed average score across all questions: ' + str(avg_score) + '/10' if avg_score else ''}
Provide a final summary in this EXACT format β no extra text:
Overall Score: X/10
Top Strength: <one sentence>
Top Area to Improve: <one specific, actionable sentence>
Weak Topics: <comma-separated list of 2-3 topic areas to work on>
Final Tip: <one motivating, specific sentence>"""
try:
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are an expert interview coach giving final feedback."},
{"role": "user", "content": user_prompt},
],
temperature=0.4,
)
raw = (response.choices[0].message.content or "").strip()
return _parse_summary(raw, avg_score)
except Exception as e:
print(f"[generate_final_summary] Error: {e}")
fallback = "Overall Score: 5/10\nTop Strength: Completed the interview.\nTop Area to Improve: Practice clearer answers.\nWeak Topics: Communication, Technical Depth\nFinal Tip: Keep practicing β consistency builds confidence!"
return _parse_summary(fallback, avg_score)
def _parse_summary(raw: str, avg_score=None) -> dict:
"""Parse summary text into a structured dict."""
result = {
"raw": raw,
"overall_score": avg_score or 5,
"overall_score_str": f"{avg_score}/10" if avg_score else "5/10",
"top_strength": "",
"top_area_to_improve": "",
"weak_topics": [],
"final_tip": "",
}
for line in raw.split('\n'):
if ':' not in line:
continue
key, _, value = line.partition(':')
key = key.strip().lower().replace(' ', '_')
value = value.strip()
if key == "overall_score":
result["overall_score_str"] = value
try:
result["overall_score"] = float(value.split('/')[0])
except ValueError:
pass
elif key == "top_strength":
result["top_strength"] = value
elif key == "top_area_to_improve":
result["top_area_to_improve"] = value
elif key == "weak_topics":
result["weak_topics"] = [t.strip() for t in value.split(',')]
elif key == "final_tip":
result["final_tip"] = value
return result |