01_basic_fast_context.py
1 """ 2 Example 1: Basic Fast Context Usage 3 4 This example demonstrates the basic usage of Fast Context for rapid code search. 5 Fast Context is 10-20x faster than traditional sequential search because it: 6 - Executes up to 8 tool calls in parallel 7 - Limits search to 4 turns maximum 8 - Uses a lightweight model optimized for search 9 """ 10 11 from praisonaiagents.context.fast import FastContext 12 import time 13 14 def main(): 15 print("=" * 70) 16 print("Fast Context - Basic Usage Example") 17 print("=" * 70) 18 19 # Create FastContext instance pointing to the praisonai-agents codebase 20 fc = FastContext( 21 workspace_path="/Users/praison/praisonai-package/src/praisonai-agents" 22 ) 23 24 # Example 1: Simple pattern search 25 print("\n1. Simple Pattern Search") 26 print("-" * 40) 27 28 start = time.perf_counter() 29 result = fc.search("def chat") 30 elapsed = (time.perf_counter() - start) * 1000 31 32 print(f" Query: 'def chat'") 33 print(f" Files found: {result.total_files}") 34 print(f" Search time: {result.search_time_ms}ms (total: {elapsed:.0f}ms)") 35 print(f" Turns used: {result.turns_used}") 36 print(f" Tool calls: {result.total_tool_calls}") 37 38 if result.files: 39 print("\n Top matches:") 40 for f in result.files[:3]: 41 print(f" - {f.path}") 42 for lr in f.line_ranges[:2]: 43 print(f" Lines {lr.start}-{lr.end}") 44 45 # Example 2: Search for class definitions 46 print("\n2. Search for Class Definitions") 47 print("-" * 40) 48 49 result = fc.search("class Agent") 50 print(f" Query: 'class Agent'") 51 print(f" Files found: {result.total_files}") 52 print(f" Search time: {result.search_time_ms}ms") 53 54 if result.files: 55 print("\n Files containing 'class Agent':") 56 for f in result.files[:5]: 57 print(f" - {f.path} (relevance: {f.relevance_score:.2f})") 58 59 # Example 3: Search for imports 60 print("\n3. Search for Specific Imports") 61 print("-" * 40) 62 63 result = fc.search("from typing import") 64 print(f" Query: 'from typing import'") 65 print(f" Files found: {result.total_files}") 66 print(f" Search time: {result.search_time_ms}ms") 67 68 # Example 4: Get formatted context string 69 print("\n4. Get Formatted Context for Agent") 70 print("-" * 40) 71 72 context = fc.get_context_for_agent("authentication", max_files=3) 73 print(f" Context length: {len(context)} characters") 74 print(f" Preview:") 75 preview = context[:300].replace('\n', '\n ') 76 print(f" {preview}...") 77 78 print("\n" + "=" * 70) 79 print("Fast Context provides rapid, parallel code search!") 80 print("=" * 70) 81 82 83 if __name__ == "__main__": 84 main()