00_agent_fast_context_basic.py
1 """ 2 Fast Context - Basic Example 3 4 FastContext provides rapid code search capabilities. 5 Use it directly from the context module for code search. 6 7 FastContext provides: 8 - 10-20x faster code search than traditional methods 9 - Parallel execution (8 concurrent searches) 10 - Automatic caching for repeated queries 11 12 Note: FastContext is now accessed via the context module directly, 13 not as Agent parameters. For context management with agents, 14 use Agent(context=True) or Agent(context=ManagerConfig(...)). 15 """ 16 17 from praisonaiagents import Agent 18 from praisonaiagents.context import ManagerConfig 19 20 def main(): 21 print("=" * 60) 22 print("Agent with Context Management - Basic Example") 23 print("=" * 60) 24 25 # Create an agent with context management enabled 26 # This is the recommended way to use context features 27 agent = Agent( 28 name="CodeAssistant", 29 instructions="You are a helpful code assistant.", 30 context=True, # Enable context management with defaults 31 ) 32 33 print("\n✓ Agent created with context management enabled") 34 print(f" - context_manager: {agent.context_manager is not None}") 35 if agent.context_manager: 36 print(f" - auto_compact: {agent.context_manager.config.auto_compact}") 37 print(f" - strategy: {agent.context_manager.config.strategy}") 38 39 # For FastContext code search, use the module directly 40 print("\n" + "-" * 60) 41 print("Using FastContext for code search...") 42 print("-" * 60) 43 44 try: 45 from praisonaiagents.context.fast import FastContext 46 47 fc = FastContext( 48 workspace_path=".", 49 model="gpt-4o-mini", 50 max_turns=4, 51 max_parallel=8, 52 ) 53 54 result = fc.search("class Agent") 55 print(f"\n✓ Search for 'class Agent':") 56 print(f" Files found: {result.total_files}") 57 print(f" Search time: {result.search_time_ms}ms") 58 59 if result.total_files > 0: 60 context = fc.get_context_for_agent("class Agent") 61 print(f" Context length: {len(context)} characters") 62 except ImportError: 63 print("\n⚠ FastContext not available (optional feature)") 64 except Exception as e: 65 print(f"\n⚠ FastContext search failed: {e}") 66 67 print("\n" + "=" * 60) 68 print("Context management is now unified via context= param!") 69 print("=" * 60) 70 71 72 if __name__ == "__main__": 73 main()