Assert-style validation library for AI outputs - ensure your LLMs behave exactly as expected
Project description
Aisert 🚀
Assert-style validation library for AI outputs - ensure your LLMs behave exactly as expected.
Installation
# Basic installation
pip install aisert
# With semantic validation (sentence-transformers)
pip install aisert[sentence-transformers]
# With HuggingFace support
pip install aisert[huggingface]
# All optional features
pip install aisert[all]
Quick Start
from aisert import Aisert, AisertConfig
# Simple validation (no dependencies required)
result = (
Aisert("Paris is the capital of France.")
.assert_contains(["Paris", "France"])
.assert_not_contains(["spam", "inappropriate"])
.collect()
)
print(f"Validation passed: {result.status}")
# Advanced validation with token counting and semantic similarity
config = AisertConfig(
token_provider="openai",
token_model="gpt-4",
semantic_provider="sentence_transformers",
semantic_model="all-MiniLM-L6-v2"
)
result = (
Aisert("AI is transforming technology.", config)
.assert_tokens(max_tokens=50)
.assert_semantic_matches("artificial intelligence technology", threshold=0.7)
.collect()
)
Features
- 🔗 Fluent Interface: Chain multiple validations with readable API
- 📝 Multiple Validators: Schema, content, token count, semantic similarity
- ⚡ Optional Dependencies: Install only what you need
- 🎯 Flexible Modes: Strict (exceptions) or non-strict (collect errors)
- 🌐 Multi-Provider: OpenAI, Anthropic, HuggingFace, Google
- 🔧 Extensible: Custom validators via base classes
- 🚀 Production Ready: Thread-safe with model caching
Use Cases
- 🛡️ Content Moderation: Filter inappropriate content in real-time
- ✅ API Response Validation: Ensure LLM outputs meet quality standards
- 🧪 Testing AI Systems: Automated testing for AI applications
- 📊 Quality Monitoring: Track AI model performance in production
- 🔄 CI/CD Integration: Validate AI-generated content in pipelines
- 📈 A/B Testing: Compare different AI model outputs
Validation Types
# Content validation (no dependencies)
Aisert(content).assert_contains(["required", "keywords"])
Aisert(content).assert_not_contains(["spam", "inappropriate"])
# Schema validation (Pydantic models)
Aisert(json_content).assert_schema(UserModel)
# Token counting (requires API keys)
Aisert(content, config).assert_tokens(max_tokens=100)
# Semantic similarity (requires sentence-transformers)
Aisert(content).assert_semantic_matches("expected meaning", threshold=0.8)
Configuration
# Simple configuration
config = AisertConfig(
token_provider="openai",
token_model="gpt-4",
semantic_provider="sentence_transformers",
semantic_model="all-MiniLM-L6-v2"
)
# Set API keys (for token counting)
export OPENAI_API_KEY="your-key"
export ANTHROPIC_API_KEY="your-key"
Error Handling
# Strict mode (default) - raises exceptions
try:
Aisert(content).assert_contains(["required"])
except AisertError as e:
print(f"Validation failed: {e}")
# Non-strict mode - collects all errors
result = (
Aisert(content)
.assert_contains(["term1"], strict=False)
.assert_tokens(100, strict=False)
.collect()
)
if not result.status:
print("Some validations failed:", result.rules)
Documentation
- 📚 Examples - Configuration, usage patterns, production use cases
- 📖 API Reference - Complete API documentation
- 🔧 Custom Validators - Extend with your own validators
Requirements
- Python: >= 3.9
- Dependencies: Optional based on features used
- API Keys: Only for token counting (OpenAI, Anthropic, etc.)
- Memory: 100-500MB for semantic models (optional)
License
MIT License
Links
- GitHub - Source code and issues
- Documentation - Complete documentation
- PyPI - Package repository
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
aisert-0.2.0.tar.gz
(30.6 kB
view details)
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
aisert-0.2.0-py3-none-any.whl
(32.2 kB
view details)
File details
Details for the file aisert-0.2.0.tar.gz.
File metadata
- Download URL: aisert-0.2.0.tar.gz
- Upload date:
- Size: 30.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
040d0371d876c76c2138e92b7809c1de325ee105eac16712d08b5cdadc507ae4
|
|
| MD5 |
8f4e9f2dece0f10d0d6b1350beb99e18
|
|
| BLAKE2b-256 |
95e2e9857826b83d9da00d91f22dd3d07b6b0ab6412f9d47bdeb9fdb1ffad581
|
File details
Details for the file aisert-0.2.0-py3-none-any.whl.
File metadata
- Download URL: aisert-0.2.0-py3-none-any.whl
- Upload date:
- Size: 32.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1038ce44a696aac1ca7e2e523fcea64244fcc1f6cfe406be471c3e8665725d83
|
|
| MD5 |
6d03f52417c15765632d606f1412168a
|
|
| BLAKE2b-256 |
a2a67687e19318bf9c60f1caf11de4e97e1dc86bcae19db241e85dddaf5f163e
|