File size: 3,373 Bytes
90a947b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
validator module-input and data validation
"""
from typing import Tuple
from src.config.settings import settings


class Validator:
    """validator class"""

    @staticmethod
    def validate_api_key(api_key: str) -> Tuple[bool, str]:
        """api validation"""
        if not api_key or api_key.strip() == "":
            return False, "API KEY should not be empty!"

        if len(api_key.strip()) < 10:
            return False, "API KEY value error"

        return True, "API KEY validation passed"

    @staticmethod
    def validate_base_url(base_url: str) -> Tuple[bool, str]:
        """URL validation"""
        if not base_url or base_url.strip() == "":
            return False, "Base URL should not be empty!"

        if not (base_url.startswith("http://") or base_url.startswith("https://")):
            return False, "Base URL should start with <http://> or <https://>"

        return True, "Base URL validation passed"

    @staticmethod
    def validate_models(attack_model: str, victim_model: str) -> Tuple[bool, str]:
        """model validation"""
        if not attack_model or attack_model.strip() == "":
            return False, "Please select the attack model's name"

        if not victim_model or victim_model.strip() == "":
            return False, "Please select the Victim model's name"

        if attack_model not in settings.AVAILABLE_MODELS:
            return False, f"Attack model '{attack_model}' is not available!"

        if victim_model not in settings.AVAILABLE_MODELS:
            return False, f"Victim model '{victim_model}' is not available!"

        return True, "Model selection validation passed!"

    @staticmethod
    def validate_topic(topic: str) -> Tuple[bool, str]:
        """topic validation"""
        if not topic or topic.strip() == "":
            return False, "Topic should not be empty!"

        if len(topic.strip()) < 3:
            return False, "Topic length should be more than 3 strings!"

        if len(topic.strip()) > 200:
            return False, "Topic length should be less than 200 strings!"

        return True, "Topic validation passed!"

    @staticmethod
    def validate_max_rounds(max_rounds: int) -> Tuple[bool, str]:
        """max rounds validation"""
        if max_rounds < 1:
            return False, "Max rounds should not be larger than:0!"

        if max_rounds > settings.MAX_CONVERSATION_ROUNDS:
            return False, f"Max rounds should be smaller than:{settings.MAX_CONVERSATION_ROUNDS}!"

        return True, "Max round validation passed!"

    @staticmethod
    def validate_all_inputs(api_key: str,
                            base_url: str,
                            attack_model: str,
                            victim_model: str,
                            topic: str,
                            max_rounds: int) -> Tuple[bool, str]:
        """validate all data"""
        validations = [
            Validator.validate_api_key(api_key),
            Validator.validate_base_url(base_url),
            Validator.validate_models(attack_model, victim_model),
            Validator.validate_topic(topic),
            Validator.validate_max_rounds(max_rounds)
        ]

        for is_valid, message in validations:
            if not is_valid:
                return False, message

        return True, "All data validation passed!"