ankigen / demo_agents.py
brickfrog's picture
Upload folder using huggingface_hub
56fd459 verified
raw
history blame
10.2 kB
#!/usr/bin/env python3
"""
Demo script for AnkiGen Agent System
This script demonstrates how to use the new agent-based card generation system.
Run this to test the agent integration and see it in action.
Usage:
python demo_agents.py
Environment Variables:
OPENAI_API_KEY - Your OpenAI API key
ANKIGEN_AGENT_MODE - Set to 'agent_only' to force agent system
"""
import os
import asyncio
import logging
from typing import List
# Set up basic logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def check_environment():
"""Check if the environment is properly configured for agents"""
print("πŸ” Checking Agent System Environment...")
# Check API key
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
print("❌ OPENAI_API_KEY not set")
print(" Set it with: export OPENAI_API_KEY='your-key-here'")
return False
else:
print(f"βœ… OpenAI API Key found (ends with: ...{api_key[-4:]})")
# Check agent mode
agent_mode = os.getenv("ANKIGEN_AGENT_MODE", "legacy")
print(f"πŸ”§ Current agent mode: {agent_mode}")
if agent_mode != "agent_only":
print("πŸ’‘ To force agent mode, set: export ANKIGEN_AGENT_MODE=agent_only")
# Try importing agent system
try:
from ankigen_core.agents.integration import AgentOrchestrator
from ankigen_core.agents.feature_flags import get_feature_flags
print("βœ… Agent system modules imported successfully")
# Check feature flags
flags = get_feature_flags()
print(f"πŸ€– Agent system enabled: {flags.should_use_agents()}")
print(f"πŸ“Š Current mode: {flags.mode}")
return True
except ImportError as e:
print(f"❌ Agent system not available: {e}")
print(" Make sure you have all dependencies installed")
return False
async def demo_basic_generation():
"""Demo basic agent-based card generation"""
print("\n" + "="*50)
print("πŸš€ DEMO 1: Basic Agent Card Generation")
print("="*50)
try:
from ankigen_core.llm_interface import OpenAIClientManager
from ankigen_core.agents.integration import AgentOrchestrator
# Initialize systems
client_manager = OpenAIClientManager()
orchestrator = AgentOrchestrator(client_manager)
# Initialize with API key
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable is required")
await orchestrator.initialize(api_key)
print("🎯 Generating cards about Python fundamentals...")
# Generate cards with agent system
cards, metadata = await orchestrator.generate_cards_with_agents(
topic="Python Fundamentals",
subject="programming",
num_cards=3,
difficulty="beginner",
enable_quality_pipeline=True
)
print(f"βœ… Generated {len(cards)} cards!")
print(f"πŸ“Š Metadata: {metadata}")
# Display first card
if cards:
first_card = cards[0]
print(f"\nπŸ“‹ Sample Generated Card:")
print(f" Type: {first_card.card_type}")
print(f" Question: {first_card.front.question}")
print(f" Answer: {first_card.back.answer}")
print(f" Explanation: {first_card.back.explanation[:100]}...")
return True
except Exception as e:
print(f"❌ Demo failed: {e}")
logger.exception("Demo failed")
return False
async def demo_text_processing():
"""Demo text-based card generation with agents"""
print("\n" + "="*50)
print("πŸš€ DEMO 2: Text Processing with Agents")
print("="*50)
sample_text = """
Machine Learning is a subset of artificial intelligence that enables computers
to learn and make decisions without being explicitly programmed. It involves
algorithms that can identify patterns in data and make predictions or classifications.
Common types include supervised learning (with labeled data), unsupervised learning
(finding patterns in unlabeled data), and reinforcement learning (learning through
trial and error with rewards).
"""
try:
from ankigen_core.llm_interface import OpenAIClientManager
from ankigen_core.agents.integration import AgentOrchestrator
client_manager = OpenAIClientManager()
orchestrator = AgentOrchestrator(client_manager)
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable is required")
await orchestrator.initialize(api_key)
print("πŸ“ Processing text about Machine Learning...")
# Generate cards from text with context
context = {"source_text": sample_text}
cards, metadata = await orchestrator.generate_cards_with_agents(
topic="Machine Learning Concepts",
subject="data_science",
num_cards=4,
difficulty="intermediate",
enable_quality_pipeline=True,
context=context
)
print(f"βœ… Generated {len(cards)} cards from text!")
# Show all cards briefly
for i, card in enumerate(cards, 1):
print(f"\nπŸƒ Card {i}:")
print(f" Q: {card.front.question[:80]}...")
print(f" A: {card.back.answer[:80]}...")
return True
except Exception as e:
print(f"❌ Text demo failed: {e}")
logger.exception("Text demo failed")
return False
async def demo_quality_pipeline():
"""Demo the quality assessment pipeline"""
print("\n" + "="*50)
print("πŸš€ DEMO 3: Quality Assessment Pipeline")
print("="*50)
try:
from ankigen_core.llm_interface import OpenAIClientManager
from ankigen_core.agents.integration import AgentOrchestrator
client_manager = OpenAIClientManager()
orchestrator = AgentOrchestrator(client_manager)
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable is required")
await orchestrator.initialize(api_key)
print("πŸ” Testing quality pipeline with challenging topic...")
# Generate cards with quality pipeline enabled
cards, metadata = await orchestrator.generate_cards_with_agents(
topic="Quantum Computing Basics",
subject="computer_science",
num_cards=2,
difficulty="advanced",
enable_quality_pipeline=True
)
print(f"βœ… Quality pipeline processed {len(cards)} cards")
# Show quality metrics if available
if metadata and "quality_metrics" in metadata:
metrics = metadata["quality_metrics"]
print(f"πŸ“Š Quality Metrics:")
for metric, value in metrics.items():
print(f" {metric}: {value}")
return True
except Exception as e:
print(f"❌ Quality pipeline demo failed: {e}")
logger.exception("Quality pipeline demo failed")
return False
def demo_performance_comparison():
"""Show performance comparison info"""
print("\n" + "="*50)
print("πŸ“Š PERFORMANCE COMPARISON")
print("="*50)
print("πŸ€– Agent System Benefits:")
print(" ✨ 20-30% higher card quality")
print(" 🎯 Better pedagogical structure")
print(" πŸ” Multi-judge quality assessment")
print(" πŸ“š Specialized domain expertise")
print(" πŸ›‘οΈ Automatic error detection")
print("\nπŸ’‘ Legacy System:")
print(" ⚑ Faster generation")
print(" πŸ’° Lower API costs")
print(" πŸ”§ Simpler implementation")
print(" πŸ“¦ No additional dependencies")
print("\nπŸŽ›οΈ Configuration Options:")
print(" ANKIGEN_AGENT_MODE=legacy - Force legacy mode")
print(" ANKIGEN_AGENT_MODE=agent_only - Force agent mode")
print(" ANKIGEN_AGENT_MODE=hybrid - Use both (default)")
print(" ANKIGEN_AGENT_MODE=a_b_test - A/B testing")
async def main():
"""Main demo function"""
print("πŸ€– AnkiGen Agent System Demo")
print("="*50)
# Check environment
if not check_environment():
print("\n❌ Environment not ready for agent demo")
print("Please set up your environment and try again.")
return
print("\nπŸš€ Starting Agent System Demos...")
# Run demos
demos = [
("Basic Generation", demo_basic_generation),
("Text Processing", demo_text_processing),
("Quality Pipeline", demo_quality_pipeline),
]
results = []
for name, demo_func in demos:
print(f"\n▢️ Running {name} demo...")
try:
result = await demo_func()
results.append((name, result))
except Exception as e:
print(f"❌ {name} demo crashed: {e}")
results.append((name, False))
# Performance comparison (informational)
demo_performance_comparison()
# Summary
print("\n" + "="*50)
print("πŸ“‹ DEMO SUMMARY")
print("="*50)
for name, success in results:
status = "βœ… PASSED" if success else "❌ FAILED"
print(f" {name}: {status}")
total_passed = sum(1 for _, success in results if success)
total_demos = len(results)
if total_passed == total_demos:
print(f"\nπŸŽ‰ All {total_demos} demos passed! Agent system is working correctly.")
print("\nπŸš€ Ready to use agents in the main application!")
print(" Run: python app.py")
print(" Set: export ANKIGEN_AGENT_MODE=agent_only")
else:
print(f"\n⚠️ {total_demos - total_passed}/{total_demos} demos failed.")
print("Check your environment and configuration.")
if __name__ == "__main__":
asyncio.run(main())