forked from confident-ai/deepeval
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-tracing
- Loading branch information
Showing
33 changed files
with
1,013 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__: str = "1.4.9" | ||
__version__: str = "1.5.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .types import Guard | ||
from .guard import guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Optional, List | ||
from pydantic import BaseModel | ||
|
||
|
||
class APIGuard(BaseModel): | ||
input: str | ||
response: str | ||
guards: List[str] | ||
purpose: Optional[str] = None | ||
allowed_entities: Optional[List[str]] = None | ||
system_prompt: Optional[str] = None | ||
include_reason: bool | ||
|
||
|
||
class GuardResult(BaseModel): | ||
guard: str | ||
score: int | ||
reason: Optional[str] | ||
|
||
|
||
class GuardResponseData(BaseModel): | ||
results: List[GuardResult] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from typing import Optional, List | ||
|
||
from deepeval.guardrails.api import APIGuard, GuardResponseData | ||
from deepeval.confident.api import Api, HttpMethods, Endpoints | ||
from deepeval.telemetry import capture_guardrails | ||
from deepeval.guardrails.types import Guard | ||
from deepeval.guardrails.types import ( | ||
purpose_entities_dependent_guards, | ||
entities_dependent_guards, | ||
purpose_dependent_guards, | ||
) | ||
from deepeval.utils import is_confident | ||
|
||
|
||
BASE_URL = "https://internal.evals.confident-ai.com" | ||
|
||
|
||
def guard( | ||
input: str, | ||
response: str, | ||
guards: List[Guard], | ||
purpose: Optional[str] = None, | ||
allowed_entities: Optional[List[str]] = None, | ||
system_prompt: Optional[str] = None, | ||
include_reason: bool = False, | ||
): | ||
with capture_guardrails( | ||
guards=guards, | ||
include_reason=include_reason, | ||
include_system_prompt=(system_prompt != None), | ||
): | ||
# Check for missing parameters | ||
for guard in guards: | ||
if ( | ||
guard in purpose_dependent_guards | ||
or guard in purpose_entities_dependent_guards | ||
): | ||
if purpose is None and system_prompt is None: | ||
raise ValueError( | ||
f"Guard {guard.value} requires a purpose but none was provided." | ||
) | ||
|
||
if ( | ||
guard in entities_dependent_guards | ||
or guard in purpose_entities_dependent_guards | ||
): | ||
if allowed_entities is None and system_prompt is None: | ||
raise ValueError( | ||
f"Guard {guard.value} requires allowed entities but none were provided or list was empty." | ||
) | ||
|
||
# Prepare parameters for API request | ||
guard_params = APIGuard( | ||
input=input, | ||
response=response, | ||
guards=[g.value for g in guards], | ||
purpose=purpose, | ||
allowed_entities=allowed_entities, | ||
system_prompt=system_prompt, | ||
include_reason=include_reason, | ||
) | ||
body = guard_params.model_dump(by_alias=True, exclude_none=True) | ||
|
||
# API request | ||
if is_confident(): | ||
api = Api(base_url=BASE_URL) | ||
response = api.send_request( | ||
method=HttpMethods.POST, | ||
endpoint=Endpoints.GUARD_ENDPOINT, | ||
body=body, | ||
) | ||
try: | ||
GuardResponseData(**response) | ||
except TypeError as e: | ||
raise Exception("Incorrect result format:", e) | ||
results = response["results"] | ||
if not include_reason: | ||
for result in results: | ||
del result["reason"] | ||
return results | ||
else: | ||
raise Exception("To use DeepEval guardrails, run `deepeval login`") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from enum import Enum | ||
|
||
|
||
class Guard(Enum): | ||
PRIVACY = "Privacy" | ||
INTELLECTUAL_PROPERTY = "Intellectual Property" | ||
MISINFORMATION_DISINFORMATION = "Misinformation & Disinformation" | ||
SPECIALIZED_FINANCIAL_ADVICE = "Specialized Financial Advice" | ||
OFFENSIVE = "Offensive" | ||
BIAS = "BIAS" | ||
PII_API_DB = "API and Database Access" | ||
PII_DIRECT = "Direct PII Disclosure" | ||
PII_SESSION = "Session PII Leak" | ||
PII_SOCIAL = "Social Engineering PII Disclosure" | ||
DATA_LEAKAGE = "Data Leakage" | ||
CONTRACTS = "Contracts" | ||
EXCESSIVE_AGENCY = "Excessive Agency" | ||
HALLUCINATION = "Hallucination" | ||
IMITATION = "Imitation" | ||
POLITICS = "Political Statements" | ||
OVERRELIANCE = "Overreliance" | ||
DEBUG_ACCESS = "Debug Access" | ||
RBAC = "Role-Based Access Control" | ||
SHELL_INJECTION = "Shell Injection" | ||
SQL_INJECTION = "SQL Injection" | ||
PROMPT_EXTRACTION = "Prompt Extraction" | ||
SSRF = "Server Side Request Forgery" | ||
BOLA = "Broken Object Level Authorization" | ||
BFLA = "Broken Function Level Authorization" | ||
COMPETITORS = "Competitors" | ||
HIJACKING = "Hijacking" | ||
RELIGION = "Religion" | ||
VIOLENT_CRIME = "Violent Crimes" | ||
NON_VIOLENT_CRIME = "Non Violent Crimes" | ||
SEX_CRIME = "Sex Crimes" | ||
CHILD_EXPLOITATION = "Child Exploitation" | ||
INDISCRIMINATE_WEAPONS = "Indiscriminate Weapons" | ||
HATE = "Hate" | ||
SELF_HARM = "Self Harm" | ||
SEXUAL_CONTENT = "Sexual Content" | ||
CYBERCRIME = "Cybercrime" | ||
CHEMICAL_BIOLOGICAL_WEAPONS = "Chemical & Biological Weapons" | ||
ILLEGAL_DRUGS = "Illegal Drugs" | ||
COPYRIGHT_VIOLATIONS = "Copyright Violations" | ||
HARASSMENT_BULLYING = "Harassment & Bullying" | ||
ILLEGAL_ACTIVITIES = "Illegal Activities" | ||
GRAPHIC_CONTENT = "Graphic Content" | ||
UNSAFE_PRACTICES = "Unsafe Practices" | ||
RADICALIZATION = "Radicalization" | ||
PROFANITY = "Profanity" | ||
INSULTS = "Insults" | ||
|
||
|
||
# Lists of guards that require purpose, entities, or both | ||
purpose_dependent_guards = [ | ||
Guard.BFLA, | ||
Guard.BIAS, | ||
Guard.HALLUCINATION, | ||
Guard.HIJACKING, | ||
Guard.OVERRELIANCE, | ||
Guard.PROMPT_EXTRACTION, | ||
Guard.RBAC, | ||
Guard.SSRF, | ||
Guard.COMPETITORS, | ||
Guard.RELIGION, | ||
] | ||
|
||
entities_dependent_guards = [Guard.BOLA, Guard.IMITATION] | ||
|
||
purpose_entities_dependent_guards = [ | ||
Guard.PII_API_DB, | ||
Guard.PII_DIRECT, | ||
Guard.PII_SESSION, | ||
Guard.PII_SOCIAL, | ||
Guard.COMPETITORS, | ||
Guard.RELIGION, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .monitor import monitor | ||
from .feedback import send_feedback | ||
from .monitor import monitor, a_monitor | ||
from .feedback import send_feedback, a_send_feedback | ||
from .api import Link |
Oops, something went wrong.