Skip to content

Commit

Permalink
Merge pull request #1 from guardrails-ai/fit-n-finish
Browse files Browse the repository at this point in the history
Fit & Finish
  • Loading branch information
CalebCourier authored Feb 19, 2024
2 parents c3830b2 + f1f72a1 commit 31f404d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr_qc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ jobs:
python-version: 3.11
- name: Run qa
run: |
pip install ".[dev]"
make dev
make qa
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
dev:
pip install -e ".[dev]"
python3 validator/post-install.py

lint:
ruff check .

tests:
pytest ./test
test:
pytest ./tests

type:
pyright validator
Expand Down
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,15 @@ In this example, we apply the validator to a string output generated by an LLM.

```python
# Import Guard and Validator
from guardrails import Guard
from guardrails.hub import CompetitorCheck

# Initialize Validator
val = CompetitorCheck(
competitors=["Apple", "Samsung",]
on_fail="noop"
)

# Setup Guard
guard = Guard.from_string(
validators=[val, ...],
)
guard = Guard().use(CompetitorCheck, ["Apple", "Samsung"])

guard.parse("The apple doesn't fall far from the tree.") # Validator passes
guard.parse("Apple just released a new iPhone.") # Validator fails
guard.validate("The apple doesn't fall far from the tree.") # Validator passes
guard.validate("Apple just released a new iPhone.") # Validator fails
```

### Validating JSON output via Python
Expand All @@ -53,13 +47,13 @@ In this example, we apply the validator to a string that is a field within a Pyd

```python
# Import Guard and Validator
from pydantic import BaseModel
from pydantic import BaseModel, Field
from guardrails.hub import CompetitorCheck
from guardrails import Guard

# Initialize Validator
val = CompetitorCheck(
competitors=["Apple", "Samsung",]
competitors=["Apple", "Samsung"],
on_fail="noop"
)

Expand Down Expand Up @@ -92,14 +86,14 @@ Initializes a new instance of the Validator class.

**Parameters:**

- **`competitors`** _(list(str))_: List of names of competitors to avoid.
- **`competitors`** *(List[str]):* List of names of competitors to avoid.
- **`on_fail`** *(str, Callable):* The policy to enact when a validator fails. If `str`, must be one of `reask`, `fix`, `filter`, `refrain`, `noop`, `exception` or `fix_reask`. Otherwise, must be a function that is called when the validator fails.

</ul>

<br/>

**`__call__(self, value, metadata={}) → ValidationOutcome`**
**`validate(self, value, metadata={}) → ValidationResult`**

<ul>

Expand Down
File renamed without changes.
35 changes: 2 additions & 33 deletions validator/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import nltk
import spacy
import re
from typing import Callable, Dict, List, Optional

Expand All @@ -10,22 +12,6 @@
register_validator,
)

try:
import nltk # type: ignore
except ImportError:
nltk = None # type: ignore

if nltk is not None:
try:
nltk.data.find("tokenizers/punkt")
except LookupError:
nltk.download("punkt")

try:
import spacy
except ImportError:
spacy = None


@register_validator(name="guardrails/competitor_check", data_type="string")
class CompetitorCheck(Validator):
Expand All @@ -47,18 +33,6 @@ def __init__(
super().__init__(competitors=competitors, on_fail=on_fail)
self._competitors = competitors
model = "en_core_web_trf"
if spacy is None:
raise ImportError(
"You must install spacy in order to use the CompetitorCheck validator."
)

if not spacy.util.is_package(model):
logger.info(
f"Spacy model {model} not installed. "
"Download should start now and take a few minutes."
)
spacy.cli.download(model) # type: ignore

self.nlp = spacy.load(model)

def exact_match(self, text: str, competitors: List[str]) -> List[str]:
Expand Down Expand Up @@ -134,11 +108,6 @@ def validate(self, value: str, metadata=Dict) -> ValidationResult:
ValidationResult: The validation result.
"""

if nltk is None:
raise ImportError(
"`nltk` library is required for `competitors-check` validator. "
"Please install it with `poetry add nltk`."
)
sentences = nltk.sent_tokenize(value)
flagged_sentences = []
filtered_sentences = []
Expand Down
19 changes: 16 additions & 3 deletions validator/post-install.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
print("post-install starting...")
print("This is where you would do things like download nltk tokenizers or login to the HuggingFace hub...")
print("post-install complete!")
import nltk
import spacy

try:
nltk.data.find("tokenizers/punkt")
except LookupError:
nltk.download("punkt")


model = "en_core_web_trf"
if not spacy.util.is_package(model):
print(
f"Spacy model {model} not installed. "
"Download should start now and take a few minutes."
)
spacy.cli.download(model) # type: ignore

0 comments on commit 31f404d

Please sign in to comment.