Spaces:
Runtime error
Runtime error
Update modules/llm_handler.py
Browse files- modules/llm_handler.py +45 -21
modules/llm_handler.py
CHANGED
|
@@ -6,37 +6,49 @@ from groq import Groq
|
|
| 6 |
from modules.web_search import search_for_example_answers
|
| 7 |
|
| 8 |
# Initialize the Groq client
|
| 9 |
-
# It will automatically use the API key from your Hugging Face secrets
|
| 10 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 11 |
-
MODEL = "llama3-
|
| 12 |
|
| 13 |
def generate_question(interview_type, document_text):
|
| 14 |
prompt = f"As an expert {interview_type} interviewer, ask one relevant, open-ended question based on this document:\n\n---\n{document_text}\n---"
|
| 15 |
try:
|
| 16 |
chat_completion = client.chat.completions.create(
|
| 17 |
-
messages=[{"role": "user", "content": prompt}],
|
| 18 |
-
model=MODEL,
|
| 19 |
)
|
| 20 |
return chat_completion.choices[0].message.content
|
| 21 |
except Exception as e:
|
| 22 |
return f"Error generating question from API: {e}"
|
| 23 |
|
| 24 |
def evaluate_answer(question, answer):
|
|
|
|
| 25 |
prompt = f"""
|
| 26 |
-
You are
|
| 27 |
-
You MUST provide a score from 1-10 for each of the following three categories.
|
| 28 |
-
The format MUST be exactly `Category: [SCORE]/10`.
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
chat_completion = client.chat.completions.create(
|
| 38 |
-
messages=[{"role": "user", "content": prompt}],
|
| 39 |
-
model=MODEL,
|
| 40 |
)
|
| 41 |
return chat_completion.choices[0].message.content
|
| 42 |
except Exception as e:
|
|
@@ -46,28 +58,40 @@ def parse_scores_from_evaluation(evaluation_text: str) -> dict:
|
|
| 46 |
scores = {
|
| 47 |
'Factual Accuracy': 0,
|
| 48 |
'Relevance & Directness': 0,
|
| 49 |
-
'Structure & Clarity': 0
|
| 50 |
}
|
| 51 |
-
|
|
|
|
| 52 |
matches = re.findall(pattern, evaluation_text, re.IGNORECASE)
|
| 53 |
|
| 54 |
for match in matches:
|
| 55 |
category_name, score_value = match[0].strip(), int(match[1])
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
scores[category_name] = score_value
|
| 58 |
|
| 59 |
print(f"📊 Parsed scores: {scores}")
|
| 60 |
return scores
|
| 61 |
|
| 62 |
def generate_holistic_feedback(full_interview_log):
|
|
|
|
| 63 |
prompt = f"""
|
| 64 |
-
You are a senior
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
"""
|
| 67 |
try:
|
| 68 |
chat_completion = client.chat.completions.create(
|
| 69 |
-
messages=[{"role": "user", "content": prompt}],
|
| 70 |
-
model=MODEL,
|
| 71 |
)
|
| 72 |
return chat_completion.choices[0].message.content
|
| 73 |
except Exception as e:
|
|
|
|
| 6 |
from modules.web_search import search_for_example_answers
|
| 7 |
|
| 8 |
# Initialize the Groq client
|
|
|
|
| 9 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 10 |
+
MODEL = "llama3-70b-8192" # Use the more powerful 70B model for detailed analysis
|
| 11 |
|
| 12 |
def generate_question(interview_type, document_text):
|
| 13 |
prompt = f"As an expert {interview_type} interviewer, ask one relevant, open-ended question based on this document:\n\n---\n{document_text}\n---"
|
| 14 |
try:
|
| 15 |
chat_completion = client.chat.completions.create(
|
| 16 |
+
messages=[{"role": "user", "content": prompt}], model=MODEL
|
|
|
|
| 17 |
)
|
| 18 |
return chat_completion.choices[0].message.content
|
| 19 |
except Exception as e:
|
| 20 |
return f"Error generating question from API: {e}"
|
| 21 |
|
| 22 |
def evaluate_answer(question, answer):
|
| 23 |
+
# This new prompt demands a much higher level of detail
|
| 24 |
prompt = f"""
|
| 25 |
+
You are a meticulous and insightful interview coach. Your task is to provide a highly detailed evaluation of a candidate's answer.
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
**INTERVIEW QUESTION:**
|
| 28 |
+
"{question}"
|
| 29 |
+
---
|
| 30 |
+
**CANDIDATE'S ANSWER:**
|
| 31 |
+
"{answer}"
|
| 32 |
+
---
|
| 33 |
+
**YOUR TASK:**
|
| 34 |
+
Provide a detailed, multi-part evaluation. You MUST include scores and written analysis for each section.
|
| 35 |
+
|
| 36 |
+
**1. Score Breakdown (Format: `Category: [SCORE]/10`)**
|
| 37 |
+
You must provide a score for each of the following:
|
| 38 |
+
- Factual Accuracy: [SCORE]/10
|
| 39 |
+
- Relevance & Directness: [SCORE]/10
|
| 40 |
+
- Structure & Clarity (STAR Method): [SCORE]/10
|
| 41 |
+
|
| 42 |
+
**2. Detailed Written Evaluation**
|
| 43 |
+
- **Strengths:** What did the candidate do well? (e.g., "Good use of a specific example...")
|
| 44 |
+
- **Areas for Improvement:** What were the key weaknesses? (e.g., "The result of the action was unclear...")
|
| 45 |
+
|
| 46 |
+
**3. Concrete Suggestion**
|
| 47 |
+
- **Example Rephrasing:** Provide a short example of how they could have phrased a key part of their answer more effectively.
|
| 48 |
"""
|
| 49 |
try:
|
| 50 |
chat_completion = client.chat.completions.create(
|
| 51 |
+
messages=[{"role": "user", "content": prompt}], model=MODEL
|
|
|
|
| 52 |
)
|
| 53 |
return chat_completion.choices[0].message.content
|
| 54 |
except Exception as e:
|
|
|
|
| 58 |
scores = {
|
| 59 |
'Factual Accuracy': 0,
|
| 60 |
'Relevance & Directness': 0,
|
| 61 |
+
'Structure & Clarity (STAR Method)': 0
|
| 62 |
}
|
| 63 |
+
# Updated regex to match the new category name
|
| 64 |
+
pattern = r"(Factual Accuracy|Relevance & Directness|Structure & Clarity \(STAR Method\)):\s*\[?(\d{1,2})\]?\/10"
|
| 65 |
matches = re.findall(pattern, evaluation_text, re.IGNORECASE)
|
| 66 |
|
| 67 |
for match in matches:
|
| 68 |
category_name, score_value = match[0].strip(), int(match[1])
|
| 69 |
+
# Normalize the key for consistency
|
| 70 |
+
if "Structure" in category_name:
|
| 71 |
+
scores['Structure & Clarity (STAR Method)'] = score_value
|
| 72 |
+
elif category_name in scores:
|
| 73 |
scores[category_name] = score_value
|
| 74 |
|
| 75 |
print(f"📊 Parsed scores: {scores}")
|
| 76 |
return scores
|
| 77 |
|
| 78 |
def generate_holistic_feedback(full_interview_log):
|
| 79 |
+
# This prompt is also enhanced for more detail
|
| 80 |
prompt = f"""
|
| 81 |
+
You are a senior career strategist reviewing a candidate's full interview performance.
|
| 82 |
+
Based on the entire Q&A log, provide a detailed "Overall Performance Summary" and an "Actionable Improvement Plan".
|
| 83 |
+
|
| 84 |
+
**FULL INTERVIEW LOG:**
|
| 85 |
+
---
|
| 86 |
+
{full_interview_log}
|
| 87 |
+
---
|
| 88 |
+
**YOUR TASK:**
|
| 89 |
+
1. **Overall Performance Summary:** Write a detailed paragraph summarizing the candidate's performance. Analyze their communication style, confidence, and consistency. Identify the most significant recurring strengths and weaknesses across all answers.
|
| 90 |
+
2. **Actionable Improvement Plan:** Provide a bulleted list of the top 3 most critical and specific actions the candidate must take. For each action, explain *why* it's important and provide a *concrete example*. (e.g., "- Action: Quantify your achievements. Why: It demonstrates impact. Example: Instead of 'improved the system,' say 'reduced server response time by 15%.'").
|
| 91 |
"""
|
| 92 |
try:
|
| 93 |
chat_completion = client.chat.completions.create(
|
| 94 |
+
messages=[{"role": "user", "content": prompt}], model=MODEL
|
|
|
|
| 95 |
)
|
| 96 |
return chat_completion.choices[0].message.content
|
| 97 |
except Exception as e:
|