Skip to content

Commit

Permalink
updated guards
Browse files Browse the repository at this point in the history
  • Loading branch information
penguine-ip committed Jan 10, 2025
1 parent ecbd831 commit 997cd4f
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion c.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
user_input = "Hi my name is alex and I live on Maple Street 123"
output = "I'm sorry but I can't answer this"

guard_results = guardrails.guard(user_input, output)
guard_results = guardrails.guard_input(user_input)
print(guard_results)
2 changes: 1 addition & 1 deletion deepeval/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__: str = "2.1.5"
__version__: str = "2.1.6"
3 changes: 3 additions & 0 deletions deepeval/guardrails/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class ApiGuardrails(BaseModel):
guards: List[ApiGuard]
type: GuardType

class Config:
use_enum_values = True


class GuardResult(BaseModel):
breached: bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ def __init__(

@property
def __name__(self):
if self.guard_type == GuardType.INPUT:
return "Cybersecurity Input Guard"
elif self.guard_type == GuardType.OUTPUT:
return "Cybersecurity Output Guard"
"Cybersecurity Guard"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class GraphicContentGuard(BaseGuard):
class GraphicContentGuard(BaseDecorativeGuard):

def __init__(self):
self.guard_type = GuardType.OUTPUT
Expand Down
17 changes: 14 additions & 3 deletions deepeval/guardrails/guardrails.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def guard_input(self, input: str):
"Guardrails cannot guard LLM responses when no guards are provided."
)

with capture_guardrails(guards=self.guards):
with capture_guardrails(
guards=[guard.__name__ for guard in self.guards]
):
# Prepare parameters for API request
api_guards = []
for guard in self.guards:
Expand All @@ -38,10 +40,15 @@ def guard_input(self, input: str):
api_guards.append(api_guard)

api_guardrails = ApiGuardrails(
guards=api_guards, type=GuardType.INPUT
input=input,
output=input,
guards=api_guards,
type=GuardType.INPUT,
)
body = api_guardrails.model_dump(by_alias=True, exclude_none=True)

print(body)

# API request
if is_confident():
api = Api(base_url=BASE_URL)
Expand All @@ -62,7 +69,9 @@ def guard_output(self, input: str, response: str):
"Guardrails cannot guard LLM responses when no guards are provided."
)

with capture_guardrails(guards=self.guards):
with capture_guardrails(
guards=[guard.__name__ for guard in self.guards]
):
# Prepare parameters for API request
api_guards = []
for guard in self.guards:
Expand All @@ -85,11 +94,13 @@ def guard_output(self, input: str, response: str):
# API request
if is_confident():
api = Api(base_url=BASE_URL)
print("!!!!!")
response = api.send_request(
method=HttpMethods.POST,
endpoint=Endpoints.GUARDRAILS_ENDPOINT,
body=body,
)
print(response)
return GuardsResponseData(**response).result
else:
raise Exception(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class HallucinationGuard(BaseGuard):
class HallucinationGuard(BaseDecorativeGuard):
def __init__(self):
self.guard_type = GuardType.OUTPUT

Expand Down
4 changes: 2 additions & 2 deletions deepeval/guardrails/illegal_guard/illegal_guard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class IllegalGuard(BaseGuard):
class IllegalGuard(BaseDecorativeGuard):
def __init__(self):
self.guard_type = GuardType.OUTPUT

Expand Down
4 changes: 2 additions & 2 deletions deepeval/guardrails/jailbreaking_guard/jailbreaking_guard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class JailbreakingGuard(BaseGuard):
class JailbreakingGuard(BaseDecorativeGuard):
def __init__(self):
self.guard_type = GuardType.INPUT

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class ModernizationGuard(BaseGuard):
class ModernizationGuard(BaseDecorativeGuard):

def __init__(self):
self.guard_type = GuardType.OUTPUT
Expand Down
4 changes: 2 additions & 2 deletions deepeval/guardrails/privacy_guard/privacy_guard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class PrivacyGuard(BaseGuard):
class PrivacyGuard(BaseDecorativeGuard):
def __init__(self):
self.guard_type = GuardType.INPUT

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class PromptInjectionGuard(BaseGuard):
class PromptInjectionGuard(BaseDecorativeGuard):
def __init__(self):
self.guard_type = GuardType.INPUT

Expand Down
4 changes: 2 additions & 2 deletions deepeval/guardrails/syntax_guard/syntax_guard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class SyntaxGuard(BaseGuard):
class SyntaxGuard(BaseDecorativeGuard):

def __init__(self):
self.guard_type = GuardType.OUTPUT
Expand Down
4 changes: 2 additions & 2 deletions deepeval/guardrails/topical_guard/topical_guard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class TopicalGuard(BaseGuard):
class TopicalGuard(BaseDecorativeGuard):

def __init__(self):
self.guard_type = GuardType.INPUT
Expand Down
4 changes: 2 additions & 2 deletions deepeval/guardrails/toxicity_guard/toxicity_guard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from deepeval.guardrails.base_guard import BaseGuard
from deepeval.guardrails.base_guard import BaseDecorativeGuard
from deepeval.guardrails.types import GuardType


class ToxicityGuard(BaseGuard):
class ToxicityGuard(BaseDecorativeGuard):
def __init__(self):
self.guard_type = GuardType.OUTPUT

Expand Down
2 changes: 0 additions & 2 deletions deepeval/monitor/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from deepeval.monitor.api import (
APIEvent,
EventHttpResponse,
CustomPropertyType,
CustomProperty,
Link,
)

Expand Down
4 changes: 2 additions & 2 deletions deepeval/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ def capture_red_teamer_run(


@contextmanager
def capture_guardrails(guards: List):
def capture_guardrails(guards: List[str]):
if not telemetry_opt_out():
with tracer.start_as_current_span(f"Ran guardrails") as span:
span.set_attribute("user.unique_id", get_unique_id())
for guard in guards:
span.set_attribute(f"vulnerability.{guard.get_guard_name()}", 1)
span.set_attribute(f"vulnerability.{guard}", 1)
set_last_feature(Feature.GUARDRAIL)
yield span
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "deepeval"
version = "2.1.5"
version = "2.1.6"
description = "The LLM Evaluation Framework"
authors = ["Jeffrey Ip <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 997cd4f

Please sign in to comment.