forked from opendatahub-io/opendatahub-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
381 lines (299 loc) · 12.8 KB
/
utils.py
File metadata and controls
381 lines (299 loc) · 12.8 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
import http
import json
from typing import Any
import requests
from requests import Response
from timeout_sampler import retry
from tests.model_explainability.guardrails.constants import GuardrailsDetectionPrompt
from utilities.exceptions import UnexpectedValueError
from utilities.guardrails import get_auth_headers
from utilities.opendatahub_logger import get_logger
LOGGER = get_logger(name=__name__)
def get_chat_detections_payload(content: str, model: str, detectors: dict[str, Any] | None = None) -> dict[str, Any]:
"""
Constructs a chat detections payload for a given content string.
Args:
content: The user's message content.
model: The model identifier to be used.
detectors: Optional. A dictionary specifying detectors to be used.
If None, detectors are not included in the payload.
Returns:
A dictionary representing the chat detections payload.
"""
payload: dict[str, Any] = {
"model": model,
"messages": [
{"role": "user", "content": content},
],
"temperature": 0,
}
if detectors is not None:
payload["detectors"] = detectors
return payload
def verify_and_parse_response(response: Response) -> Any:
assert response.status_code == http.HTTPStatus.OK, (
f"Expected status code {http.HTTPStatus.OK}, got {response.status_code}"
)
response_json = response.json()
LOGGER.info(f"Guardrails Orchestrator detection response:\n{json.dumps(response_json, indent=4)}")
return response_json
def assert_no_errors(errors: list[str], failure_message_prefix: str) -> None:
assert not errors, f"{failure_message_prefix}:\n" + "\n".join(f"- {error}" for error in errors)
def verify_detection(
detections_list: list[dict[str, Any]],
detector_id: str,
detection_name: str,
detection_type: str,
expected_detection_text: str | None = None,
) -> list[str]:
"""
Helper to verify detection results.
Args:
detections_list: List of detection objects
detector_id: Expected detector ID
detection_name: Expected detection name
detection_type: Expected detection type
expected_detection_text: Expected text (if None, just checks text exists and is non-empty)
Returns:
List of error messages
"""
errors = []
if len(detections_list) == 0:
errors.append("Expected detections")
return errors
results = detections_list[0].get("results", [])
if len(results) == 0:
errors.append("Expected at least one detection result, but got 0.")
return errors
detection = results[0]
if detection["detector_id"] != detector_id:
errors.append(f"Expected detector_id {detector_id}, got {detection['detector_id']}")
if detection["detection"] != detection_name:
errors.append(f"Expected detection name {detection_name}, got {detection['detection']}")
if detection["detection_type"] != detection_type:
errors.append(f"Expected detection_type {detection_type}, got {detection['detection_type']}")
detection_text_actual = detection.get("text", "")
if expected_detection_text:
if detection_text_actual != expected_detection_text:
errors.append(f"Expected text {expected_detection_text}, got {detection_text_actual}")
else:
if not detection_text_actual or len(detection_text_actual.strip()) == 0:
errors.append("Expected detection text to be present and non-empty")
return errors
def verify_builtin_detector_unsuitable_input_response(
response: Response, detector_id: str, detection_name: str, detection_type: str, detection_text: str
) -> None:
"""
Verify that a guardrails response indicates an unsuitable input.
Args:
response: The HTTP response object from the guardrails API
detector_id: Expected detector ID
detection_name: Expected detection name
detection_type: Expected detection type
detection_text: Expected detected text
"""
response_data = verify_and_parse_response(response=response)
errors = []
if not response_data:
raise UnexpectedValueError("Expected non-empty response data but got an empty response.")
warnings = response_data.get("warnings")
unsuitable_input_warning: str = "UNSUITABLE_INPUT"
if warnings is None:
raise UnexpectedValueError("Expected warnings in response, got None")
elif len(warnings) != 1:
errors.append(f"Expected 1 warning in response, got {len(warnings)}")
elif warnings[0]["type"] != unsuitable_input_warning:
errors.append(f"Expected warning type {unsuitable_input_warning}, got {warnings[0]['type']}")
input_detections = response_data.get("detections", {}).get("input", [])
if len(input_detections) != 1:
errors.append(f"Expected 1 input detection, but got {len(input_detections)}.")
else:
errors.extend(
verify_detection(
detections_list=input_detections,
detector_id=detector_id,
detection_name=detection_name,
detection_type=detection_type,
expected_detection_text=detection_text,
)
)
assert_no_errors(errors=errors, failure_message_prefix="Input detection verification failed")
def verify_builtin_detector_unsuitable_output_response(
response: Response, detector_id: str, detection_name: str, detection_type: str
) -> None:
"""
Verify that a guardrails response indicates an unsuitable output.
Args:
response: The HTTP response object from the guardrails API
detector_id: Expected detector ID
detection_name: Expected detection name
detection_type: Expected detection type
"""
response_data = verify_and_parse_response(response=response)
errors = []
unsuitable_output_warning = "UNSUITABLE_OUTPUT"
warnings = response_data.get("warnings", [])
if len(warnings) != 1:
errors.append(f"Expected 1 warning in response, got {len(warnings)}")
elif warnings[0]["type"] != unsuitable_output_warning:
errors.append(f"Expected warning type {unsuitable_output_warning}, got {warnings[0]['type']}")
output_detections = response_data.get("detections", {}).get("output", [])
if len(output_detections) < 1:
errors.append(f"Expected at least one output detection, but got {len(output_detections)}.")
else:
errors.extend(
verify_detection(
detections_list=output_detections,
detector_id=detector_id,
detection_name=detection_name,
detection_type=detection_type,
)
)
assert_no_errors(errors=errors, failure_message_prefix="Unsuitable output detection verification failed")
def verify_negative_detection_response(response: Response) -> None:
"""
Verify that a guardrails response indicates no PII detection (negative case).
Args:
response: The HTTP response object from the guardrails API
"""
response_data = verify_and_parse_response(response=response)
errors = []
warnings = response_data.get("warnings")
if warnings:
errors.append(f"Expected no warnings, got {warnings}")
detections = response_data.get("detections")
if detections:
errors.append(f"Expected no detections, got {detections}")
choices = response_data.get("choices", [])
if len(choices) != 1:
errors.append(f"Expected one choice in response, got {len(choices)}")
else:
finish_reason = choices[0].get("finish_reason")
if finish_reason != "stop":
errors.append(f"Expected finish_reason 'stop', got '{finish_reason}'")
message = choices[0].get("message", {})
content = message.get("content")
if not content:
errors.append("Expected message content, got none.")
refusal = message.get("refusal")
if refusal:
errors.append(f"Expected refusal to be null, got {refusal}")
assert_no_errors(errors=errors, failure_message_prefix="Negative detection verification failed")
def create_detector_config(*detector_names: str) -> dict[str, dict[str, Any]]:
detectors_dict = {name: {} for name in detector_names}
return {
"input": detectors_dict.copy(),
"output": detectors_dict.copy(),
}
def verify_health_info_response(host, token, ca_bundle_file):
response = requests.get(url=f"https://{host}/info", headers=get_auth_headers(token=token), verify=ca_bundle_file)
assert response.status_code == http.HTTPStatus.OK
healthy_status = "HEALTHY"
response_data = response.json()
mismatches = []
for service_name, service_info in response_data["services"].items():
if service_info["status"] != healthy_status:
mismatches.append(f"Service {service_name} is not healthy: {service_info['status']}")
assert not mismatches, f"GuardrailsOrchestrator service failures: {mismatches}"
def _send_guardrails_orchestrator_post_request(
url: str,
token: str,
ca_bundle_file: str,
payload: dict[str, Any],
) -> requests.Response:
response = requests.post(
url=url,
headers=get_auth_headers(token=token),
json=payload,
verify=ca_bundle_file,
)
if response.status_code != http.HTTPStatus.OK:
raise TimeoutError(f"Endpoint not available. Status code: {response.status_code}, response: {response.text}")
return response
def send_chat_detections_request(
url: str,
token: str,
ca_bundle_file: str,
content: str,
model: str,
detectors: dict[str, Any] | None = None,
) -> requests.Response:
payload = get_chat_detections_payload(content=content, model=model, detectors=detectors)
return _send_guardrails_orchestrator_post_request(
url=url, token=token, ca_bundle_file=ca_bundle_file, payload=payload
)
@retry(exceptions_dict={TimeoutError: []}, wait_timeout=120, sleep=4)
def send_and_verify_unsuitable_input_detection(
url: str,
token: str,
ca_bundle_file: str,
prompt: GuardrailsDetectionPrompt,
model: str,
detectors: dict[str, Any] | None = None,
):
"""Send a prompt to the GuardrailsOrchestrator and verify that it triggers an unsuitable input detection"""
response = send_chat_detections_request(
url=url, token=token, ca_bundle_file=ca_bundle_file, content=prompt.content, model=model, detectors=detectors
)
verify_builtin_detector_unsuitable_input_response(
response=response,
detector_id=prompt.detector_id,
detection_name=prompt.detection_name,
detection_type=prompt.detection_type,
detection_text=prompt.detection_text,
)
return response
@retry(exceptions_dict={TimeoutError: []}, wait_timeout=120, sleep=1)
def send_and_verify_unsuitable_output_detection(
url: str,
token: str,
ca_bundle_file: str,
prompt: GuardrailsDetectionPrompt,
model: str,
detectors: dict[str, Any] | None = None,
):
"""Send a prompt to the GuardrailsOrchestrator and verify that it triggers an unsuitable output detection"""
response = send_chat_detections_request(
url=url, token=token, ca_bundle_file=ca_bundle_file, content=prompt.content, model=model, detectors=detectors
)
verify_builtin_detector_unsuitable_output_response(
response=response,
detector_id=prompt.detector_id,
detection_name=prompt.detection_name,
detection_type=prompt.detection_type,
)
return response
@retry(exceptions_dict={TimeoutError: []}, wait_timeout=10, sleep=1)
def send_and_verify_negative_detection(
url: str,
token: str,
ca_bundle_file: str,
content: str,
model: str,
detectors: dict[str, Any] | None = None,
):
"""Send a prompt to the GuardrailsOrchestrator and verify that it doesn't trigger any detection"""
response = send_chat_detections_request(
url=url, token=token, ca_bundle_file=ca_bundle_file, content=content, model=model, detectors=detectors
)
verify_negative_detection_response(response=response)
return response
@retry(exceptions_dict={TimeoutError: []}, wait_timeout=10, sleep=1)
def send_and_verify_standalone_detection(
url: str,
token: str,
ca_bundle_file: str,
detector_name: str,
content: str,
expected_min_score: float = 0.9,
):
"""Send a prompt to the standalone detections endpoint and verify that it triggers a detection"""
payload = {"detectors": {detector_name: {}}, "content": content}
response = _send_guardrails_orchestrator_post_request(
url=url, token=token, ca_bundle_file=ca_bundle_file, payload=payload
)
data = response.json()
assert "detections" in data, f"Expected 'detections' key in response, got: {data}"
score = data["detections"][0]["score"]
assert score > expected_min_score, f"Expected score > {expected_min_score}, got {score}"
return response