-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,106 additions
and
69 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,3 +1,9 @@ | ||
from .pipeline_prompt_injection_identifier import PipelinePromptInjectionIdentifier | ||
from .bert_score import BertScoreClassifier | ||
from .bleu import BleuClassifier | ||
from .pipeline_prompt_injection import PipelinePromptInjectionClassifier | ||
|
||
__all__ = ["PipelinePromptInjectionIdentifier"] | ||
__all__ = [ | ||
"BertScoreClassifier", | ||
"BleuClassifier", | ||
"PipelinePromptInjectionClassifier", | ||
] |
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,46 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Any, Dict, List | ||
|
||
import evaluate | ||
|
||
from ...core import BaseTextClassifier, Score | ||
|
||
|
||
@dataclass | ||
class BertScoreClassifier(BaseTextClassifier[Dict[str, Any]]): | ||
"""A classifier that computes BERTScore for text inputs.""" | ||
|
||
threshold: float = 0.8 | ||
model_type: str = "distilbert-base-uncased" | ||
bertscore: evaluate.EvaluationModule = field(default_factory=lambda: evaluate.load("bertscore"), init=False) | ||
|
||
def score(self, input: str, references: List[str] | None = None) -> Score[Dict[str, Any]]: | ||
"""Score the input using BERTScore computed by the evaluate module. | ||
Args: | ||
input (str): The input text to be scored. | ||
references (List[str], optional): List of reference texts. Defaults to None. | ||
Raises: | ||
ValueError: If references is None or if the number of references is not equal to 1. | ||
Returns: | ||
Score[Dict[str, Any]]: A Score object representing the BERTScore of the input. | ||
""" | ||
if not references or not len(references) == 1: | ||
raise ValueError("The number of references must be exactly 1.") | ||
|
||
score = self.bertscore.compute( | ||
predictions=[input], | ||
references=[references[0]], | ||
model_type=self.model_type, | ||
) | ||
|
||
f1_score = score["f1"][0] | ||
|
||
return Score[Dict[str, Any]]( | ||
flagged=f1_score < self.threshold, | ||
value=score, | ||
description="Returns True if the f1 score is less than the threshold", | ||
explanation=f"The f1 score for the input and reference is {f1_score}", | ||
) |
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,45 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Any, Dict, List | ||
|
||
import evaluate | ||
|
||
from ...core import BaseTextClassifier, Score | ||
|
||
|
||
@dataclass | ||
class BleuClassifier(BaseTextClassifier[Dict[str, Any]]): | ||
"""A classifier that computes BLEU score for text inputs.""" | ||
|
||
threshold: float = 0.2 | ||
bleu: evaluate.EvaluationModule = field(default_factory=lambda: evaluate.load("bleu"), init=False) | ||
|
||
def score(self, input: str, references: List[str] | None = None) -> Score[Dict[str, Any]]: | ||
"""Score the input using BLEU score computed by the evaluate module. | ||
Args: | ||
input (str): The input text to be scored. | ||
references (List[str], optional): List of reference texts. Defaults to None. | ||
Raises: | ||
ValueError: If the number of references is not equal to 1. | ||
Returns: | ||
Score[Dict[str, Any]]: A Score object representing the BLEU score of the input. | ||
""" | ||
if not references or not len(references) == 1: | ||
raise ValueError("The number of references must be exactly 1.") | ||
|
||
score = self.bleu.compute( | ||
predictions=[input], | ||
references=[references[0]], | ||
max_order=2, | ||
) | ||
|
||
bleu_score = score["bleu"] | ||
|
||
return Score[Dict[str, Any]]( | ||
flagged=bleu_score < self.threshold, | ||
value=score, | ||
description="Returns True if the bleu score is less than the threshold", | ||
explanation=f"The bleu score for the input and reference is {bleu_score}", | ||
) |
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
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
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
Oops, something went wrong.