maigp-light 1.0.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.
- maigp_light-1.0.0/.gitignore +46 -0
- maigp_light-1.0.0/LICENSE.md +74 -0
- maigp_light-1.0.0/PKG-INFO +167 -0
- maigp_light-1.0.0/README.md +55 -0
- maigp_light-1.0.0/aigp_light/__init__.py +43 -0
- maigp_light-1.0.0/aigp_light/adapters/__init__.py +1 -0
- maigp_light-1.0.0/aigp_light/adapters/openai.py +56 -0
- maigp_light-1.0.0/aigp_light/config.py +118 -0
- maigp_light-1.0.0/aigp_light/core.py +367 -0
- maigp_light-1.0.0/aigp_light/policy.py +242 -0
- maigp_light-1.0.0/aigp_light/responses.py +143 -0
- maigp_light-1.0.0/aigp_light/signals.py +160 -0
- maigp_light-1.0.0/aigp_light/trace.py +164 -0
- maigp_light-1.0.0/pyproject.toml +58 -0
- maigp_light-1.0.0/tests/test_core.py +295 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
.venv/
|
|
8
|
+
.eggs/
|
|
9
|
+
|
|
10
|
+
# Node
|
|
11
|
+
node_modules/
|
|
12
|
+
dist/
|
|
13
|
+
|
|
14
|
+
# Go
|
|
15
|
+
bin/
|
|
16
|
+
|
|
17
|
+
# Rust
|
|
18
|
+
target/
|
|
19
|
+
|
|
20
|
+
# .NET
|
|
21
|
+
bin/
|
|
22
|
+
obj/
|
|
23
|
+
|
|
24
|
+
# Java
|
|
25
|
+
*.class
|
|
26
|
+
out/
|
|
27
|
+
|
|
28
|
+
# IDE
|
|
29
|
+
.idea/
|
|
30
|
+
.vscode/
|
|
31
|
+
.kiro/
|
|
32
|
+
*.swp
|
|
33
|
+
|
|
34
|
+
# OS
|
|
35
|
+
.DS_Store
|
|
36
|
+
Thumbs.db
|
|
37
|
+
|
|
38
|
+
# Secrets (never commit)
|
|
39
|
+
.env
|
|
40
|
+
*.pem
|
|
41
|
+
*.key
|
|
42
|
+
*.pdf
|
|
43
|
+
BACKLOG/
|
|
44
|
+
patent/
|
|
45
|
+
docs/.archive/
|
|
46
|
+
review/
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# AIGP-Light Source Available License
|
|
2
|
+
|
|
3
|
+
Version 1.0, July 2026
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2024-2026 Causum. All rights reserved.
|
|
6
|
+
|
|
7
|
+
## Grant of Rights
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person or organization
|
|
10
|
+
obtaining a copy of this software and associated documentation files (the
|
|
11
|
+
"Software"), to:
|
|
12
|
+
|
|
13
|
+
1. **Use** the Software for any purpose, including commercial applications,
|
|
14
|
+
without restriction.
|
|
15
|
+
|
|
16
|
+
2. **Copy and distribute** the Software in unmodified form, provided this
|
|
17
|
+
license notice is included in all copies.
|
|
18
|
+
|
|
19
|
+
3. **Integrate** the Software into applications, products, and services
|
|
20
|
+
without royalty or fee.
|
|
21
|
+
|
|
22
|
+
## Restrictions
|
|
23
|
+
|
|
24
|
+
The following actions require prior written permission from Causum
|
|
25
|
+
(licensing@causum.com):
|
|
26
|
+
|
|
27
|
+
1. **Modification** — You may not modify, alter, adapt, or create derivative
|
|
28
|
+
works based on the Software without explicit written permission from Causum.
|
|
29
|
+
|
|
30
|
+
2. **Forking for distribution** — You may not distribute modified versions of
|
|
31
|
+
the Software, whether under this name or any other name.
|
|
32
|
+
|
|
33
|
+
3. **Relicensing** — You may not sublicense or apply a different license to
|
|
34
|
+
the Software or any portion thereof.
|
|
35
|
+
|
|
36
|
+
## What This Means in Practice
|
|
37
|
+
|
|
38
|
+
- ✅ Use in production — freely
|
|
39
|
+
- ✅ Use commercially — freely
|
|
40
|
+
- ✅ Bundle in your application — freely
|
|
41
|
+
- ✅ Read and learn from the source — freely
|
|
42
|
+
- ✅ Report issues and suggest changes — welcome
|
|
43
|
+
- ✅ Contribute changes back (via PR) — welcome (subject to CLA)
|
|
44
|
+
- ❌ Modify and distribute your own version — requires permission
|
|
45
|
+
- ❌ Fork and publish under a different name — requires permission
|
|
46
|
+
- ❌ Remove or change this license — prohibited
|
|
47
|
+
|
|
48
|
+
## Contribution
|
|
49
|
+
|
|
50
|
+
Contributions submitted via pull request are welcome. By submitting a
|
|
51
|
+
contribution, you agree to assign copyright of your contribution to Causum
|
|
52
|
+
and agree that your contribution will be distributed under this same license.
|
|
53
|
+
|
|
54
|
+
## No Warranty
|
|
55
|
+
|
|
56
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
57
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
58
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
59
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
60
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
61
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
62
|
+
SOFTWARE.
|
|
63
|
+
|
|
64
|
+
## Contact
|
|
65
|
+
|
|
66
|
+
For modification permissions, commercial licensing inquiries, or questions:
|
|
67
|
+
- Email: licensing@causum.com
|
|
68
|
+
- Repository: https://github.com/owner-spec/aigp-protocol
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
AIGP-Light is part of the AIGP protocol family.
|
|
73
|
+
AIGP is registered with the U.S. Copyright Office (Case #1-15160968741, #1-15180695311).
|
|
74
|
+
Patent-pending.
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: maigp-light
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: AIGP-Light: Minimal detect & respond governance for AI model calls — zero dependencies, RFC-010 compatible traces
|
|
5
|
+
Project-URL: Homepage, https://github.com/owner-spec/aigp-protocol
|
|
6
|
+
Project-URL: Documentation, https://github.com/owner-spec/aigp-protocol/blob/main/sdks/AIGP-Light-Getting-Started.md
|
|
7
|
+
Project-URL: Repository, https://github.com/owner-spec/aigp-protocol
|
|
8
|
+
Project-URL: Issues, https://github.com/owner-spec/aigp-protocol/issues
|
|
9
|
+
Author-email: Causum <engineering@causum.com>
|
|
10
|
+
License: # AIGP-Light Source Available License
|
|
11
|
+
|
|
12
|
+
Version 1.0, July 2026
|
|
13
|
+
|
|
14
|
+
Copyright (c) 2024-2026 Causum. All rights reserved.
|
|
15
|
+
|
|
16
|
+
## Grant of Rights
|
|
17
|
+
|
|
18
|
+
Permission is hereby granted, free of charge, to any person or organization
|
|
19
|
+
obtaining a copy of this software and associated documentation files (the
|
|
20
|
+
"Software"), to:
|
|
21
|
+
|
|
22
|
+
1. **Use** the Software for any purpose, including commercial applications,
|
|
23
|
+
without restriction.
|
|
24
|
+
|
|
25
|
+
2. **Copy and distribute** the Software in unmodified form, provided this
|
|
26
|
+
license notice is included in all copies.
|
|
27
|
+
|
|
28
|
+
3. **Integrate** the Software into applications, products, and services
|
|
29
|
+
without royalty or fee.
|
|
30
|
+
|
|
31
|
+
## Restrictions
|
|
32
|
+
|
|
33
|
+
The following actions require prior written permission from Causum
|
|
34
|
+
(licensing@causum.com):
|
|
35
|
+
|
|
36
|
+
1. **Modification** — You may not modify, alter, adapt, or create derivative
|
|
37
|
+
works based on the Software without explicit written permission from Causum.
|
|
38
|
+
|
|
39
|
+
2. **Forking for distribution** — You may not distribute modified versions of
|
|
40
|
+
the Software, whether under this name or any other name.
|
|
41
|
+
|
|
42
|
+
3. **Relicensing** — You may not sublicense or apply a different license to
|
|
43
|
+
the Software or any portion thereof.
|
|
44
|
+
|
|
45
|
+
## What This Means in Practice
|
|
46
|
+
|
|
47
|
+
- ✅ Use in production — freely
|
|
48
|
+
- ✅ Use commercially — freely
|
|
49
|
+
- ✅ Bundle in your application — freely
|
|
50
|
+
- ✅ Read and learn from the source — freely
|
|
51
|
+
- ✅ Report issues and suggest changes — welcome
|
|
52
|
+
- ✅ Contribute changes back (via PR) — welcome (subject to CLA)
|
|
53
|
+
- ❌ Modify and distribute your own version — requires permission
|
|
54
|
+
- ❌ Fork and publish under a different name — requires permission
|
|
55
|
+
- ❌ Remove or change this license — prohibited
|
|
56
|
+
|
|
57
|
+
## Contribution
|
|
58
|
+
|
|
59
|
+
Contributions submitted via pull request are welcome. By submitting a
|
|
60
|
+
contribution, you agree to assign copyright of your contribution to Causum
|
|
61
|
+
and agree that your contribution will be distributed under this same license.
|
|
62
|
+
|
|
63
|
+
## No Warranty
|
|
64
|
+
|
|
65
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
66
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
67
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
68
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
69
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
70
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
71
|
+
SOFTWARE.
|
|
72
|
+
|
|
73
|
+
## Contact
|
|
74
|
+
|
|
75
|
+
For modification permissions, commercial licensing inquiries, or questions:
|
|
76
|
+
- Email: licensing@causum.com
|
|
77
|
+
- Repository: https://github.com/owner-spec/aigp-protocol
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
AIGP-Light is part of the AIGP protocol family.
|
|
82
|
+
AIGP is registered with the U.S. Copyright Office (Case #1-15160968741, #1-15180695311).
|
|
83
|
+
Patent-pending.
|
|
84
|
+
License-File: LICENSE.md
|
|
85
|
+
Keywords: ai,aigp,circuit-breaker,detect,governance,llm,maigp,model-safety,respond
|
|
86
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
87
|
+
Classifier: Intended Audience :: Developers
|
|
88
|
+
Classifier: Programming Language :: Python :: 3
|
|
89
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
90
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
91
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
92
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
93
|
+
Classifier: Topic :: Security
|
|
94
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
95
|
+
Requires-Python: >=3.10
|
|
96
|
+
Provides-Extra: anthropic
|
|
97
|
+
Requires-Dist: anthropic>=0.20; extra == 'anthropic'
|
|
98
|
+
Provides-Extra: dev
|
|
99
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
100
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
101
|
+
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
|
|
102
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
103
|
+
Requires-Dist: pyyaml>=6.0; extra == 'dev'
|
|
104
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
105
|
+
Provides-Extra: langchain
|
|
106
|
+
Requires-Dist: langchain-core>=0.2; extra == 'langchain'
|
|
107
|
+
Provides-Extra: openai
|
|
108
|
+
Requires-Dist: openai>=1.0; extra == 'openai'
|
|
109
|
+
Provides-Extra: yaml
|
|
110
|
+
Requires-Dist: pyyaml>=6.0; extra == 'yaml'
|
|
111
|
+
Description-Content-Type: text/markdown
|
|
112
|
+
|
|
113
|
+
# maigp-light
|
|
114
|
+
|
|
115
|
+
**Minimal detect & respond governance for AI model calls.**
|
|
116
|
+
|
|
117
|
+
Zero dependencies. Five minutes to integrate. Forward-compatible with the full AIGP/MAIGP protocol.
|
|
118
|
+
|
|
119
|
+
## Install
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
pip install maigp-light
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Quick Start
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from aigp_light import AigpLight
|
|
129
|
+
|
|
130
|
+
gov = AigpLight.from_yaml("aigp-light.yaml")
|
|
131
|
+
|
|
132
|
+
# Wrap any model call
|
|
133
|
+
response = gov.call(
|
|
134
|
+
client.chat.completions.create,
|
|
135
|
+
model="gpt-4o",
|
|
136
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
137
|
+
)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## What It Does
|
|
141
|
+
|
|
142
|
+
- **Detects** behavioral deviation: latency spikes, error rates, refusal patterns, token explosions, cost overruns
|
|
143
|
+
- **Responds** automatically: LOG, ALERT, RATE_LIMIT, FALLBACK, BLOCK, CIRCUIT_BREAK
|
|
144
|
+
- **Traces** every call in RFC-010 §5.3 format (forward-compatible with full AIGP)
|
|
145
|
+
|
|
146
|
+
## What It Doesn't Need
|
|
147
|
+
|
|
148
|
+
No external services. No governance server. No delegation tokens. No consent providers. No network calls. Everything runs locally from a YAML policy file.
|
|
149
|
+
|
|
150
|
+
## Documentation
|
|
151
|
+
|
|
152
|
+
- [Getting Started Guide](https://github.com/owner-spec/aigp-protocol/blob/main/sdks/AIGP-Light-Getting-Started.md)
|
|
153
|
+
- [Design RFC](https://github.com/owner-spec/aigp-protocol/blob/main/specification/papers/)
|
|
154
|
+
|
|
155
|
+
## Upgrade Path
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
maigp-light → maigp-agent-core → maigp-client (full protocol)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Your traces carry forward. Your trust history accumulates from day one.
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
Source Available — free to use, no modification without permission. See [LICENSE.md](https://github.com/owner-spec/aigp-protocol/blob/main/sdks/python/aigp-light/LICENSE.md).
|
|
166
|
+
|
|
167
|
+
© 2024-2026 Causum. All rights reserved.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# maigp-light
|
|
2
|
+
|
|
3
|
+
**Minimal detect & respond governance for AI model calls.**
|
|
4
|
+
|
|
5
|
+
Zero dependencies. Five minutes to integrate. Forward-compatible with the full AIGP/MAIGP protocol.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install maigp-light
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from aigp_light import AigpLight
|
|
17
|
+
|
|
18
|
+
gov = AigpLight.from_yaml("aigp-light.yaml")
|
|
19
|
+
|
|
20
|
+
# Wrap any model call
|
|
21
|
+
response = gov.call(
|
|
22
|
+
client.chat.completions.create,
|
|
23
|
+
model="gpt-4o",
|
|
24
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
25
|
+
)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## What It Does
|
|
29
|
+
|
|
30
|
+
- **Detects** behavioral deviation: latency spikes, error rates, refusal patterns, token explosions, cost overruns
|
|
31
|
+
- **Responds** automatically: LOG, ALERT, RATE_LIMIT, FALLBACK, BLOCK, CIRCUIT_BREAK
|
|
32
|
+
- **Traces** every call in RFC-010 §5.3 format (forward-compatible with full AIGP)
|
|
33
|
+
|
|
34
|
+
## What It Doesn't Need
|
|
35
|
+
|
|
36
|
+
No external services. No governance server. No delegation tokens. No consent providers. No network calls. Everything runs locally from a YAML policy file.
|
|
37
|
+
|
|
38
|
+
## Documentation
|
|
39
|
+
|
|
40
|
+
- [Getting Started Guide](https://github.com/owner-spec/aigp-protocol/blob/main/sdks/AIGP-Light-Getting-Started.md)
|
|
41
|
+
- [Design RFC](https://github.com/owner-spec/aigp-protocol/blob/main/specification/papers/)
|
|
42
|
+
|
|
43
|
+
## Upgrade Path
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
maigp-light → maigp-agent-core → maigp-client (full protocol)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Your traces carry forward. Your trust history accumulates from day one.
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
Source Available — free to use, no modification without permission. See [LICENSE.md](https://github.com/owner-spec/aigp-protocol/blob/main/sdks/python/aigp-light/LICENSE.md).
|
|
54
|
+
|
|
55
|
+
© 2024-2026 Causum. All rights reserved.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""AIGP-Light: Minimal detect & respond governance for AI model calls.
|
|
2
|
+
|
|
3
|
+
Zero-dependency, local-first governance that produces RFC-010 §5.3 compatible traces.
|
|
4
|
+
|
|
5
|
+
Usage:
|
|
6
|
+
from aigp_light import AigpLight
|
|
7
|
+
|
|
8
|
+
gov = AigpLight.from_yaml("aigp-light.yaml")
|
|
9
|
+
response = gov.call(client.chat.completions.create,
|
|
10
|
+
model="gpt-4o", messages=messages)
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
__version__ = "1.0.0"
|
|
14
|
+
|
|
15
|
+
from .config import PolicyConfigError
|
|
16
|
+
from .core import AigpLight
|
|
17
|
+
from .policy import Breach, PolicyEngine, PolicyResult, ResponseAction
|
|
18
|
+
from .responses import (
|
|
19
|
+
AigpLightError,
|
|
20
|
+
CircuitOpenError,
|
|
21
|
+
GovernanceBlockedError,
|
|
22
|
+
RateLimitedError,
|
|
23
|
+
)
|
|
24
|
+
from .signals import CallObservation, SignalSnapshot, SignalStore
|
|
25
|
+
from .trace import TraceEmitter, TraceSpan
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"AigpLight",
|
|
29
|
+
"AigpLightError",
|
|
30
|
+
"Breach",
|
|
31
|
+
"CallObservation",
|
|
32
|
+
"CircuitOpenError",
|
|
33
|
+
"GovernanceBlockedError",
|
|
34
|
+
"PolicyConfigError",
|
|
35
|
+
"PolicyEngine",
|
|
36
|
+
"PolicyResult",
|
|
37
|
+
"RateLimitedError",
|
|
38
|
+
"ResponseAction",
|
|
39
|
+
"SignalSnapshot",
|
|
40
|
+
"SignalStore",
|
|
41
|
+
"TraceEmitter",
|
|
42
|
+
"TraceSpan",
|
|
43
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""AIGP-Light framework adapters."""
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""AIGP-Light OpenAI adapter — governed wrapper for OpenAI API calls."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from ..core import AigpLight
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class LightOpenAI:
|
|
11
|
+
"""AIGP-Light governed OpenAI client.
|
|
12
|
+
|
|
13
|
+
Drop-in wrapper providing local detect & respond governance.
|
|
14
|
+
|
|
15
|
+
Usage:
|
|
16
|
+
from openai import OpenAI
|
|
17
|
+
from aigp_light.adapters.openai import LightOpenAI
|
|
18
|
+
|
|
19
|
+
governed = LightOpenAI(client=OpenAI(), policy="aigp-light.yaml")
|
|
20
|
+
response = governed.chat("gpt-4o", messages)
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, client: Any, policy: str | dict):
|
|
24
|
+
self._client = client
|
|
25
|
+
if isinstance(policy, str):
|
|
26
|
+
self._gov = AigpLight.from_yaml(policy)
|
|
27
|
+
else:
|
|
28
|
+
self._gov = AigpLight.from_dict(policy)
|
|
29
|
+
|
|
30
|
+
def chat(self, model: str, messages: list, **kwargs) -> Any:
|
|
31
|
+
"""Governed chat completion."""
|
|
32
|
+
return self._gov.call(
|
|
33
|
+
self._client.chat.completions.create,
|
|
34
|
+
model=model, messages=messages, **kwargs
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
async def achat(self, model: str, messages: list, **kwargs) -> Any:
|
|
38
|
+
"""Async governed chat completion."""
|
|
39
|
+
return await self._gov.acall(
|
|
40
|
+
self._client.chat.completions.create,
|
|
41
|
+
model=model, messages=messages, **kwargs
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def metrics(self) -> dict:
|
|
46
|
+
"""Current governance metrics."""
|
|
47
|
+
return self._gov.metrics
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def traces(self) -> list[dict]:
|
|
51
|
+
"""Emitted traces."""
|
|
52
|
+
return self._gov.traces
|
|
53
|
+
|
|
54
|
+
def flush(self) -> None:
|
|
55
|
+
"""Flush trace buffer."""
|
|
56
|
+
self._gov.flush()
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Configuration loader for AIGP-Light policy files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PolicyConfigError(Exception):
|
|
10
|
+
"""Invalid policy configuration."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, path: str, details: str):
|
|
13
|
+
self.path = path
|
|
14
|
+
self.details = details
|
|
15
|
+
super().__init__(f"Invalid policy config at '{path}': {details}")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def load_config(path: str) -> dict:
|
|
19
|
+
"""Load policy configuration from YAML or JSON file.
|
|
20
|
+
|
|
21
|
+
Supports:
|
|
22
|
+
- .yaml / .yml files (requires pyyaml)
|
|
23
|
+
- .json files (stdlib)
|
|
24
|
+
|
|
25
|
+
Raises PolicyConfigError on invalid config.
|
|
26
|
+
"""
|
|
27
|
+
if not os.path.isfile(path):
|
|
28
|
+
raise PolicyConfigError(path, "File not found")
|
|
29
|
+
|
|
30
|
+
ext = os.path.splitext(path)[1].lower()
|
|
31
|
+
|
|
32
|
+
try:
|
|
33
|
+
if ext in (".yaml", ".yml"):
|
|
34
|
+
return _load_yaml(path)
|
|
35
|
+
elif ext == ".json":
|
|
36
|
+
return _load_json(path)
|
|
37
|
+
else:
|
|
38
|
+
# Try YAML first, fall back to JSON
|
|
39
|
+
try:
|
|
40
|
+
return _load_yaml(path)
|
|
41
|
+
except (ImportError, Exception):
|
|
42
|
+
return _load_json(path)
|
|
43
|
+
except PolicyConfigError:
|
|
44
|
+
raise
|
|
45
|
+
except Exception as e:
|
|
46
|
+
raise PolicyConfigError(path, str(e))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def validate_config(config: dict) -> dict:
|
|
50
|
+
"""Validate policy configuration and apply defaults.
|
|
51
|
+
|
|
52
|
+
Returns the config with defaults applied.
|
|
53
|
+
Raises PolicyConfigError on invalid config.
|
|
54
|
+
"""
|
|
55
|
+
if not isinstance(config, dict):
|
|
56
|
+
raise PolicyConfigError("<dict>", "Config must be a dictionary")
|
|
57
|
+
|
|
58
|
+
# Apply defaults
|
|
59
|
+
config.setdefault("version", "1.0")
|
|
60
|
+
config.setdefault("app_id", "")
|
|
61
|
+
config.setdefault("model_id", "")
|
|
62
|
+
config.setdefault("detection", {})
|
|
63
|
+
config.setdefault("response", {})
|
|
64
|
+
config.setdefault("trace", {})
|
|
65
|
+
|
|
66
|
+
# Trace defaults
|
|
67
|
+
trace = config["trace"]
|
|
68
|
+
trace.setdefault("output", "file")
|
|
69
|
+
trace.setdefault("path", "./aigp-traces/")
|
|
70
|
+
trace.setdefault("format", "jsonl")
|
|
71
|
+
trace.setdefault("buffer_size", 10)
|
|
72
|
+
trace.setdefault("include_detection", True)
|
|
73
|
+
|
|
74
|
+
# Detection defaults
|
|
75
|
+
det = config["detection"]
|
|
76
|
+
if "latency" in det:
|
|
77
|
+
det["latency"].setdefault("window_seconds", 60)
|
|
78
|
+
if "refusal_rate" in det:
|
|
79
|
+
det["refusal_rate"].setdefault("window_seconds", 300)
|
|
80
|
+
det["refusal_rate"].setdefault("patterns", [
|
|
81
|
+
"I cannot", "I'm unable", "I apologize, but", "As an AI"
|
|
82
|
+
])
|
|
83
|
+
if "error_rate" in det:
|
|
84
|
+
det["error_rate"].setdefault("window_seconds", 60)
|
|
85
|
+
if "token_explosion" in det:
|
|
86
|
+
det["token_explosion"].setdefault("baseline_window_seconds", 3600)
|
|
87
|
+
if "cost" in det:
|
|
88
|
+
det["cost"].setdefault("pricing", {})
|
|
89
|
+
det["cost"]["pricing"].setdefault("input_per_million", 2.50)
|
|
90
|
+
det["cost"]["pricing"].setdefault("output_per_million", 10.00)
|
|
91
|
+
|
|
92
|
+
return config
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _load_yaml(path: str) -> dict:
|
|
96
|
+
"""Load YAML file."""
|
|
97
|
+
try:
|
|
98
|
+
import yaml
|
|
99
|
+
except ImportError:
|
|
100
|
+
raise PolicyConfigError(
|
|
101
|
+
path,
|
|
102
|
+
"pyyaml not installed. Install with: pip install aigp-light[yaml] "
|
|
103
|
+
"or use JSON config instead."
|
|
104
|
+
)
|
|
105
|
+
with open(path, "r") as f:
|
|
106
|
+
data = yaml.safe_load(f)
|
|
107
|
+
if not isinstance(data, dict):
|
|
108
|
+
raise PolicyConfigError(path, "YAML root must be a mapping")
|
|
109
|
+
return data
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _load_json(path: str) -> dict:
|
|
113
|
+
"""Load JSON file."""
|
|
114
|
+
with open(path, "r") as f:
|
|
115
|
+
data = json.load(f)
|
|
116
|
+
if not isinstance(data, dict):
|
|
117
|
+
raise PolicyConfigError(path, "JSON root must be an object")
|
|
118
|
+
return data
|