-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_user.py
More file actions
183 lines (140 loc) · 4.99 KB
/
validate_user.py
File metadata and controls
183 lines (140 loc) · 4.99 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
import json
import sys
import os
import re
from pathlib import Path
from typing import Any, Dict
from dotenv import load_dotenv
load_dotenv()
try:
from groq import Groq
except ImportError:
print("Error: groq library not installed. Run: pip install groq")
sys.exit(1)
from prompts import get_validation_prompt
class UserValidator:
def __init__(self):
self.client = Groq()
self.model = os.getenv("GROQ_MODEL")
def validate(self, user_data: Dict[str, Any]) -> Dict[str, Any]:
prompt = get_validation_prompt(user_data)
try:
response = self.client.chat.completions.create(
model=self.model,
max_completion_tokens=1024,
messages=[
{
"role": "user",
"content": prompt
}
]
)
response_text = response.choices[0].message.content
print("DEBUG: \n" + response_text)
result = self._parse_llm_response(response_text)
if not self._validate_schema(result):
return {
"is_valid": False,
"errors": ["Invalid response schema from validator"],
"warnings": []
}
return result
except json.JSONDecodeError as e:
return {
"is_valid": False,
"errors": [f"Failed to parse validator response: {str(e)}"],
"warnings": []
}
except Exception as e:
return {
"is_valid": False,
"errors": [f"Validation error: {str(e)}"],
"warnings": []
}
def _parse_llm_response(self, response_text: str) -> Dict[str, Any]:
json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', response_text, re.DOTALL)
if json_match:
response_text = json_match.group(1)
try:
start_idx = response_text.find('{')
end_idx = response_text.rfind('}')
if start_idx != -1 and end_idx != -1:
json_str = response_text[start_idx:end_idx + 1]
return json.loads(json_str)
except (json.JSONDecodeError, ValueError):
pass
raise json.JSONDecodeError(
"Could not find valid JSON in response",
response_text,
0
)
def _validate_schema(self, result: Dict[str, Any]) -> bool:
required_keys = {"is_valid", "errors", "warnings"}
if not isinstance(result, dict):
return False
if set(result.keys()) != required_keys:
return False
if not isinstance(result.get("is_valid"), bool):
return False
if not isinstance(result.get("errors"), list):
return False
if not isinstance(result.get("warnings"), list):
return False
for item in result.get("errors", []):
if not isinstance(item, str):
return False
for item in result.get("warnings", []):
if not isinstance(item, str):
return False
return True
def load_json_file(file_path: str) -> Dict[str, Any]:
if not os.path.exists(file_path):
raise FileNotFoundError(f"Input file not found: {file_path}")
try:
with open(file_path, 'r') as f:
data = json.load(f)
return data
except json.JSONDecodeError as e:
raise json.JSONDecodeError(
f"Invalid JSON in {file_path}",
e.doc,
e.pos
)
def main():
if len(sys.argv) < 2:
print("Usage: python validate_user.py <input_file.json>")
print("\nExample: python validate_user.py examples/input1.json")
sys.exit(1)
input_file = sys.argv[1]
try:
user_data = load_json_file(input_file)
validator = UserValidator()
result = validator.validate(user_data)
print(json.dumps(result, indent=2))
sys.exit(0 if result.get("is_valid") else 1)
except FileNotFoundError as e:
error_result = {
"is_valid": False,
"errors": [str(e)],
"warnings": []
}
print(json.dumps(error_result, indent=2))
sys.exit(1)
except json.JSONDecodeError as e:
error_result = {
"is_valid": False,
"errors": [f"JSON parsing error: {str(e)}"],
"warnings": []
}
print(json.dumps(error_result, indent=2))
sys.exit(1)
except Exception as e:
error_result = {
"is_valid": False,
"errors": [f"Unexpected error: {str(e)}"],
"warnings": []
}
print(json.dumps(error_result, indent=2))
sys.exit(1)
if __name__ == "__main__":
main()