Download tests/test_agent_cache.py from MCP-1st-Birthday/BirdScopeAI: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/spaces/MCP-1st-Birthday/BirdScopeAI/resolve/main/tests/test_agent_cache.py
- Command line
-
hf download hf://spaces/MCP-1st-Birthday/BirdScopeAI/tests/test_agent_cache.py
-
curl -L -o test_agent_cache.py https://huggingface.co/spaces/MCP-1st-Birthday/BirdScopeAI/resolve/main/tests/test_agent_cache.py
2.68 kB
| import asyncio | |
| import os | |
| import sys | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| # Add parent directory to sys.path so we can import ebird_tools | |
| parent_dir = Path(__file__).parent.parent | |
| sys.path.insert(0, str(parent_dir)) | |
| from langgraph_agent import AgentFactory | |
| from agent_cache import get_or_create_agent, get_cache_stats | |
| async def test_cache(): | |
| # Test data | |
| session_id = "test_session_123" | |
| openai_key = os.environ.get("OPENAI_API_KEY") | |
| # Check for API key (similar to main app) | |
| if not openai_key: | |
| print("❌ OPENAI_API_KEY environment variable not set!") | |
| print("Please set your OpenAI API key to run this test.") | |
| print("Example: export OPENAI_API_KEY='sk-your-key-here'") | |
| return | |
| print("="*50) | |
| print("[TEST 1]: Create agent (cache miss)") | |
| print("="*50) | |
| agent1 = await get_or_create_agent( | |
| session_id=session_id, | |
| provider="openai", | |
| api_key=openai_key, | |
| model="gpt-4o-mini", | |
| mode="Specialized Subagents (3 Specialists)", | |
| agent_factory_method=lambda: AgentFactory.create_subagent_orchestrator( | |
| model="gpt-4o-mini", | |
| api_key=openai_key, | |
| provider="openai", | |
| mode="Specialized Subagents (3 Specialists)" | |
| ) | |
| ) | |
| print(f"\nCache stats: {get_cache_stats()}") | |
| print("\n"+"="*50) | |
| print("[TEST 2: Get same agent (cache hit)") | |
| print("="*50) | |
| agent2 = await get_or_create_agent( | |
| session_id=session_id, | |
| provider="openai", | |
| api_key=openai_key, | |
| model="gpt-4o-mini", | |
| mode="Specialized Subagents (3 Specialists)", | |
| agent_factory_method=lambda: AgentFactory.create_subagent_orchestrator( | |
| model="gpt-4o-mini", | |
| api_key=openai_key, | |
| provider="openai", | |
| mode="Specialized Subagents (3 Specialists)" | |
| ) | |
| ) | |
| print(f"\nCache stats: {get_cache_stats()}") | |
| # Verify they're the same object | |
| print(f"\nSame agent object? {agent1 is agent2}") | |
| print("\n"+"="*50) | |
| print("[TEST 3]: Different session (cache miss)") | |
| print("=" * 50) | |
| agent3 = await get_or_create_agent( | |
| session_id="different_session_456", | |
| provider="openai", | |
| api_key=openai_key, | |
| model="gpt-4o-mini", | |
| mode="Specialized Subagents (3 Specialists)", | |
| agent_factory_method=lambda: AgentFactory.create_subagent_orchestrator( | |
| model="gpt-4o-mini", | |
| api_key=openai_key, | |
| provider="openai", | |
| mode="Specialized Subagents (3 Specialists)" | |
| ) | |
| ) | |
| print(f"\nCache stats: {get_cache_stats()}") | |
| asyncio.run(test_cache()) |