-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator.py
More file actions
227 lines (184 loc) · 9.45 KB
/
Copy pathevaluator.py
File metadata and controls
227 lines (184 loc) · 9.45 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
"""LLM integration for candidate evaluation and message generation."""
import json
import google.generativeai as genai
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
from config import Config
from rich.console import Console
console = Console()
class GeminiEvaluator:
"""Handles Gemini API calls for evaluation and message generation."""
def __init__(self, config: Config):
"""Initialize Gemini client."""
self.config = config
genai.configure(api_key=config.gemini_api_key)
# Load prompts
self.evaluation_prompt_template = self._load_prompt('evaluation.txt')
self.invitation_prompt_template = self._load_prompt('invitation.txt')
# Configure model
self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
def _load_prompt(self, filename: str) -> str:
"""Load prompt template from file."""
prompt_path = self.config.prompts_dir / filename
with open(prompt_path, 'r') as f:
return f.read()
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=2, min=2, max=8),
retry=retry_if_exception_type((Exception,))
)
def evaluate_candidate(self, profile: dict) -> dict:
"""
Evaluate a candidate profile using Gemini.
Returns dict with 'score' (int) and 'reasoning' (str).
"""
try:
# Build prompt with explicit JSON instruction
prompt = self.evaluation_prompt_template.replace('{profile}', profile['full_text'])
prompt += "\n\nIMPORTANT: Return ONLY valid JSON with no additional text. Format: {\"score\": <number>, \"reasoning\": \"<text>\"}"
# Configure for JSON response (simplified for older API versions)
generation_config = {
'temperature': 0.7,
}
# Make API call
response = self.model.generate_content(
prompt,
generation_config=generation_config
)
# Extract and parse JSON from response
response_text = response.text.strip()
# Try to extract JSON if wrapped in markdown code blocks
if '```json' in response_text:
response_text = response_text.split('```json')[1].split('```')[0].strip()
elif '```' in response_text:
response_text = response_text.split('```')[1].split('```')[0].strip()
result = json.loads(response_text)
# Validate response
if 'score' not in result or 'reasoning' not in result:
raise ValueError("Invalid response format from Gemini")
# Ensure score is in valid range
result['score'] = max(1, min(10, int(result['score'])))
# Truncate reasoning if too long
if len(result['reasoning']) > 300:
result['reasoning'] = result['reasoning'][:297] + '...'
return result
except json.JSONDecodeError as e:
console.print(f"[red]Error parsing Gemini response: {e}[/red]")
raise
except Exception as e:
console.print(f"[red]Error evaluating candidate: {e}[/red]")
raise
def generate_invitation(self, profile: dict, max_attempts: int = 3) -> str:
"""
Generate personalized invitation message using Gemini.
Includes retry logic and deterministic name injection fallback.
Returns message string.
"""
name = profile.get('name', 'Unknown')
first_name = profile.get('first_name', '') or (name.split()[0] if name else '')
last_message = None
last_error = None
for attempt in range(max_attempts):
try:
# Build prompt with profile data - prioritize startup info
bio_str = profile.get('bio', '')[:250] if profile.get('bio') else 'their background'
prompt = self.invitation_prompt_template
prompt = prompt.replace('{first_name}', first_name)
prompt = prompt.replace('{bio}', bio_str)
# Emphasize name requirement more on retries
if attempt > 0:
prompt += f"\n\nCRITICAL: You MUST include the name '{first_name}' in the greeting. Start with 'Hi {first_name},' exactly."
prompt += "\n\nIMPORTANT: Return ONLY valid JSON with no additional text. Format: {\"message\": \"<your message>\"}"
# Configure for JSON response
generation_config = {
'temperature': 0.8 - (attempt * 0.1), # Reduce temperature on retries
}
# Make API call
response = self.model.generate_content(
prompt,
generation_config=generation_config
)
# Extract and parse JSON from response
response_text = response.text.strip()
# Try to extract JSON if wrapped in markdown code blocks
if '```json' in response_text:
response_text = response_text.split('```json')[1].split('```')[0].strip()
elif '```' in response_text:
response_text = response_text.split('```')[1].split('```')[0].strip()
result = json.loads(response_text)
message = result.get('message', '')
last_message = message
# Validate message
if self._validate_message(message, name, first_name):
return message
console.print(f"[yellow]Attempt {attempt + 1}: Message validation failed, retrying...[/yellow]")
except json.JSONDecodeError as e:
console.print(f"[yellow]Attempt {attempt + 1}: JSON parse error: {e}[/yellow]")
last_error = e
except Exception as e:
console.print(f"[yellow]Attempt {attempt + 1}: Error: {e}[/yellow]")
last_error = e
# All retries exhausted - try deterministic name injection as last resort
if last_message:
console.print(f"[yellow]Applying deterministic name injection fallback[/yellow]")
fixed_message = self._inject_name_into_message(last_message, name, first_name)
if self._validate_message(fixed_message, name, first_name):
return fixed_message
# Complete failure
if last_error:
raise last_error
raise ValueError(f"Failed to generate valid invitation after {max_attempts} attempts")
def _validate_message(self, message: str, name: str, first_name: str = None) -> bool:
"""
Validate generated invitation message.
Returns True if valid, False otherwise.
"""
# Check length
if len(message) < 50 or len(message) > 300:
console.print(f"[yellow]Message length invalid: {len(message)} chars[/yellow]")
return False
# Check if name is mentioned (case-insensitive)
# Accept either full name OR first name
name_found = False
message_lower = message.lower()
if name.lower() in message_lower:
name_found = True
elif first_name and first_name.lower() in message_lower:
name_found = True
else:
# Try extracting first name from full name
extracted_first = name.split()[0] if name else ''
if extracted_first and len(extracted_first) > 1 and extracted_first.lower() in message_lower:
name_found = True
if not name_found:
console.print(f"[yellow]Message doesn't mention candidate name ({name})[/yellow]")
return False
# Check for generic phrases
generic_phrases = [
'dear candidate',
'to whom it may concern',
'dear sir/madam',
'hello there'
]
for phrase in generic_phrases:
if phrase in message_lower:
console.print(f"[yellow]Message contains generic phrase: {phrase}[/yellow]")
return False
return True
def _inject_name_into_message(self, message: str, name: str, first_name: str = None) -> str:
"""
Deterministically inject name into message if missing.
Returns modified message with name included.
"""
target_name = first_name if first_name else name.split()[0] if name else 'there'
message_lower = message.lower()
# If name already present, return as-is
if target_name.lower() in message_lower or name.lower() in message_lower:
return message
# Inject name at appropriate point
greetings = ['hi!', 'hi,', 'hey!', 'hey,', 'hello!', 'hello,']
for greeting in greetings:
if message_lower.startswith(greeting):
# Insert name after greeting
return message[:len(greeting)] + f" {target_name}," + message[len(greeting):]
# If no greeting, prepend "Hi {name}, "
return f"Hi {target_name}, " + message[0].lower() + message[1:]