Skip to content

Commit 7153745

Browse files
authored
Merge pull request #1109 from guardrails-ai/fix_server_mismatch
2 parents f835998 + a926ef3 commit 7153745

File tree

12 files changed

+280
-9
lines changed

12 files changed

+280
-9
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__
2+
*.pyc
3+
*.pyo

.github/workflows/server_ci.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Server CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
build-test-server:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Check out head
14+
uses: actions/checkout@v3
15+
with:
16+
persist-credentials: false
17+
18+
- name: Set up QEMU
19+
uses: docker/setup-qemu-action@master
20+
with:
21+
platforms: linux/amd64
22+
23+
- name: Set up Docker Buildx
24+
uses: docker/setup-buildx-action@master
25+
with:
26+
platforms: linux/amd64
27+
28+
- name: Build Docker image
29+
uses: docker/build-push-action@v6
30+
with:
31+
context: .
32+
file: server_ci/Dockerfile
33+
platforms: linux/amd64
34+
push: false
35+
tags: guardrails:${{ github.sha }}
36+
load: true
37+
build-args: |
38+
GUARDRAILS_TOKEN=${{ secrets.GUARDRAILS_API_KEY }}
39+
40+
- name: Start Docker container
41+
run: |
42+
docker run -d --name guardrails-container -p 8000:8000 -e OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }} guardrails:${{ github.sha }}
43+
44+
- name: Wait for Docker container to be ready
45+
run: |
46+
for i in {1..30}; do
47+
if docker exec guardrails-container curl -s http://localhost:8000/; then
48+
echo "Server is up!"
49+
break
50+
fi
51+
echo "Waiting for server..."
52+
sleep 5
53+
done
54+
55+
- name: Run Pytest
56+
run: |
57+
pip install pytest openai guardrails-ai
58+
pytest server_ci/tests
59+
docker stop guardrails-container
60+
docker rm guardrails-container

docs/getting_started/guardrails_server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ We'll use the `create` command on the guardrails CLI to do this. We'll specify t
3535

3636

3737
```bash
38-
guardrails create --validators hub://guardrails/gibberish_text --name gibberish_guard
38+
guardrails create --validators hub://guardrails/gibberish_text --guard-name gibberish_guard
3939
```
4040

4141
:::note

docs/how_to_guides/hosting_with_docker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ First, we need to install any validators we want to use from the hub. In this ex
2828

2929

3030
```
31-
guardrails create --validators=hub://guardrails/regex_match --name=title-case
31+
guardrails create --validators=hub://guardrails/regex_match --guard-name=title-case
3232
```
3333

3434

guardrails/cli/create.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def create_command(
2222
default="",
2323
help="A comma-separated list of validator hub URIs.",
2424
),
25-
name: Optional[str] = typer.Option(
25+
guard_name: Optional[str] = typer.Option(
2626
default=None, help="The name of the guard to define in the file."
2727
),
2828
local_models: Optional[bool] = typer.Option(
@@ -78,13 +78,15 @@ def create_command(
7878
local_models,
7979
dry_run,
8080
)
81-
if name is None and validators:
82-
name = "Guard"
81+
if guard_name is None and validators:
82+
guard_name = "Guard"
8383
if len(installed_validators) > 0:
84-
name = installed_validators[0] + "Guard"
84+
guard_name = installed_validators[0] + "Guard"
8585

86-
console.print(f"No name provided for guard. Defaulting to {name}")
87-
new_config_file = generate_config_file(installed_validators, name)
86+
console.print(
87+
"No guard name provided for guard. Defaulting to {guard_name}"
88+
)
89+
new_config_file = generate_config_file(installed_validators, guard_name)
8890

8991
if dry_run:
9092
console.print(f"Not actually saving output to [bold]{filepath}[/bold]")

guardrails/cli/telemetry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ def trace_if_enabled(command_name: str):
1919
("machine", platform.machine()),
2020
("processor", platform.processor()),
2121
],
22+
False,
23+
False,
2224
)

guardrails/utils/hub_telemetry_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ def extract_current_context(self):
104104
context = self._prop.extract(carrier=self._carrier)
105105
return context
106106

107-
def create_new_span(self, span_name: str, attributes: list):
107+
def create_new_span(
108+
self,
109+
span_name: str,
110+
attributes: list,
111+
# todo deprecate these in 060
112+
is_parent: bool, #
113+
has_parent: bool, # no-qa
114+
):
108115
"""Creates a new span within the tracer with the given name and
109116
attributes.
110117

server_ci/Dockerfile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
FROM python:3.11-slim
2+
3+
ARG GUARDRAILS_TOKEN
4+
ARG GUARDRAILS_TEMPLATE="guard-template.json"
5+
6+
# Set environment variables to avoid writing .pyc files and to unbuffer Python output
7+
ENV PYTHONDONTWRITEBYTECODE=1
8+
ENV PYTHONUNBUFFERED=1
9+
ENV LOGLEVEL="DEBUG"
10+
ENV GUARDRAILS_LOG_LEVEL="DEBUG"
11+
ENV APP_ENVIRONMENT="production"
12+
ENV GUARDRAILS_TEMPLATE=$GUARDRAILS_TEMPLATE
13+
14+
WORKDIR /app
15+
16+
# Install Git and necessary dependencies
17+
RUN apt-get update && \
18+
apt-get install -y make git curl gcc jq pipx && \
19+
apt-get clean && \
20+
rm -rf /var/lib/apt/lists/*
21+
22+
RUN pipx install poetry
23+
24+
# Ensure poetry is available in the PATH
25+
ENV PATH="/root/.local/bin:$PATH"
26+
27+
# Copy the entrypoint script
28+
COPY /server_ci/entry.sh /app/entry.sh
29+
COPY ../ /app/guardrails
30+
31+
# Install guardrails, the guardrails API, and gunicorn
32+
# openai optional. only used for integration testing
33+
RUN pip install "gunicorn" "guardrails-api"
34+
35+
WORKDIR /app/guardrails
36+
37+
RUN poetry install
38+
39+
RUN pip install ./
40+
41+
RUN guardrails configure --enable-metrics --enable-remote-inferencing --token $GUARDRAILS_TOKEN
42+
43+
# bring in base template
44+
COPY /server_ci/$GUARDRAILS_TEMPLATE /app/$GUARDRAILS_TEMPLATE
45+
46+
# Install Hub Deps and create config.py
47+
RUN guardrails create --template /app/$GUARDRAILS_TEMPLATE
48+
49+
50+
RUN cp -r /usr/local/lib/python3.11/site-packages/guardrails/hub/* /app/guardrails/guardrails/hub
51+
52+
# Expose port 8000 for the application
53+
EXPOSE 8000
54+
55+
# Command to start the Gunicorn server with specified settings
56+
CMD ["/bin/bash", "/app/entry.sh"]

server_ci/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import json
2+
import os
3+
from guardrails import Guard
4+
5+
try:
6+
file_path = os.path.join(os.getcwd(), "guard-template.json")
7+
with open(file_path, "r") as fin:
8+
guards = json.load(fin)["guards"] or []
9+
except json.JSONDecodeError:
10+
print("Error parsing guards from JSON")
11+
SystemExit(1)
12+
13+
# instantiate guards
14+
guard0 = Guard.from_dict(guards[0])

server_ci/entry.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
gunicorn \
2+
--workers 3 \
3+
--threads 2 \
4+
--bind 0.0.0.0:8000 \
5+
--worker-class gthread \
6+
--timeout 30 \
7+
--keep-alive 20 \
8+
--preload \
9+
--graceful-timeout 60 \
10+
"guardrails_api.app:create_app()"

0 commit comments

Comments
 (0)