Spaces:
Running
Running
YanBoChen
commited on
Commit
Β·
890989b
1
Parent(s):
6c249e5
feat(retrieval) 1st MVP: enhance search logging and deduplication logic with distance threshold
Browse files- src/retrieval.py +26 -12
- tests/test_retrieval.py +206 -0
src/retrieval.py
CHANGED
|
@@ -195,6 +195,9 @@ class BasicRetrievalSystem:
|
|
| 195 |
top_k
|
| 196 |
)
|
| 197 |
|
|
|
|
|
|
|
|
|
|
| 198 |
results = {
|
| 199 |
"query": query,
|
| 200 |
"emergency_results": emergency_results,
|
|
@@ -234,7 +237,7 @@ class BasicRetrievalSystem:
|
|
| 234 |
# Format results
|
| 235 |
results = []
|
| 236 |
for idx, distance in zip(indices, distances):
|
| 237 |
-
chunk_data = chunks[
|
| 238 |
result = {
|
| 239 |
"type": source_type, # Using 'type' to match metadata
|
| 240 |
"chunk_id": idx,
|
|
@@ -267,7 +270,7 @@ class BasicRetrievalSystem:
|
|
| 267 |
# Combine all results
|
| 268 |
all_results = emergency_results + treatment_results
|
| 269 |
|
| 270 |
-
# Remove duplicates based on
|
| 271 |
unique_results = self._remove_duplicates(all_results)
|
| 272 |
|
| 273 |
# Sort by distance
|
|
@@ -286,23 +289,34 @@ class BasicRetrievalSystem:
|
|
| 286 |
logger.error(f"Post-processing failed: {e}")
|
| 287 |
raise
|
| 288 |
|
| 289 |
-
def _remove_duplicates(self, results: List[Dict]) -> List[Dict]:
|
| 290 |
"""
|
| 291 |
-
Remove duplicate results based on
|
| 292 |
|
| 293 |
Args:
|
| 294 |
results: List of search results
|
|
|
|
| 295 |
|
| 296 |
Returns:
|
| 297 |
-
Deduplicated results
|
| 298 |
"""
|
| 299 |
-
|
| 300 |
unique_results = []
|
| 301 |
|
| 302 |
-
for
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
return unique_results
|
|
|
|
| 195 |
top_k
|
| 196 |
)
|
| 197 |
|
| 198 |
+
# Log individual index results
|
| 199 |
+
logger.info(f"Search results: Emergency={len(emergency_results)}, Treatment={len(treatment_results)}")
|
| 200 |
+
|
| 201 |
results = {
|
| 202 |
"query": query,
|
| 203 |
"emergency_results": emergency_results,
|
|
|
|
| 237 |
# Format results
|
| 238 |
results = []
|
| 239 |
for idx, distance in zip(indices, distances):
|
| 240 |
+
chunk_data = chunks[idx] # chunks is a list, use integer index directly
|
| 241 |
result = {
|
| 242 |
"type": source_type, # Using 'type' to match metadata
|
| 243 |
"chunk_id": idx,
|
|
|
|
| 270 |
# Combine all results
|
| 271 |
all_results = emergency_results + treatment_results
|
| 272 |
|
| 273 |
+
# Remove duplicates based on distance similarity
|
| 274 |
unique_results = self._remove_duplicates(all_results)
|
| 275 |
|
| 276 |
# Sort by distance
|
|
|
|
| 289 |
logger.error(f"Post-processing failed: {e}")
|
| 290 |
raise
|
| 291 |
|
| 292 |
+
def _remove_duplicates(self, results: List[Dict], distance_threshold: float = 0.1) -> List[Dict]:
|
| 293 |
"""
|
| 294 |
+
Remove duplicate results based on distance threshold
|
| 295 |
|
| 296 |
Args:
|
| 297 |
results: List of search results
|
| 298 |
+
distance_threshold: Maximum distance difference to consider as duplicate
|
| 299 |
|
| 300 |
Returns:
|
| 301 |
+
Deduplicated results with logging statistics
|
| 302 |
"""
|
| 303 |
+
original_count = len(results)
|
| 304 |
unique_results = []
|
| 305 |
|
| 306 |
+
for current in results:
|
| 307 |
+
is_unique = True
|
| 308 |
+
current_dist = current["distance"]
|
| 309 |
+
|
| 310 |
+
# Check distance similarity with already kept results
|
| 311 |
+
for kept in unique_results:
|
| 312 |
+
if abs(current_dist - kept["distance"]) < distance_threshold:
|
| 313 |
+
is_unique = False
|
| 314 |
+
break
|
| 315 |
+
|
| 316 |
+
if is_unique:
|
| 317 |
+
unique_results.append(current)
|
| 318 |
+
|
| 319 |
+
final_count = len(unique_results)
|
| 320 |
+
logger.info(f"Deduplication stats: {original_count} β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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test suite for BasicRetrievalSystem
|
| 3 |
+
This module tests the core retrieval functionality including:
|
| 4 |
+
- System initialization
|
| 5 |
+
- Basic search functionality
|
| 6 |
+
- Deduplication logic
|
| 7 |
+
- Result formatting
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import sys
|
| 11 |
+
import os
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
import logging
|
| 14 |
+
|
| 15 |
+
print("\n=== Phase 1: Initializing Test Environment ===")
|
| 16 |
+
# Add src to python path
|
| 17 |
+
current_dir = Path(__file__).parent.resolve()
|
| 18 |
+
project_root = current_dir.parent
|
| 19 |
+
sys.path.append(str(project_root / "src"))
|
| 20 |
+
|
| 21 |
+
print(f"β’ Current directory: {current_dir}")
|
| 22 |
+
print(f"β’ Project root: {project_root}")
|
| 23 |
+
print(f"β’ Python path added: {project_root / 'src'}")
|
| 24 |
+
|
| 25 |
+
# Change working directory to project root for file access
|
| 26 |
+
os.chdir(project_root)
|
| 27 |
+
print(f"β’ Changed working directory to: {project_root}")
|
| 28 |
+
|
| 29 |
+
from retrieval import BasicRetrievalSystem #type: ignore
|
| 30 |
+
|
| 31 |
+
class TestRetrievalSystem:
|
| 32 |
+
"""Test suite for basic retrieval system functionality"""
|
| 33 |
+
|
| 34 |
+
def setup_class(self):
|
| 35 |
+
"""Initialize test environment"""
|
| 36 |
+
print("\n=== Phase 2: Setting up Test Environment ===")
|
| 37 |
+
|
| 38 |
+
# Setup logging to capture our logs
|
| 39 |
+
logging.basicConfig(
|
| 40 |
+
level=logging.INFO,
|
| 41 |
+
format='%(levelname)s:%(name)s:%(message)s',
|
| 42 |
+
handlers=[
|
| 43 |
+
logging.StreamHandler(),
|
| 44 |
+
logging.FileHandler('test_retrieval.log')
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
print("β’ Initializing BasicRetrievalSystem...")
|
| 50 |
+
self.retrieval = BasicRetrievalSystem(embedding_dim=768)
|
| 51 |
+
print("β
Retrieval system initialized successfully")
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"β Failed to initialize retrieval system: {e}")
|
| 55 |
+
raise
|
| 56 |
+
|
| 57 |
+
def test_system_initialization(self):
|
| 58 |
+
"""Test system initialization components"""
|
| 59 |
+
print("\n=== Phase 3: System Initialization Test ===")
|
| 60 |
+
|
| 61 |
+
print("β’ Checking embedding model...")
|
| 62 |
+
assert self.retrieval.embedding_model is not None, "Embedding model not loaded"
|
| 63 |
+
print("β Embedding model loaded")
|
| 64 |
+
|
| 65 |
+
print("β’ Checking emergency index...")
|
| 66 |
+
assert self.retrieval.emergency_index is not None, "Emergency index not loaded"
|
| 67 |
+
print("β Emergency index loaded")
|
| 68 |
+
|
| 69 |
+
print("β’ Checking treatment index...")
|
| 70 |
+
assert self.retrieval.treatment_index is not None, "Treatment index not loaded"
|
| 71 |
+
print("β Treatment index loaded")
|
| 72 |
+
|
| 73 |
+
print("β’ Checking chunk data...")
|
| 74 |
+
assert len(self.retrieval.emergency_chunks) > 0, "Emergency chunks not loaded"
|
| 75 |
+
assert len(self.retrieval.treatment_chunks) > 0, "Treatment chunks not loaded"
|
| 76 |
+
print(f"β Emergency chunks: {len(self.retrieval.emergency_chunks)}")
|
| 77 |
+
print(f"β Treatment chunks: {len(self.retrieval.treatment_chunks)}")
|
| 78 |
+
|
| 79 |
+
print("β
System initialization test passed")
|
| 80 |
+
|
| 81 |
+
def test_basic_search_functionality(self):
|
| 82 |
+
"""Test basic search functionality with medical queries"""
|
| 83 |
+
print("\n=== Phase 4: Basic Search Functionality Test ===")
|
| 84 |
+
|
| 85 |
+
test_queries = [
|
| 86 |
+
"What is the treatment for acute myocardial infarction?",
|
| 87 |
+
"How to manage chest pain in emergency?",
|
| 88 |
+
"Acute stroke treatment protocol"
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
for i, query in enumerate(test_queries, 1):
|
| 92 |
+
print(f"\nπ Test Query {i}/3: {query}")
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
results = self.retrieval.search(query)
|
| 96 |
+
|
| 97 |
+
# Basic structure checks
|
| 98 |
+
assert "query" in results, "Query not in results"
|
| 99 |
+
assert "processed_results" in results, "Processed results not found"
|
| 100 |
+
assert "total_results" in results, "Total results count missing"
|
| 101 |
+
|
| 102 |
+
processed_results = results["processed_results"]
|
| 103 |
+
print(f"β’ Results returned: {len(processed_results)}")
|
| 104 |
+
|
| 105 |
+
# Check result format
|
| 106 |
+
for j, result in enumerate(processed_results[:3], 1): # Check first 3
|
| 107 |
+
assert "type" in result, f"Result {j} missing 'type' field"
|
| 108 |
+
assert "text" in result, f"Result {j} missing 'text' field"
|
| 109 |
+
assert "distance" in result, f"Result {j} missing 'distance' field"
|
| 110 |
+
assert "chunk_id" in result, f"Result {j} missing 'chunk_id' field"
|
| 111 |
+
|
| 112 |
+
print(f" R-{j} [{result['type']}] (distance: {result['distance']:.3f}): {result['text'][:60]}...")
|
| 113 |
+
|
| 114 |
+
print(f"β Query {i} completed successfully")
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"β Query {i} failed: {e}")
|
| 118 |
+
raise
|
| 119 |
+
|
| 120 |
+
print("\nβ
Basic search functionality test passed")
|
| 121 |
+
|
| 122 |
+
def test_deduplication_logic(self):
|
| 123 |
+
"""Test the new distance-based deduplication logic"""
|
| 124 |
+
print("\n=== Phase 5: Deduplication Logic Test ===")
|
| 125 |
+
|
| 126 |
+
# Create test data with similar distances
|
| 127 |
+
test_results = [
|
| 128 |
+
{"text": "Sample text 1", "distance": 0.1, "type": "emergency", "chunk_id": 1},
|
| 129 |
+
{"text": "Sample text 2", "distance": 0.105, "type": "emergency", "chunk_id": 2}, # Should be considered duplicate
|
| 130 |
+
{"text": "Sample text 3", "distance": 0.2, "type": "treatment", "chunk_id": 3},
|
| 131 |
+
{"text": "Sample text 4", "distance": 0.3, "type": "treatment", "chunk_id": 4}
|
| 132 |
+
]
|
| 133 |
+
|
| 134 |
+
print(f"β’ Original results: {len(test_results)}")
|
| 135 |
+
for i, result in enumerate(test_results, 1):
|
| 136 |
+
print(f" Test-{i}: distance={result['distance']}, type={result['type']}")
|
| 137 |
+
|
| 138 |
+
# Test deduplication
|
| 139 |
+
unique_results = self.retrieval._remove_duplicates(test_results, distance_threshold=0.1)
|
| 140 |
+
|
| 141 |
+
print(f"β’ After deduplication: {len(unique_results)}")
|
| 142 |
+
for i, result in enumerate(unique_results, 1):
|
| 143 |
+
print(f" Kept-{i}: distance={result['distance']}, type={result['type']}")
|
| 144 |
+
|
| 145 |
+
# Verify deduplication worked
|
| 146 |
+
assert len(unique_results) < len(test_results), "Deduplication should remove some results"
|
| 147 |
+
print("β Distance-based deduplication working correctly")
|
| 148 |
+
|
| 149 |
+
print("β
Deduplication logic test passed")
|
| 150 |
+
|
| 151 |
+
def test_result_statistics(self):
|
| 152 |
+
"""Test result statistics and logging"""
|
| 153 |
+
print("\n=== Phase 6: Result Statistics Test ===")
|
| 154 |
+
|
| 155 |
+
query = "Emergency cardiac arrest management"
|
| 156 |
+
print(f"β’ Testing with query: {query}")
|
| 157 |
+
|
| 158 |
+
# Capture logs by running search
|
| 159 |
+
results = self.retrieval.search(query)
|
| 160 |
+
|
| 161 |
+
# Verify we get statistics
|
| 162 |
+
assert "total_results" in results, "Total results missing"
|
| 163 |
+
assert "processing_info" in results, "Processing info missing"
|
| 164 |
+
|
| 165 |
+
total_results = results["total_results"]
|
| 166 |
+
duplicates_removed = results["processing_info"]["duplicates_removed"]
|
| 167 |
+
|
| 168 |
+
print(f"β’ Total results: {total_results}")
|
| 169 |
+
print(f"β’ Duplicates removed: {duplicates_removed}")
|
| 170 |
+
print("β Statistics logging working correctly")
|
| 171 |
+
|
| 172 |
+
print("β
Result statistics test passed")
|
| 173 |
+
|
| 174 |
+
def main():
|
| 175 |
+
"""Run all retrieval system tests"""
|
| 176 |
+
print("\n" + "="*60)
|
| 177 |
+
print("COMPREHENSIVE RETRIEVAL SYSTEM TEST SUITE")
|
| 178 |
+
print("="*60)
|
| 179 |
+
|
| 180 |
+
test = TestRetrievalSystem()
|
| 181 |
+
|
| 182 |
+
try:
|
| 183 |
+
test.setup_class()
|
| 184 |
+
test.test_system_initialization()
|
| 185 |
+
test.test_basic_search_functionality()
|
| 186 |
+
test.test_deduplication_logic()
|
| 187 |
+
test.test_result_statistics()
|
| 188 |
+
|
| 189 |
+
print("\n" + "="*60)
|
| 190 |
+
print("π ALL RETRIEVAL SYSTEM TESTS COMPLETED SUCCESSFULLY!")
|
| 191 |
+
print("="*60)
|
| 192 |
+
print("β
System initialization validated")
|
| 193 |
+
print("β
Basic search functionality confirmed")
|
| 194 |
+
print("β
Distance-based deduplication working")
|
| 195 |
+
print("β
Result statistics and logging verified")
|
| 196 |
+
print("="*60)
|
| 197 |
+
|
| 198 |
+
except Exception as e:
|
| 199 |
+
print("\n" + "="*60)
|
| 200 |
+
print("β RETRIEVAL SYSTEM TESTS FAILED!")
|
| 201 |
+
print(f"Error: {str(e)}")
|
| 202 |
+
print("="*60)
|
| 203 |
+
raise
|
| 204 |
+
|
| 205 |
+
if __name__ == "__main__":
|
| 206 |
+
main()
|