/ examples / knowledge / compression_demo.py
compression_demo.py
  1  #!/usr/bin/env python3
  2  """
  3  Example: Context Compression Demo
  4  
  5  This example demonstrates:
  6  1. Context compression for large retrieved content
  7  2. Token budget management
  8  3. Agent-centric knowledge retrieval with unique codes
  9  
 10  Requirements:
 11  - pip install praisonaiagents[knowledge]
 12  - OPENAI_API_KEY environment variable
 13  
 14  Usage:
 15      python compression_demo.py
 16  """
 17  
 18  import os
 19  import tempfile
 20  import shutil
 21  
 22  from praisonaiagents import Agent
 23  
 24  
 25  # Unique verification code that CANNOT be guessed
 26  SECRET_CODE = "COMPRESS-8K4M2"
 27  
 28  
 29  def create_verbose_document(temp_dir: str) -> str:
 30      """Create a verbose document with a hidden unique code."""
 31      filepath = os.path.join(temp_dir, 'verbose_policy.txt')
 32      
 33      # Create a document with lots of filler text but one unique code
 34      with open(filepath, 'w') as f:
 35          f.write("""
 36  ACME CORPORATION COMPREHENSIVE POLICY MANUAL
 37  =============================================
 38  
 39  CHAPTER 1: INTRODUCTION
 40  -----------------------
 41  
 42  This comprehensive policy manual outlines all procedures, guidelines, and 
 43  requirements for employees of Acme Corporation. All employees are expected 
 44  to read and understand this document in its entirety. This manual supersedes 
 45  all previous versions and is effective immediately upon publication.
 46  
 47  The purpose of this manual is to provide clear guidance on company policies,
 48  procedures, and expectations. It covers a wide range of topics including but
 49  not limited to workplace conduct, benefits, leave policies, and security
 50  protocols.
 51  
 52  CHAPTER 2: WORKPLACE CONDUCT
 53  ----------------------------
 54  
 55  All employees are expected to maintain professional conduct at all times.
 56  This includes treating colleagues with respect, maintaining a clean workspace,
 57  and adhering to the company dress code. Violations of workplace conduct
 58  policies may result in disciplinary action up to and including termination.
 59  
 60  Professional behavior includes punctuality, meeting deadlines, and effective
 61  communication with team members. Employees should report any concerns about
 62  workplace conduct to their immediate supervisor or Human Resources.
 63  
 64  CHAPTER 3: BENEFITS AND COMPENSATION
 65  ------------------------------------
 66  
 67  Acme Corporation offers a comprehensive benefits package to all full-time
 68  employees. This includes health insurance, dental coverage, vision care,
 69  and a 401(k) retirement plan with company matching up to 6% of salary.
 70  
 71  Employees are eligible for benefits after completing 90 days of employment.
 72  Open enrollment occurs annually in November. Changes to benefits outside
 73  of open enrollment require a qualifying life event.
 74  
 75  CHAPTER 4: LEAVE POLICIES
 76  -------------------------
 77  
 78  Employees accrue paid time off (PTO) based on years of service:
 79  - 0-2 years: 15 days per year
 80  - 3-5 years: 20 days per year
 81  - 6+ years: 25 days per year
 82  
 83  Sick leave is provided separately at 10 days per year. Unused sick leave
 84  may be carried over to the following year up to a maximum of 30 days.
 85  
 86  CHAPTER 5: SECURITY PROTOCOLS
 87  -----------------------------
 88  
 89  All employees must follow strict security protocols to protect company
 90  assets and confidential information. This includes using strong passwords,
 91  locking workstations when away, and reporting any suspicious activity.
 92  
 93  IMPORTANT: The master security verification code is: COMPRESS-8K4M2
 94  
 95  This code is required for accessing secure areas and systems. Do not share
 96  this code with unauthorized personnel. Misuse of security credentials is
 97  grounds for immediate termination.
 98  
 99  CHAPTER 6: EMERGENCY PROCEDURES
100  -------------------------------
101  
102  In case of emergency, employees should follow the evacuation procedures
103  posted throughout the building. Assembly points are located in the parking
104  lot on the north side of the building.
105  
106  Fire drills are conducted quarterly. All employees must participate in
107  fire drills and know the location of the nearest emergency exit.
108  
109  CHAPTER 7: CONCLUSION
110  ---------------------
111  
112  This policy manual is a living document and may be updated periodically.
113  Employees will be notified of any changes via email and updated copies
114  will be made available on the company intranet.
115  
116  Questions about any policy should be directed to Human Resources.
117  
118  Document Version: 3.2.1
119  Last Updated: 2024-12-01
120  """)
121      
122      return filepath
123  
124  
125  def main():
126      temp_dir = tempfile.mkdtemp(prefix='praison_compress_')
127      
128      try:
129          print("=" * 60)
130          print("Example: Context Compression Demo")
131          print("=" * 60)
132          
133          # Create verbose document
134          doc_path = create_verbose_document(temp_dir)
135          print(f"\nCreated verbose document: {doc_path}")
136          print(f"Secret code embedded: {SECRET_CODE}")
137          
138          # Demonstrate compression
139          print("\n" + "-" * 40)
140          print("Compression Demo")
141          print("-" * 40)
142          
143          try:
144              from praisonaiagents.rag import ContextCompressor
145              
146              # Read the document
147              with open(doc_path, 'r') as f:
148                  content = f.read()
149              
150              print(f"\nOriginal document length: {len(content)} characters")
151              
152              # Compress with different ratios
153              for ratio in [0.3, 0.5, 0.7]:
154                  compressor = ContextCompressor(
155                      max_tokens=2000,
156                      target_ratio=ratio,
157                  )
158                  result = compressor.compress([content], query="security verification code")
159                  
160                  print(f"\nCompression ratio {ratio}:")
161                  print(f"  Original tokens: {result.original_tokens}")
162                  print(f"  Compressed tokens: {result.compressed_tokens}")
163                  print(f"  Actual ratio: {result.compressed_tokens / max(result.original_tokens, 1):.2f}")
164                  
165                  # Check if secret code is preserved
166                  compressed_text = " ".join(result.chunks) if result.chunks else ""
167                  if SECRET_CODE in compressed_text:
168                      print(f"  ✅ Secret code preserved in compressed output")
169                  else:
170                      print(f"  ⚠️ Secret code may have been removed (query-focused compression)")
171                      
172          except ImportError as e:
173              print(f"Note: Compression module not available: {e}")
174          
175          # Create agent with knowledge
176          print("\n" + "-" * 40)
177          print("Agent Knowledge Retrieval Test")
178          print("-" * 40)
179          
180          agent = Agent(
181              name="PolicyExpert",
182              instructions="""You are a company policy expert.
183  Answer questions based ONLY on the provided knowledge context.
184  When asked about codes, provide the EXACT code from the documents.
185  Be concise but accurate.""",
186              knowledge=[temp_dir],
187              user_id="compression_demo_user",
188              output="verbose",
189          )
190          
191          # Test retrieval of the unique code
192          print("\nQ: What is the master security verification code?")
193          response = agent.chat("What is the master security verification code?")
194          print(f"A: {response}")
195          
196          if SECRET_CODE in response.upper():
197              print(f"\n✅ VERIFIED: Agent correctly retrieved the secret code!")
198          else:
199              print(f"\n❌ WARNING: Expected code {SECRET_CODE} not found in response")
200          
201          print("\n" + "=" * 60)
202          print("Demo Complete")
203          print("=" * 60)
204          
205      finally:
206          shutil.rmtree(temp_dir, ignore_errors=True)
207  
208  
209  if __name__ == "__main__":
210      main()