-
Notifications
You must be signed in to change notification settings - Fork 1
Basic Streaming and In-Memory Guard Support #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 38 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
c25810c
start lite build
CalebCourier 24867fa
litellm support
CalebCourier 17fe0d7
streaming support
CalebCourier f58b0ca
update lock file
CalebCourier 71f533e
add valid length validator
CalebCourier afbd0d7
register hub validators on startup
CalebCourier 5d20925
send back new field for validated stream errors
CalebCourier 0aff6b8
add some simple validators
CalebCourier fb4934c
disable otel on streaming
CalebCourier c80914a
custom installs for validators
CalebCourier 4c02184
point competitor_check to frontend_demo branch
CalebCourier 9798ac5
turn off oss metrics
CalebCourier 194b67e
dont send tracer to guard
CalebCourier 792a7dd
no max request
CalebCourier 212f35d
remove all telemetry from validate
CalebCourier af24df3
try printing response status
CalebCourier c06bde0
add many_shot_jailbreak to custom install
CalebCourier 60257bd
add api-spec download to build scripts
zsimjee bada0a0
update jailbreak repo to clean
CalebCourier e1c46e7
more limit removals
CalebCourier 8b0c363
log for every error
CalebCourier 39525fe
fix error handling
CalebCourier 7203a60
lite image changes
zsimjee c275813
lite build updates, default embed func
CalebCourier bc12034
Update how to run
zsimjee eb71536
update guardrails branch
CalebCourier a70dc92
merge main
CalebCourier 67af5f6
lint fix
CalebCourier 33d43ca
install dev dependencies before running qa
CalebCourier c5cf785
In memory guard client (#34)
nichwch 2367430
update guardrails branch
CalebCourier b8f9699
lint fixes, start test fixes
CalebCourier a0325b5
start fixing tests
CalebCourier 5b7ba42
fix tets
CalebCourier fc72222
setup api spec
CalebCourier ceb47a0
local imports
CalebCourier bdd3fd0
rename test file, use venv in qa
CalebCourier 47ec637
split qa steps, empty config, include sample config
CalebCourier c9a557f
add test coverage for memory guard
nichwch 13b437e
lint
nichwch 6c61c1d
Merge pull request #42 from guardrails-ai/nichwch/guard-coverage
nichwch 81e4245
ignore empty num_reask
CalebCourier 46575ec
Update readme with dev instructions, add compose file for infra, upda…
CalebCourier 34e3078
drop test coverage for now; all tests will be re-written as part of s…
CalebCourier 21104a0
make otel checks negative since otel env var is negative
CalebCourier 15eded7
default open api key to env var
CalebCourier c8e0ad5
remove print statements
CalebCourier fbc37d5
auto format
CalebCourier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
FROM public.ecr.aws/docker/library/python:3.11.6-slim | ||
|
||
ARG GITHUB_TOKEN | ||
ARG HF_TOKEN | ||
|
||
COPY .guardrailsrc /root/.guardrailsrc | ||
|
||
# COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.7.1 /lambda-adapter /opt/extensions/lambda-adapter | ||
# COPY ./opentelemetry-lambda-layer /opt | ||
|
||
# Create app directory | ||
WORKDIR /app | ||
|
||
# check the version | ||
RUN python3 --version | ||
# start the virtual environment | ||
RUN python3 -m venv /opt/venv | ||
|
||
# Enable venv | ||
ENV PATH="/opt/venv/bin:$PATH" | ||
|
||
# Install git and curl | ||
RUN apt-get update | ||
RUN apt-get install -y git curl gcc jq | ||
|
||
# Copy the requirements file | ||
COPY requirements*.txt . | ||
|
||
RUN curl https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem -o ./global-bundle.pem | ||
|
||
# Install app dependencies | ||
RUN pip install -r requirements-lock.txt | ||
|
||
# Download punkt data | ||
RUN python -m nltk.downloader -d /opt/nltk_data punkt | ||
|
||
# RUN guardrails hub install hub://guardrails/profanity_free | ||
RUN guardrails hub install hub://guardrails/valid_length | ||
RUN guardrails hub install hub://guardrails/lowercase | ||
RUN guardrails hub install hub://guardrails/regex_match | ||
|
||
COPY ./custom-install ./custom-install | ||
|
||
RUN python ./custom-install/install.py | ||
|
||
# Freeze dependencies | ||
RUN pip freeze > requirements-lock.txt | ||
|
||
# Copy the whole folder inside the Image filesystem | ||
COPY . . | ||
|
||
EXPOSE 8000 | ||
|
||
CMD gunicorn --bind 0.0.0.0:8000 --timeout=90 --threads=10 --limit-request-line=0 --limit-request-fields=1000 --limit-request-field_size=0 "app:create_app()" | ||
# CMD gunicorn --forwarded-allow-ips="*" --bind 0.0.0.0:8000 --timeout=60 --threads=10 "app:create_app()" |
This file contains hidden or 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
CalebCourier marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
''' | ||
All guards defined here will be initialized, if and only if | ||
the application is using in memory guards. | ||
|
||
The application will use in memory guards if pg_host is left | ||
undefined. Otherwise, a postgres instance will be started | ||
and guards will be persisted into postgres. In that case, | ||
these guards will not be initialized. | ||
''' | ||
|
||
from guardrails import Guard |
This file contains hidden or 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,113 @@ | ||
import os | ||
import sys | ||
import logging | ||
import json | ||
from typing import Any, Dict | ||
from rich.console import Console | ||
from guardrails.cli.hub.install import ( | ||
get_site_packages_location, | ||
install_hub_module, | ||
run_post_install, | ||
add_to_hub_inits | ||
) | ||
from guardrails.cli.server.module_manifest import ModuleManifest | ||
from string import Template | ||
|
||
console = Console() | ||
|
||
os.environ[ | ||
"COLOREDLOGS_LEVEL_STYLES" | ||
] = "spam=white,faint;success=green,bold;debug=magenta;verbose=blue;notice=cyan,bold;warning=yellow;error=red;critical=background=red" # noqa | ||
LEVELS = { | ||
"SPAM": 5, | ||
"VERBOSE": 15, | ||
"NOTICE": 25, | ||
"SUCCESS": 35, | ||
} | ||
for key in LEVELS: | ||
logging.addLevelName(LEVELS.get(key), key) # type: ignore | ||
logger = logging.getLogger("custom-install") | ||
|
||
|
||
def load_manifest(fileName: str) -> Dict[str, Any]: | ||
with open(f"custom-install/manifests/{fileName}") as manifest_file: | ||
content = manifest_file.read() | ||
return json.loads(content) | ||
|
||
custom_manifests = { | ||
"guardrails/provenance_llm": load_manifest("provenance-llm.json"), | ||
"guardrails/detect_pii": load_manifest("detect-pii.json"), | ||
"guardrails/competitor_check": load_manifest("competitor-check.json"), | ||
"guardrails/many_shot_jailbreak": load_manifest("jailbreak.json"), | ||
"tryolabs/restricttotopic": load_manifest("restrict-to-topic.json"), | ||
} | ||
|
||
def get_validator_manifest(module_name) -> ModuleManifest: | ||
manifest = custom_manifests.get(module_name, {}) | ||
return ModuleManifest.from_dict(manifest) | ||
|
||
def custom_install(package_uri: str): | ||
"""Install a validator from the Hub.""" | ||
if not package_uri.startswith("hub://"): | ||
logger.error("Invalid URI!") | ||
sys.exit(1) | ||
|
||
console.print(f"\nInstalling {package_uri}...\n") | ||
logger.log( | ||
level=LEVELS.get("SPAM"), msg=f"Installing {package_uri}..." # type: ignore | ||
) | ||
|
||
# Validation | ||
module_name = package_uri.replace("hub://", "") | ||
|
||
# Prep | ||
with console.status("Fetching manifest", spinner="bouncingBar"): | ||
module_manifest = get_validator_manifest(module_name) | ||
site_packages = get_site_packages_location() | ||
|
||
# Install | ||
with console.status("Downloading dependencies", spinner="bouncingBar"): | ||
install_hub_module(module_manifest, site_packages) | ||
|
||
# Post-install | ||
with console.status("Running post-install setup", spinner="bouncingBar"): | ||
run_post_install(module_manifest, site_packages) | ||
add_to_hub_inits(module_manifest, site_packages) | ||
|
||
success_message_cli = Template( | ||
"""✅Successfully installed ${module_name}! | ||
|
||
[bold]Import validator:[/bold] | ||
from guardrails.hub import ${export} | ||
|
||
[bold]Get more info:[/bold] | ||
https://hub.guardrailsai.com/validator/${id} | ||
""" | ||
).safe_substitute( | ||
module_name=package_uri, | ||
id=module_manifest.id, | ||
export=module_manifest.exports[0], | ||
) | ||
success_message_logger = Template( | ||
"""✅Successfully installed ${module_name}! | ||
|
||
Import validator: | ||
from guardrails.hub import ${export} | ||
|
||
Get more info: | ||
https://hub.guardrailsai.com/validator/${id} | ||
""" | ||
).safe_substitute( | ||
module_name=package_uri, | ||
id=module_manifest.id, | ||
export=module_manifest.exports[0], | ||
) | ||
console.print(success_message_cli) # type: ignore | ||
logger.log(level=LEVELS.get("SPAM"), msg=success_message_logger) # type: ignore | ||
|
||
|
||
# custom_install("hub://guardrails/provenance_llm") | ||
custom_install("hub://tryolabs/restricttotopic") | ||
# custom_install("hub://guardrails/detect_pii") | ||
# custom_install("hub://guardrails/competitor_check") | ||
# custom_install("hub://guardrails/many_shot_jailbreak") |
This file contains hidden or 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 @@ | ||
{ | ||
"name": "Competitor Check", | ||
"author": { | ||
"name": "Guardrails AI", | ||
"email": "contact@guardrailsai.com" | ||
}, | ||
"maintainers": [{ | ||
"name": "Karan Acharya", | ||
"email": "karan@guardrailsai.com" | ||
}], | ||
"repository": { | ||
"url": "https://github.com/guardrails-ai/competitor_check.git", | ||
"branch": "frontend_demo" | ||
}, | ||
"index": "./__init__.py", | ||
"exports": [ | ||
"CompetitorCheck" | ||
], | ||
"tags": { | ||
"language": [ | ||
"en" | ||
], | ||
"certification": [ | ||
"Guardrails Certified" | ||
], | ||
"contentType": [ | ||
"string" | ||
], | ||
"infrastructureRequirements": [ | ||
"ML" | ||
], | ||
"riskCategory": [ | ||
"Brand risk" | ||
], | ||
"useCases": [ | ||
"Chatbots", | ||
"Customer Support" | ||
] | ||
}, | ||
"id": "guardrails/competitor_check", | ||
"namespace": "guardrails", | ||
"packageName": "competitor_check", | ||
"moduleName": "validator", | ||
"requiresAuth": false, | ||
"postInstall": "post-install.py" | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.