File size: 12,738 Bytes
09281fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Test RAG System Integration
Tests the complete flow: Document Processor β†’ Vector Database β†’ Query Parser β†’ LLM Reasoning β†’ RAG System
"""

import os
import tempfile
import shutil
from pathlib import Path

def test_rag_system():
    """Test the complete RAG system workflow"""
    print("πŸš€ RAG System Integration Test")
    print("="*50)
    print("Testing: Document Processor β†’ Vector Database β†’ Query Parser β†’ LLM Reasoning β†’ RAG System")
    print("="*50)
    
    try:
        # Import RAG system
        print("πŸ”„ Importing RAG system...")
        from rag_system import AdvancedRAGSystem
        print("βœ… RAG system imported successfully")
        
        # Initialize RAG system
        print("πŸ”„ Initializing RAG system...")
        rag_system = AdvancedRAGSystem(
            use_gpu=False,  # Use CPU for testing
            vector_db_path="./test_rag_db"
        )
        print("βœ… RAG system initialized")
        
        # Validate system
        print("πŸ”„ Validating system components...")
        validation = rag_system.validate_system()
        
        if validation['overall_status']:
            print("βœ… All components validated successfully")
        else:
            print("⚠️  Some components have issues:")
            for error in validation['errors']:
                print(f"   - {error}")
        
        # Create test documents
        print("\nπŸ”„ Creating test documents...")
        test_docs = create_test_documents()
        
        # Ingest documents
        print("πŸ”„ Ingesting documents...")
        total_chunks = 0
        for doc_info in test_docs:
            try:
                chunks = rag_system.ingest_document(doc_info['file_path'])
                total_chunks += len(chunks)
                print(f"   βœ… Ingested {len(chunks)} chunks from {doc_info['name']}")
            except Exception as e:
                print(f"   ❌ Failed to ingest {doc_info['name']}: {e}")
        
        print(f"βœ… Total chunks ingested: {total_chunks}")
        
        # Test queries
        test_queries = [
            "Is heart surgery covered under this policy?",
            "What's the waiting period for dental procedures?",
            "How do I file a claim?",
            "What documents are needed for medical claims?",
            "Are pre-existing conditions covered?"
        ]
        
        print(f"\nπŸ”„ Testing {len(test_queries)} queries...")
        
        results = []
        for i, query in enumerate(test_queries, 1):
            print(f"\n--- Query {i}: {query} ---")
            
            try:
                # Process query through RAG system
                result = rag_system.process_query(query, n_results=3)
                
                print(f"   Processing Time: {result.processing_time:.2f}s")
                print(f"   Query Type: {result.parsed_query.query_type}")
                print(f"   Intent: {result.parsed_query.intent}")
                print(f"   Confidence: {result.parsed_query.confidence:.2f}")
                print(f"   Search Results: {len(result.search_results)}")
                print(f"   Decision: {result.reasoning_result.decision}")
                print(f"   Reasoning Confidence: {result.reasoning_result.confidence_score:.2f}")
                
                # Show top search result
                if result.search_results:
                    top_result = result.search_results[0]
                    print(f"   Top Result: {top_result.content[:100]}...")
                    print(f"   Source: {top_result.source_file}")
                    print(f"   Similarity: {top_result.similarity_score:.3f}")
                
                # Show reasoning justification
                if result.reasoning_result.justification:
                    print(f"   Justification: {result.reasoning_result.justification[:150]}...")
                
                results.append({
                    'query': query,
                    'result': result,
                    'success': True
                })
                
            except Exception as e:
                print(f"   ❌ Query processing failed: {e}")
                results.append({
                    'query': query,
                    'result': None,
                    'success': False,
                    'error': str(e)
                })
        
        # Generate summary report
        print(f"\n{'='*50}")
        print("πŸ“Š RAG SYSTEM TEST RESULTS")
        print(f"{'='*50}")
        
        successful_queries = sum(1 for r in results if r['success'])
        total_queries = len(results)
        
        print(f"Total Queries Tested: {total_queries}")
        print(f"Successful Queries: {successful_queries}")
        print(f"Success Rate: {successful_queries/total_queries*100:.1f}%")
        
        # Detailed results
        print(f"\nπŸ“‹ DETAILED RESULTS:")
        for i, result in enumerate(results, 1):
            if result['success']:
                rag_result = result['result']
                status = "βœ…"
                decision = rag_result.reasoning_result.decision
                confidence = rag_result.reasoning_result.confidence_score
                print(f"{i}. {status} {result['query']}")
                print(f"   Decision: {decision}")
                print(f"   Confidence: {confidence:.2f}")
                print(f"   Search Results: {len(rag_result.search_results)}")
            else:
                print(f"{i}. ❌ {result['query']}")
                print(f"   Error: {result['error']}")
        
        # Test system statistics
        print(f"\nπŸ“Š SYSTEM STATISTICS:")
        stats = rag_system.get_system_statistics()
        print(f"   Vector Database: {stats.get('vector_database', {}).get('total_chunks', 0)} chunks")
        print(f"   Audit Trail: {stats.get('audit_trail', {}).get('total_entries', 0)} entries")
        print(f"   Successful Queries: {stats.get('audit_trail', {}).get('successful_queries', 0)}")
        
        # Test audit trail
        print(f"\nπŸ“‹ AUDIT TRAIL SAMPLE:")
        audit_trail = rag_system.get_audit_trail()
        if audit_trail:
            latest_entry = audit_trail[-1]
            print(f"   Latest Action: {latest_entry.get('action', 'unknown')}")
            print(f"   Status: {latest_entry.get('status', 'unknown')}")
            print(f"   Timestamp: {latest_entry.get('timestamp', 'unknown')}")
        
        # Cleanup
        print(f"\n🧹 Cleaning up...")
        cleanup_test_data()
        
        print(f"\nπŸŽ‰ RAG system test completed!")
        
        if successful_queries == total_queries:
            print("βœ… All queries processed successfully!")
            print("🎯 RAG System is working perfectly!")
        else:
            print("⚠️  Some queries failed. Check the detailed results above.")
        
        return successful_queries == total_queries
        
    except Exception as e:
        print(f"❌ RAG system test failed: {e}")
        import traceback
        traceback.print_exc()
        return False

def create_test_documents():
    """Create test documents for RAG system"""
    test_dir = tempfile.mkdtemp()
    print(f"πŸ“ Created test directory: {test_dir}")
    
    docs = []
    
    # Create policy document
    policy_content = """
    MEDICAL INSURANCE POLICY
    
    COVERAGE DETAILS:
    - Heart surgery: Covered up to $50,000
    - Dental procedures: Covered up to $2,000 annually
    - Prescription medications: 80% coverage
    - Hospital stays: Up to $1,000 per day
    - Specialist consultations: $100 per visit
    
    WAITING PERIODS:
    - General medical: 30 days
    - Pre-existing conditions: 12 months
    - Dental procedures: 6 months
    - Major surgeries: 90 days
    
    CLAIM PROCEDURES:
    - Submit claim form within 30 days
    - Include medical certificate
    - Provide original receipts and bills
    - Processing time: 10-15 business days
    
    EXCLUSIONS:
    - Cosmetic procedures
    - Experimental treatments
    - Injuries from dangerous activities
    - Pre-existing conditions (first 12 months)
    """
    
    policy_path = os.path.join(test_dir, "medical_policy.txt")
    with open(policy_path, 'w', encoding='utf-8') as f:
        f.write(policy_content)
    
    docs.append({
        'name': 'Medical Policy',
        'file_path': policy_path,
        'type': 'policy'
    })
    
    # Create claims guide
    claims_content = """
    CLAIMS PROCESSING GUIDE
    
    REQUIRED DOCUMENTS:
    1. Completed claim form
    2. Medical certificate from doctor
    3. Original receipts and bills
    4. Prescription details (if applicable)
    5. Hospital discharge summary (if hospitalized)
    
    PROCESSING TIMES:
    - Standard claims: 10-15 business days
    - Urgent claims: 3-5 business days
    - Complex cases: 20-30 business days
    
    CLAIM LIMITS:
    - Maximum annual benefit: $100,000
    - Maximum per claim: $25,000
    - Deductible: $500 per year
    
    SUBMISSION METHODS:
    - Online portal
    - Mobile app
    - Mail to claims department
    - In-person at service centers
    """
    
    claims_path = os.path.join(test_dir, "claims_guide.txt")
    with open(claims_path, 'w', encoding='utf-8') as f:
        f.write(claims_content)
    
    docs.append({
        'name': 'Claims Guide',
        'file_path': claims_path,
        'type': 'guide'
    })
    
    return docs

def cleanup_test_data():
    """Clean up test data"""
    try:
        import time
        import gc
        
        # Force garbage collection
        gc.collect()
        time.sleep(2)
        
        # Remove test directories
        test_dirs = ["./test_rag_db", "./test_vector_db", "./temp_test_db"]
        for dir_path in test_dirs:
            if os.path.exists(dir_path):
                try:
                    shutil.rmtree(dir_path, ignore_errors=True)
                    print(f"   βœ… Cleaned {dir_path}")
                except Exception as e:
                    print(f"   ⚠️  Could not clean {dir_path}: {e}")
        
        # Remove temporary files
        temp_files = [f for f in os.listdir('.') if f.startswith('temp_')]
        for file in temp_files:
            try:
                os.remove(file)
                print(f"   βœ… Removed {file}")
            except Exception as e:
                print(f"   ⚠️  Could not remove {file}: {e}")
                
    except Exception as e:
        print(f"   ⚠️  Cleanup warning: {e}")

def test_individual_components():
    """Test individual components before RAG system"""
    print("\nπŸ§ͺ TESTING INDIVIDUAL COMPONENTS")
    print("="*40)
    
    components = {
        'Document Processor': 'document_processer',
        'Vector Database': 'vector_database', 
        'Query Parser': 'query_parser',
        'LLM Reasoning': 'llm_reasoning'
    }
    
    results = {}
    
    for name, module in components.items():
        print(f"\nπŸ”„ Testing {name}...")
        try:
            __import__(module)
            print(f"   βœ… {name} imported successfully")
            results[name] = True
        except Exception as e:
            print(f"   ❌ {name} import failed: {e}")
            results[name] = False
    
    # Summary
    print(f"\nπŸ“Š COMPONENT TEST RESULTS:")
    passed = sum(results.values())
    total = len(results)
    
    for name, result in results.items():
        status = "βœ… PASS" if result else "❌ FAIL"
        print(f"   {name}: {status}")
    
    print(f"\nOverall: {passed}/{total} components ready")
    
    return passed == total

def main():
    """Main test runner"""
    print("πŸš€ RAG System Test Suite")
    print("="*50)
    
    # Test individual components first
    components_ready = test_individual_components()
    
    if not components_ready:
        print("\n❌ Some components are not ready. Please fix the issues above.")
        return False
    
    print(f"\n{'='*50}")
    print("πŸ”„ RUNNING RAG SYSTEM INTEGRATION TEST")
    print(f"{'='*50}")
    
    # Test RAG system
    success = test_rag_system()
    
    if success:
        print(f"\nπŸŽ‰ RAG System Integration Test PASSED!")
        print("βœ… All components working together successfully")
        print("🎯 Your RAG system is ready for production use!")
    else:
        print(f"\n⚠️  RAG System Integration Test FAILED!")
        print("❌ Some issues need to be resolved")
    
    print(f"\nπŸ’‘ Next steps:")
    print("   1. Add your actual documents")
    print("   2. Customize the query processing")
    print("   3. Fine-tune the reasoning engine")
    print("   4. Deploy to production")

if __name__ == "__main__":
    main()