-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
257 lines (214 loc) · 8.53 KB
/
Copy pathmodels.py
File metadata and controls
257 lines (214 loc) · 8.53 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
from __future__ import annotations
from datetime import datetime, timezone
from enum import Enum
from typing import Any
from uuid import uuid4
from pydantic import BaseModel, Field, field_validator, model_validator
from config import get_logger
logger = get_logger(__name__)
class DecisionValue(str, Enum):
REJECT = "reject"
HOLD = "hold"
SHORTLIST = "shortlist"
class ConfidenceValue(str, Enum):
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
class RequirementType(str, Enum):
MUST_HAVE = "must_have"
NICE_TO_HAVE = "nice_to_have"
RESPONSIBILITY = "responsibility"
RED_FLAG = "red_flag"
def _clamp_score(value: Any, fallback: float = 0.0) -> float:
try:
numeric_value = float(value)
except (TypeError, ValueError):
return fallback
return max(0.0, min(100.0, numeric_value))
def _ensure_string_list(value: Any) -> list[str]:
if value is None:
return []
if isinstance(value, list):
normalized_items = []
for item in value:
text = str(item).strip()
if text:
normalized_items.append(text)
return normalized_items
text = str(value).strip()
return [text] if text else []
class VacancyScorecard(BaseModel):
must_have: list[str] = Field(default_factory=list)
nice_to_have: list[str] = Field(default_factory=list)
responsibilities: list[str] = Field(default_factory=list)
soft_skills: list[str] = Field(default_factory=list)
red_flags: list[str] = Field(default_factory=list)
@field_validator(
"must_have",
"nice_to_have",
"responsibilities",
"soft_skills",
"red_flags",
mode="before",
)
@classmethod
def normalize_list_fields(cls, value: Any) -> list[str]:
return _ensure_string_list(value)
class FitMatrixItem(BaseModel):
criterion: str
type: RequirementType
found: bool = False
evidence: list[str] = Field(default_factory=list)
criterion_score: float = 0.0
confidence: ConfidenceValue = ConfidenceValue.LOW
@field_validator("criterion")
@classmethod
def validate_criterion(cls, value: str) -> str:
normalized_value = value.strip()
if not normalized_value:
raise ValueError("criterion must not be empty")
return normalized_value
@field_validator("evidence", mode="before")
@classmethod
def normalize_evidence(cls, value: Any) -> list[str]:
return _ensure_string_list(value)
@field_validator("criterion_score")
@classmethod
def validate_criterion_score(cls, value: float) -> float:
if not 0 <= value <= 100:
raise ValueError("criterion_score must be between 0 and 100")
return float(value)
class AnalysisResult(BaseModel):
score: float
strengths: list[str] = Field(default_factory=list)
weaknesses: list[str] = Field(default_factory=list)
missing_skills: list[str] = Field(default_factory=list)
summary: str
candidate_name: str = ""
decision: DecisionValue = DecisionValue.HOLD
confidence: ConfidenceValue = ConfidenceValue.LOW
vacancy_scorecard: VacancyScorecard = Field(default_factory=VacancyScorecard)
fit_matrix: list[FitMatrixItem] = Field(default_factory=list)
risks: list[str] = Field(default_factory=list)
interview_questions: list[str] = Field(default_factory=list)
heuristic_score: float = 0.0
llm_score: float = 0.0
final_score: float = 0.0
@field_validator(
"score",
"heuristic_score",
"llm_score",
"final_score",
)
@classmethod
def validate_score_fields(cls, value: float) -> float:
if not 0 <= value <= 100:
raise ValueError("score fields must be between 0 and 100")
return float(value)
@field_validator(
"strengths",
"weaknesses",
"missing_skills",
"risks",
"interview_questions",
mode="before",
)
@classmethod
def normalize_text_lists(cls, value: Any) -> list[str]:
return _ensure_string_list(value)
@field_validator("summary", mode="before")
@classmethod
def normalize_summary(cls, value: Any) -> str:
text = str(value or "").strip()
if not text:
raise ValueError("summary must not be empty")
return text
@field_validator("candidate_name", mode="before")
@classmethod
def normalize_candidate_name(cls, value: Any) -> str:
return str(value or "").strip()
@model_validator(mode="after")
def sync_score_and_final_score(self) -> AnalysisResult:
if self.final_score == 0.0 and self.score != 0.0:
self.final_score = self.score
if self.score != self.final_score:
self.score = self.final_score
return self
class HistoryRecord(BaseModel):
id: str = Field(default_factory=lambda: str(uuid4()))
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
candidate_name: str
vacancy_excerpt: str
analysis: AnalysisResult
@field_validator("candidate_name", "vacancy_excerpt")
@classmethod
def validate_non_empty_text(cls, value: str) -> str:
normalized_value = value.strip()
if not normalized_value:
raise ValueError("text fields must not be empty")
return normalized_value
def build_analysis_result(payload: dict[str, Any] | None, candidate_name: str = "") -> AnalysisResult:
logger.debug(
"Building analysis result from payload",
extra={
"candidate_name": candidate_name,
"has_payload": bool(payload),
"payload_keys": sorted(payload.keys()) if payload else [],
},
)
payload = payload or {}
required_defaults_applied: list[str] = []
normalized_payload: dict[str, Any] = {
"candidate_name": str(payload.get("candidate_name") or candidate_name).strip(),
"strengths": _ensure_string_list(payload.get("strengths")),
"weaknesses": _ensure_string_list(payload.get("weaknesses")),
"missing_skills": _ensure_string_list(payload.get("missing_skills")),
"summary": str(payload.get("summary") or "Insufficient evidence to generate a summary.").strip(),
"decision": payload.get("decision") or DecisionValue.HOLD.value,
"confidence": payload.get("confidence") or ConfidenceValue.LOW.value,
"vacancy_scorecard": payload.get("vacancy_scorecard") or {},
"fit_matrix": payload.get("fit_matrix") or [],
"risks": _ensure_string_list(payload.get("risks")),
"interview_questions": _ensure_string_list(payload.get("interview_questions")),
"heuristic_score": _clamp_score(payload.get("heuristic_score"), fallback=0.0),
"llm_score": _clamp_score(payload.get("llm_score"), fallback=0.0),
}
if "summary" not in payload or not str(payload.get("summary") or "").strip():
required_defaults_applied.append("summary")
for list_field_name in ("strengths", "weaknesses", "missing_skills"):
if list_field_name not in payload:
required_defaults_applied.append(list_field_name)
raw_final_score = payload.get("final_score", payload.get("score"))
raw_score = payload.get("score", raw_final_score)
normalized_final_score = _clamp_score(raw_final_score, fallback=_clamp_score(raw_score, 0.0))
normalized_payload["final_score"] = normalized_final_score
normalized_payload["score"] = normalized_final_score
if "vacancy_scorecard" not in payload:
required_defaults_applied.append("vacancy_scorecard")
if "fit_matrix" not in payload:
required_defaults_applied.append("fit_matrix")
if "decision" not in payload:
required_defaults_applied.append("decision")
if "confidence" not in payload:
required_defaults_applied.append("confidence")
if "risks" not in payload:
required_defaults_applied.append("risks")
if "interview_questions" not in payload:
required_defaults_applied.append("interview_questions")
if required_defaults_applied:
logger.warning(
"Analysis payload required fallback normalization",
extra={"defaults_applied": sorted(set(required_defaults_applied))},
)
else:
logger.debug("Analysis payload contained all expected keys")
result = AnalysisResult.model_validate(normalized_payload)
logger.debug(
"Analysis result built successfully",
extra={
"candidate_name": result.candidate_name,
"final_score": result.final_score,
"fit_matrix_items": len(result.fit_matrix),
},
)
return result