-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_query.py
More file actions
executable file
·401 lines (338 loc) · 16.4 KB
/
rag_query.py
File metadata and controls
executable file
·401 lines (338 loc) · 16.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
"""
RAG Query Engine for YouTube Transcripts
Uses LLM-based extraction and synthesis (works with LM Studio and OpenAI)
"""
import json
import sys
import argparse
import logging
from typing import List, Dict, Optional, Union
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
import config
from chunker import chunk_video_transcripts
from llm_client import LLMClient
def setup_logging(verbose: bool = False, log_file: Optional[str] = None):
"""Setup logging configuration."""
level = logging.DEBUG if verbose else logging.INFO
log_format = '%(asctime)s - %(levelname)s - %(message)s'
# Configure root logger
root_logger = logging.getLogger()
root_logger.setLevel(level)
# Clear existing handlers
root_logger.handlers = []
# Console handler (always) - unbuffered
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(level)
console_handler.setFormatter(logging.Formatter(log_format, datefmt='%Y-%m-%d %H:%M:%S'))
# Force immediate flushing
console_handler.stream = sys.stdout
root_logger.addHandler(console_handler)
# Force Python to use unbuffered output
sys.stdout.reconfigure(line_buffering=True) if hasattr(sys.stdout, 'reconfigure') else None
# File handler (if specified)
if log_file:
file_handler = logging.FileHandler(log_file, mode='a', encoding='utf-8')
file_handler.setLevel(logging.DEBUG) # Always DEBUG for file logs
file_handler.setFormatter(logging.Formatter(log_format, datefmt='%Y-%m-%d %H:%M:%S'))
root_logger.addHandler(file_handler)
print(f"Detailed logs will be saved to: {log_file}")
logger = logging.getLogger(__name__)
for handler in logger.handlers:
handler.flush()
return logger
class RAGQueryEngine:
"""Query engine for YouTube transcripts using LLM-based extraction"""
def __init__(self, transcript_files: Union[str, List[str]], api_type: Optional[str] = None, logger: Optional[logging.Logger] = None):
"""
Initialize query engine.
Args:
transcript_files: Path to JSON file(s) with transcripts (string or list of strings)
api_type: 'lmstudio' or 'openai' (legacy, uses EXTRACTION_API_TYPE and SYNTHESIS_API_TYPE if None)
logger: Logger instance (creates one if None)
"""
# Support both single file and list of files
if isinstance(transcript_files, str):
self.transcript_files = [transcript_files]
else:
self.transcript_files = transcript_files
# Keep backward compatibility
self.transcript_file = self.transcript_files[0] if self.transcript_files else None
self.logger = logger or logging.getLogger(__name__)
# Use separate API types for extraction and synthesis
extraction_api_type = config.EXTRACTION_API_TYPE if api_type is None else api_type
synthesis_api_type = config.SYNTHESIS_API_TYPE if api_type is None else api_type
self.logger.info(f"Initializing LLM clients... (Extraction: {extraction_api_type}, Synthesis: {synthesis_api_type})")
self.extraction_client = LLMClient(api_type=extraction_api_type, logger=self.logger)
self.synthesis_client = LLMClient(api_type=synthesis_api_type, logger=self.logger)
# Keep llm_client for backward compatibility (uses extraction client)
self.llm_client = self.extraction_client
self.logger.info("LLM clients initialized")
self.transcripts_data = None
self.chunks = None
# Load transcripts
self.logger.info("Loading transcripts...")
self.load_transcripts()
# Pre-chunk transcripts
self.logger.info("Chunking transcripts...")
self.chunk_transcripts()
self.logger.info("Initialization complete")
def load_transcripts(self):
"""Load transcripts from JSON file(s)"""
self.transcripts_data = []
for transcript_file in self.transcript_files:
try:
with open(transcript_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Get transcripts array
file_transcripts = data.get('transcripts', [])
# Filter only successful transcripts
file_transcripts = [
t for t in file_transcripts
if t.get('success') and t.get('transcript_full_text')
]
# Add source file info to each transcript
for t in file_transcripts:
t['_source_file'] = transcript_file
self.transcripts_data.extend(file_transcripts)
self.logger.info(f"Loaded {len(file_transcripts)} videos from {transcript_file}")
except FileNotFoundError:
self.logger.error(f"Transcript file not found: {transcript_file}")
except json.JSONDecodeError as e:
self.logger.error(f"Invalid JSON file {transcript_file}: {e}")
if not self.transcripts_data:
self.logger.error("No transcripts loaded from any file!")
sys.exit(1)
self.logger.info(f"Total: {len(self.transcripts_data)} videos loaded from {len(self.transcript_files)} file(s)")
def chunk_transcripts(self):
"""Chunk all transcripts by combining videos (videos are never split)"""
self.logger.info("Chunking transcripts (combining videos, not splitting)...")
self.chunks = chunk_video_transcripts(
self.transcripts_data,
max_tokens=config.MAX_CHUNK_TOKENS,
model=config.EXTRACTION_MODEL
)
total_videos_in_chunks = sum(chunk.get('num_videos', 1) for chunk in self.chunks)
avg_videos = total_videos_in_chunks / len(self.chunks) if self.chunks else 0
self.logger.info(f"Created {len(self.chunks)} chunks containing {total_videos_in_chunks} videos (avg {avg_videos:.1f} videos per chunk)")
def extract_relevant_content_parallel(
self,
question: str,
max_parallel: Optional[int] = None,
return_logs: bool = False
) -> tuple:
"""
Extract relevant content from all chunks in parallel.
Args:
question: User's question
max_parallel: Maximum parallel LLM calls (uses config if None)
return_logs: If True, returns logs along with extracted content
Returns:
List of extracted content with metadata, and optionally extraction logs
"""
max_parallel = max_parallel or config.MAX_PARALLEL_EXTRACTIONS
self.logger.info(f"Extracting relevant content from {len(self.chunks)} chunks...")
self.logger.info(f"Using {max_parallel} parallel workers")
extracted_content = []
extraction_logs = [] # Store logs for each extraction
# Process chunks in parallel
with ThreadPoolExecutor(max_workers=max_parallel) as executor:
# Submit all tasks
futures = {
executor.submit(
self.extraction_client.extract_relevant_content,
chunk['text'],
question,
chunk['videos'], # Pass list of videos in chunk
chunk['chunk_id'], # Pass chunk_id for logging
None, # model (uses default)
None, # temperature (uses default)
return_logs # return_logs flag
): chunk
for chunk in self.chunks
}
# Process results with progress bar (show time estimates)
with tqdm(total=len(futures), desc="Extracting content", unit="chunk",
bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}]') as pbar:
for future in as_completed(futures):
chunk = futures[future]
try:
result = future.result(timeout=300) # 5 minute timeout per chunk
if return_logs:
extracted_text, log_data = result
extraction_logs.append(log_data)
# Check log_data to see what the extraction actually returned
if log_data.get('result') == 'NO_RELEVANT_CONTENT':
self.logger.debug(f"Chunk {chunk['chunk_id']} - Log indicates NO_RELEVANT_CONTENT")
extracted_text = None # Treat as no content
elif log_data.get('result') == 'ERROR':
self.logger.warning(f"Chunk {chunk['chunk_id']} - Extraction had error: {log_data.get('error', 'Unknown error')}")
extracted_text = None # Treat as no content
else:
extracted_text = result
# Check if we got valid content (not empty, not "NO_RELEVANT_CONTENT")
if extracted_text and extracted_text.strip():
# Check if it's explicitly NO_RELEVANT_CONTENT (case-insensitive)
text_upper = extracted_text.strip().upper()
if text_upper == "NO_RELEVANT_CONTENT" or text_upper.startswith("NO_RELEVANT_CONTENT"):
self.logger.debug(f"Chunk {chunk['chunk_id']} - Explicitly marked as NO_RELEVANT_CONTENT")
else:
# For chunks with multiple videos, create entries for each video
# The extracted text should already have video attribution
for video in chunk['videos']:
extracted_content.append({
'content': extracted_text.strip(), # Contains video attribution
'video_id': video['video_id'],
'video_title': video['video_title'],
'video_url': video.get('video_url', ''),
'chunk_id': chunk['chunk_id']
})
self.logger.info(f"Added extracted content from chunk {chunk['chunk_id']} ({len(chunk['videos'])} videos, {len(extracted_text)} chars)")
else:
self.logger.debug(f"Chunk {chunk['chunk_id']} - No valid content (extracted_text is empty or None)")
except Exception as e:
self.logger.error(f"Error processing chunk {chunk['chunk_id']}: {e}", exc_info=True)
if return_logs:
extraction_logs.append({
"chunk_id": chunk.get('chunk_id'),
"error": str(e),
"result": "ERROR"
})
pbar.update(1)
self.logger.info(f"Extracted relevant content from {len(extracted_content)} chunks")
if return_logs:
return extracted_content, extraction_logs
return extracted_content
def query(
self,
question: str,
max_parallel: Optional[int] = None,
return_logs: bool = False
) -> Dict[str, any]:
"""
Query the transcript database.
Args:
question: User's question
max_parallel: Maximum parallel extractions (uses config if None)
return_logs: If True, includes detailed logs in the response
Returns:
Dict with 'answer', 'sources', metadata, and optionally 'logs'
"""
# Stage 1: Extract relevant content from chunks
if return_logs:
extracted_content, extraction_logs = self.extract_relevant_content_parallel(question, max_parallel, return_logs=True)
else:
extracted_content = self.extract_relevant_content_parallel(question, max_parallel, return_logs=False)
extraction_logs = []
if not extracted_content:
result = {
'answer': "No relevant content found in the transcripts for your question.",
'sources': [],
'extracted_chunks': 0,
'total_chunks': len(self.chunks)
}
if return_logs:
result['logs'] = {
'extraction_logs': extraction_logs,
'synthesis_log': None
}
return result
# Stage 2: Deduplicate similar content (simple approach: keep all for now)
# TODO: Add deduplication if needed
# Stage 3: Synthesize final answer
self.logger.info("Synthesizing final answer...")
result = self.synthesis_client.synthesize_answer(question, extracted_content, return_logs=return_logs)
result['extracted_chunks'] = len(extracted_content)
result['total_chunks'] = len(self.chunks)
result['total_videos'] = len(self.transcripts_data)
result['extracted_content'] = extracted_content # Store for context reuse
if return_logs:
result['logs'] = {
'extraction_logs': extraction_logs,
'synthesis_log': result.pop('synthesis_log', None)
}
return result
def main():
"""Main CLI interface"""
parser = argparse.ArgumentParser(
description='Query YouTube transcripts using RAG with LLM extraction'
)
parser.add_argument(
'transcript_file',
help='Path to JSON file with transcripts (from transcript_fetcher.py)'
)
parser.add_argument(
'question',
help='Question to ask about the transcripts'
)
parser.add_argument(
'--api-type',
choices=['lmstudio', 'openai'],
default=None,
help='API type to use (default: from config or environment)'
)
parser.add_argument(
'--max-parallel',
type=int,
default=None,
help=f'Maximum parallel extractions (default: {config.MAX_PARALLEL_EXTRACTIONS})'
)
parser.add_argument(
'--config',
action='store_true',
help='Show configuration and exit'
)
parser.add_argument(
'--verbose', '-v',
action='store_true',
help='Enable verbose logging (show all LLM responses)'
)
parser.add_argument(
'--log-file',
type=str,
default=None,
help='Save detailed logs to file (includes full AI inputs/outputs)'
)
args = parser.parse_args()
if args.config:
config.print_config()
return
# Setup logging
logger = setup_logging(verbose=args.verbose, log_file=args.log_file)
# Initialize query engine
logger.info("Starting query engine initialization...")
sys.stdout.flush()
try:
engine = RAGQueryEngine(args.transcript_file, api_type=args.api_type, logger=logger)
logger.info("Query engine initialized successfully")
except KeyboardInterrupt:
logger.error("Interrupted by user")
sys.exit(1)
except Exception as e:
logger.error(f"Error initializing query engine: {e}", exc_info=True)
sys.exit(1)
# Query
print(f"\nQuestion: {args.question}\n")
result = engine.query(args.question, max_parallel=args.max_parallel)
# Print results
print("\n" + "="*80)
print("ANSWER:")
print("="*80)
print(result['answer'])
print("\n" + "="*80)
print("SOURCES:")
print("="*80)
for source in result['sources']:
print(f"- {source['video_title']}")
if source.get('video_url'):
print(f" URL: {source['video_url']}")
print("\n" + "="*80)
print("STATISTICS:")
print("="*80)
print(f"Total videos: {result.get('total_videos', 0)}")
print(f"Total chunks: {result.get('total_chunks', 0)}")
print(f"Relevant chunks: {result.get('extracted_chunks', 0)}")
print(f"Sources: {len(result['sources'])}")
if __name__ == '__main__':
main()