Repository navigation
Add fastapi bim cde guardrail for retail store roll-outs - #24
Conversation
Contribution License Agreement - Valid 🟢All commits contain valid signoff. As a confirmation, remember that you are agreeing to our Contribution License Agreement. |
0e0ad7e to
9b5de06
Compare
There was a problem hiding this comment.
This file isn’t loaded by the app, which builds the gates in Python. Could we either load this config in the example or remove it?
There was a problem hiding this comment.
Good catch! Removed config.cerbia.yaml since the example configures the gates dynamically in Python.
| app = FastAPI( | ||
| title="CerbIA BIM Common Data Environment (CDE) Guardrail API", | ||
| description=( | ||
| "Production-grade Security Gate middleware securing AI Agent interactions " |
There was a problem hiding this comment.
Could we change "Production-grade" to "Illustrative example" and say that the model response is simulated? For example: "Illustrative FastAPI example showing input and output scanning with CerbIA."
There was a problem hiding this comment.
Updated the docstrings and FastAPI metadata to describe it as an illustrative example with simulated model responses, as suggested.
|
|
||
| | Package Code | Package Name (ES / EN) | Discipline Code | Discipline Name (ES / EN) | | ||
| | :--- | :--- | :---: | :--- | | ||
| | `000GRL` | GESTIÓN PROYECTO / PROJECT MANAGEMENT | `000` | GESTIÓN / MANAGEMENT | |
There was a problem hiding this comment.
Could we keep the matrix data and its labels in English too? The package and discipline names are currently Spanish, which makes the example harder to follow for readers who don’t speak Spanish. If the Spanish DNI case is important for demonstrating PII detection, that input can stay with a short English explanation.
There was a problem hiding this comment.
Updated! All package and discipline labels are now in English (200HVAC - HVAC & Mechanical Works / HVAC), while keeping the Spanish DNI test case clearly contextualized in English for PII validation.
|
|
||
| --- | ||
|
|
||
| ## 🏗️ BIM Work Breakdown Structure (WBS) Matrix |
There was a problem hiding this comment.
Could we simplify this and focus on the CerbIA flow? The BIM matrix and extra endpoints add a lot of domain-specific detail. One English package/discipline example should be enough to show input scanning, model handling, and output scanning.
There was a problem hiding this comment.
Done! Trimmed down the matrix to a single illustrative package/discipline (200HVAC / HVAC) to keep the primary focus on the CerbIA security gate flow.
| from fastapi import FastAPI, HTTPException, status | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| try: |
There was a problem hiding this comment.
Could we use one import instead of catching ImportError and retrying? The fallback can hide an import error from inside bim_matrix. from bim_matrix import ... works for the script, and the README can use uvicorn --app-dir examples/fastapi-bim-guardrail app:app --reload for the server.
There was a problem hiding this comment.
Switched to direct from bim_matrix import ... and updated the README instructions to use uvicorn --app-dir examples/fastapi-bim-guardrail app:app --reload.
| - Validates AI response before dispatching to the client. | ||
| """ | ||
| # Verify package validity | ||
| valid_entries = get_matrix_entries_by_package(request.package_code) |
There was a problem hiding this comment.
Could we also check that this discipline belongs to the package? Right now 200CLI with NOTREAL still returns 200 APPROVED. For example:
if not any(entry.discipline_code.upper() == request.discipline_code.upper() for entry in valid_entries):
raise HTTPException(status_code=400, detail="Invalid discipline for package")There was a problem hiding this comment.
Added the discipline-to-package check and test coverage for it. Requests with mismatched or invalid disciplines now return HTTP 400 Bad Request.
|
|
||
|
|
||
| BIM_WBS_MATRIX: Final[tuple[BimMatrixEntry, ...]] = ( | ||
| BimMatrixEntry("000GRL", "GESTIÓN PROYECTO", "PROJECT MANAGEMENT", "000", "GESTIÓN", "MANAGEMENT"), |
There was a problem hiding this comment.
Could we remove this Spanish matrix and keep just one small English package/discipline example? The current values make the source harder to follow, and most of this matrix isn’t needed to show how CerbIA is integrated.
There was a problem hiding this comment.
Simplified to a single English BimMatrixEntry('200HVAC', 'HVAC & Mechanical Works', 'HVAC', 'Heating, Ventilation & Air Conditioning').
There was a problem hiding this comment.
Could we turn these into pytest tests and assert the expected status and response for each scenario? At the moment, a wrong result is only printed, so the script still exits successfully. For example:
def test_prompt_injection_is_blocked(client: TestClient) -> None:
response = client.post("/api/v1/bim/query", json=prompt_injection_request)
assert response.status_code == 400
assert response.json()["detail"]["error"] == "SECURITY_GATE_BLOCKED_INPUT"Then pytest will fail if a scenario stops working.
There was a problem hiding this comment.
Converted the entire scenario runner into a standard pytest test suite with TestClient(app) and explicit assertions across all 11 test cases (200, 400, 404, 422).
|
|
||
| ## 🛡️ Security Architecture: Threat Model & Guardrails | ||
|
|
||
| ``` |
There was a problem hiding this comment.
Could we use a Mermaid diagram here instead of the ASCII art? It’ll be easier to read and maintain. For example:
flowchart TD
A[User prompt] --> B{Input gate}
B -->|Unsafe| C[HTTP 400]
B -->|Safe| D[Model response]
D --> E{Output gate}
E -->|Leaked canary or sensitive data| F[HTTP 422]
E -->|Safe| G[HTTP 200]
Since the model is simulated in this example, “Model response” may be clearer than “LLM inference”.
There was a problem hiding this comment.
Replaced the ASCII diagram with the suggested Mermaid flowchart TD diagram and clarified the model response as simulated.
- Model illustrative retail store BIM WBS package and discipline in English - Integrate CerbIA input and output security gates into a FastAPI service - Add canary token protection against confidential tender budget exfiltration - Provide comprehensive pytest test suite covering input safety and output gates Signed-off-by: jmcaamanog <jose.caamano@live.com>
9b5de06 to
78a55ca
Compare
|
Hi @sergiobe-itx! Thanks a lot for the thorough and helpful review! 🚀 All suggested changes have been implemented:
All checks are passing green, REUSE 3.3 compliant, and commits are signed with SSH. Ready for your review! |
|
@jmcaamanog Please create a dependency group named "examples" and add ...
[dependency-groups]
dev = [
"pre-commit==4.6.1",
...
"typer==0.27.0",
]
examples = [
"fastapi",
"httpx",
"uvicorn",
]Then regenerate the lockfile and include the updated uv.lock: This should resolve the missing FastAPI dependency during pytest collection. |
…corn Signed-off-by: jmcaamanog <jose.caamano@live.com>
|
Done @sergiobe-itx! Added the |
Summary of Changes
Adds a production-grade integration example in
examples/fastapi-bim-guardrail/demonstrating CerbIA as an asynchronous security gate middleware for an AI Assistant operating in an International Retail BIM Common Data Environment (CDE).bim_matrix.py) segregating Management / Contracting Packages (000GRLto900MET) from BIM Technical Disciplines (ARQ,FAC,ENV,CLI,HUM,PCI,ELE,ALU,RED,ATH,CTV,VIS,EST).config.cerbia.yaml).test_scenarios.pyverifying 5 end-to-end real-world scenarios.README.md).Validation
python test_scenarios.py).reuse lintpassed with 0 errors).ruff check&ruff format).Signed-off-by: jmcaamanog <jose.caamano@live.com>).