-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
225 lines (198 loc) · 7.03 KB
/
utils.py
File metadata and controls
225 lines (198 loc) · 7.03 KB
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
import os
import re
import json
import datetime
import uuid
import logging
import requests
import tempfile
from typing import Any, Optional, Dict
from dotenv import load_dotenv
from langchain_core.prompts import PromptTemplate
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
load_dotenv()
# --- Prompts ---
candidate_evaluation_prompt = PromptTemplate.from_template(
"""
You are an experienced hiring manager. Evaluate the candidate strictly against the provided job requirements.
JOB REQUIREMENTS (text):
{job_requirements}
CANDIDATE RESUME (structured JSON):
{resume_json}
TASK:
1. Analyze how well the candidate meets the job requirements.
2. Provide a single numeric score from 0.0 to 10.0 (higher = better fit).
3. Provide a concise justification (maximum 2 sentences) summarizing the key strengths and gaps relevant to the role.
4. Base your evaluation ONLY on the information explicitly present in the resume.
OUTPUT FORMAT (must be valid JSON, no extra text, no explanations):
{{
"score": float (0-10),
"justification": "<max 2 sentences>"
}}
"""
)
resume_parse_prompt = PromptTemplate.from_template(
"""
You are a strict parser. Convert the following resume text into JSON matching the "Standard schema" shown below. Output ONLY valid JSON.
Standard schema:
{{
"id": "uuid",
"name": "string",
"label": "string (job title/role)",
"contact": {{
"email": "string",
"phone": "string",
"city": "string",
"region": "string",
"country": "string",
"links": [
{{"label":"GitHub","url":"string"}},
{{"label":"LinkedIn","url":"string"}}
]
}},
"summary": "string",
"experience": [
{{
"id":"uuid",
"title":"string",
"company":"string",
"location":"string",
"start_date":"YYYY-MM",
"end_date":"YYYY-MM or PRESENT",
"employment_type":"Full-time/Part-time/Contract/Internship",
"achievements":["string"],
"metrics":[{{"metric":"revenue/efficiency/etc","value":"string"}}],
"keywords":["string"]
}}
],
"education":[
{{
"id":"uuid",
"degree":"string",
"field":"string",
"school":"string",
"start_date":"YYYY-MM",
"end_date":"YYYY-MM",
"gpa":"string",
"honors":"string"
}}
],
"projects":[
{{
"id":"uuid",
"title":"string",
"description":"string",
"technologies":["string"],
"link":"string",
"start_date":"YYYY-MM",
"end_date":"YYYY-MM"
}}
],
"skills":[
{{"name":"string","level":"beginner|intermediate|advanced|expert","years":number}}
],
"certifications":[
{{"name":"string","issuer":"string","date":"YYYY-MM"}}
],
"languages":[
{{"language":"string","proficiency":"basic|conversational|fluent|native"}}
],
"volunteer":[
{{"role":"string","organization":"string","start_date":"YYYY-MM","end_date":"YYYY-MM","description":"string"}}
],
"updated_at":"YYYY-MM-DD"
}}
Resume text:
---
{resume_text}
---
Rules:
- Normalize dates to YYYY-MM or "PRESENT".
- Extract contact emails, phones, links into contact.links.
- For each experience, include at least 1 bullet in achievements.
"""
)
# --- Core Functions ---
def llm_invoke(instruction: str) -> str:
"""Invokes the LLM (Groq) with the given instruction."""
try:
from langchain_groq import ChatGroq
# Using a standard Groq model for reliability
model_name = os.getenv("GROQ_MODEL", "openai/gpt-oss-20b")
llm = ChatGroq(model=model_name, temperature=0)
response = llm.invoke(input=instruction)
return response.content
except Exception as e:
logger.error(f"Error invoking Groq LLM: {e}")
# Fallback or re-raise
raise
def extract_json_from_markdown(md: str) -> Any:
"""Extracts JSON content from a markdown code block or raw string."""
pattern = r'```(?:json)?\s*([\s\S]*?)\s*```'
match = re.search(pattern, md, re.MULTILINE)
json_text = match.group(1) if match else md.strip()
try:
return json.loads(json_text)
except json.JSONDecodeError as e:
logger.error(f"Failed to parse JSON: {json_text[:100]}...")
raise ValueError(f"Invalid JSON content: {e}")
def extract_candidate_info(resume_url_or_path: str) -> Dict:
"""Downloads (if URL) and parses a PDF resume into structured JSON."""
from langchain_community.document_loaders import PDFMinerLoader
is_url = resume_url_or_path.startswith("http")
if is_url:
logger.info(f"Downloading resume from URL: {resume_url_or_path}")
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
response = requests.get(resume_url_or_path)
response.raise_for_status()
tmp.write(response.content)
tmp_path = tmp.name
else:
tmp_path = resume_url_or_path
try:
logger.info(f"Parsing PDF: {tmp_path}")
loader = PDFMinerLoader(tmp_path)
doc = loader.load()
if not doc:
raise ValueError("PDF loaded but no content found.")
resume_text = doc[0].page_content
response_content = llm_invoke(resume_parse_prompt.format(resume_text=resume_text))
return extract_json_from_markdown(response_content)
finally:
if is_url and os.path.exists(tmp_path):
os.remove(tmp_path)
def llm_score(resume_content: Any, job_requirements: str) -> Dict:
"""Scores a candidate's resume against job requirements using LLM."""
if isinstance(resume_content, dict):
resume_content = json.dumps(resume_content)
instruction = candidate_evaluation_prompt.format(
job_requirements=job_requirements,
resume_json=resume_content
)
response_content = llm_invoke(instruction)
return extract_json_from_markdown(response_content)
def schedule_jitsi_meeting(email: str, name:str, time :str) -> Dict:
meet_id = uuid.uuid4().hex[:8]
room = f"{name}-Interview_Invite-{meet_id}"
link = f"https://meet.jit.si/{room}"
return {
"candidate_email": email,
"meeting_id": meet_id,
"meeting_url": link,
"meeting_time": time
}
def send_email(candidate_email: str, subject: str, body: str) -> None:
"""Sends an email using the internal mail module."""
import mail
try:
logger.info(f"Attempting to send email to {candidate_email}...")
mail.send(subject=subject, receiver_mail=candidate_email, html_content=body)
logger.info(f"✅ Email sent successfully to {candidate_email}")
except Exception as e:
logger.error(f"❌ Failed to send email to {candidate_email}: {e}")
raise