Governed AI Chatbot
Build a chatbot where every AI response passes through ARQERA's governance engine before reaching the user. Dangerous, biased, or policy-violating responses are caught and blocked.
The complete source code is available at examples/governed-chatbot.
Overview
The pattern is simple:
- User sends a message
- Your AI generates a response
- ARQERA evaluates the response before showing it
- Safe responses are delivered; unsafe responses are blocked
User → AI generates response → ARQERA governance check → User sees response
↓
BLOCKED → User sees "Response blocked by governance"
Prerequisites
pip install arqera
export ARQERA_API_KEY="ak_your_key_here"
Implementation
from arqera import ArqeraClient
client = ArqeraClient()
def governed_chat(user_message: str, ai_response: str) -> str:
"""Govern an AI response before showing it to the user."""
# Evaluate the AI's response through governance
result = client.governance.evaluate(
"chat.respond",
description=ai_response,
context={"user_message": user_message},
)
if result.passed:
return ai_response
elif result.needs_approval:
return f"[This response needs human review: {result.explanation}]"
elif result.blocked:
# Log which laws were violated
failed = [e for e in result.evaluations if e.result.value in ("fail", "warn")]
for law in failed:
print(f" {law.result.value.upper()}: {law.law_name} - {law.reason}")
return "[Response blocked by governance]"
Full Working Example
"""Governed AI Chatbot — Every AI response is governed before the user sees it."""
import os
import sys
from arqera import ArqeraClient
def mock_ai_response(user_message: str) -> str:
"""Simulate an AI response. Replace with your actual AI model."""
if "delete" in user_message.lower():
return "Sure, I'll run DELETE FROM users WHERE active = false;"
if "fire" in user_message.lower():
return "I recommend terminating 30% of the workforce to cut costs."
return "Based on my analysis, the quarterly revenue grew 12% year-over-year."
def main():
api_key = os.environ.get("ARQERA_API_KEY")
if not api_key:
print("Error: Set ARQERA_API_KEY environment variable")
sys.exit(1)
client = ArqeraClient(api_key=api_key)
print("Governed AI Chatbot")
print("Every response is evaluated by ARQERA's governance engine.")
print("Try asking about 'delete users' or 'fire employees'.\n")
while True:
try:
user_input = input("You: ")
except (EOFError, KeyboardInterrupt):
break
if user_input.strip().lower() in ("quit", "exit"):
break
# Step 1: AI generates a response
ai_response = mock_ai_response(user_input)
# Step 2: Govern the response before showing it
result = client.governance.evaluate(
"chat.respond",
description=ai_response,
context={"user_message": user_input},
)
# Step 3: Act on the verdict
if result.passed:
print(f" PROCEED ({result.duration_ms:.0f}ms)")
print(f"AI: {ai_response}\n")
elif result.needs_approval:
print(f" ESCALATE ({result.duration_ms:.0f}ms)")
print(f"AI: [Response needs human review]\n")
elif result.blocked:
print(f" BLOCK ({result.duration_ms:.0f}ms)")
print(f"AI: [Response blocked by governance]\n")
client.close()
if __name__ == "__main__":
main()
Run It
cd examples/governed-chatbot
pip install -r requirements.txt
python main.py
What Happens
| User Says | AI Wants to Say | ARQERA Verdict | User Sees |
|---|---|---|---|
| "Show me the revenue" | "Revenue grew 12% YoY" | PROCEED | The AI response |
| "Delete inactive users" | "Sure, I'll run DELETE FROM users..." | BLOCK | "[Response blocked]" |
| "Fire some employees" | "I recommend terminating 30%..." | ESCALATE | "[Needs human review]" |
Next Steps
- Governed AI Agent — govern a multi-step agent, not just chat
- Python SDK — full SDK reference
- Verdicts — understand PROCEED, ESCALATE, and BLOCK