Implement a basic input guardrail system:
add_blocked_keyword(kw): Add to blocklistset_allowed_topics(topics): Set allowed topics (empty list = allow all)check(input_text): Validate input
Validation Logic:
- If any blocked keyword found → blocked (reason: 'Blocked keyword: X')
- If allowed_topics non-empty AND no topic keyword found → blocked (reason: 'Off-topic')
- Otherwise → allowed
Constraints:
- Case-insensitive matching
- Return first violation found
- Empty input is allowed
Examples
Example 1:
Input:
g = InputGuardrail(); g.add_blocked_keyword('hack'); g.check('How to hack?')Output:
{'allowed': False, 'reason': 'Blocked keyword: hack'}Explanation: Blocked keyword detected in input
Example 2:
Input:
g = InputGuardrail(); g.set_allowed_topics(['weather']); g.check('Tell me a joke')Output:
{'allowed': False, 'reason': 'Off-topic'}Explanation: No allowed topic keywords found
Starter Code
class InputGuardrail:
"""
Basic guardrail for filtering agent inputs.
Blocks harmful or out-of-scope requests.
"""
def __init__(self):
self.blocked_keywords = []
self.allowed_topics = []
def add_blocked_keyword(self, keyword):
# Your implementation here
pass
def set_allowed_topics(self, topics):
# Your implementation here
pass
def check(self, input_text):
"""
Check input against guardrails.
Returns {'allowed': bool, 'reason': str or None}
"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready