-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.py
More file actions
1494 lines (1268 loc) · 49.9 KB
/
analyze.py
File metadata and controls
1494 lines (1268 loc) · 49.9 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Skills analysis on collected job JSON files.
Usage:
python analyze.py # analyze today's file
python analyze.py --file data/jobs_2026-04-01.json
python analyze.py --all # merge all JSON files in data/
python analyze.py --llm # also extract skills via LLM (free, via 9router)
python analyze.py --all --llm
python analyze.py --promote # promote LLM-discovered candidates (≥2 jobs) into skills
python analyze.py --promote 3 # same but threshold = 3 jobs
python analyze.py --all --promote # promote first, then analyze with enriched skills
python analyze.py --candidates # show pending skill candidates queue
DB: data/skills.db stores skills catalog + LLM results (auto-created on first run).
To add a term without touching code:
sqlite3 data/skills.db "INSERT OR IGNORE INTO skills(category_id,term) SELECT id,'hetzner' FROM categories WHERE name='Cloud'"
To reject a candidate so it never gets promoted:
sqlite3 data/skills.db "UPDATE skill_candidates SET status='rejected' WHERE term='<term>'"
To add an alias (e.g. German synonym):
sqlite3 data/skills.db \\
"INSERT INTO skill_aliases(skill_id,alias,canonical,lang,alias_type)
SELECT id,'Deutsch','deutsch','de','translation' FROM skills WHERE term='german'"
"""
import argparse
import json
import logging
import re
import sqlite3
import time
import unicodedata
from logging.handlers import RotatingFileHandler
from argparse import Namespace
from collections import Counter, deque
from datetime import date
from pathlib import Path
import pandas as pd
from rich.console import Group
from rich.live import Live
from rich.markup import escape
from rich.panel import Panel
from rich.progress import (
BarColumn,
MofNCompleteColumn,
Progress,
TaskProgressColumn,
TextColumn,
TimeElapsedColumn,
track,
)
from ui_rich import (
console,
is_compact,
make_table,
metric_title,
percent_bar,
print_info,
print_panel,
print_section,
print_success,
)
from config import (
OUTPUT_DIR,
ANALYSIS_ACTIVITY_LOG_BACKUP_COUNT,
ANALYSIS_ACTIVITY_LOG_FILENAME,
ANALYSIS_ACTIVITY_LOG_MAX_BYTES,
NINEROUTER_BASE_URL,
NINEROUTER_MODEL,
NINEROUTER_FALLBACK_MODEL,
NINEROUTER_API_KEY,
LLM_RATE_LIMIT_MAX_WAIT_SECONDS,
LLM_MAX_INPUT_CHARS,
LLM_MAX_OUTPUT_TOKENS,
LLM_RESPONSE_FORMAT_JSON_OBJECT,
RETRY_AFTER_BUFFER_SECONDS,
LLM_CANDIDATE_THRESHOLD,
LLM_LOW_SIGNAL_REFERENCE_SUM,
LLM_LOW_SIGNAL_WARN_BELOW_SUM,
LLM_LOW_SIGNAL_WINDOW_JOBS,
)
from analysis_candidates import (
SKIP_TERMS,
apply_candidates,
print_candidates,
promote_llm_to_candidates,
)
from analysis_db import (
canonical_linkedin_job_key,
init_db,
load_skills,
normalize_term,
open_db,
)
from analysis_llm_cache import _llm_cache_get, _llm_cache_set, _url_key
# ── Display formatting constants ──────────────────────────────────────────────
_REPORT_TOP_SKILLS_COUNT = 30
_REPORT_TOP_CATEGORIES_COUNT = 8
_REPORT_TOP_LOCATIONS_COUNT = 15
_REPORT_TOP_SALARY_COUNT = 20
_REPORT_TOP_MISSING_SKILLS_COUNT = 50
# Rolling log under the progress bar when LLM + verbose (shows cache vs API, waits, errors).
_ACTIVITY_LOG_MAX_LINES = 8
_LOG_LINE_MAX_CHARS = 100
_activity_rotating_logger: logging.Logger | None = None
def _get_activity_rotating_logger() -> logging.Logger:
"""Singleton logger writing the same lines as the Live panel to a size-rotating file."""
global _activity_rotating_logger
if _activity_rotating_logger is not None:
return _activity_rotating_logger
log_path = Path(OUTPUT_DIR) / ANALYSIS_ACTIVITY_LOG_FILENAME
log_path.parent.mkdir(parents=True, exist_ok=True)
logger = logging.getLogger("stackpulse.analysis.activity")
logger.setLevel(logging.INFO)
logger.handlers.clear()
handler = RotatingFileHandler(
log_path,
maxBytes=ANALYSIS_ACTIVITY_LOG_MAX_BYTES,
backupCount=ANALYSIS_ACTIVITY_LOG_BACKUP_COUNT,
encoding="utf-8",
)
handler.setFormatter(
logging.Formatter("%(asctime)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
)
logger.addHandler(handler)
logger.propagate = False
_activity_rotating_logger = logger
return logger
class AnalysisActivityLog:
"""Fixed-size ring buffer of status lines for Live + LLM progress UI."""
__slots__ = ("_lines", "_mirror_to_file")
def __init__(
self,
maxlen: int = _ACTIVITY_LOG_MAX_LINES,
*,
mirror_to_file: bool = True,
) -> None:
self._lines: deque[str] = deque(maxlen=maxlen)
self._mirror_to_file = mirror_to_file
def push(self, line: str) -> None:
raw = line.strip()
if not raw:
return
display = raw
if len(raw) > _LOG_LINE_MAX_CHARS:
display = raw[: _LOG_LINE_MAX_CHARS - 1] + "…"
self._lines.append(display)
if self._mirror_to_file:
_get_activity_rotating_logger().info("%s", raw)
def render(self) -> str:
if not self._lines:
return "[dim]Waiting for first job…[/dim]"
# Titles/URLs may contain "[" — escape so Rich does not treat them as markup.
return "\n".join(escape(line) for line in self._lines)
def _llm_emit(activity_log: AnalysisActivityLog | None, message: str) -> None:
"""Append to Live activity log, or print when no log (legacy / non-UI callers)."""
if activity_log is not None:
activity_log.push(message)
else:
print(message)
def _activity_log_only(activity_log: AnalysisActivityLog | None, message: str) -> None:
"""Rich Live lines only — never print (keeps non-verbose CLI runs quiet on cache/API noise)."""
if activity_log is not None:
activity_log.push(message)
def _llm_stored_skill_row_count(llm_skills: dict[str, list[str]] | None) -> int:
"""Match ``extract_skills_llm`` / DB storage: one row per term per category key (incl. ``_matched``)."""
if not llm_skills:
return 0
return sum(len(v) for v in llm_skills.values())
def _rolling_llm_low_signal_warn(
window: deque[int],
llm_row_count: int,
activity_log: AnalysisActivityLog | None,
state: dict[str, bool],
) -> None:
"""Warn once per episode when the rolling sum of LLM stored rows falls below threshold.
``state`` must be a dict with key ``low_signal_active`` (bool), mutated across jobs.
"""
window.append(llm_row_count)
if len(window) < LLM_LOW_SIGNAL_WINDOW_JOBS:
return
total = sum(window)
below = total < LLM_LOW_SIGNAL_WARN_BELOW_SUM
if below and not state.get("low_signal_active", False):
_activity_log_only(
activity_log,
" ⚠ LLM low signal: last "
f"{LLM_LOW_SIGNAL_WINDOW_JOBS} jobs → {total} stored skill row(s) combined "
f"(<{LLM_LOW_SIGNAL_WARN_BELOW_SUM}; reference ≈{LLM_LOW_SIGNAL_REFERENCE_SUM} "
f"for {LLM_LOW_SIGNAL_WINDOW_JOBS} jobs at typical regex richness). "
f"Per-job row counts: {list(window)} — "
"LLM rows are not comparable to regex hit counts; if the model always returns "
"almost nothing, check prompt/model/JSON output.",
)
state["low_signal_active"] = True
elif not below:
state["low_signal_active"] = False
# ── LLM extraction ────────────────────────────────────────────────────────────
def _build_llm_prompt(skills: dict[str, list[tuple[str, str]]]) -> str:
"""Build a skills-aware LLM prompt.
Serializes existing skill terms grouped by category so the LLM can match
against known terms and only flag genuinely new discoveries.
"""
# Deduplicate display terms per category (aliases share the same display)
lines = []
categories_list = []
for category, term_pairs in skills.items():
unique_terms = sorted({display for display, _ in term_pairs})
lines.append(f" {category}: {', '.join(unique_terms)}")
categories_list.append(category)
skills_block = "\n".join(lines)
categories_block = ", ".join(f'"{c}"' for c in categories_list)
return f"""You are a skill extraction assistant. Below is a catalog of known technical skills grouped by category.
KNOWN SKILLS:
{skills_block}
TASK: Analyze the job description and:
1. List ALL known skill terms that are mentioned or clearly implied. Use the EXACT term from the catalog — do not paraphrase.
2. List any genuinely NEW technical skills/tools/protocols NOT in the catalog. For each, use the EXACT category name from this list — copy verbatim: {categories_block}.
Return ONLY JSON:
{{
"matched": ["term1", "term2"],
"new_terms": [{{"term": "newterm", "category": "Category Name"}}]
}}
Rules:
- "matched" must contain ONLY exact terms from the catalog above, lowercase
- "matched" and "new_terms" must be valid JSON only: comma-separated quoted strings and objects — no sentences, no phrases like "is not mentioned", and no commentary inside or between array elements
- "new_terms": only include specific, concrete technologies, tools, libraries, or protocols — NOT generic concepts like "debugging", "scalability", "containerization", "restful apis", "ci/cd pipelines", "async programming", "design patterns"
- Do NOT include soft skills or company names
- Return ONLY the JSON, no explanation; malformed JSON is discarded
Job description:
"""
def _normalize_text_for_llm(text: str) -> str:
"""Clean job title + description before sending to the LLM.
Scraped HTML/JSON often contains CRLF runs, NBSPs, zero-width characters, and
choppy line breaks (e.g. ``assessment.\\nWe may``). Models may mirror that
structure and emit malformed or truncated pseudo-JSON. Normalizing yields plain
prose paragraphs for a more stable completion.
"""
if not text:
return ""
text = text.replace("\x00", "")
for z in ("\u200b", "\u200c", "\u200d", "\ufeff"):
text = text.replace(z, "")
text = text.replace("\r\n", "\n").replace("\r", "\n")
text = text.replace("\t", " ")
text = unicodedata.normalize("NFKC", text)
lines: list[str] = []
for line in text.split("\n"):
line = re.sub(r"[ \u00a0]+", " ", line).strip()
lines.append(line)
text = "\n".join(lines)
text = re.sub(r"\n{3,}", "\n\n", text)
return text.strip()
def _parse_retry_after(error_message: str) -> int | None:
"""Parse the suggested wait time (seconds) from a 429 error message.
Handles both:
"Please try again in 18m0.864s" (groq TPD exhaustion)
"reset after 1m 4s" (per-minute window reset)
Returns seconds rounded up plus RETRY_AFTER_BUFFER_SECONDS, or None if unparseable.
"""
for pattern in (
r"try again in (?:(\d+)m\s*)?(\d+(?:\.\d+)?)s",
r"reset after (?:(\d+)m\s*)?(\d+(?:\.\d+)?)s",
):
match = re.search(pattern, str(error_message))
if match:
minutes = int(match.group(1)) if match.group(1) else 0
seconds = float(match.group(2))
return int(minutes * 60 + seconds) + RETRY_AFTER_BUFFER_SECONDS
return None
def _repair_json_trailing_commas(s: str) -> str:
"""Remove illegal trailing commas before ``}`` or ``]`` (common LLM mistakes)."""
out = s
for _ in range(16):
nxt = re.sub(r",(\s*[\]}])", r"\1", out)
if nxt == out:
break
out = nxt
return out
def _slice_first_json_object(raw: str) -> str | None:
"""Return the first balanced ``{ ... }`` slice, respecting JSON string escapes."""
start = raw.find("{")
if start < 0:
return None
depth = 0
in_str = False
esc = False
for i in range(start, len(raw)):
ch = raw[i]
if in_str:
if esc:
esc = False
elif ch == "\\":
esc = True
elif ch == '"':
in_str = False
continue
if ch == '"':
in_str = True
continue
if ch == "{":
depth += 1
elif ch == "}":
depth -= 1
if depth == 0:
return raw[start : i + 1]
return None
def _parse_llm_json(raw: str) -> dict:
"""Parse model output as JSON; tolerate extra prose, trailing commas, fenced noise."""
text = raw.strip()
if not text:
raise ValueError("LLM returned empty content")
bases: list[str] = [text]
sliced = _slice_first_json_object(text)
if sliced and sliced not in bases:
bases.append(sliced)
last_err: Exception | None = None
for base in bases:
for variant in (base, _repair_json_trailing_commas(base)):
try:
out = json.loads(variant)
except json.JSONDecodeError as err:
last_err = err
continue
if isinstance(out, dict):
return out
last_err = ValueError("LLM JSON root must be an object")
preview = text[:320] + ("…" if len(text) > 320 else "")
raise ValueError(
f"LLM output was not valid JSON (preview): {preview!r}"
) from last_err
def _format_llm_error(exc: BaseException) -> str:
"""Short, actionable message for logs (avoid dumping full 404 bodies every job)."""
s = str(exc)
low = s.lower()
if "404" in s and (
"credential" in low
or "model_not_found" in low
or "no active" in low
or "invalid_request_error" in low
):
return (
"404: provider/model unavailable in 9router "
"(use NINEROUTER_MODEL=9router-combo or add provider credentials)"
)
if len(s) > 200:
return s[:200] + "…"
return s
_LLM_JSON_PARSE_REPAIR_SUFFIX = (
"\n\nIMPORTANT: Your previous reply was not valid JSON. "
"Output exactly one JSON object. In \"matched\" and \"new_terms\", "
"use only comma-separated quoted strings and objects; do not write sentences, "
"explanations, or commentary inside arrays.\n"
)
def _strip_llm_fences(raw: str) -> str:
text = raw.strip()
if text.startswith("```"):
text = re.sub(r"^```(?:json)?\s*", "", text)
text = re.sub(r"\s*```$", "", text)
return text
def _response_format_unsupported(exc: BaseException) -> bool:
"""True if the server likely rejected OpenAI ``response_format`` (retry without it)."""
msg = str(exc).lower()
if "response_format" in msg or "json_object" in msg:
return True
code = getattr(exc, "status_code", None)
if code == 400 and "parameter" in msg and (
"unknown" in msg or "unsupported" in msg or "invalid" in msg
):
return True
return False
def _llm_chat_completions_create(client, model: str, user_content: str):
"""Call chat completions; optional ``json_object`` mode with one downgrade retry."""
from openai import APIError
create_kwargs = dict(
model=model,
messages=[{"role": "user", "content": user_content}],
max_tokens=LLM_MAX_OUTPUT_TOKENS,
temperature=0,
)
if not LLM_RESPONSE_FORMAT_JSON_OBJECT:
return client.chat.completions.create(**create_kwargs)
try:
return client.chat.completions.create(
**create_kwargs,
response_format={"type": "json_object"},
)
except APIError as err:
if _response_format_unsupported(err):
return client.chat.completions.create(**create_kwargs)
raise
def _llm_call(
client,
model: str,
prompt: str,
text: str,
activity_log: AnalysisActivityLog | None = None,
) -> dict:
"""Execute a single LLM extraction call and return parsed JSON.
On invalid JSON from the model, retries once with a stricter suffix (same HTTP/429 rules as callers).
Raises on any error — callers handle retries and fallback.
"""
body = text[:LLM_MAX_INPUT_CHARS]
user_content = prompt + body
response = _llm_chat_completions_create(client, model, user_content)
raw = response.choices[0].message.content
if raw is None:
raise ValueError("LLM returned no message content")
raw = _strip_llm_fences(raw)
try:
return _parse_llm_json(raw)
except ValueError:
_activity_log_only(
activity_log,
" LLM: invalid JSON — retrying with stricter instruction…",
)
user_content = prompt + body + _LLM_JSON_PARSE_REPAIR_SUFFIX
response = _llm_chat_completions_create(client, model, user_content)
raw = response.choices[0].message.content
if raw is None:
raise ValueError("LLM returned no message content")
raw = _strip_llm_fences(raw)
return _parse_llm_json(raw)
def _call_llm_with_retry(
client,
model: str,
prompt: str,
text: str,
url: str,
attempt_label: str,
activity_log: AnalysisActivityLog | None = None,
) -> dict | None:
"""Execute one LLM call with at most one retry on short 429 windows."""
import json as _json
from openai import APIConnectionError, APIError, RateLimitError
try:
return _llm_call(client, model, prompt, text, activity_log=activity_log)
except RateLimitError as rate_limit_error:
wait_seconds = _parse_retry_after(str(rate_limit_error))
if wait_seconds is not None and wait_seconds <= LLM_RATE_LIMIT_MAX_WAIT_SECONDS:
_llm_emit(
activity_log,
f" [LLM] 429 ({attempt_label}) — sleep {wait_seconds}s, retry…",
)
time.sleep(wait_seconds)
try:
return _llm_call(
client, model, prompt, text, activity_log=activity_log
)
except RateLimitError as retry_error:
_llm_emit(
activity_log,
f" [LLM] 429 again ({attempt_label}): {retry_error}",
)
return None
wait_display = f"{wait_seconds}s" if wait_seconds else "unknown"
_llm_emit(
activity_log,
f" [LLM] 429 ({attempt_label}) — wait {wait_display} exceeds limit, skip",
)
return None
except (APIError, APIConnectionError, _json.JSONDecodeError, ValueError) as error:
_llm_emit(
activity_log,
f" [LLM] failed {url[:60]}…: {_format_llm_error(error)}",
)
return None
def _extract_skills_with_models(
text: str,
url: str,
client,
prompt: str,
activity_log: AnalysisActivityLog | None = None,
) -> dict | None:
"""Run primary model, then optional fallback model on failure."""
result = _call_llm_with_retry(
client,
NINEROUTER_MODEL,
prompt,
text,
url,
NINEROUTER_MODEL,
activity_log=activity_log,
)
if result is not None:
return result
if not NINEROUTER_FALLBACK_MODEL:
return None
_llm_emit(activity_log, f" [LLM] primary failed — trying {NINEROUTER_FALLBACK_MODEL}")
return _call_llm_with_retry(
client,
NINEROUTER_FALLBACK_MODEL,
prompt,
text,
url,
f"fallback:{NINEROUTER_FALLBACK_MODEL}",
activity_log=activity_log,
)
def _normalize_llm_result(
raw_result: dict, skills: dict[str, list[tuple[str, str]]]
) -> dict[str, list[str]]:
"""Convert LLM output to internal cache format.
LLM returns: {"matched": ["python", ...], "new_terms": [{"term": "x", "category": "Y"}, ...]}
Internal: {"_matched": ["python", ...], "Category Name": ["new_term", ...]}
The "_matched" key is an internal marker — _llm_cache_set translates it to
is_matched=1 with the actual skills category when writing to DB.
"""
if "matched" not in raw_result and "new_terms" not in raw_result:
# Old-format LLM result — treat all as new discoveries
return raw_result
# Build set of valid skill display terms for validation
valid_terms = {display for terms in skills.values() for display, _ in terms}
normalized: dict[str, list[str]] = {}
# Matched terms — validate against known skills
matched = raw_result.get("matched", [])
if matched:
validated = [
t for t in matched if isinstance(t, str) and t.lower() in valid_terms
]
if validated:
normalized["_matched"] = validated
# New terms — store under their suggested category
new_terms = raw_result.get("new_terms", [])
for entry in new_terms:
if not isinstance(entry, dict):
continue
term = entry.get("term", "")
category = entry.get("category", "")
if term and category:
normalized.setdefault(category, []).append(term.lower())
return normalized
def extract_skills_llm(
text: str,
url: str,
conn: sqlite3.Connection,
client,
prompt: str = "",
taxonomy: dict[str, list[tuple[str, str]]] | None = None,
activity_log: AnalysisActivityLog | None = None,
) -> dict[str, list[str]]:
"""Call LLM via 9router to extract skills. Uses DB cache to avoid re-calls.
On 429:
- If the suggested wait is ≤ LLM_RATE_LIMIT_MAX_WAIT_SECONDS, sleeps and retries once.
- If NINEROUTER_FALLBACK_MODEL is configured, tries that next.
- Otherwise logs a warning and returns {}.
``activity_log`` (when set) receives human-readable lines for the Live progress UI.
"""
cache_key = _url_key(url)
cached = _llm_cache_get(conn, cache_key)
if cached is not None:
_activity_log_only(activity_log, " LLM: cache hit (no HTTP)")
return cached
text = _normalize_text_for_llm(text)
_activity_log_only(
activity_log,
f" LLM: calling {NINEROUTER_MODEL} — waiting on API…",
)
t0 = time.perf_counter()
result = _extract_skills_with_models(
text, url, client, prompt, activity_log=activity_log
)
elapsed = time.perf_counter() - t0
if result is None:
_activity_log_only(
activity_log,
f" LLM: no usable JSON after {elapsed:.1f}s",
)
return {}
# Normalize LLM output before caching
if taxonomy is not None:
result = _normalize_llm_result(result, taxonomy)
n_terms = sum(len(v) for v in result.values())
_activity_log_only(
activity_log,
f" LLM: OK in {elapsed:.1f}s → {n_terms} skill row(s) to store",
)
_llm_cache_set(conn, url, cache_key, result)
return result
# ── Regex-based taxonomy extraction ──────────────────────────────────────────
def extract_skills(
text: str, taxonomy: dict[str, list[tuple[str, str]]]
) -> dict[str, list[str]]:
"""Match taxonomy terms and aliases against text.
taxonomy format: {category: [(display_term, regex_pattern), ...]}
Returns {category: [display_term, ...]} — aliases resolve to their canonical display term,
duplicates within a category are suppressed.
"""
text_lower = text.lower()
matched_skills: dict[str, list[str]] = {}
for category, term_pairs in taxonomy.items():
deduplicated_displays: set[str] = set()
category_hits: list[str] = []
for display, pattern in term_pairs:
if display not in deduplicated_displays and re.search(
r"\b" + pattern + r"\b", text_lower
):
deduplicated_displays.add(display)
category_hits.append(display)
if category_hits:
matched_skills[category] = category_hits
return matched_skills
# ── Data loading ──────────────────────────────────────────────────────────────
def load_jobs(paths: list[Path]) -> list[dict]:
"""Load and deduplicate jobs from one or more JSON files."""
all_jobs = []
for path in paths:
with open(path, encoding="utf-8") as file_handle:
all_jobs.extend(json.load(file_handle))
seen_url_keys: set[str] = set()
deduplicated_jobs = []
for job in all_jobs:
url_key = canonical_linkedin_job_key(job.get("linkedin_url"))
if not url_key:
deduplicated_jobs.append(job)
continue
if url_key in seen_url_keys:
continue
seen_url_keys.add(url_key)
deduplicated_jobs.append(job)
return deduplicated_jobs
# ── Analysis ──────────────────────────────────────────────────────────────────
def _build_comprehensive_by_category(
skills_found: dict[str, list[str]],
llm_skills: dict[str, list[str]],
skills_catalog: dict[str, list[tuple[str, str]]],
) -> dict[str, list[str]]:
"""Merge regex hits with LLM skills into unified per-category dict.
Matched LLM terms (under '_matched' key) are routed to their catalog category
via reverse lookup. New discoveries are stored under their LLM-suggested category.
"""
merged: dict[str, list[str]] = {
cat: list(hits) for cat, hits in skills_found.items()
}
if not llm_skills:
return merged
# Build reverse index: display_term -> category
term_to_cat: dict[str, str] = {}
for cat, term_pairs in skills_catalog.items():
for display, _ in term_pairs:
term_to_cat.setdefault(display, cat)
for llm_cat, skills in llm_skills.items():
for skill in skills:
if llm_cat == "_matched":
target_cat = term_to_cat.get(skill.lower())
if target_cat is None:
continue
else:
target_cat = llm_cat
existing = merged.setdefault(target_cat, [])
if skill.lower() not in {s.lower() for s in existing}:
existing.append(skill)
return merged
def _analyze_job_row(
job: dict,
taxonomy: dict[str, list[tuple[str, str]]],
llm_client,
conn: sqlite3.Connection | None,
llm_prompt: str,
activity_log: AnalysisActivityLog | None,
) -> dict:
"""One job → one DataFrame row dict (regex + optional LLM)."""
description = job.get("job_description") or ""
title = job.get("job_title") or ""
combined_text = f"{title} {description}"
url = job.get("linkedin_url", "")
skills_found = extract_skills(combined_text, taxonomy)
llm_skills: dict[str, list[str]] = {}
if llm_client and conn is not None:
llm_skills = extract_skills_llm(
combined_text,
url,
conn,
llm_client,
prompt=llm_prompt,
taxonomy=taxonomy,
activity_log=activity_log,
)
skills_by_cat = _build_comprehensive_by_category(
skills_found,
llm_skills,
taxonomy,
)
regex_skills_flat = [skill for hits in skills_found.values() for skill in hits]
all_skills_comprehensive = [
skill for hits in skills_by_cat.values() for skill in hits
]
return {
"job_title": job.get("job_title"),
"company": job.get("company"),
"location": job.get("location"),
"search_location": job.get("search_location"),
"posted_date": job.get("posted_date"),
"salary_extracted": job.get("salary_extracted"),
"linkedin_url": url,
"scraped_date": job.get("scraped_date"),
"applicant_count": job.get("applicant_count"),
"skills_raw": skills_found,
"skills_by_category": skills_by_cat,
"all_skills_flat": regex_skills_flat,
"all_skills_comprehensive": all_skills_comprehensive,
"skills_llm": llm_skills,
"has_description": bool(description.strip()),
}
def analyze(
jobs: list[dict],
taxonomy: dict[str, list[tuple[str, str]]],
llm_client=None,
conn: sqlite3.Connection | None = None,
*,
verbose: bool = True,
activity_log_file: bool = True,
) -> pd.DataFrame:
"""Build a DataFrame with per-job metadata and extracted skills.
When ``verbose`` is True and LLM is enabled, shows a Rich progress bar plus a
rolling log of cache hits, API waits, and responses. Regex-only runs use a
simple progress bar.
When ``activity_log_file`` is True (default), the same lines are appended to
``data/analysis_activity.log`` with size rotation (see config).
"""
llm_prompt = _build_llm_prompt(taxonomy) if llm_client else ""
_bar_label = (
"Per-job: regex + LLM"
if llm_client
else "Per-job: regex taxonomy"
)
use_llm_live_ui = (
verbose
and llm_client is not None
and conn is not None
and len(jobs) > 0
)
use_llm_quiet_file_log = (
not verbose
and activity_log_file
and llm_client is not None
and conn is not None
and len(jobs) > 0
)
job_rows: list[dict] = []
if use_llm_live_ui:
activity_log = AnalysisActivityLog(mirror_to_file=activity_log_file)
llm_row_window: deque[int] = deque(maxlen=LLM_LOW_SIGNAL_WINDOW_JOBS)
llm_low_signal_state: dict[str, bool] = {"low_signal_active": False}
if activity_log_file:
_get_activity_rotating_logger().info(
"=== session start | %d job(s) | LLM + verbose ===",
len(jobs),
)
progress = Progress(
TextColumn("[bold]{task.description}"),
BarColumn(bar_width=None),
MofNCompleteColumn(),
TaskProgressColumn(),
TimeElapsedColumn(),
console=console,
transient=False,
)
task_id = progress.add_task(_bar_label, total=len(jobs))
total = len(jobs)
def render_live() -> Group:
return Group(
progress,
Panel(
activity_log.render(),
title="[bold cyan]LLM & pipeline[/bold cyan]",
subtitle=f"[dim]last {_ACTIVITY_LOG_MAX_LINES} lines[/dim]",
border_style="dim",
padding=(0, 1),
),
)
with Live(
render_live(),
console=console,
refresh_per_second=12,
) as live:
for idx, job in enumerate(jobs, start=1):
title_raw = (job.get("job_title") or "—").strip() or "—"
_activity_log_only(
activity_log,
f"[{idx}/{total}] {title_raw}",
)
row_out = _analyze_job_row(
job,
taxonomy,
llm_client,
conn,
llm_prompt,
activity_log,
)
job_rows.append(row_out)
_rolling_llm_low_signal_warn(
llm_row_window,
_llm_stored_skill_row_count(row_out.get("skills_llm")),
activity_log,
llm_low_signal_state,
)
progress.advance(task_id)
live.update(render_live())
return pd.DataFrame(job_rows)
if use_llm_quiet_file_log:
activity_log = AnalysisActivityLog(mirror_to_file=True)
llm_row_window_q: deque[int] = deque(maxlen=LLM_LOW_SIGNAL_WINDOW_JOBS)
llm_low_signal_state_q: dict[str, bool] = {"low_signal_active": False}
_get_activity_rotating_logger().info(
"=== session start | %d job(s) | LLM + quiet | file log only ===",
len(jobs),
)
total = len(jobs)
for idx, job in enumerate(jobs, start=1):
title_raw = (job.get("job_title") or "—").strip() or "—"
_activity_log_only(
activity_log,
f"[{idx}/{total}] {title_raw}",
)
row_out = _analyze_job_row(
job,
taxonomy,
llm_client,
conn,
llm_prompt,
activity_log,
)
job_rows.append(row_out)
_rolling_llm_low_signal_warn(
llm_row_window_q,
_llm_stored_skill_row_count(row_out.get("skills_llm")),
activity_log,
llm_low_signal_state_q,
)
return pd.DataFrame(job_rows)
job_iter = track(
jobs,
description=_bar_label,
total=len(jobs),
disable=not verbose or len(jobs) == 0,
)
for job in job_iter:
job_rows.append(
_analyze_job_row(
job,
taxonomy,
llm_client,
conn,
llm_prompt,
None,
)
)
return pd.DataFrame(job_rows)
def _print_top_skills(df: pd.DataFrame) -> None:
"""Print top skill frequencies across all postings.
Uses the comprehensive column (regex + LLM merged) when available.
"""
col = (
"all_skills_comprehensive"
if "all_skills_comprehensive" in df.columns
else "all_skills_flat"
)
all_skills: Counter = Counter()
for skills_list in df[col]:
all_skills.update(skills_list)
has_llm = "skills_llm" in df.columns and df["skills_llm"].apply(bool).any()
label = (