-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
384 lines (320 loc) · 11.4 KB
/
Copy pathquery.py
File metadata and controls
384 lines (320 loc) · 11.4 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python3
"""
RAG query CLI tool for TextStruct.
This tool allows interactive querying of indexed textbook content with
metadata-aware filtering by pedagogical role, chapter, and section.
Usage:
python src/query.py <vector_store_path> [--query "your question"]
python src/query.py <vector_store_path> --interactive
"""
import argparse
import json
from pathlib import Path
from typing import Optional
from src.rag_retriever import load_retriever, format_results
from src.embeddings import EmbeddingEngine
PEDAGOGICAL_ROLES = [
"definition",
"explanation",
"procedure",
"example",
"question",
"result",
"reference",
"evidence",
"visual_proxy",
"context",
]
def print_separator():
"""Print a visual separator."""
print("\n" + "=" * 80 + "\n")
def query_interactive(retriever, embedding_engine):
"""
Interactive query mode with role-based filtering.
Args:
retriever: RAGRetriever instance
embedding_engine: EmbeddingEngine instance
"""
print("=" * 80)
print("TextStruct RAG Interactive Query")
print("=" * 80)
# Show stats
stats = retriever.get_stats()
print(f"\nIndexed chunks: {stats['num_chunks']}")
if "chunks_by_role" in stats:
print("\nChunks by pedagogical role:")
for role, count in sorted(stats["chunks_by_role"].items(), key=lambda x: -x[1])[:5]:
pct = (count / stats["num_chunks"]) * 100
print(f" {role}: {count} ({pct:.1f}%)")
print("\nCommands:")
print(" - Type your query and press Enter")
print(" - Use 'role:<role_name>' to filter by pedagogical role")
print(" - Use 'section:<section_name>' to filter by section")
print(" - Use 'top:<n>' to get top n results (default: 5)")
print(" - Use 'sql:<WHERE clause>' for advanced SQL filtering (LanceDB only)")
print(" - Type 'help' to see available roles")
print(" - Type 'stats' to see index statistics")
print(" - Type 'exit' or 'quit' to exit")
print_separator()
while True:
try:
# Get user input
query = input("Query: ").strip()
if not query:
continue
# Handle special commands
if query.lower() in ["exit", "quit", "q"]:
print("Goodbye!")
break
if query.lower() == "help":
print("\nAvailable pedagogical roles:")
for role in PEDAGOGICAL_ROLES:
print(f" - {role}")
print_separator()
continue
if query.lower() == "stats":
stats = retriever.get_stats()
print(f"\nTotal chunks: {stats['num_chunks']}")
print(f"Embedding dimension: {stats['embedding_dim']}")
if "chunks_by_role" in stats:
print("\nChunks by pedagogical role:")
for role, count in sorted(stats["chunks_by_role"].items(), key=lambda x: -x[1]):
pct = (count / stats["num_chunks"]) * 100
print(f" {role}: {count} ({pct:.1f}%)")
if "chunks_by_section" in stats:
print(f"\nUnique sections: {len(stats['chunks_by_section'])}")
print_separator()
continue
# Parse query modifiers
role_filter = None
section_filter = None
sql_filter = None
top_k = 5
query_text = query
# Extract modifiers
parts = query.split()
remaining_parts = []
for part in parts:
if part.startswith("role:"):
role_filter = part[5:]
elif part.startswith("section:"):
section_filter = part[8:]
elif part.startswith("sql:"):
# SQL filter can span multiple words, so grab rest of query
sql_idx = query.index("sql:")
sql_filter = query[sql_idx + 4:].strip()
# Remove SQL part from query
query_text = query[:sql_idx].strip()
break
elif part.startswith("top:"):
try:
top_k = int(part[4:])
except ValueError:
print(f"[WARN] Invalid top value: {part[4:]}, using default (5)")
else:
remaining_parts.append(part)
if not sql_filter:
query_text = " ".join(remaining_parts)
if not query_text:
print("[ERROR] Please provide a query text")
continue
# Execute query
print(f"\nSearching for: '{query_text}'")
if role_filter:
print(f"Filtered by role: {role_filter}")
if section_filter:
print(f"Filtered by section: {section_filter}")
if sql_filter:
print(f"SQL filter: {sql_filter}")
print(f"Top {top_k} results:\n")
# Use SQL query if available, otherwise use regular query
if sql_filter and hasattr(retriever, 'query_sql'):
results = retriever.query_sql(
query_text=query_text,
top_k=top_k,
sql_filter=sql_filter,
)
else:
if sql_filter:
print("[WARN] SQL filters not supported by current vector store, using dict filters")
results = retriever.query(
query_text=query_text,
top_k=top_k,
pedagogical_role=role_filter,
section=section_filter,
)
# Display results
if not results:
print("No results found.")
else:
print(format_results(results, max_text_length=300))
print_separator()
except KeyboardInterrupt:
print("\n\nGoodbye!")
break
except Exception as e:
print(f"\n[ERROR] {e}")
print_separator()
def query_single(
retriever,
query_text: str,
top_k: int = 5,
role: Optional[str] = None,
section: Optional[str] = None,
sql_filter: Optional[str] = None,
output_json: Optional[Path] = None,
):
"""
Execute a single query and print results.
Args:
retriever: RAGRetriever instance
query_text: Query text
top_k: Number of results
role: Optional role filter
section: Optional section filter
sql_filter: Optional SQL WHERE clause (LanceDB only)
output_json: Optional path to save results as JSON
"""
print(f"Query: {query_text}")
if role:
print(f"Role filter: {role}")
if section:
print(f"Section filter: {section}")
if sql_filter:
print(f"SQL filter: {sql_filter}")
print()
# Use SQL query if available, otherwise use regular query
if sql_filter and hasattr(retriever, 'query_sql'):
results = retriever.query_sql(
query_text=query_text,
top_k=top_k,
sql_filter=sql_filter,
)
else:
if sql_filter:
print("[WARN] SQL filters not supported by current vector store, using dict filters")
results = retriever.query(
query_text=query_text,
top_k=top_k,
pedagogical_role=role,
section=section,
)
if not results:
print("No results found.")
return
# Print results
print(format_results(results, max_text_length=300))
# Save to JSON if requested
if output_json:
output_data = []
for chunk, score in results:
output_data.append({
"chunk": chunk,
"similarity_score": score,
})
output_json.parent.mkdir(parents=True, exist_ok=True)
with open(output_json, "w", encoding="utf-8") as f:
json.dump(output_data, f, indent=2, ensure_ascii=False)
print(f"\nResults saved to {output_json}")
def main():
parser = argparse.ArgumentParser(
description="Query indexed textbook content using RAG",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Interactive mode
python src/query.py data/output/book1/vector_store.lancedb --interactive
# Single query
python src/query.py data/output/book1/vector_store.lancedb --query "What is a triangle?"
# Query with role filter
python src/query.py data/output/book1/vector_store.lancedb \\
--query "triangle" --role definition
# Query with SQL filter (LanceDB only)
python src/query.py data/output/book1/vector_store.lancedb \\
--query "triangles" --sql-filter "page_start >= 5 AND page_start <= 10"
# Query with multiple filters
python src/query.py data/output/book1/vector_store.lancedb \\
--query "solve equations" --role procedure --top-k 3
"""
)
parser.add_argument(
"vector_store",
type=Path,
help="Path to vector store file (.pkl or .lancedb)",
)
parser.add_argument(
"--query",
"-q",
type=str,
help="Query text (use --interactive for interactive mode)",
)
parser.add_argument(
"--interactive",
"-i",
action="store_true",
help="Start interactive query mode",
)
parser.add_argument(
"--top-k",
"-k",
type=int,
default=5,
help="Number of results to return (default: 5)",
)
parser.add_argument(
"--role",
"-r",
type=str,
choices=PEDAGOGICAL_ROLES,
help="Filter by pedagogical role",
)
parser.add_argument(
"--section",
"-s",
type=str,
help="Filter by section name",
)
parser.add_argument(
"--sql-filter",
type=str,
help="SQL WHERE clause for advanced filtering (LanceDB only, e.g., 'page_start >= 5 AND page_start <= 10')",
)
parser.add_argument(
"--output",
"-o",
type=Path,
help="Save results to JSON file",
)
args = parser.parse_args()
# Validate vector store path
if not args.vector_store.exists():
print(f"[ERROR] Vector store not found: {args.vector_store}")
print("\nTo build a vector store, run:")
print(" python src/main.py <pdf> --refine --semantic-chunk --classify-roles --build-index")
return
# Load retriever
print(f"[INFO] Loading vector store from {args.vector_store}...")
# Set up embedding engine with cache
cache_dir = args.vector_store.parent.parent / ".doctr_cache" / "embeddings"
embedding_engine = EmbeddingEngine(cache_dir=cache_dir)
retriever = load_retriever(args.vector_store, embedding_engine)
print(f"[INFO] Loaded {retriever.get_stats()['num_chunks']} chunks")
print()
# Execute query
if args.interactive:
query_interactive(retriever, embedding_engine)
elif args.query:
query_single(
retriever,
query_text=args.query,
top_k=args.top_k,
role=args.role,
section=args.section,
sql_filter=args.sql_filter,
output_json=args.output,
)
else:
parser.print_help()
print("\n[ERROR] Please provide --query or --interactive")
if __name__ == "__main__":
main()