substrai-guardrailgraph 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- substrai_guardrailgraph-0.1.0/.gitignore +45 -0
- substrai_guardrailgraph-0.1.0/LICENSE +21 -0
- substrai_guardrailgraph-0.1.0/PKG-INFO +244 -0
- substrai_guardrailgraph-0.1.0/README.md +208 -0
- substrai_guardrailgraph-0.1.0/pyproject.toml +75 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/__init__.py +19 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/checks/__init__.py +20 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/checks/cost.py +124 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/checks/injection.py +192 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/checks/pii.py +200 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/checks/topics.py +153 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/checks/toxicity.py +185 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/cli/__init__.py +1 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/cli/commands/__init__.py +2 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/cli/commands/dev.py +52 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/cli/commands/init.py +242 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/cli/commands/test.py +146 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/cli/commands/validate.py +81 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/cli/main.py +104 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/core/__init__.py +1 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/core/actions.py +28 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/core/check.py +244 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/core/config.py +213 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/core/context.py +36 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/core/pipeline.py +336 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/core/result.py +122 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/middleware/__init__.py +6 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/middleware/base.py +152 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/middleware/wrapper.py +91 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/observability/__init__.py +6 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/observability/audit.py +103 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/observability/metrics.py +95 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/packs/__init__.py +5 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/packs/financial.py +121 -0
- substrai_guardrailgraph-0.1.0/src/guardrailgraph/packs/hipaa.py +153 -0
- substrai_guardrailgraph-0.1.0/tests/__init__.py +0 -0
- substrai_guardrailgraph-0.1.0/tests/test_check.py +111 -0
- substrai_guardrailgraph-0.1.0/tests/test_checks.py +183 -0
- substrai_guardrailgraph-0.1.0/tests/test_middleware.py +112 -0
- substrai_guardrailgraph-0.1.0/tests/test_packs.py +83 -0
- substrai_guardrailgraph-0.1.0/tests/test_pipeline.py +163 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
*.egg
|
|
21
|
+
|
|
22
|
+
# Virtual environments
|
|
23
|
+
.venv/
|
|
24
|
+
venv/
|
|
25
|
+
ENV/
|
|
26
|
+
|
|
27
|
+
# IDE
|
|
28
|
+
.idea/
|
|
29
|
+
.vscode/
|
|
30
|
+
*.swp
|
|
31
|
+
*.swo
|
|
32
|
+
|
|
33
|
+
# Testing
|
|
34
|
+
.pytest_cache/
|
|
35
|
+
.coverage
|
|
36
|
+
htmlcov/
|
|
37
|
+
.mypy_cache/
|
|
38
|
+
|
|
39
|
+
# OS
|
|
40
|
+
.DS_Store
|
|
41
|
+
Thumbs.db
|
|
42
|
+
|
|
43
|
+
# Audit logs (generated)
|
|
44
|
+
*.jsonl
|
|
45
|
+
reports/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gaurav Kumar Sinha / SubstrAI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: substrai-guardrailgraph
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Composable AI safety pipeline framework with industry compliance packs
|
|
5
|
+
Project-URL: Homepage, https://github.com/substrai/guardrailgraph
|
|
6
|
+
Project-URL: Documentation, https://substrai.github.io/guardrailgraph
|
|
7
|
+
Project-URL: Repository, https://github.com/substrai/guardrailgraph
|
|
8
|
+
Project-URL: Issues, https://github.com/substrai/guardrailgraph/issues
|
|
9
|
+
Author-email: Gaurav Kumar Sinha <gaurav@substrai.dev>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: ai-safety,aws-lambda,compliance,dag,guardrails,hipaa,llm,pipeline,serverless
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Classifier: Topic :: Security
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Requires-Dist: pyyaml>=6.0
|
|
25
|
+
Provides-Extra: all
|
|
26
|
+
Requires-Dist: boto3>=1.28.0; extra == 'all'
|
|
27
|
+
Provides-Extra: aws
|
|
28
|
+
Requires-Dist: boto3>=1.28.0; extra == 'aws'
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: mypy>=1.0; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
32
|
+
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
|
|
33
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
34
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# GuardrailGraph
|
|
38
|
+
|
|
39
|
+
> **Composable AI safety pipeline framework** — define guardrails as a DAG of checks that work across any LLM provider, with industry-specific compliance packs for HIPAA, SOX, GDPR, and FedRAMP.
|
|
40
|
+
|
|
41
|
+
[](https://pypi.org/project/substrai-guardrailgraph/)
|
|
42
|
+
[](https://www.npmjs.com/package/@substrai/guardrailgraph)
|
|
43
|
+
[](https://opensource.org/licenses/MIT)
|
|
44
|
+
|
|
45
|
+
## Why GuardrailGraph?
|
|
46
|
+
|
|
47
|
+
Every enterprise deploying LLMs needs guardrails. Current options are either provider-locked (Bedrock Guardrails), complex (NeMo Guardrails), or limited (Guardrails AI). GuardrailGraph is the first framework that combines:
|
|
48
|
+
|
|
49
|
+
- **Composable DAG execution** — checks run in parallel for low latency
|
|
50
|
+
- **Provider agnostic** — works with Bedrock, OpenAI, Anthropic, or any LLM
|
|
51
|
+
- **Industry compliance packs** — HIPAA, SOX, GDPR out of the box
|
|
52
|
+
- **Serverless-native** — designed for AWS Lambda from day one
|
|
53
|
+
- **Simple API** — `@check` decorator + `pipeline()` builder
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# Python
|
|
59
|
+
pip install substrai-guardrailgraph
|
|
60
|
+
|
|
61
|
+
# npm (TypeScript/JavaScript)
|
|
62
|
+
npm install @substrai/guardrailgraph
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Quick Start
|
|
66
|
+
|
|
67
|
+
### 5-Minute Setup
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from guardrailgraph import pipeline, check, Action
|
|
71
|
+
from guardrailgraph.checks import pii_check, toxicity_check, injection_check
|
|
72
|
+
|
|
73
|
+
# Create a pipeline with built-in checks
|
|
74
|
+
my_pipeline = pipeline(
|
|
75
|
+
name="my-app",
|
|
76
|
+
checks=[
|
|
77
|
+
pii_check(action=Action.REDACT),
|
|
78
|
+
toxicity_check(threshold=0.7),
|
|
79
|
+
injection_check(),
|
|
80
|
+
],
|
|
81
|
+
mode="fail-closed",
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
# Run guardrails on any text
|
|
85
|
+
result = my_pipeline.run("User input here")
|
|
86
|
+
|
|
87
|
+
if result.allowed:
|
|
88
|
+
# Safe to forward to LLM
|
|
89
|
+
text = result.modified_text or "User input here"
|
|
90
|
+
else:
|
|
91
|
+
# Content blocked
|
|
92
|
+
print(f"Blocked: {result.action.value}")
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Custom Checks
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from guardrailgraph import check, Action
|
|
99
|
+
|
|
100
|
+
@check(name="profanity", action=Action.BLOCK, threshold=0.7)
|
|
101
|
+
def check_profanity(text: str) -> dict:
|
|
102
|
+
"""Custom profanity detection."""
|
|
103
|
+
bad_words = ["badword1", "badword2"]
|
|
104
|
+
found = [w for w in bad_words if w in text.lower()]
|
|
105
|
+
return {
|
|
106
|
+
"detected": len(found) > 0,
|
|
107
|
+
"confidence": min(len(found) / 2.0, 1.0),
|
|
108
|
+
"matched": found,
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Industry Compliance Packs
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from guardrailgraph import pipeline
|
|
116
|
+
from guardrailgraph.packs import hipaa, financial
|
|
117
|
+
|
|
118
|
+
# HIPAA-compliant healthcare chatbot
|
|
119
|
+
healthcare = pipeline(
|
|
120
|
+
name="patient-assistant",
|
|
121
|
+
packs=[hipaa.full()],
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
# SOX-compliant financial advisor
|
|
125
|
+
finance = pipeline(
|
|
126
|
+
name="investment-advisor",
|
|
127
|
+
packs=[financial.sox()],
|
|
128
|
+
mode="fail-closed",
|
|
129
|
+
)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Middleware Integration
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
from guardrailgraph.middleware import guardrail
|
|
136
|
+
|
|
137
|
+
@guardrail(pipeline=my_pipeline)
|
|
138
|
+
def call_llm(prompt: str) -> str:
|
|
139
|
+
"""Your LLM call — automatically wrapped with guardrails."""
|
|
140
|
+
import boto3
|
|
141
|
+
client = boto3.client("bedrock-runtime")
|
|
142
|
+
# ... invoke model ...
|
|
143
|
+
return response
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## YAML Configuration
|
|
147
|
+
|
|
148
|
+
```yaml
|
|
149
|
+
# guardrailgraph.yaml
|
|
150
|
+
project:
|
|
151
|
+
name: "my-app-guardrails"
|
|
152
|
+
version: "1.0.0"
|
|
153
|
+
|
|
154
|
+
pipeline:
|
|
155
|
+
mode: fail-closed
|
|
156
|
+
timeout_ms: 500
|
|
157
|
+
parallel: true
|
|
158
|
+
|
|
159
|
+
checks:
|
|
160
|
+
- name: pii-detection
|
|
161
|
+
type: builtin/pii
|
|
162
|
+
action: redact
|
|
163
|
+
config:
|
|
164
|
+
entity_types: [SSN, PHONE, EMAIL, CREDIT_CARD]
|
|
165
|
+
|
|
166
|
+
- name: toxicity
|
|
167
|
+
type: builtin/toxicity
|
|
168
|
+
action: block
|
|
169
|
+
config:
|
|
170
|
+
threshold: 0.7
|
|
171
|
+
|
|
172
|
+
- name: prompt-injection
|
|
173
|
+
type: builtin/injection
|
|
174
|
+
action: block
|
|
175
|
+
config:
|
|
176
|
+
sensitivity: high
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## CLI
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
# Scaffold a new project
|
|
183
|
+
guardrailgraph init my-project
|
|
184
|
+
guardrailgraph init my-project --pack hipaa
|
|
185
|
+
|
|
186
|
+
# Development
|
|
187
|
+
guardrailgraph dev # Interactive testing
|
|
188
|
+
guardrailgraph test # Run tests
|
|
189
|
+
guardrailgraph test --adversarial # Adversarial suite
|
|
190
|
+
guardrailgraph validate # Validate config
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Built-in Checks
|
|
194
|
+
|
|
195
|
+
| Check | Description | Default Action |
|
|
196
|
+
|-------|-------------|----------------|
|
|
197
|
+
| `pii_check()` | Detects SSN, phone, email, credit card, IP | REDACT |
|
|
198
|
+
| `toxicity_check()` | Scores hate, violence, sexual, self-harm | BLOCK |
|
|
199
|
+
| `topic_check()` | Block/allow specific topics | BLOCK |
|
|
200
|
+
| `injection_check()` | Prompt injection defense | BLOCK |
|
|
201
|
+
| `cost_check()` | Token/cost limits per request | BLOCK |
|
|
202
|
+
|
|
203
|
+
## Architecture
|
|
204
|
+
|
|
205
|
+
```
|
|
206
|
+
Input → [Check 1] ──→ [Check 2] ──→ [Check 3]
|
|
207
|
+
(parallel) (parallel) (parallel)
|
|
208
|
+
↓ ↓ ↓
|
|
209
|
+
[PASS/BLOCK/REDACT/FLAG_FOR_REVIEW]
|
|
210
|
+
↓
|
|
211
|
+
[Final Decision + Audit Log]
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Checks execute as a **DAG** (directed acyclic graph). Independent checks run in parallel for minimum latency. Dependent checks run sequentially.
|
|
215
|
+
|
|
216
|
+
## Integration with LambdaLLM
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
from lambdallm import handler, Model
|
|
220
|
+
from guardrailgraph import pipeline
|
|
221
|
+
from guardrailgraph.packs import hipaa
|
|
222
|
+
|
|
223
|
+
@handler(
|
|
224
|
+
model=Model.CLAUDE_3_SONNET,
|
|
225
|
+
guardrails=pipeline(packs=[hipaa.full()]),
|
|
226
|
+
)
|
|
227
|
+
def lambda_handler(event, context):
|
|
228
|
+
return context.invoke("Answer: {q}", q=event["body"]["question"])
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Comparison
|
|
232
|
+
|
|
233
|
+
| Feature | Bedrock Guardrails | NeMo | Guardrails AI | **GuardrailGraph** |
|
|
234
|
+
|---------|-------------------|------|---------------|-------------------|
|
|
235
|
+
| Provider agnostic | ❌ | ❌ | Partial | ✅ |
|
|
236
|
+
| Composable DAG | ❌ | ❌ | ❌ | ✅ |
|
|
237
|
+
| Industry packs | ❌ | ❌ | ❌ | ✅ |
|
|
238
|
+
| Serverless-native | Managed | ❌ | ❌ | ✅ |
|
|
239
|
+
| Custom checks | Limited | Complex | Yes | ✅ Simple |
|
|
240
|
+
| Open source | ❌ | ✅ | ✅ | ✅ MIT |
|
|
241
|
+
|
|
242
|
+
## License
|
|
243
|
+
|
|
244
|
+
MIT © [Gaurav Kumar Sinha](https://github.com/substrai)
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# GuardrailGraph
|
|
2
|
+
|
|
3
|
+
> **Composable AI safety pipeline framework** — define guardrails as a DAG of checks that work across any LLM provider, with industry-specific compliance packs for HIPAA, SOX, GDPR, and FedRAMP.
|
|
4
|
+
|
|
5
|
+
[](https://pypi.org/project/substrai-guardrailgraph/)
|
|
6
|
+
[](https://www.npmjs.com/package/@substrai/guardrailgraph)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
|
|
9
|
+
## Why GuardrailGraph?
|
|
10
|
+
|
|
11
|
+
Every enterprise deploying LLMs needs guardrails. Current options are either provider-locked (Bedrock Guardrails), complex (NeMo Guardrails), or limited (Guardrails AI). GuardrailGraph is the first framework that combines:
|
|
12
|
+
|
|
13
|
+
- **Composable DAG execution** — checks run in parallel for low latency
|
|
14
|
+
- **Provider agnostic** — works with Bedrock, OpenAI, Anthropic, or any LLM
|
|
15
|
+
- **Industry compliance packs** — HIPAA, SOX, GDPR out of the box
|
|
16
|
+
- **Serverless-native** — designed for AWS Lambda from day one
|
|
17
|
+
- **Simple API** — `@check` decorator + `pipeline()` builder
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Python
|
|
23
|
+
pip install substrai-guardrailgraph
|
|
24
|
+
|
|
25
|
+
# npm (TypeScript/JavaScript)
|
|
26
|
+
npm install @substrai/guardrailgraph
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
### 5-Minute Setup
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from guardrailgraph import pipeline, check, Action
|
|
35
|
+
from guardrailgraph.checks import pii_check, toxicity_check, injection_check
|
|
36
|
+
|
|
37
|
+
# Create a pipeline with built-in checks
|
|
38
|
+
my_pipeline = pipeline(
|
|
39
|
+
name="my-app",
|
|
40
|
+
checks=[
|
|
41
|
+
pii_check(action=Action.REDACT),
|
|
42
|
+
toxicity_check(threshold=0.7),
|
|
43
|
+
injection_check(),
|
|
44
|
+
],
|
|
45
|
+
mode="fail-closed",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# Run guardrails on any text
|
|
49
|
+
result = my_pipeline.run("User input here")
|
|
50
|
+
|
|
51
|
+
if result.allowed:
|
|
52
|
+
# Safe to forward to LLM
|
|
53
|
+
text = result.modified_text or "User input here"
|
|
54
|
+
else:
|
|
55
|
+
# Content blocked
|
|
56
|
+
print(f"Blocked: {result.action.value}")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Custom Checks
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from guardrailgraph import check, Action
|
|
63
|
+
|
|
64
|
+
@check(name="profanity", action=Action.BLOCK, threshold=0.7)
|
|
65
|
+
def check_profanity(text: str) -> dict:
|
|
66
|
+
"""Custom profanity detection."""
|
|
67
|
+
bad_words = ["badword1", "badword2"]
|
|
68
|
+
found = [w for w in bad_words if w in text.lower()]
|
|
69
|
+
return {
|
|
70
|
+
"detected": len(found) > 0,
|
|
71
|
+
"confidence": min(len(found) / 2.0, 1.0),
|
|
72
|
+
"matched": found,
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Industry Compliance Packs
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from guardrailgraph import pipeline
|
|
80
|
+
from guardrailgraph.packs import hipaa, financial
|
|
81
|
+
|
|
82
|
+
# HIPAA-compliant healthcare chatbot
|
|
83
|
+
healthcare = pipeline(
|
|
84
|
+
name="patient-assistant",
|
|
85
|
+
packs=[hipaa.full()],
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
# SOX-compliant financial advisor
|
|
89
|
+
finance = pipeline(
|
|
90
|
+
name="investment-advisor",
|
|
91
|
+
packs=[financial.sox()],
|
|
92
|
+
mode="fail-closed",
|
|
93
|
+
)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Middleware Integration
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from guardrailgraph.middleware import guardrail
|
|
100
|
+
|
|
101
|
+
@guardrail(pipeline=my_pipeline)
|
|
102
|
+
def call_llm(prompt: str) -> str:
|
|
103
|
+
"""Your LLM call — automatically wrapped with guardrails."""
|
|
104
|
+
import boto3
|
|
105
|
+
client = boto3.client("bedrock-runtime")
|
|
106
|
+
# ... invoke model ...
|
|
107
|
+
return response
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## YAML Configuration
|
|
111
|
+
|
|
112
|
+
```yaml
|
|
113
|
+
# guardrailgraph.yaml
|
|
114
|
+
project:
|
|
115
|
+
name: "my-app-guardrails"
|
|
116
|
+
version: "1.0.0"
|
|
117
|
+
|
|
118
|
+
pipeline:
|
|
119
|
+
mode: fail-closed
|
|
120
|
+
timeout_ms: 500
|
|
121
|
+
parallel: true
|
|
122
|
+
|
|
123
|
+
checks:
|
|
124
|
+
- name: pii-detection
|
|
125
|
+
type: builtin/pii
|
|
126
|
+
action: redact
|
|
127
|
+
config:
|
|
128
|
+
entity_types: [SSN, PHONE, EMAIL, CREDIT_CARD]
|
|
129
|
+
|
|
130
|
+
- name: toxicity
|
|
131
|
+
type: builtin/toxicity
|
|
132
|
+
action: block
|
|
133
|
+
config:
|
|
134
|
+
threshold: 0.7
|
|
135
|
+
|
|
136
|
+
- name: prompt-injection
|
|
137
|
+
type: builtin/injection
|
|
138
|
+
action: block
|
|
139
|
+
config:
|
|
140
|
+
sensitivity: high
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## CLI
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
# Scaffold a new project
|
|
147
|
+
guardrailgraph init my-project
|
|
148
|
+
guardrailgraph init my-project --pack hipaa
|
|
149
|
+
|
|
150
|
+
# Development
|
|
151
|
+
guardrailgraph dev # Interactive testing
|
|
152
|
+
guardrailgraph test # Run tests
|
|
153
|
+
guardrailgraph test --adversarial # Adversarial suite
|
|
154
|
+
guardrailgraph validate # Validate config
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Built-in Checks
|
|
158
|
+
|
|
159
|
+
| Check | Description | Default Action |
|
|
160
|
+
|-------|-------------|----------------|
|
|
161
|
+
| `pii_check()` | Detects SSN, phone, email, credit card, IP | REDACT |
|
|
162
|
+
| `toxicity_check()` | Scores hate, violence, sexual, self-harm | BLOCK |
|
|
163
|
+
| `topic_check()` | Block/allow specific topics | BLOCK |
|
|
164
|
+
| `injection_check()` | Prompt injection defense | BLOCK |
|
|
165
|
+
| `cost_check()` | Token/cost limits per request | BLOCK |
|
|
166
|
+
|
|
167
|
+
## Architecture
|
|
168
|
+
|
|
169
|
+
```
|
|
170
|
+
Input → [Check 1] ──→ [Check 2] ──→ [Check 3]
|
|
171
|
+
(parallel) (parallel) (parallel)
|
|
172
|
+
↓ ↓ ↓
|
|
173
|
+
[PASS/BLOCK/REDACT/FLAG_FOR_REVIEW]
|
|
174
|
+
↓
|
|
175
|
+
[Final Decision + Audit Log]
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Checks execute as a **DAG** (directed acyclic graph). Independent checks run in parallel for minimum latency. Dependent checks run sequentially.
|
|
179
|
+
|
|
180
|
+
## Integration with LambdaLLM
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
from lambdallm import handler, Model
|
|
184
|
+
from guardrailgraph import pipeline
|
|
185
|
+
from guardrailgraph.packs import hipaa
|
|
186
|
+
|
|
187
|
+
@handler(
|
|
188
|
+
model=Model.CLAUDE_3_SONNET,
|
|
189
|
+
guardrails=pipeline(packs=[hipaa.full()]),
|
|
190
|
+
)
|
|
191
|
+
def lambda_handler(event, context):
|
|
192
|
+
return context.invoke("Answer: {q}", q=event["body"]["question"])
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Comparison
|
|
196
|
+
|
|
197
|
+
| Feature | Bedrock Guardrails | NeMo | Guardrails AI | **GuardrailGraph** |
|
|
198
|
+
|---------|-------------------|------|---------------|-------------------|
|
|
199
|
+
| Provider agnostic | ❌ | ❌ | Partial | ✅ |
|
|
200
|
+
| Composable DAG | ❌ | ❌ | ❌ | ✅ |
|
|
201
|
+
| Industry packs | ❌ | ❌ | ❌ | ✅ |
|
|
202
|
+
| Serverless-native | Managed | ❌ | ❌ | ✅ |
|
|
203
|
+
| Custom checks | Limited | Complex | Yes | ✅ Simple |
|
|
204
|
+
| Open source | ❌ | ✅ | ✅ | ✅ MIT |
|
|
205
|
+
|
|
206
|
+
## License
|
|
207
|
+
|
|
208
|
+
MIT © [Gaurav Kumar Sinha](https://github.com/substrai)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "substrai-guardrailgraph"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Composable AI safety pipeline framework with industry compliance packs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Gaurav Kumar Sinha", email = "gaurav@substrai.dev" },
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"ai-safety",
|
|
17
|
+
"guardrails",
|
|
18
|
+
"llm",
|
|
19
|
+
"compliance",
|
|
20
|
+
"hipaa",
|
|
21
|
+
"serverless",
|
|
22
|
+
"aws-lambda",
|
|
23
|
+
"dag",
|
|
24
|
+
"pipeline",
|
|
25
|
+
]
|
|
26
|
+
classifiers = [
|
|
27
|
+
"Development Status :: 4 - Beta",
|
|
28
|
+
"Intended Audience :: Developers",
|
|
29
|
+
"License :: OSI Approved :: MIT License",
|
|
30
|
+
"Programming Language :: Python :: 3",
|
|
31
|
+
"Programming Language :: Python :: 3.9",
|
|
32
|
+
"Programming Language :: Python :: 3.10",
|
|
33
|
+
"Programming Language :: Python :: 3.11",
|
|
34
|
+
"Programming Language :: Python :: 3.12",
|
|
35
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
36
|
+
"Topic :: Security",
|
|
37
|
+
]
|
|
38
|
+
dependencies = [
|
|
39
|
+
"pyyaml>=6.0",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.optional-dependencies]
|
|
43
|
+
aws = ["boto3>=1.28.0"]
|
|
44
|
+
all = ["boto3>=1.28.0"]
|
|
45
|
+
dev = [
|
|
46
|
+
"pytest>=7.0",
|
|
47
|
+
"pytest-asyncio>=0.21",
|
|
48
|
+
"pytest-cov>=4.0",
|
|
49
|
+
"ruff>=0.1.0",
|
|
50
|
+
"mypy>=1.0",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[project.urls]
|
|
54
|
+
Homepage = "https://github.com/substrai/guardrailgraph"
|
|
55
|
+
Documentation = "https://substrai.github.io/guardrailgraph"
|
|
56
|
+
Repository = "https://github.com/substrai/guardrailgraph"
|
|
57
|
+
Issues = "https://github.com/substrai/guardrailgraph/issues"
|
|
58
|
+
|
|
59
|
+
[project.scripts]
|
|
60
|
+
guardrailgraph = "guardrailgraph.cli.main:main"
|
|
61
|
+
|
|
62
|
+
[tool.hatch.build.targets.wheel]
|
|
63
|
+
packages = ["src/guardrailgraph"]
|
|
64
|
+
|
|
65
|
+
[tool.pytest.ini_options]
|
|
66
|
+
testpaths = ["tests"]
|
|
67
|
+
asyncio_mode = "auto"
|
|
68
|
+
|
|
69
|
+
[tool.ruff]
|
|
70
|
+
target-version = "py39"
|
|
71
|
+
line-length = 100
|
|
72
|
+
|
|
73
|
+
[tool.mypy]
|
|
74
|
+
python_version = "3.9"
|
|
75
|
+
strict = true
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""GuardrailGraph — Composable AI safety pipeline framework."""
|
|
2
|
+
|
|
3
|
+
from guardrailgraph.core.actions import Action
|
|
4
|
+
from guardrailgraph.core.check import check, Check
|
|
5
|
+
from guardrailgraph.core.pipeline import pipeline, Pipeline
|
|
6
|
+
from guardrailgraph.core.result import CheckResult, PipelineResult
|
|
7
|
+
from guardrailgraph.core.context import CheckContext
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0"
|
|
10
|
+
__all__ = [
|
|
11
|
+
"Action",
|
|
12
|
+
"check",
|
|
13
|
+
"Check",
|
|
14
|
+
"pipeline",
|
|
15
|
+
"Pipeline",
|
|
16
|
+
"CheckResult",
|
|
17
|
+
"PipelineResult",
|
|
18
|
+
"CheckContext",
|
|
19
|
+
]
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Built-in guardrail checks — PII, toxicity, topic restriction, and more."""
|
|
2
|
+
|
|
3
|
+
from guardrailgraph.checks.pii import pii_check, PiiDetector
|
|
4
|
+
from guardrailgraph.checks.toxicity import toxicity_check, ToxicityScorer
|
|
5
|
+
from guardrailgraph.checks.topics import topic_check, TopicRestrictor
|
|
6
|
+
from guardrailgraph.checks.injection import injection_check, InjectionDetector
|
|
7
|
+
from guardrailgraph.checks.cost import cost_check, CostLimiter
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"pii_check",
|
|
11
|
+
"PiiDetector",
|
|
12
|
+
"toxicity_check",
|
|
13
|
+
"ToxicityScorer",
|
|
14
|
+
"topic_check",
|
|
15
|
+
"TopicRestrictor",
|
|
16
|
+
"injection_check",
|
|
17
|
+
"InjectionDetector",
|
|
18
|
+
"cost_check",
|
|
19
|
+
"CostLimiter",
|
|
20
|
+
]
|