Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge
Browse files Browse the repository at this point in the history
nichwch committed Jun 25, 2024
2 parents 9125578 + fbcb8b6 commit 6661b07
Showing 10 changed files with 102 additions and 82 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -25,11 +25,6 @@ jobs:
pip install build;
continue-on-error: false

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20

- name: Build the module
env:
PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
2 changes: 1 addition & 1 deletion guardrails_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.0"
__version__ = "0.0.0a0"
7 changes: 6 additions & 1 deletion guardrails_api/app.py
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ def register_config(config: Optional[str] = None):
SourceFileLoader("config", config_file_path).load_module()


def create_app(env: Optional[str] = None, config: Optional[str] = None):
def create_app(env: Optional[str] = None, config: Optional[str] = None, port: Optional[int] = None):
if os.environ.get("APP_ENVIRONMENT") != "production":
from dotenv import load_dotenv

@@ -56,6 +56,11 @@ def create_app(env: Optional[str] = None, config: Optional[str] = None):
env_file_path = os.path.abspath(env_file)
load_dotenv(env_file_path)

set_port = port or os.environ.get("PORT", 8000)
host = os.environ.get("HOST", "http://localhost")
self_endpoint = os.environ.get("SELF_ENDPOINT", f"{host}:{set_port}")
os.environ["SELF_ENDPOINT"] = self_endpoint

register_config(config)

app = Flask(__name__)
22 changes: 16 additions & 6 deletions guardrails_api/cli/start.py
Original file line number Diff line number Diff line change
@@ -29,12 +29,22 @@ def start(
env: Optional[str] = typer.Option(
default="",
help="An env file to load environment variables from.",
prompt=".env file (optional)",
),
config: Optional[str] = typer.Option(
default="",
help="A config file to load Guards from.",
prompt="config file (optional)",
),
timeout: Optional[int] = typer.Option(
default=5,
help="Gunicorn worker timeout.",
),
threads: Optional[int] = typer.Option(
default=10,
help="Number of Gunicorn worker threads.",
),
port: Optional[int] = typer.Option(
default=8000,
help="The port to run the server on.",
),
):
# TODO: If these are empty,
@@ -43,8 +53,8 @@ def start(
config = config or None

options = {
"bind": "0.0.0.0:8000",
"timeout": 5,
"threads": 10,
"bind": f"0.0.0.0:{port}",
"timeout": timeout,
"threads": threads,
}
StandaloneApplication(create_app(env, config), options).run()
StandaloneApplication(create_app(env, config, port), options).run()
3 changes: 1 addition & 2 deletions guardrails_api/clients/cache_client.py
Original file line number Diff line number Diff line change
@@ -15,9 +15,8 @@ def initialize(self, app):
self.cache = Cache(
app,
config={
"CACHE_TYPE": "simple",
"CACHE_TYPE": "SimpleCache",
"CACHE_DEFAULT_TIMEOUT": 300,
"CACHE_NO_NULL_WARNING": True,
"CACHE_THRESHOLD": 50
}
)
4 changes: 3 additions & 1 deletion guardrails_api/default.env
Original file line number Diff line number Diff line change
@@ -4,5 +4,7 @@ LOGLEVEL="INFO"
GUARDRAILS_LOG_LEVEL="INFO"
GUARDRAILS_PROCESS_COUNT=1
SELF_ENDPOINT=http://localhost:8000
API_KEY=dapi9b2c2e2e384cce5ba50588dede8b48ae
HOST=http://localhost
PORT=8000
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
API_KEY=dapi9b2c2e2e384cce5ba50588dede8b48ae
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ readme = "README.md"
keywords = ["Guardrails", "Guardrails AI", "Guardrails API", "Guardrails API"]
requires-python = ">= 3.8.1"
dependencies = [
"guardrails-ai@git+https://github.com/guardrails-ai/guardrails.git@0.5.0-dev",
"guardrails-ai>=0.5.0a2",
"flask>=3.0.3,<4",
"Flask-SQLAlchemy>=3.1.1,<4",
"Flask-Caching>=2.3.0,<3",
113 changes: 53 additions & 60 deletions requirements-lock.txt
Original file line number Diff line number Diff line change
@@ -2,96 +2,84 @@ aiohttp==3.9.5
aiosignal==1.3.1
annotated-types==0.7.0
anyio==4.4.0
asgiref==3.8.1
arrow==1.3.0
attrs==23.2.0
blinker==1.8.2
boto3==1.34.115
botocore==1.34.115
certifi==2024.2.2
boto3==1.34.132
botocore==1.34.132
cachelib==0.9.0
certifi==2024.6.2
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
coloredlogs==15.0.1
cryptography==42.0.7
cryptography==42.0.8
Deprecated==1.2.14
distro==1.9.0
faiss-cpu==1.8.0
filelock==3.14.0
Faker==25.9.1
filelock==3.15.4
Flask==3.0.3
Flask-Caching==2.3.0
Flask-Cors==4.0.1
Flask-SQLAlchemy==3.1.1
fqdn==1.5.1
frozenlist==1.4.1
fsspec==2024.5.0
googleapis-common-protos==1.63.0
fsspec==2024.6.0
googleapis-common-protos==1.63.2
griffe==0.36.9
grpcio==1.64.0
guardrails-ai @ git+https://github.com/guardrails-ai/guardrails.git@fd77007dfe823f8cb32cd314b78e5f63aea71e9a
grpcio==1.64.1
guardrails-ai==0.5.0a2
guardrails-api @ file:///Users/calebcourier/Projects/gr-mono/guardrails-cdk/guardrails-api
guardrails-api-client==0.3.8
gunicorn==22.0.0
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
huggingface-hub==0.23.2
huggingface-hub==0.23.4
humanfriendly==10.0
idna==3.7
importlib-metadata==7.0.0
ijson==3.3.0
importlib_metadata==7.1.0
isoduration==20.11.0
itsdangerous==2.2.0
Jinja2==3.1.4
jmespath==1.0.1
joblib==1.4.2
jsonpatch==1.33
jsonpointer==2.4
jsonpointer==3.0.0
jsonref==1.1.0
jsonschema==4.22.0
jsonschema-specifications==2023.12.1
jwt==1.3.1
langchain-core==0.1.52
langsmith==0.1.65
litellm==1.39.3
langchain-core==0.2.9
langsmith==0.1.82
litellm==1.40.25
lxml==4.9.4
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
multidict==6.0.5
nltk==3.8.1
numpy==1.26.4
openai==1.30.5
opentelemetry-api==1.24.0
opentelemetry-distro==0.45b0
opentelemetry-exporter-otlp-proto-common==1.24.0
opentelemetry-exporter-otlp-proto-grpc==1.24.0
opentelemetry-exporter-otlp-proto-http==1.24.0
opentelemetry-instrumentation==0.45b0
opentelemetry-instrumentation-asgi==0.45b0
opentelemetry-instrumentation-asyncio==0.45b0
opentelemetry-instrumentation-aws-lambda==0.45b0
opentelemetry-instrumentation-boto3sqs==0.45b0
opentelemetry-instrumentation-botocore==0.45b0
opentelemetry-instrumentation-dbapi==0.45b0
opentelemetry-instrumentation-flask==0.45b0
opentelemetry-instrumentation-grpc==0.45b0
opentelemetry-instrumentation-httpx==0.45b0
opentelemetry-instrumentation-jinja2==0.45b0
opentelemetry-instrumentation-logging==0.45b0
opentelemetry-instrumentation-requests==0.45b0
opentelemetry-instrumentation-sqlalchemy==0.45b0
opentelemetry-instrumentation-sqlite3==0.45b0
opentelemetry-instrumentation-tortoiseorm==0.45b0
opentelemetry-instrumentation-urllib==0.45b0
opentelemetry-instrumentation-urllib3==0.45b0
opentelemetry-instrumentation-wsgi==0.45b0
opentelemetry-propagator-aws-xray==1.0.1
opentelemetry-proto==1.24.0
opentelemetry-sdk==1.24.0
opentelemetry-semantic-conventions==0.45b0
opentelemetry-test-utils==0.45b0
opentelemetry-util-http==0.45b0
orjson==3.10.3
packaging==23.2
openai==1.35.3
opentelemetry-api==1.25.0
opentelemetry-exporter-otlp-proto-common==1.25.0
opentelemetry-exporter-otlp-proto-grpc==1.25.0
opentelemetry-exporter-otlp-proto-http==1.25.0
opentelemetry-instrumentation==0.46b0
opentelemetry-instrumentation-flask==0.46b0
opentelemetry-instrumentation-wsgi==0.46b0
opentelemetry-proto==1.25.0
opentelemetry-sdk==1.25.0
opentelemetry-semantic-conventions==0.46b0
opentelemetry-util-http==0.46b0
orjson==3.10.5
packaging==24.1
protobuf==4.25.3
psycopg2-binary==2.9.9
pycparser==2.22
pydantic==2.7.2
pydantic_core==2.18.3
pydantic==2.7.4
pydantic_core==2.18.4
pydash==7.0.7
Pygments==2.18.0
python-dateutil==2.9.0.post0
@@ -100,23 +88,28 @@ PyYAML==6.0.1
referencing==0.35.1
regex==2023.12.25
requests==2.32.3
rfc3339-validator==0.1.4
rfc3987==1.3.8
rich==13.7.1
rpds-py==0.18.1
rstr==3.2.2
s3transfer==0.10.1
setuptools==70.0.0
s3transfer==0.10.2
setuptools==70.1.0
shellingham==1.5.4
six==1.16.0
sniffio==1.3.1
SQLAlchemy==2.0.30
tenacity==8.3.0
SQLAlchemy==2.0.31
tenacity==8.4.2
tiktoken==0.7.0
tokenizers==0.19.1
tqdm==4.66.4
typer==0.9.4
typing_extensions==4.12.0
urllib3==2.2.1
types-python-dateutil==2.9.0.20240316
typing_extensions==4.12.2
uri-template==1.3.0
urllib3==2.2.2
webcolors==24.6.0
Werkzeug==3.0.3
wrapt==1.16.0
yarl==1.9.4
zipp==3.19.0
zipp==3.19.2
22 changes: 19 additions & 3 deletions tests/blueprints/test_guards.py
Original file line number Diff line number Diff line change
@@ -44,8 +44,8 @@ def test_route_setup(mocker):

from guardrails_api.blueprints.guards import guards_bp

assert guards_bp.route_call_count == 3
assert guards_bp.routes == ["/", "/<guard_name>", "/<guard_name>/validate"]
assert guards_bp.route_call_count == 4
assert guards_bp.routes == ["/", "/<guard_name>", "/<guard_name>/validate", "/<guard_name>/history/<call_id>"]


def test_guards__get(mocker):
@@ -362,6 +362,7 @@ def test_validate__raises_bad_request__num_reasks(mocker):
def test_validate__parse(mocker):
os.environ["PGHOST"] = "localhost"
mock_outcome = ValidationOutcome(
call_id="mock-call-id",
raw_llm_output="Hello world!",
validated_output="Hello world!",
validation_passed=True,
@@ -386,6 +387,10 @@ def test_validate__parse(mocker):
"guardrails_api.blueprints.guards.guard_client.get_guard",
return_value=mock_guard,
)

mocker.patch(
"guardrails_api.blueprints.guards.CacheClient.set"
)

# mocker.patch("guardrails_api.blueprints.guards.get_tracer", return_value=mock_tracer)

@@ -430,6 +435,7 @@ def test_validate__parse(mocker):
# set_attribute_spy.assert_has_calls(expected_calls)

assert response == {
"callId": "mock-call-id",
"validatedOutput": "Hello world!",
"validationPassed": True,
"rawLlmOutput": "Hello world!",
@@ -442,7 +448,10 @@ def test_validate__call(mocker):
os.environ["PGHOST"] = "localhost"
mock_guard = MockGuardStruct()
mock_outcome = ValidationOutcome(
raw_llm_output="Hello world!", validated_output=None, validation_passed=False
call_id="mock-call-id",
raw_llm_output="Hello world!",
validated_output=None,
validation_passed=False
)

mock___call__ = mocker.patch.object(MockGuardStruct, "__call__")
@@ -475,6 +484,12 @@ def test_validate__call(mocker):
"guardrails_api.blueprints.guards.get_llm_callable",
return_value="openai.Completion.create",
)

mocker.patch(
"guardrails_api.blueprints.guards.CacheClient.set"
)



# mocker.patch("guardrails_api.blueprints.guards.get_tracer", return_value=mock_tracer)

@@ -523,6 +538,7 @@ def test_validate__call(mocker):
# set_attribute_spy.assert_has_calls(expected_calls)

assert response == {
"callId": "mock-call-id",
"validationPassed": False,
"validatedOutput": None,
"rawLlmOutput": "Hello world!",
4 changes: 2 additions & 2 deletions tests/cli/test_start.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ def test_start(mocker):

from guardrails_api.cli.start import start

start("env", "config")
start("env", "config", 5, 10, 8000)

mock_gunicorn.assert_called_once_with(
mock_flask_app,
@@ -20,4 +20,4 @@ def test_start(mocker):
},
)

mock_create_app.assert_called_once_with("env", "config")
mock_create_app.assert_called_once_with("env", "config", 8000)

0 comments on commit 6661b07

Please sign in to comment.