Repository navigation
Expand file tree
/
Copy pathmatcher.py
More file actions
208 lines (167 loc) · 6.63 KB
/
Copy pathmatcher.py
File metadata and controls
208 lines (167 loc) · 6.63 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
from dotenv import load_dotenv
load_dotenv()
import json
import logging
import os
from parser import match_jobs_for_resume
from pathlib import Path
from typing import Any, Dict, List
from groq import Groq
import requests
from config import CONFIG
# Configure minimal logging
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
USE_GROQ = Groq is not None and GROQ_API_KEY is not None
import tiktoken
def truncate_text_to_tokens(text: str, max_tokens: int = 2000) -> str:
"""Truncate text to a max number of tokens using Llama-compatible tokenizer."""
if not text:
return ""
encoding = tiktoken.get_encoding("cl100k_base") # good proxy for Llama 3
tokens = encoding.encode(text)
if len(tokens) <= max_tokens:
return text
return encoding.decode(tokens[:max_tokens]) + " [...] (truncated)"
def _build_prompt(resume_text: str, jobs: list) -> str:
# ✅ Truncate resume to ~1500 tokens
safe_resume = truncate_text_to_tokens(resume_text, max_tokens=1500)
job_blocks = []
for job in jobs:
title = job.get("title", "N/A")
company = job.get("company", "N/A")
desc = job.get("description", "No description")
location = job.get("location", "N/A")
# ✅ Truncate each job description to ~400 tokens (5 jobs × 400 = 2000)
safe_desc = truncate_text_to_tokens(desc, max_tokens=400)
block = f"""Job: {title}
Company: {company}
Location: {location}
Description: {safe_desc}"""
job_blocks.append(block)
jobs_section = "\n\n---\n\n".join(job_blocks)
prompt = f"""You are an expert recruiter. Analyze the candidate's resume and match it to the following job postings.
Resume:
\"\"\"
{safe_resume}
\"\"\"
Jobs:
\"\"\"
{jobs_section}
\"\"\"
For each job, provide:
- job_title (copy exactly from input)
- match_score (0–10, be strict)
- match_reason (1–2 sentences)
- skill_gaps (list of missing skills, or empty list)
- apply_link (if available in job data) [YOU MUST PROVIDE THIS]
Output ONLY a JSON array of objects with those keys. No other text."""
return prompt
def _call_llm(prompt: str, st) -> str:
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
return "[]"
try:
response = requests.post(
"https://api.groq.com/openai/v1/chat/completions",
headers={
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
},
json={
"model": CONFIG['llm_model'],
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.2,
"max_tokens": 6000,
"response_format": {"type": "json_object"}
},
timeout=20
)
response.raise_for_status()
data = response.json()
return data["choices"][0]["message"]["content"]
except Exception as e:
# Log to Streamlit UI for debugging
# st.error(f"⚠️ Groq API failed (via requests): {e}")
return "[]"
def generate_job_match_report(resume_file_path: str, n_results: int = 10) -> Dict:
"""
Match jobs in batches of 5 to avoid LLM token limits.
Returns sorted matches by match_score (descending).
"""
try:
result = match_jobs_for_resume(
resume_file_path, n_results * 2
) # Fetch more to allow top-N after scoring
resume_text = result["resume_text"]
raw_jobs = result["matches"]["metadatas"]
if not raw_jobs:
return {"error": "No job matches found."}
all_matches = []
batch_size = 5
# Process in batches
for i in range(0, len(raw_jobs), batch_size):
batch = raw_jobs[i : i + batch_size]
prompt = _build_prompt(resume_text, batch)
raw_response = _call_llm(prompt)
try:
parsed = json.loads(raw_response)
if isinstance(parsed, list):
# Merge LLM output with full job metadata
for j, llm_job in enumerate(parsed):
if j >= len(batch):
break
full_job = batch[j].copy()
full_job.update(
{
"match_score": float(llm_job.get("match_score", 0)),
"match_reason": llm_job.get("match_reason", ""),
"skill_gaps": llm_job.get("skill_gaps", []),
"apply_link": llm_job.get(
"apply_link", full_job.get("source_url", "")
),
}
)
all_matches.append(full_job)
except (json.JSONDecodeError, ValueError) as e:
logger.error(f"Batch {i//batch_size + 1} JSON parse failed: {e}")
continue
if not all_matches:
return {"error": "No valid matches returned from LLM."}
# Sort by match_score descending
all_matches.sort(key=lambda x: x.get("match_score", 0), reverse=True)
# Return top n_results
return {"matches": all_matches[:n_results]}
except Exception as e:
logger.exception("Unexpected error in generate_job_match_report")
return {"error": f"Unexpected error: {str(e)}"}
# formatter.py
def format_job_match_report(report: dict) -> str:
if "error" in report:
return f"Error: {report['error']}\n"
if not report.get("matches"):
return "No job matches found.\n"
lines = ["### Top Job Matches\n"]
for i, job in enumerate(report["matches"], 1):
title = job.get("job_title", "N/A")
company = job.get("company", "N/A")
score = job.get("match_score", 0)
reason = job.get("match_reason", "").strip()
gaps = job.get("skill_gaps", [])
link = job.get("apply_link", "").strip()
lines.append(f"\n#### {i}. **{title}**")
lines.append(f"\n**Company**: {company}")
lines.append(f"\n**Match Score**: {score} / 10")
lines.append(f"\n**Why it matches**: {reason}")
if gaps:
gap_list = "\n - ".join(gaps)
lines.append(f"\n**Skill Gaps**:\n - {gap_list}")
else:
lines.append("\n**Skill Gaps**: None")
if link:
# Clean extra whitespace in URL
clean_link = link.split()[0] if link else ""
lines.append(f"\n**Apply**: [View Job]({clean_link})")
lines.append("---")
return "\n".join(lines).strip()