Duration: 60 minutes
Difficulty: Intermediate
By the end of this module, you will:
- Install and configure QWED
- Run your first verification
- Build a Production Verification
- Handle errors gracefully
- Optimize for performance
How verification works in practice:
graph TB
A[User Request] --> B[LLM Response]
B --> C[QWED Client]
C --> D{Domain?}
D -->|Math| E[client.verify_math<br/>→ SymPy Engine]
D -->|Logic| F[client.verify_logic<br/>→ Z3 Solver]
D -->|Code| G[client.verify_code<br/>→ AST Parser]
E --> H{Result}
F --> H
G --> H
H -->|✅ Verified| I[Return to User<br/>With Proof]
H -->|❌ Error| J[Log & Alert<br/>Fallback Value]
style C fill:#2196f3
style E fill:#4caf50
style F fill:#9c27b0
style G fill:#f44336
style I fill:#4caf50
style J fill:#ff9800
This is what you'll build in this module!
Required:
- Python 3.10+
- pip or conda
Optional (for free local LLMs):
- Ollama - for $0 cost verification
# Basic installation
pip install qwed
# With PII masking support
pip install 'qwed[pii]'
# With LangChain integration
pip install 'qwed[langchain]'
# Everything
pip install 'qwed[all]'QWED supports multiple LLM providers:
# Install Ollama
# Download from: https://ollama.ai
# Pull a model
ollama pull llama3
# Start Ollama server (runs on localhost:11434)
ollama servefrom qwed_sdk import QWEDLocal
client = QWEDLocal(
base_url="http://localhost:11434/v1",
model="llama3"
)import os
os.environ["OPENAI_API_KEY"] = "sk-..."
from qwed_sdk import QWEDLocal
client = QWEDLocal(
provider="openai",
model="gpt-4o-mini" # Cheapest, fastest
)import os
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."
from qwed_sdk import QWEDLocal
client = QWEDLocal(
provider="anthropic",
model="claude-3-haiku-20240307"
)Notebook: 01-first-verification.ipynb
Time: 10 minutes
from qwed_sdk import QWEDLocal
# Initialize client
client = QWEDLocal(
base_url="http://localhost:11434/v1", # Free Ollama
model="llama3"
)
# Verify a math problem
result = client.verify_math("What is the derivative of x^2?")
print(f"Verified: {result.verified}") # True
print(f"Answer: {result.value}") # 2*x
print(f"Confidence: {result.confidence}") # 100%
print(f"Method: {result.evidence['method']}") # symbolicclass VerificationResult:
verified: bool # True if proven correct
value: any # The verified answer
confidence: float # 0-100% (100% for symbolic proofs)
evidence: dict # How it was verified
error: str | None # Error message if failed# Calculus
result = client.verify_math("Integrate x^2 from 0 to 1")
print(result.value) # 1/3
# Algebra
result = client.verify_math("Solve x^2 - 4 = 0")
print(result.value) # [-2, 2]
# Finance
result = client.verify_math("Compound interest: $10,000 at 5% for 10 years")
print(result.value) # $16,288.95Notebook: 02-production-patterns.ipynb
Time: 20 minutes
# Dangerous: Trusting LLM directly
def calculate_loan_payment(principal, rate, years):
prompt = f"Calculate monthly payment for ${principal} loan at {rate}% for {years} years"
return call_llm(prompt) # ❌ NO VERIFICATIONfrom qwed_sdk import QWEDLocal
client = QWEDLocal(provider="openai", model="gpt-4o-mini")
def verified_loan_payment(principal: float, rate: float, years: int) -> float:
"""
Calculate loan payment with QWED verification.
Returns verified result or raises error.
"""
query = f"""
Calculate monthly payment for a ${principal:,.2f} loan
at {rate}% annual interest over {years} years.
Use the standard loan payment formula.
"""
result = client.verify_math(query)
if result.verified:
return result.value
else:
raise ValueError(f"Verification failed: {result.error}")
# Usage
try:
payment = verified_loan_payment(100000, 5.0, 30)
print(f"Monthly payment: ${payment:,.2f}")
except ValueError as e:
print(f"Error: {e}")
# Handle error (log, fallback, human escalation)See: examples/financial_calculator.py
class FinancialCalculator:
def __init__(self):
self.client = QWEDLocal(provider="openai")
def calculate_with_verification(self, query: str):
"""All calculations go through verification."""
result = self.client.verify_math(query)
if not result.verified:
# Log the failure
logger.error(f"Verification failed: {query}")
# Alert monitoring system
alert_ops_team(query, result.error)
# Raise error
raise VerificationError(result.error)
return result.valuedef verify_with_fallback(query: str):
"""Try multiple providers if one fails."""
providers = ["openai", "anthropic", "gemini"]
for provider in providers:
try:
client = Q WEDLocal(provider=provider)
result = client.verify_math(query)
if result.verified:
return result
except Exception as e:
logger.warning(f"{provider} failed: {e}")
continue
raise VerificationError("All providers failed")def verify_or_escalate(query: str):
"""Verify, or send to human if fails."""
result = client.verify_math(query)
if result.verified:
return result.value
else:
# Send to human review queue
task_id = create_review_task(
query=query,
error=result.error,
priority="high"
)
return f"Pending human review (task: {task_id})"def verify_or_safe_default(query: str, safe_default=None):
"""Return safe default if verification fails."""
try:
result = client.verify_math(query)
return result.value if result.verified else safe_default
except Exception:
return safe_default
# Usage
interest = verify_or_safe_default(
"Calculate interest on $1M at 5%",
safe_default=50000 # Conservative estimate
)QWED automatically caches verification results:
client = QWEDLocal(
provider="openai",
use_cache=True # Default: True
)
# First call: Hits OpenAI API
result1 = client.verify_math("What is 2+2?") # ~500ms, costs $0.001
# Second call: Returns from cache
result2 = client.verify_math("What is 2+2?") # <10ms, costs $0
# 50-80% cost savings in production!# Coming soon:
import asyncio
async def batch_verify(queries: list[str]):
tasks = [client.verify_math_async(q) for q in queries]
return await asyncio.gather(*tasks)
# Verify 100 queries in parallel
results = await batch_verify(hundred_queries)- Use caching - Enabled by default
- Pick fast models -
gpt-4o-mini>gpt-4o - Local LLMs for dev - Ollama is free & fast
- Batch similar queries - Reduces API calls
See: examples/healthcare_dosage.py
from qwed_sdk import QWEDLocal
class DosageCalculator:
def __init__(self):
self.client = QWEDLocal(
provider="openai",
mask_pii=True # HIPAA compliance
)
def calculate_dosage(self, weight_kg: float, drug: str) -> float:
query = f"Calculate {drug} dosage for {weight_kg}kg patient"
result = self.client.verify_math(query)
if not result.verified:
raise SafetyError("Cannot verify dosage - aborting")
return result.valueSee: examples/ecommerce_pricing.py
def calculate_final_price(base_price: float, discount_pct: float, tax_rate: float):
query = f"""
Calculate final price:
- Base: ${base_price}
- Discount: {discount_pct}%
- Tax: {tax_rate}%
"""
result = client.verify_math(query)
if result.verified:
return round(result.value, 2)
else:
# Log error, use conservative fallback
logger.error(f"Price verification failed: {result.error}")
return base_price * (1 + tax_rate/100) # No discount if unsuredef verify_code_safety(code: str) -> dict:
"""Check if code is safe to execute."""
result = client.verify_code(code)
return {
"safe": result.verified,
"issues": result.evidence.get("dangerous_patterns", []),
"recommendation": "ALLOW" if result.verified else "BLOCK"
}
# Usage
code = "user_input = input(); eval(user_input)"
safety = verify_code_safety(code)
print(safety)
# {'safe': False, 'issues': ['eval()'], 'recommendation': 'BLOCK'}Create a function that calculates tip with verification:
def calculate_tip(bill: float, tip_percent: float) -> float:
"""
Calculate tip amount with QWED verification.
"""
# Your code here
pass
# Test:
print(calculate_tip(100, 20)) # Should: 20.0Solution
def calculate_tip(bill: float, tip_percent: float) -> float:
query = f"Calculate {tip_percent}% tip on ${bill}"
result = client.verify_math(query)
if result.verified:
return result.value
else:
# Fallback to manual calc
return bill * (tip_percent / 100)Improve the tip calculator with retry logic:
def robust_calculate_tip(bill: float, tip_percent: float, max_retries: int = 3) -> float:
# Add retry logic here
passCreate a decorator that adds verification to any function:
def verified(func):
"""Decorator that verifies function output."""
def wrapper(*args, **kwargs):
# Your code here
pass
return wrapper
@verified
def calculate_interest(principal, rate):
return f"Calculate interest on ${principal} at {rate}%"Ready for advanced patterns?
Learn PII masking, LangChain integration, and enterprise deployment patterns!
Stuck or have questions? 💬 Start a Discussion