| |
| """ |
| 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 |
|
|
| |
| 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...") |
| |
| |
| 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:]})") |
| |
| |
| 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: |
| from ankigen_core.agents.integration import AgentOrchestrator |
| from ankigen_core.agents.feature_flags import get_feature_flags |
| print("β
Agent system modules imported successfully") |
| |
| |
| 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 |
| |
| |
| 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("π― Generating cards about Python fundamentals...") |
| |
| |
| 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}") |
| |
| |
| 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...") |
| |
| |
| 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!") |
| |
| |
| 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...") |
| |
| |
| 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") |
| |
| |
| 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) |
| |
| |
| 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...") |
| |
| |
| 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)) |
| |
| |
| demo_performance_comparison() |
| |
| |
| 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()) |