-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspect_cache.py
More file actions
248 lines (190 loc) · 7.62 KB
/
inspect_cache.py
File metadata and controls
248 lines (190 loc) · 7.62 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
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
"""
Cache Inspector
View and analyze cached questions
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from config import VECTOR_DB_PATH
from modules.database import VectorDatabase
def display_cached_questions(db: VectorDatabase, max_display: int = None):
"""
Display all cached questions with details
Args:
db: VectorDatabase instance
max_display: Maximum questions to display (None = all)
"""
cache_collection = db.collections[db.QUESTIONS_CACHE_COLLECTION]
results = cache_collection.get(limit=10000)
if not results or not results['documents']:
print("No cached questions found")
return
total = len(results['documents'])
print(f"\n{'='*70}")
print(f"CACHED QUESTIONS ({total} total)")
print(f"{'='*70}\n")
questions_to_show = max_display if max_display else total
for i in range(min(questions_to_show, total)):
question = results['documents'][i]
metadata = results['metadatas'][i]
answer = metadata.get('answer', 'No answer')
source = metadata.get('source', 'unknown')
model = metadata.get('model_used', 'unknown')
accuracy = metadata.get('accuracy', 0.0)
usage_count = metadata.get('usage_count', 1)
source_file = metadata.get('source_file', 'unknown')
print(f"{'─'*70}")
print(f"Question {i+1}:")
print(f"{'─'*70}")
print(f"Q: {question}")
print(f"\nA: {answer}")
print(f"\nMetadata:")
print(f" Source: {source}")
print(f" Source File: {source_file}")
print(f" Model: {model}")
print(f" Accuracy: {accuracy:.3f}")
print(f" Usage Count: {usage_count}")
print()
def search_cached_questions(db: VectorDatabase, search_term: str):
"""
Search cached questions by keyword
Args:
db: VectorDatabase instance
search_term: Keyword to search for
"""
cache_collection = db.collections[db.QUESTIONS_CACHE_COLLECTION]
results = cache_collection.get(limit=10000)
if not results or not results['documents']:
print("No cached questions found")
return
search_term_lower = search_term.lower()
matches = []
for i, question in enumerate(results['documents']):
if search_term_lower in question.lower():
matches.append((i, question, results['metadatas'][i]))
print(f"\n{'='*70}")
print(f"SEARCH RESULTS for '{search_term}' ({len(matches)} matches)")
print(f"{'='*70}\n")
for idx, (i, question, metadata) in enumerate(matches):
answer = metadata.get('answer', 'No answer')
print(f"[{idx+1}] {question}")
print(f" A: {answer[:100]}{'...' if len(answer) > 100 else ''}")
print()
def show_statistics(db: VectorDatabase):
"""
Show statistics about cached questions
Args:
db: VectorDatabase instance
"""
cache_collection = db.collections[db.QUESTIONS_CACHE_COLLECTION]
results = cache_collection.get(limit=10000)
if not results or not results['documents']:
print("No cached questions found")
return
total = len(results['documents'])
# Analyze metadata
sources = {}
models = {}
source_files = {}
total_usage = 0
for metadata in results['metadatas']:
source = metadata.get('source', 'unknown')
sources[source] = sources.get(source, 0) + 1
model = metadata.get('model_used', 'unknown')
models[model] = models.get(model, 0) + 1
source_file = metadata.get('source_file', 'unknown')
source_files[source_file] = source_files.get(source_file, 0) + 1
total_usage += metadata.get('usage_count', 1)
avg_usage = total_usage / total if total > 0 else 0
print(f"\n{'='*70}")
print("CACHE STATISTICS")
print(f"{'='*70}\n")
print(f"Total Questions: {total}")
print(f"Total Usage Count: {total_usage}")
print(f"Average Usage per Question: {avg_usage:.2f}\n")
print("By Source:")
for source, count in sources.items():
percentage = (count / total * 100) if total > 0 else 0
print(f" {source}: {count} ({percentage:.1f}%)")
print("\nBy Model:")
for model, count in models.items():
percentage = (count / total * 100) if total > 0 else 0
print(f" {model}: {count} ({percentage:.1f}%)")
print("\nBy Source File:")
for file, count in sorted(source_files.items(), key=lambda x: x[1], reverse=True):
percentage = (count / total * 100) if total > 0 else 0
print(f" {file}: {count} ({percentage:.1f}%)")
print()
def export_to_file(db: VectorDatabase, output_file: str = "cached_questions.txt"):
"""
Export cached questions to a text file
Args:
db: VectorDatabase instance
output_file: Output file path
"""
cache_collection = db.collections[db.QUESTIONS_CACHE_COLLECTION]
results = cache_collection.get(limit=10000)
if not results or not results['documents']:
print("No cached questions found")
return
with open(output_file, 'w', encoding='utf-8') as f:
f.write("="*70 + "\n")
f.write(f"CACHED QUESTIONS ({len(results['documents'])} total)\n")
f.write("="*70 + "\n\n")
for i, question in enumerate(results['documents']):
metadata = results['metadatas'][i]
answer = metadata.get('answer', 'No answer')
source = metadata.get('source', 'unknown')
model = metadata.get('model_used', 'unknown')
f.write(f"Question {i+1}:\n")
f.write(f"{'-'*70}\n")
f.write(f"Q: {question}\n\n")
f.write(f"A: {answer}\n\n")
f.write(f"Source: {source} | Model: {model}\n")
f.write("="*70 + "\n\n")
print(f"✓ Exported {len(results['documents'])} questions to {output_file}")
def main():
"""Main menu for cache inspection"""
print("\n" + "="*70)
print("CACHE INSPECTOR")
print("="*70 + "\n")
# Initialize database
db = VectorDatabase(db_path=VECTOR_DB_PATH)
if not db.initialize_db():
print("✗ Failed to initialize database")
return
while True:
print("\nOptions:")
print(" 1. View all cached questions")
print(" 2. View first N questions")
print(" 3. Search questions by keyword")
print(" 4. Show statistics")
print(" 5. Export to file")
print(" 6. Exit")
choice = input("\nSelect option (1-6): ").strip()
if choice == '1':
display_cached_questions(db)
elif choice == '2':
try:
n = int(input("How many questions to display? "))
display_cached_questions(db, max_display=n)
except ValueError:
print("Invalid number")
elif choice == '3':
search_term = input("Enter search term: ").strip()
if search_term:
search_cached_questions(db, search_term)
elif choice == '4':
show_statistics(db)
elif choice == '5':
output_file = input("Output file name (default: cached_questions.txt): ").strip()
if not output_file:
output_file = "cached_questions.txt"
export_to_file(db, output_file)
elif choice == '6':
print("\nGoodbye!")
break
else:
print("Invalid option")
if __name__ == "__main__":
main()