context_required_example.py
1 #!/usr/bin/env python3 2 """ 3 Example: Knowledge Retrieval with Context-Required Questions 4 5 This example demonstrates: 6 1. Agent answers questions that REQUIRE retrieved context (not general knowledge) 7 2. Unique codes/secrets that cannot be guessed 8 3. Verification that context is text, not file path 9 10 Requirements: 11 - pip install praisonaiagents[knowledge] 12 - OPENAI_API_KEY environment variable 13 14 Usage: 15 python context_required_example.py 16 """ 17 18 import os 19 import tempfile 20 import shutil 21 22 from praisonaiagents import Agent 23 24 25 def main(): 26 temp_dir = tempfile.mkdtemp(prefix='praison_ctx_') 27 28 try: 29 # Create test document with UNIQUE content that cannot be guessed 30 doc = os.path.join(temp_dir, 'company_policy.txt') 31 with open(doc, 'w') as f: 32 f.write(""" 33 Acme Corporation Internal Policy Document 34 ========================================== 35 36 Remote Work Policy: 37 - Employees may work from home up to 3 days per week 38 - Manager approval is required for remote work 39 - Manager approval code: ZEBRA-71 40 - All requests must be submitted via the HR portal 41 42 Expense Reimbursement: 43 - Maximum daily meal allowance: $47.50 44 - Mileage rate: $0.655 per mile 45 - Expense submission code: TIGER-42 46 - Receipts required for amounts over $25 47 48 IT Security: 49 - VPN access code prefix: FALCON- 50 - Password reset hotline: 555-0199 51 - Security incident code: HAWK-88 52 53 This document is confidential. Last updated: 2024-01-15. 54 """) 55 56 print("=" * 60) 57 print("Example: Context-Required Knowledge Retrieval") 58 print("=" * 60) 59 print(f"\nDocument location: {doc}") 60 print("\nThis example asks questions that CANNOT be answered") 61 print("without the retrieved context (unique codes/values).\n") 62 63 # Create agent with knowledge 64 agent = Agent( 65 name="PolicyExpert", 66 instructions="""You are a company policy expert. 67 Answer questions based ONLY on the provided knowledge context. 68 If the answer is not in the context, say 'I don't have that information.' 69 Be precise and quote exact values when asked about codes or numbers.""", 70 knowledge=[temp_dir], 71 memory={"user_id": "demo_user"}, 72 output="verbose", 73 ) 74 75 # Question 1: Unique code that requires context 76 print("-" * 40) 77 print("Question 1: What is the manager approval code for remote work?") 78 print("-" * 40) 79 response = agent.chat("What is the manager approval code for remote work?") 80 print(f"\nAnswer: {response}\n") 81 82 # Verify the answer contains the unique code 83 if "ZEBRA-71" in response.upper(): 84 print("✅ VERIFIED: Agent correctly retrieved the unique code from context!\n") 85 else: 86 print("❌ WARNING: Agent did not find the code - check retrieval\n") 87 88 # Question 2: Another unique value 89 print("-" * 40) 90 print("Question 2: What is the maximum daily meal allowance?") 91 print("-" * 40) 92 response = agent.chat("What is the maximum daily meal allowance?") 93 print(f"\nAnswer: {response}\n") 94 95 if "47.50" in response or "$47.50" in response: 96 print("✅ VERIFIED: Agent correctly retrieved the specific amount!\n") 97 else: 98 print("❌ WARNING: Agent did not find the amount - check retrieval\n") 99 100 # Question 3: Security code 101 print("-" * 40) 102 print("Question 3: What is the security incident code?") 103 print("-" * 40) 104 response = agent.chat("What is the security incident code?") 105 print(f"\nAnswer: {response}\n") 106 107 if "HAWK-88" in response.upper(): 108 print("✅ VERIFIED: Agent correctly retrieved the security code!\n") 109 else: 110 print("❌ WARNING: Agent did not find the code - check retrieval\n") 111 112 # Show context preview to prove it's text, not path 113 print("=" * 60) 114 print("Context Verification") 115 print("=" * 60) 116 context, _ = agent._get_knowledge_context("policy codes", use_rag=True) 117 print(f"\nRetrieved context preview (first 300 chars):") 118 print("-" * 40) 119 print(context[:300] if context else "NO CONTEXT") 120 print("-" * 40) 121 122 # Verify context is text, not path 123 if temp_dir in context: 124 print("\n❌ ERROR: Context contains file path instead of text!") 125 else: 126 print("\n✅ VERIFIED: Context contains actual text, not file path!") 127 128 finally: 129 shutil.rmtree(temp_dir, ignore_errors=True) 130 131 132 if __name__ == "__main__": 133 main()