Add testing module (ab_test, metrics, validator)
This commit is contained in:
43
src/promptforge/testing/validator.py
Normal file
43
src/promptforge/testing/validator.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import re
|
||||
from typing import Any, Dict, List, Optional
|
||||
import json
|
||||
|
||||
from ..core.prompt import ValidationRule
|
||||
from ..core.exceptions import ValidationError
|
||||
|
||||
|
||||
class Validator:
|
||||
def __init__(self, rules: Optional[List[ValidationRule]] = None):
|
||||
self.rules = rules or []
|
||||
|
||||
def validate(self, response: str) -> List[str]:
|
||||
errors = []
|
||||
|
||||
for rule in self.rules:
|
||||
if rule.type == "regex":
|
||||
if rule.pattern and not re.search(rule.pattern, response):
|
||||
errors.append(rule.message or f"Response failed regex validation")
|
||||
|
||||
elif rule.type == "json":
|
||||
try:
|
||||
json.loads(response)
|
||||
except json.JSONDecodeError:
|
||||
errors.append(rule.message or "Response is not valid JSON")
|
||||
|
||||
elif rule.type == "length":
|
||||
min_len = rule.json_schema.get("minLength", 0) if rule.json_schema else 0
|
||||
max_len = rule.json_schema.get("maxLength", float("inf")) if rule.json_schema else float("inf")
|
||||
if len(response) < min_len or len(response) > max_len:
|
||||
errors.append(rule.message or f"Response length must be between {min_len} and {max_len}")
|
||||
|
||||
return errors
|
||||
|
||||
def is_valid(self, response: str) -> bool:
|
||||
return len(self.validate(response)) == 0
|
||||
|
||||
@staticmethod
|
||||
def from_prompt_rules(rules: List[Dict[str, Any]]) -> "Validator":
|
||||
validation_rules = []
|
||||
for rule_data in rules:
|
||||
validation_rules.append(ValidationRule(**rule_data))
|
||||
return Validator(validation_rules)
|
||||
Reference in New Issue
Block a user