SDK for logging AI/ML decisions with compliance tracking
Project description
AI Audit Trail SDK - Python
Drop-in SDK for logging AI/ML decisions with compliance tracking. Get GDPR and EU AI Act compliant audit trails in under 15 minutes.
Installation
pip install ai-audit-sdk
Quick Start
from ai_audit_sdk import AuditLogger
import openai
```python
# Initialize the logger with your API key
logger = AuditLogger(api_key="your_api_key")
For development and testing, use your sandbox API key:
# Development/Testing with sandbox tenant
logger = AuditLogger(api_key="your_sandbox_api_key")
Your existing OpenAI code
client = openai.OpenAI() response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Hello, world!"}] )
Log the decision for compliance
logger.log_decision( input_text="Hello, world!", output_text=response.choices[0].message.content, model_name="gpt-4", metadata={ "user_id": "user123", "session_id": "session456" } )
## Features
- **Asynchronous Logging**: Won't slow down your application
- **Compliance Ready**: GDPR Article 22 and EU AI Act support
- **Multi-Model Support**: Works with any AI/ML model
- **Secure**: API key authentication with encrypted transmission
- **Reliable**: Built-in error handling and timeout management
## API Reference
### AuditLogger
Main class for logging AI decisions.
```python
logger = AuditLogger(api_key="your_key")
Production:
logger = AuditLogger(api_key="prod_api_key_xxx")
Development/Testing:
logger = AuditLogger(api_key="sandbox_api_key_xxx")
Note: All traffic goes through the production infrastructure at
https://explainableai.azurewebsites.net. Tenant isolation is handled via API keys, not URLs.
Methods
log_decision(input_text, output_text, ...)
Log an AI decision asynchronously (recommended).
logger.log_decision(
input_text="The input prompt",
output_text="The AI response",
model_name="gpt-4", # optional, defaults to 'unknown'
metadata={ # optional
"user_id": "user123",
"session_id": "session456",
"tags": ["production", "chat"]
},
confidence=0.95, # optional, 0.0 to 1.0
response_time=1200, # optional, in milliseconds
provider="openai", # optional
model_version="2024-02-01", # optional
risk_level="low", # optional: 'low', 'medium', 'high'
prompt_tokens=100, # optional
completion_tokens=50, # optional
total_tokens=150, # optional
cost_micros=1000, # optional, cost in millionths of currency unit
external_ref="req_123", # optional, your internal reference
data_subject_id="user_123", # optional, for GDPR compliance
lawful_basis="consent", # optional, GDPR lawful basis
automated_decision=True, # optional, GDPR Article 22
redact_pii=False, # optional, redact PII from stored data
priority="normal" # optional: 'low', 'normal', 'high'
)
Log an AI decision asynchronously (recommended).
Parameters:
input_text(str): The input prompt or dataoutput_text(str): The AI model's outputmodel_name(str): Name of the AI model usedmetadata(dict): Additional context (user_id, session_id, etc.)confidence(float): Model confidence score (0.0 to 1.0)response_time(int): Response time in milliseconds
log_decision_sync(...)
Same as log_decision() but blocks until complete. Returns True if successful.
Simple Function
For one-off logging:
from ai_audit_sdk import log_ai_decision
log_ai_decision(
api_key="your_key",
input_text="prompt",
output_text="response",
model_name="gpt-4"
)
Configuration
Set your API key as an environment variable:
export AI_AUDIT_API_KEY="your_api_key"
Then use it in your code:
import os
from ai_audit_sdk import AuditLogger
logger = AuditLogger(api_key=os.getenv("AI_AUDIT_API_KEY"))
Error Handling
The SDK uses fire-and-forget async logging by default. Errors are logged to stdout but won't crash your application.
For critical applications, use synchronous logging:
success = logger.log_decision_sync(input_text, output_text)
if not success:
# Handle logging failure
print("Failed to log decision")
Examples
OpenAI Integration
import openai
from ai_audit_sdk import AuditLogger
logger = AuditLogger(api_key="your_audit_key")
client = openai.OpenAI(api_key="your_openai_key")
def get_ai_response(prompt, user_id):
start_time = time.time()
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
response_time = int((time.time() - start_time) * 1000)
# Log for compliance
logger.log_decision(
input_text=prompt,
output_text=response.choices[0].message.content,
model_name="gpt-4",
metadata={"user_id": user_id},
response_time=response_time
)
return response.choices[0].message.content
Context Manager
with AuditLogger(api_key="your_key") as logger:
logger.log_decision("input", "output", "model")
# Logger automatically closes when exiting the context
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ai_audit_sdk-1.0.1.tar.gz.
File metadata
- Download URL: ai_audit_sdk-1.0.1.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68003a76153a9364ec99c8024b28015e40a726e88e95d1bd3d8b41d9a34fe8a8
|
|
| MD5 |
7564eca6f80bcf41ab07708abbb50b5f
|
|
| BLAKE2b-256 |
92b0d701606eba92a35378f97cae84f8e8716f46bf7397c8a97fd79ac4cf282a
|
File details
Details for the file ai_audit_sdk-1.0.1-py3-none-any.whl.
File metadata
- Download URL: ai_audit_sdk-1.0.1-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2928cfe4cc8f8698358427b21aeb724c8318dfcea77ce2e49e2a3a5b03ac01cb
|
|
| MD5 |
ab06275be1f60d4f1a01bc499bfe6067
|
|
| BLAKE2b-256 |
8fa5ab921e7da2848e5796ed4d507c5c8a650011c5da8832da6a2237ccc7f5b5
|