#!/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())