-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate_indices.py
More file actions
155 lines (120 loc) · 5.36 KB
/
validate_indices.py
File metadata and controls
155 lines (120 loc) · 5.36 KB
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
#!/usr/bin/env python3
"""
Validate FAISS indices after updates
"""
import json
import logging
from pathlib import Path
import faiss
import numpy as np
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class IndexValidator:
def __init__(self):
self.data_dir = Path("data")
self.indices_dir = self.data_dir / "faiss_indices"
def validate_faiss_index(self, index_path, metadata_path):
"""Validate a FAISS index and its metadata."""
try:
# Load FAISS index
index = faiss.read_index(str(index_path))
logger.info(f"Loaded index: {index_path.name}")
logger.info(f" - Index type: {type(index).__name__}")
logger.info(f" - Dimensions: {index.d}")
logger.info(f" - Total vectors: {index.ntotal}")
# Load metadata
with open(metadata_path, 'r', encoding='utf-8') as f:
metadata = json.load(f)
doc_count = len(metadata.get('documents', []))
logger.info(f" - Metadata documents: {doc_count}")
# Validate consistency
if index.ntotal != doc_count:
logger.error(f"Mismatch: {index.ntotal} vectors != {doc_count} documents")
return False
# Test search functionality
if index.ntotal > 0:
# Create a random query vector
query_vector = np.random.random((1, index.d)).astype('float32')
# Perform search
k = min(5, index.ntotal)
distances, indices = index.search(query_vector, k)
logger.info(f" - Search test successful (k={k})")
logger.info(f" - Distance range: {distances[0].min():.3f} - {distances[0].max():.3f}")
return True
except Exception as e:
logger.error(f"Validation failed for {index_path}: {e}")
return False
def validate_all_indices(self):
"""Validate all FAISS indices."""
success = True
# Find all index files
index_files = list(self.indices_dir.rglob("*.faiss"))
if not index_files:
logger.error("No FAISS index files found!")
return False
logger.info(f"Validating {len(index_files)} index files...")
for index_path in index_files:
metadata_path = index_path.with_suffix('.json')
if not metadata_path.exists():
logger.error(f"Missing metadata file: {metadata_path}")
success = False
continue
if not self.validate_faiss_index(index_path, metadata_path):
success = False
# Validate combined index specifically
combined_index = self.indices_dir / "combined_index.faiss"
if combined_index.exists():
logger.info("\nValidating combined index...")
combined_metadata = self.indices_dir / "combined_metadata.json"
if not self.validate_faiss_index(combined_index, combined_metadata):
success = False
return success
def generate_validation_report(self):
"""Generate a validation report."""
report = {
"validation_timestamp": str(Path.cwd() / "data" / "last_validation.json"),
"indices": []
}
index_files = list(self.indices_dir.rglob("*.faiss"))
for index_path in index_files:
metadata_path = index_path.with_suffix('.json')
try:
index = faiss.read_index(str(index_path))
with open(metadata_path, 'r', encoding='utf-8') as f:
metadata = json.load(f)
report["indices"].append({
"name": index_path.name,
"path": str(index_path),
"dimensions": index.d,
"vectors": index.ntotal,
"documents": len(metadata.get('documents', [])),
"created": metadata.get('created_date', 'unknown'),
"source": metadata.get('source', 'unknown'),
"status": "valid"
})
except Exception as e:
report["indices"].append({
"name": index_path.name,
"path": str(index_path),
"status": "invalid",
"error": str(e)
})
# Save report
report_path = self.data_dir / "validation_report.json"
with open(report_path, 'w', encoding='utf-8') as f:
json.dump(report, f, indent=2, ensure_ascii=False)
logger.info(f"Validation report saved to {report_path}")
return report
def main():
validator = IndexValidator()
# Validate all indices
if validator.validate_all_indices():
logger.info("✅ All indices validated successfully!")
# Generate detailed report
validator.generate_validation_report()
exit(0) # Success
else:
logger.error("❌ Index validation failed!")
exit(1) # Failure
if __name__ == "__main__":
main()