-
Notifications
You must be signed in to change notification settings - Fork 2
Add Guardrails API endpoints, validator configs and test infrastructure #11
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
50ab489
Setup repo and added bearer token based auth. #2
rkritika1508 a85ed62
Added .env.example file and updated the documentation #2
rkritika1508 e792dfa
Resolved comments #2
rkritika1508 1c6e74e
updated env in config #2
rkritika1508 3f1d2b1
Added validators
rkritika1508 a55d032
Added 3 validators and input guardrail api
rkritika1508 464da1c
Removed validator_cls from validator config and simplified code
rkritika1508 7a57b94
Fixed BanList issue and refactored code
rkritika1508 7043a5e
feat: add GET endpoint for validatorS
dennyabrain 518e066
Refactored API to get list of all validators
rkritika1508 d7828b2
Added docker setup
rkritika1508 a5a1bf1
Resolved comments
rkritika1508 e89ae6a
Fixed few comments
rkritika1508 f306ce9
Removed redundant files and fixed comments
rkritika1508 4b88f4b
Added unit tests
rkritika1508 56c0d32
feat: configure alembic; add db models and migration for request and …
dennyabrain a6ced05
Added more unit tests
rkritika1508 9183140
Made minor fixes
rkritika1508 8e0b61d
Added Guardrails AI setup steps in backend readme
rkritika1508 00f2f8a
Added integration tests and refactored file structure
rkritika1508 954d939
Added more integration tests
rkritika1508 8b24c43
Addressed documentation comments
rkritika1508 cd95c8d
Updated test scripts
rkritika1508 d66de4b
chore: testing session data in controller
dennyabrain 8785bb5
feat: add crud class for request log (wip)
dennyabrain 76a05cf
fixed env file
rkritika1508 9bffe5c
feat: database changes to log request and response to /input endpoint
dennyabrain 01b10f7
feat: add db logging for /output
dennyabrain ea4c348
updated config
rkritika1508 41bb7a0
Fixed unit tests based on new schema
rkritika1508 0847c37
Updated gitignore
rkritika1508 3932717
Updated PII remover
rkritika1508 dc3024d
Resolved comments
rkritika1508 e06dc86
Resolved comment
rkritika1508 f6df183
Updated unit tests
rkritika1508 4c64be5
Updated env fetching
rkritika1508 f5bb4be
updated route
rkritika1508 42f1817
updated setup
rkritika1508 86834a8
Resolved comments
rkritika1508 415c02b
updated ut
rkritika1508 966c0f3
Addressed comments
rkritika1508 5fbaa53
Resolved comment
rkritika1508 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| DOMAIN=localhost | ||
|
|
||
| # DOMAIN=localhost.tiangolo.com | ||
|
|
||
| # Environment: "development", "testing", "staging", "production" | ||
|
|
||
| ENVIRONMENT=development | ||
|
|
||
| PROJECT_NAME="Kaapi-Guardrails" | ||
| STACK_NAME=Kaapi-Guardrails | ||
|
|
||
| #Backend | ||
| SECRET_KEY=changethis | ||
| FIRST_SUPERUSER=superuser@example.com | ||
| FIRST_SUPERUSER_PASSWORD=changethis | ||
rkritika1508 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| EMAIL_TEST_USER="test@example.com" | ||
|
|
||
| # API Base URL for cron scripts (defaults to http://localhost:8000 if not set) | ||
| API_BASE_URL=http://localhost:8000 | ||
|
|
||
| # Postgres | ||
| POSTGRES_SERVER=localhost | ||
| POSTGRES_PORT=5432 | ||
| POSTGRES_DB=kaapi-guardrails | ||
| POSTGRES_USER=postgres | ||
| POSTGRES_PASSWORD=postgres | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| SENTRY_DSN= | ||
|
|
||
| # Configure these with your own Docker registry images | ||
|
|
||
| DOCKER_IMAGE_BACKEND=kaapi-guardrails-backend | ||
|
|
||
| # Callback Timeouts (in seconds) | ||
| CALLBACK_CONNECT_TIMEOUT=3 | ||
| CALLBACK_READ_TIMEOUT=10 | ||
|
|
||
| # require as a env if you want to use doc transformation | ||
| OPENAI_API_KEY=<ADD-KEY> | ||
| GUARDRAILS_HUB_API_KEY=<ADD-KEY> | ||
| AUTH_TOKEN=<ADD-TOKEN> | ||
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 |
|---|---|---|
| @@ -1,2 +1,84 @@ | ||
| # kaapi-guardrails | ||
| A repo for our experiments with Guardrails so can be integrated with Kaapi | ||
|
|
||
| # Kaapi Guardrails | ||
|
|
||
| [](https://www.gnu.org/licenses/agpl-3.0) | ||
|  | ||
|  | ||
| [](https://img.shields.io/github/commit-activity/m/ProjectTech4DevAI/kaapi-guardrails) | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Pre-requisites | ||
|
|
||
| - [docker](https://docs.docker.com/get-started/get-docker/) Docker | ||
| - [uv](https://docs.astral.sh/uv/) for Python package and environment management. | ||
|
|
||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Project Setup | ||
|
|
||
| You can **just fork or clone** this repository and use it as is. | ||
|
|
||
| ✨ It just works. ✨ | ||
|
|
||
| ### Configure | ||
|
|
||
| Create env file using example file | ||
|
|
||
| ```bash | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| You can then update configs in the `.env` files to customize your configurations. | ||
|
|
||
| ⚠️ Some services depend on these environment variables being set correctly. Missing or invalid values may cause startup issues. | ||
|
|
||
| ## Bootstrap & development mode | ||
|
|
||
| You have two options to start this dockerized setup, depending on whether you want to reset the database: | ||
| ### Option A: Run migrations & seed data (will reset DB) | ||
|
|
||
| Use the prestart profile to automatically run database migrations and seed data. | ||
| This profile also resets the database, so use it only when you want a fresh start. | ||
| ```bash | ||
| docker compose --profile prestart up | ||
| ``` | ||
|
|
||
| ### Option B: Start normally without resetting DB | ||
|
|
||
| If you don't want to reset the database, start the project directly: | ||
| ```bash | ||
| docker compose watch | ||
| ``` | ||
| This will start all services in watch mode for development — ideal for local iterations. | ||
|
|
||
| ### Rebuilding Images | ||
|
|
||
| ```bash | ||
| docker compose up --build -d | ||
| ``` | ||
|
|
||
| This is also necessary when: | ||
| - Dependencies change in `pyproject.toml` or `uv.lock` | ||
| - You modify Dockerfile configurations | ||
| - Changes aren't being reflected in the running containers | ||
|
|
||
| ## Backend Development | ||
|
|
||
| Backend docs: [backend/README.md](./backend/README.md). | ||
|
|
||
| ## Deployment | ||
|
|
||
| Deployment docs: [deployment.md](./deployment.md). | ||
|
|
||
| ## Development | ||
|
|
||
| General development docs: [development.md](./development.md). | ||
|
|
||
| This includes using Docker Compose, custom local domains, `.env` configurations, etc. | ||
|
|
||
| ## Release Notes | ||
|
|
||
| Check the file [release-notes.md](./release-notes.md). | ||
|
|
||
| ## Credits | ||
|
|
||
| This project was created using [full-stack-fastapi-template](https://github.com/fastapi/full-stack-fastapi-template). A big thank you to the team for creating and maintaining the template!!! | ||
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,10 @@ | ||
| __pycache__ | ||
| app.egg-info | ||
| *.pyc | ||
| .mypy_cache | ||
| .coverage | ||
| htmlcov | ||
| .cache | ||
| .venv | ||
| .DS_Store | ||
| .env |
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,49 @@ | ||
| FROM python:3.10 | ||
|
|
||
| ENV PYTHONUNBUFFERED=1 | ||
|
|
||
| WORKDIR /app/ | ||
|
|
||
| # Install uv | ||
| # Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv | ||
| COPY --from=ghcr.io/astral-sh/uv:0.5.11 /uv /uvx /bin/ | ||
|
|
||
| # Place executables in the environment at the front of the path | ||
| # Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment | ||
| ENV PATH="/app/.venv/bin:$PATH" | ||
|
|
||
| # Compile bytecode | ||
| # Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode | ||
| ENV UV_COMPILE_BYTECODE=1 | ||
|
|
||
| # uv Cache | ||
| # Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching | ||
| ENV UV_LINK_MODE=copy | ||
|
|
||
| # Install dependencies | ||
| # Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers | ||
| RUN --mount=type=cache,target=/root/.cache/uv \ | ||
| --mount=type=bind,source=uv.lock,target=uv.lock \ | ||
| --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ | ||
| uv sync --frozen --no-install-project | ||
|
|
||
| ENV PYTHONPATH=/app | ||
| RUN apt-get update && apt-get install -y jq && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| COPY ./scripts /app/scripts | ||
| RUN chmod +x /app/scripts/*.sh | ||
|
|
||
| COPY ./pyproject.toml ./uv.lock ./alembic.ini /app/ | ||
|
|
||
| COPY ./app /app/app | ||
| COPY ./tests /app/tests | ||
|
|
||
| # Sync the project | ||
| # Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers | ||
| RUN --mount=type=cache,target=/root/.cache/uv \ | ||
| uv sync | ||
|
|
||
| # ------------------------------- | ||
| # Entrypoint (runtime setup) | ||
| # ------------------------------- | ||
| ENTRYPOINT ["/app/scripts/entrypoint.sh"] |
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,156 @@ | ||
| # FastAPI Project - Backend | ||
|
|
||
| ## Requirements | ||
|
|
||
| * [Docker](https://www.docker.com/). | ||
| * [uv](https://docs.astral.sh/uv/) for Python package and environment management. | ||
|
|
||
| ## Docker Compose | ||
|
|
||
| Start the local development environment with Docker Compose following the guide in [../development.md](../development.md). | ||
|
|
||
| ## General Workflow | ||
|
|
||
| By default, the dependencies are managed with [uv](https://docs.astral.sh/uv/), go there and install it. | ||
|
|
||
| From `./backend/` you can install all the dependencies with: | ||
|
|
||
| ```console | ||
| $ uv sync | ||
| ``` | ||
|
|
||
| Then you can activate the virtual environment with: | ||
|
|
||
| ```console | ||
| $ source .venv/bin/activate | ||
| ``` | ||
|
|
||
| Make sure your editor is using the correct Python virtual environment, with the interpreter at `backend/.venv/bin/python`. | ||
|
|
||
| Modify or add SQLModel models for data and SQL tables in `./backend/app/models/`, API endpoints in `./backend/app/api/`. | ||
|
|
||
| ## VS Code | ||
|
|
||
| There are already configurations in place to run the backend through the VS Code debugger, so that you can use breakpoints, pause and explore variables, etc. | ||
|
|
||
| The setup is also already configured so you can run the tests through the VS Code Python tests tab. | ||
|
|
||
| There is also a command override that runs `fastapi run --reload` instead of the default `fastapi run`. It starts a single server process (instead of multiple, as would be for production) and reloads the process whenever the code changes. Have in mind that if you have a syntax error and save the Python file, it will break and exit, and the container will stop. After that, you can restart the container by fixing the error and running again: | ||
|
|
||
| ```console | ||
| $ docker compose watch | ||
| ``` | ||
|
|
||
| There is also a commented out `command` override, you can uncomment it and comment the default one. It makes the backend container run a process that does "nothing", but keeps the container alive. That allows you to get inside your running container and execute commands inside, for example a Python interpreter to test installed dependencies, or start the development server that reloads when it detects changes. | ||
|
|
||
| To get inside the container with a `bash` session you can start the stack with: | ||
|
|
||
| ```console | ||
| $ docker compose watch | ||
| ``` | ||
|
|
||
| and then in another terminal, `exec` inside the running container: | ||
|
|
||
| ```console | ||
| $ docker compose exec backend bash | ||
| ``` | ||
|
|
||
| You should see an output like: | ||
|
|
||
| ```console | ||
| root@7f2607af31c3:/app# | ||
| ``` | ||
|
|
||
| that means that you are in a `bash` session inside your container, as a `root` user, under the `/app` directory, this directory has another directory called "app" inside, that's where your code lives inside the container: `/app/app`. | ||
|
|
||
| There you can use the `fastapi run --reload` command to run the debug live reloading server. | ||
|
|
||
| ```console | ||
| $ fastapi run --reload app/main.py | ||
| ``` | ||
|
|
||
| ...it will look like: | ||
|
|
||
| ```console | ||
| root@7f2607af31c3:/app# fastapi run --reload app/main.py | ||
| ``` | ||
|
|
||
| and then hit enter. That runs the live reloading server that auto reloads when it detects code changes. | ||
|
|
||
| Nevertheless, if it doesn't detect a change but a syntax error, it will just stop with an error. But as the container is still alive and you are in a Bash session, you can quickly restart it after fixing the error, running the same command ("up arrow" and "Enter"). | ||
|
|
||
| ...this previous detail is what makes it useful to have the container alive doing nothing and then, in a Bash session, make it run the live reload server. | ||
|
|
||
| ## Backend tests | ||
|
|
||
| To test the backend run: | ||
|
|
||
| ```console | ||
| $ bash ./scripts/test.sh | ||
| ``` | ||
|
|
||
| The tests run with Pytest, modify and add tests to `./backend/tests/`. | ||
|
|
||
| If you use GitHub Actions the tests will run automatically. | ||
|
|
||
| ### Test running stack | ||
|
|
||
| If your stack is already up and you just want to run the tests, you can use: | ||
|
|
||
| ```bash | ||
| docker compose exec backend bash scripts/tests-start.sh | ||
| ``` | ||
|
|
||
| That `/app/scripts/tests-start.sh` script just calls `pytest` after making sure that the rest of the stack is running. If you need to pass extra arguments to `pytest`, you can pass them to that command and they will be forwarded. | ||
|
|
||
| For example, to stop on first error: | ||
|
|
||
| ```bash | ||
| docker compose exec backend bash scripts/tests-start.sh -x | ||
| ``` | ||
|
|
||
| ### Test Coverage | ||
|
|
||
| When the tests are run, a file `htmlcov/index.html` is generated, you can open it in your browser to see the coverage of the tests. | ||
|
|
||
| ## Migrations | ||
|
|
||
| As during local development your app directory is mounted as a volume inside the container, you can also run the migrations with `alembic` commands inside the container and the migration code will be in your app directory (instead of being only inside the container). So you can add it to your git repository. | ||
|
|
||
| Make sure you create a "revision" of your models and that you "upgrade" your database with that revision every time you change them. As this is what will update the tables in your database. Otherwise, your application will have errors. | ||
|
|
||
| * Start an interactive session in the backend container: | ||
|
|
||
| ```console | ||
| $ docker compose exec backend bash | ||
| ``` | ||
|
|
||
| * Alembic is already configured to import your SQLModel models from `./backend/app/models.py`. | ||
rkritika1508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| * After changing a model (for example, adding a column), inside the container, create a revision, e.g.: | ||
|
|
||
| ```console | ||
| $ alembic revision --autogenerate -m "Add column last_name to User model" | ||
| ``` | ||
|
|
||
| * Commit to the git repository the files generated in the alembic directory. | ||
|
|
||
| * After creating the revision, run the migration in the database (this is what will actually change the database): | ||
|
|
||
| ```console | ||
| $ alembic upgrade head | ||
| ``` | ||
|
|
||
| If you don't want to use migrations at all, uncomment the lines in the file at `./backend/app/core/db.py` that end in: | ||
|
|
||
| ```python | ||
| SQLModel.metadata.create_all(engine) | ||
| ``` | ||
|
|
||
| and comment the line in the file `scripts/prestart.sh` that contains: | ||
|
|
||
| ```console | ||
| $ alembic upgrade head | ||
| ``` | ||
|
|
||
| If you don't want to start with the default models and want to remove them / modify them, from the beginning, without having any previous revision, you can remove the revision files (`.py` Python files) under `./backend/app/alembic/versions/`. And then create a first migration as described above. | ||
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.