Sasidhar commited on
Commit
a7b98db
·
verified ·
1 Parent(s): 40383ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -63
app.py CHANGED
@@ -4,69 +4,6 @@ import re
4
 
5
  app = FastAPI()
6
 
7
- # Configuration for enabled guardrail checks as part of the request payload.
8
- class GuardrailsConfig(BaseModel):
9
- factual_consistency: bool = True
10
- toxicity: bool = True
11
- # Extend with more flags for additional guardrails
12
-
13
- # Request model now includes both the response and the configuration.
14
- class CheckRequest(BaseModel):
15
- response: str
16
- config: GuardrailsConfig = GuardrailsConfig() # Default config if not provided
17
-
18
- class CheckResponse(BaseModel):
19
- grounded: bool
20
- details: dict
21
-
22
- # A simple result class to hold individual check outcomes.
23
- class Result:
24
- def __init__(self):
25
- self.details = {}
26
-
27
- def add(self, rule_name: str, passed: bool):
28
- self.details[rule_name] = passed
29
-
30
- def grounded(self) -> bool:
31
- # The response is considered "grounded" if all enabled rules pass.
32
- return all(self.details.values())
33
-
34
- # Define guardrail rule classes.
35
- class FactualConsistencyRule:
36
- name = "FactualConsistency"
37
-
38
- def check(self, response_text: str) -> bool:
39
- # For demonstration: pass if the response contains the word "fact".
40
- return "fact" in response_text.lower()
41
-
42
- class ToxicityRule:
43
- name = "Toxicity"
44
-
45
- def check(self, response_text: str) -> bool:
46
- # For demonstration: fail if negative words like "hate" or "kill" are found.
47
- return not re.search(r"(hate|kill)", response_text, re.IGNORECASE)
48
-
49
- # Manager class to load and execute the enabled guardrail rules.
50
- class GuardrailsManager:
51
- def __init__(self, config: GuardrailsConfig):
52
- self.config = config
53
- self.rules = self.load_rules()
54
-
55
- def load_rules(self):
56
- rules = []
57
- if self.config.factual_consistency:
58
- rules.append(FactualConsistencyRule())
59
- if self.config.toxicity:
60
- rules.append(ToxicityRule())
61
- # Add additional rules based on configuration here.
62
- return rules
63
-
64
- def check(self, response_text: str) -> Result:
65
- result = Result()
66
- for rule in self.rules:
67
- rule_result = rule.check(response_text)
68
- result.add(rule.name, rule_result)
69
- return result
70
 
71
  # Define the POST endpoint for guardrail checking.
72
  @app.post("/guardrails/check", response_model=CheckResponse)
 
4
 
5
  app = FastAPI()
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Define the POST endpoint for guardrail checking.
9
  @app.post("/guardrails/check", response_model=CheckResponse)