Skip to content

Commit 42c1c01

Browse files
committed
🚧 first draft of wrapping three guardrails-ai inside the detector logic
1 parent 840b543 commit 42c1c01

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

detectors/guardrails_ai_wrapper/__init__.py

Whitespace-only changes.

detectors/guardrails_ai_wrapper/app.py

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import sys
3+
4+
sys.path.insert(0, os.path.abspath(".."))
5+
from common.app import logger
6+
from scheme import (
7+
ContentAnalysisHttpRequest,
8+
ContentAnalysisResponse,
9+
ContentsAnalysisResponse,
10+
)
11+
12+
from guardrails import Guard, OnFailAction
13+
from guardrails.hub import RegexMatch, CompetitorCheck, ToxicLanguage
14+
15+
16+
class Detector:
17+
def __init__(self):
18+
self.guard = Guard().use_many(
19+
RegexMatch(regex=r"^(?!.*\bpotatoe\b).*$", on_fail=OnFailAction.EXCEPTION),
20+
CompetitorCheck(
21+
["Apple", "Microsoft", "Google"], on_fail=OnFailAction.EXCEPTION
22+
),
23+
ToxicLanguage(
24+
threshold=0.5,
25+
validation_method="sentence",
26+
on_fail=OnFailAction.EXCEPTION,
27+
),
28+
)
29+
logger.info("Guardrails AI Wrapper initialized")
30+
31+
def run(self, input: ContentAnalysisHttpRequest) -> ContentsAnalysisResponse:
32+
contents_analyses = []
33+
for text in input.contents:
34+
content_analyses = []
35+
try:
36+
logger.info(f"Validating text: {text}")
37+
validation_result = self.guard.validate(text)
38+
logger.info(f"Validation successful for text: {text}")
39+
except Exception as e:
40+
logger.error(f"Validation failed for text: {text} with error: {e}")
41+
validation_result = e
42+
43+
content_analyses.append(
44+
ContentAnalysisResponse(
45+
start=0,
46+
end=len(text),
47+
detection_type="guardrails_ai",
48+
text=text,
49+
validation_result=validation_result,
50+
evidences=[],
51+
)
52+
)
53+
contents_analyses.append(content_analyses)
54+
logger.info("Content analysis completed")
55+
return contents_analyses
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from enum import Enum
2+
from typing import List, Optional, Union
3+
from pydantic import BaseModel, Field, RootModel, ConfigDict
4+
from guardrails.classes.validation_outcome import ValidationOutcome
5+
6+
7+
class Evidence(BaseModel):
8+
source: str = Field(
9+
title="Source",
10+
example="https://en.wikipedia.org/wiki/IBM",
11+
description="Source of the evidence, it can be url of the evidence etc",
12+
)
13+
14+
15+
class EvidenceType(str, Enum):
16+
url = "url"
17+
title = "title"
18+
19+
20+
class EvidenceObj(BaseModel):
21+
type: EvidenceType = Field(
22+
title="EvidenceType",
23+
example="url",
24+
description="Type field signifying the type of evidence provided. Example url, title etc",
25+
)
26+
evidence: Evidence = Field(
27+
description="Evidence object, currently only containing source, but in future can contain other optional arguments like id, etc",
28+
)
29+
30+
31+
class ContentAnalysisHttpRequest(BaseModel):
32+
contents: List[str] = Field(
33+
min_length=1,
34+
title="Contents",
35+
description="Field allowing users to provide list of texts for analysis. Note, results of this endpoint will contain analysis / detection of each of the provided text in the order they are present in the contents object.",
36+
example=["My email address is [email protected] and [email protected]"],
37+
)
38+
39+
40+
class ContentAnalysisResponse(BaseModel):
41+
model_config = ConfigDict(arbitrary_types_allowed=True)
42+
start: int = Field(example=14)
43+
end: int = Field(example=26)
44+
detection_type: str = Field(example="detection_type")
45+
text: str = Field(
46+
example="My email address is [email protected] and [email protected]"
47+
)
48+
validation_result: Union[Exception, ValidationOutcome]
49+
evidences: Optional[List[EvidenceObj]] = Field(
50+
description="Optional field providing evidences for the provided detection",
51+
default=[],
52+
)
53+
54+
55+
class ContentsAnalysisResponse(RootModel):
56+
root: List[List[ContentAnalysisResponse]] = Field(
57+
title="Response Text Content Analysis Unary Handler Api V1 Text Content Post"
58+
)
59+
60+
61+
class Error(BaseModel):
62+
code: int
63+
message: str

0 commit comments

Comments
 (0)