Repository navigation
Expand file tree
/
Copy pathagents.py
More file actions
392 lines (336 loc) Β· 13.5 KB
/
Copy pathagents.py
File metadata and controls
392 lines (336 loc) Β· 13.5 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
# agents.py
"""
MedVerify AI - Autonomous Validation Agents
Multi-agent system for healthcare provider directory validation
"""
import time
import re
from lookup_tables import (
SPECIALTY_LIST,
CITY_TYPOS,
PINCODE_TO_CITY,
is_valid_indian_phone,
is_valid_pincode,
matches_reg_pattern,
all_required_fields_present,
REQUIRED_FIELDS
)
# ============================================================================
# AGENT 1: DATA VALIDATION ENGINE
# ============================================================================
# Purpose: Validate provider data fields against strict rules
# Scoring: 5 checks Γ 20 points = 0-100
# ============================================================================
def _validate_phone(phone, issues_list):
"""
Check if phone number is valid Indian format.
Args:
phone (str): Phone number to validate
issues_list (list): List to append issues to
Returns:
int: 20 if valid, 0 if invalid
"""
if not is_valid_indian_phone(phone):
issues_list.append("Invalid phone format")
return 0
return 20
def _validate_pincode(pincode, issues_list):
"""
Check if pincode is valid 6-digit Indian postal code.
Args:
pincode (str): Pincode to validate
issues_list (list): List to append issues to
Returns:
int: 20 if valid, 0 if invalid
"""
if not is_valid_pincode(pincode):
issues_list.append("Invalid pincode format")
return 0
return 20
def _validate_specialty(specialty, issues_list):
"""
Check if specialty is in approved SPECIALTY_LIST.
Case-insensitive matching.
Args:
specialty (str): Specialty to validate
issues_list (list): List to append issues to
Returns:
int: 20 if valid, 0 if invalid
"""
if not specialty:
issues_list.append("Specialty not provided")
return 0
# Case-insensitive check
specialty_upper = str(specialty).strip().upper()
specialty_list_upper = [s.upper() for s in SPECIALTY_LIST]
if specialty_upper not in specialty_list_upper:
issues_list.append(f"Specialty '{specialty}' not in approved list")
return 0
return 20
def _validate_registration_number(registration_no, issues_list):
"""
Check if registration number matches expected pattern.
Expected: 2-4 letter prefix + 5-11 digits
Examples: MCI10012345, TN0001234, KA123456
Args:
registration_no (str): Registration number to validate
issues_list (list): List to append issues to
Returns:
int: 20 if valid, 0 if invalid
"""
if not matches_reg_pattern(registration_no):
issues_list.append(f"Registration number '{registration_no}' format invalid")
return 0
return 20
def _validate_required_fields(record, issues_list):
"""
Check if all required fields are present and non-empty.
Args:
record (dict): Record to validate
issues_list (list): List to append issues to
Returns:
int: 20 if all present, 0 if any missing
"""
if not all_required_fields_present(record):
missing = []
for field in REQUIRED_FIELDS:
if field not in record or str(record[field]).strip() == '':
missing.append(field)
issues_list.append(f"Missing required fields: {', '.join(missing)}")
return 0
return 20
def agent_1_validation(record):
"""
AGENT 1: Data Validation Engine
Validates provider record fields against strict rules.
Scoring System:
- Phone format: 20 points
- Pincode format: 20 points
- Specialty in approved list: 20 points
- Registration number pattern: 20 points
- Required fields present: 20 points
Total: 0-100 points
Args:
record (dict): Single provider record from DataFrame
Returns:
dict: {
'confidence_agent1': int (0-100),
'issues_validation': list of str,
'execution_time_agent1': float (milliseconds)
}
"""
start_time = time.time()
score = 0
issues = []
# ====================================================================
# CHECK 1: REQUIRED FIELDS PRESENT (20 points)
# ====================================================================
score += _validate_required_fields(record, issues)
# If required fields missing, we can't validate further
if score == 0:
execution_time = (time.time() - start_time) * 1000 # Convert to ms
return {
'confidence_agent1': 0,
'issues_validation': issues,
'execution_time_agent1': round(execution_time, 2)
}
# ====================================================================
# CHECK 2: PHONE FORMAT (20 points)
# ====================================================================
phone = record.get('phone', '')
score += _validate_phone(phone, issues)
# ====================================================================
# CHECK 3: PINCODE FORMAT (20 points)
# ====================================================================
pincode = record.get('pincode', '')
score += _validate_pincode(pincode, issues)
# ====================================================================
# CHECK 4: SPECIALTY IN APPROVED LIST (20 points)
# ====================================================================
specialty = record.get('specialty', '')
score += _validate_specialty(specialty, issues)
# ====================================================================
# CHECK 5: REGISTRATION NUMBER PATTERN (20 points)
# ====================================================================
registration_no = record.get('registration_no', '')
score += _validate_registration_number(registration_no, issues)
# ====================================================================
# CALCULATE EXECUTION TIME
# ====================================================================
execution_time = (time.time() - start_time) * 1000 # Convert to milliseconds
# ====================================================================
# RETURN RESULTS
# ====================================================================
return {
'confidence_agent1': score, # 0-100
'issues_validation': issues, # List of what failed
'execution_time_agent1': round(execution_time, 2) # ms
}
# ============================================================================
# TEST SUITE FOR AGENT 1
# ============================================================================
if __name__ == "__main__":
print("\n" + "="*70)
print("AGENT 1 - DATA VALIDATION ENGINE - COMPREHENSIVE TEST SUITE")
print("="*70)
# Test Case 1: Perfect Record
print("\nπ TEST 1: Perfect Record (All fields valid)")
print("-" * 70)
perfect_record = {
'id': 1,
'name': 'Dr. Rajesh Sharma',
'phone': '9876543210',
'city': 'Bangalore',
'specialty': 'Cardiology',
'registration_no': 'MCI10012345',
'years_practice': 8,
'clinic_address': '123 MG Road Bangalore',
'pincode': '560001'
}
result = agent_1_validation(perfect_record)
print(f" Confidence: {result['confidence_agent1']}/100")
print(f" Issues: {result['issues_validation'] if result['issues_validation'] else 'None'}")
print(f" Time: {result['execution_time_agent1']} ms")
assert result['confidence_agent1'] == 100, "Perfect record should score 100"
print(" β PASSED")
# Test Case 2: Invalid Phone
print("\nπ TEST 2: Invalid Phone Number")
print("-" * 70)
invalid_phone_record = {
'id': 10,
'name': 'Dr. Nisha Nair',
'phone': '98765', # Too short
'city': 'Chennai',
'specialty': 'Neurology',
'registration_no': 'MCI10012354',
'years_practice': 2,
'clinic_address': '444 Teynampet Chennai',
'pincode': '600018'
}
result = agent_1_validation(invalid_phone_record)
print(f" Confidence: {result['confidence_agent1']}/100")
print(f" Issues: {result['issues_validation']}")
print(f" Time: {result['execution_time_agent1']} ms")
assert result['confidence_agent1'] == 80, "Should have 4 valid checks, 1 invalid"
assert 'Invalid phone format' in result['issues_validation'], "Should flag phone"
print(" β PASSED")
# Test Case 3: Invalid Specialty
print("\nπ TEST 3: Invalid Specialty")
print("-" * 70)
invalid_specialty_record = {
'id': 12,
'name': 'Dr. Kavya Singh',
'phone': '9776543210',
'city': 'Mumbai',
'specialty': 'InvalidSpec', # Not in list
'registration_no': 'MCI10012356',
'years_practice': 7,
'clinic_address': '45 Fort Mumbai',
'pincode': '400001'
}
result = agent_1_validation(invalid_specialty_record)
print(f" Confidence: {result['confidence_agent1']}/100")
print(f" Issues: {result['issues_validation']}")
print(f" Time: {result['execution_time_agent1']} ms")
assert result['confidence_agent1'] == 80, "Should have 4 valid checks, 1 invalid"
assert any('Specialty' in issue for issue in result['issues_validation']), "Should flag specialty"
print(" β PASSED")
# Test Case 4: Invalid Registration
print("\nπ TEST 4: Invalid Registration Number")
print("-" * 70)
invalid_reg_record = {
'id': 9,
'name': 'Dr. Amit Joshi',
'phone': '9988123456',
'city': 'Ahmedabad',
'specialty': 'Pathology',
'registration_no': 'INVALID_REG', # Bad format
'years_practice': 6,
'clinic_address': '333 SG Highway Ahmedabad',
'pincode': '380015'
}
result = agent_1_validation(invalid_reg_record)
print(f" Confidence: {result['confidence_agent1']}/100")
print(f" Issues: {result['issues_validation']}")
print(f" Time: {result['execution_time_agent1']} ms")
assert result['confidence_agent1'] == 80, "Should have 4 valid checks, 1 invalid"
assert any('Registration' in issue for issue in result['issues_validation']), "Should flag registration"
print(" β PASSED")
# Test Case 5: Missing Required Fields
print("\nπ TEST 5: Missing Required Fields")
print("-" * 70)
missing_fields_record = {
'id': 99,
'name': 'Dr. Unknown',
# Missing phone, city, specialty, registration_no, clinic_address, pincode
}
result = agent_1_validation(missing_fields_record)
print(f" Confidence: {result['confidence_agent1']}/100")
print(f" Issues: {result['issues_validation']}")
print(f" Time: {result['execution_time_agent1']} ms")
assert result['confidence_agent1'] == 0, "Should score 0 if required fields missing"
assert any('Missing' in issue for issue in result['issues_validation']), "Should flag missing fields"
print(" β PASSED")
# Test Case 6: Multiple Issues
print("\nπ TEST 6: Multiple Issues (Invalid Phone + Specialty)")
print("-" * 70)
multi_issue_record = {
'id': 100,
'name': 'Dr. Test',
'phone': '12345', # Invalid
'city': 'TestCity',
'specialty': 'FakeSpecialty', # Invalid
'registration_no': 'MCI10012300',
'years_practice': 5,
'clinic_address': 'Test Address',
'pincode': '560001'
}
result = agent_1_validation(multi_issue_record)
print(f" Confidence: {result['confidence_agent1']}/100")
print(f" Issues: {result['issues_validation']}")
print(f" Time: {result['execution_time_agent1']} ms")
assert result['confidence_agent1'] == 60, "Should have 3 valid checks, 2 invalid"
assert len(result['issues_validation']) == 2, "Should have 2 issues"
print(" β PASSED")
# Test Case 7: +91 Format Phone
print("\nπ TEST 7: Phone with +91 Format")
print("-" * 70)
plus91_record = {
'id': 7,
'name': 'Dr. Sanjay Reddy',
'phone': '+919876123456', # +91 format
'city': 'Bangalore',
'specialty': 'Ophthalmology',
'registration_no': 'MCI10012351',
'years_practice': 10,
'clinic_address': '111 Whitefield Bangalore',
'pincode': '560066'
}
result = agent_1_validation(plus91_record)
print(f" Confidence: {result['confidence_agent1']}/100")
print(f" Issues: {result['issues_validation']}")
print(f" Time: {result['execution_time_agent1']} ms")
assert result['confidence_agent1'] == 100, "Should accept +91 format"
print(" β PASSED")
# Test Case 8: Case-Insensitive Specialty
print("\nπ TEST 8: Case-Insensitive Specialty")
print("-" * 70)
case_insensitive_record = {
'id': 50,
'name': 'Dr. Test',
'phone': '9876543210',
'city': 'Bangalore',
'specialty': 'CARDIOLOGY', # Uppercase
'registration_no': 'MCI10012500',
'years_practice': 5,
'clinic_address': 'Test Address',
'pincode': '560001'
}
result = agent_1_validation(case_insensitive_record)
print(f" Confidence: {result['confidence_agent1']}/100")
print(f" Issues: {result['issues_validation']}")
assert result['confidence_agent1'] == 100, "Should accept uppercase specialty"
print(" β PASSED")
print("\n" + "="*70)
print("β
ALL TESTS PASSED - AGENT 1 VALIDATION ENGINE WORKING CORRECTLY")
print("="*70)