-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrag_citations.py
More file actions
564 lines (476 loc) · 18.5 KB
/
rag_citations.py
File metadata and controls
564 lines (476 loc) · 18.5 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
"""
RAG Citations for Video Transcription
Enables persona chat to cite specific timestamps from transcripts.
"""
import re
import os
import sqlite3
import json
import hashlib
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass
import requests
@dataclass
class TranscriptChunk:
"""Represents a chunk of transcript with metadata."""
chunk_id: str
text: str
start_time: str # HH:MM:SS format
end_time: str # HH:MM:SS format
start_seconds: float
end_seconds: float
embedding: Optional[List[float]] = None
@dataclass
class Citation:
"""Represents a citation from the transcript."""
timestamp: str
seconds: float
text: str
relevance_score: float
@dataclass
class RAGResponse:
"""Response from RAG-augmented generation."""
response: str
citations: List[Citation]
confidence: float # 0.0 to 1.0
used_general_knowledge: bool
class TranscriptRAG:
"""
RAG (Retrieval-Augmented Generation) system for transcript citations.
Chunks transcripts, creates embeddings, and retrieves relevant sections.
"""
def __init__(
self,
db_path: str = "transcription.db",
ollama_base: str = "http://localhost:11434",
embedding_model: str = "nomic-embed-text"
):
"""
Initialize the RAG system.
Args:
db_path: Path to SQLite database
ollama_base: Ollama API base URL
embedding_model: Model to use for embeddings
"""
self.db_path = db_path
self.ollama_base = ollama_base
self.embedding_model = embedding_model
self._create_tables()
def _create_tables(self):
"""Create tables for storing chunks and embeddings."""
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS transcript_chunks (
chunk_id TEXT PRIMARY KEY,
transcription_id INTEGER NOT NULL,
chunk_index INTEGER NOT NULL,
text TEXT NOT NULL,
start_time TEXT,
end_time TEXT,
start_seconds REAL,
end_seconds REAL,
embedding_json TEXT,
FOREIGN KEY (transcription_id) REFERENCES transcriptions (id)
)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_chunks_transcription
ON transcript_chunks (transcription_id)
''')
conn.commit()
def _generate_chunk_id(self, transcription_id: int, chunk_index: int) -> str:
"""Generate unique chunk ID."""
return f"{transcription_id}_{chunk_index}"
def _parse_timestamp(self, timestamp: str) -> float:
"""Convert timestamp string to seconds."""
parts = timestamp.split(':')
if len(parts) == 3:
return int(parts[0]) * 3600 + int(parts[1]) * 60 + float(parts[2])
elif len(parts) == 2:
return int(parts[0]) * 60 + float(parts[1])
return 0.0
def _format_timestamp(self, seconds: float) -> str:
"""Convert seconds to HH:MM:SS format."""
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
def chunk_transcript(
self,
transcript: str,
chunk_size: int = 3,
overlap: int = 1
) -> List[TranscriptChunk]:
"""
Split transcript into overlapping chunks based on timestamp lines.
Args:
transcript: Full transcript text with timestamps
chunk_size: Number of lines per chunk
overlap: Number of overlapping lines between chunks
Returns:
List of TranscriptChunk objects
"""
# Parse lines with timestamps
lines = []
timestamp_pattern = re.compile(r'\[(\d{2}:\d{2}:\d{2})\]\s*(.*)')
for line in transcript.strip().split('\n'):
match = timestamp_pattern.match(line.strip())
if match:
timestamp = match.group(1)
text = match.group(2).strip()
if text: # Only include non-empty lines
lines.append({
'timestamp': timestamp,
'seconds': self._parse_timestamp(timestamp),
'text': text
})
if not lines:
# No timestamps found, treat entire transcript as single chunk
return [TranscriptChunk(
chunk_id="0",
text=transcript,
start_time="00:00:00",
end_time="00:00:00",
start_seconds=0.0,
end_seconds=0.0
)]
# Create overlapping chunks
chunks = []
step = max(1, chunk_size - overlap)
for i in range(0, len(lines), step):
chunk_lines = lines[i:i + chunk_size]
if not chunk_lines:
continue
chunk_text = ' '.join([l['text'] for l in chunk_lines])
start_time = chunk_lines[0]['timestamp']
end_time = chunk_lines[-1]['timestamp']
chunks.append(TranscriptChunk(
chunk_id=str(len(chunks)),
text=chunk_text,
start_time=start_time,
end_time=end_time,
start_seconds=chunk_lines[0]['seconds'],
end_seconds=chunk_lines[-1]['seconds']
))
return chunks
def _get_embedding(self, text: str) -> Optional[List[float]]:
"""Get embedding for text using Ollama."""
try:
response = requests.post(
f"{self.ollama_base}/api/embeddings",
json={
"model": self.embedding_model,
"prompt": text
},
timeout=30
)
response.raise_for_status()
return response.json().get("embedding")
except Exception as e:
print(f"Embedding error: {e}")
return None
def _cosine_similarity(self, vec1: List[float], vec2: List[float]) -> float:
"""Calculate cosine similarity between two vectors."""
if not vec1 or not vec2 or len(vec1) != len(vec2):
return 0.0
dot_product = sum(a * b for a, b in zip(vec1, vec2))
norm1 = sum(a * a for a in vec1) ** 0.5
norm2 = sum(b * b for b in vec2) ** 0.5
if norm1 == 0 or norm2 == 0:
return 0.0
return dot_product / (norm1 * norm2)
def index_transcript(self, transcription_id: int, transcript: str) -> int:
"""
Index a transcript for RAG retrieval.
Args:
transcription_id: Database ID of the transcription
transcript: Full transcript text
Returns:
Number of chunks indexed
"""
# Delete existing chunks for this transcription
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute(
'DELETE FROM transcript_chunks WHERE transcription_id = ?',
(transcription_id,)
)
conn.commit()
# Create chunks
chunks = self.chunk_transcript(transcript)
# Generate embeddings and store
indexed = 0
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
for i, chunk in enumerate(chunks):
embedding = self._get_embedding(chunk.text)
chunk_id = self._generate_chunk_id(transcription_id, i)
cursor.execute('''
INSERT INTO transcript_chunks
(chunk_id, transcription_id, chunk_index, text, start_time,
end_time, start_seconds, end_seconds, embedding_json)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
chunk_id,
transcription_id,
i,
chunk.text,
chunk.start_time,
chunk.end_time,
chunk.start_seconds,
chunk.end_seconds,
json.dumps(embedding) if embedding else None
))
indexed += 1
conn.commit()
return indexed
def retrieve_relevant_chunks(
self,
transcription_id: int,
query: str,
top_k: int = 5,
min_score: float = 0.3
) -> List[Tuple[TranscriptChunk, float]]:
"""
Retrieve chunks most relevant to a query.
Args:
transcription_id: Database ID of the transcription
query: User's question
top_k: Maximum number of chunks to return
min_score: Minimum similarity score threshold
Returns:
List of (chunk, score) tuples sorted by relevance
"""
# Get query embedding
query_embedding = self._get_embedding(query)
if not query_embedding:
return []
# Get all chunks for this transcription
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT chunk_id, text, start_time, end_time,
start_seconds, end_seconds, embedding_json
FROM transcript_chunks
WHERE transcription_id = ?
''', (transcription_id,))
rows = cursor.fetchall()
# Calculate similarities
scored_chunks = []
for row in rows:
embedding_json = row[6]
if not embedding_json:
continue
embedding = json.loads(embedding_json)
score = self._cosine_similarity(query_embedding, embedding)
if score >= min_score:
chunk = TranscriptChunk(
chunk_id=row[0],
text=row[1],
start_time=row[2],
end_time=row[3],
start_seconds=row[4],
end_seconds=row[5],
embedding=embedding
)
scored_chunks.append((chunk, score))
# Sort by score and return top-k
scored_chunks.sort(key=lambda x: x[1], reverse=True)
return scored_chunks[:top_k]
def is_indexed(self, transcription_id: int) -> bool:
"""Check if a transcription has been indexed."""
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute(
'SELECT COUNT(*) FROM transcript_chunks WHERE transcription_id = ?',
(transcription_id,)
)
return cursor.fetchone()[0] > 0
def get_chunk_count(self, transcription_id: int) -> int:
"""Get number of indexed chunks for a transcription."""
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute(
'SELECT COUNT(*) FROM transcript_chunks WHERE transcription_id = ?',
(transcription_id,)
)
return cursor.fetchone()[0]
class RAGPersonaChat:
"""
RAG-augmented persona chat that cites transcript sources.
"""
def __init__(
self,
rag: TranscriptRAG,
model: str = "mistral:instruct",
api_base: str = "http://localhost:11434",
options: Optional[Dict] = None
):
"""
Initialize the RAG persona chat.
Args:
rag: TranscriptRAG instance
model: Ollama model for generation
api_base: Ollama API base URL
options: Model options (temperature, etc.)
"""
self.rag = rag
self.model = model
self.api_base = api_base
self.options = options or {}
def _generate_completion(self, system: str, user: str) -> str:
"""Generate completion using Ollama."""
try:
response = requests.post(
f"{self.api_base}/api/generate",
json={
"model": self.model,
"system": system,
"prompt": user,
"stream": False,
"options": self.options
},
timeout=120
)
response.raise_for_status()
return response.json()["response"]
except Exception as e:
print(f"Generation error: {e}")
return ""
def generate_response(
self,
transcription_id: int,
persona_prompt: str,
user_query: str,
use_only_transcript: bool = True,
top_k: int = 5
) -> RAGResponse:
"""
Generate a response with citations from the transcript.
Args:
transcription_id: Database ID of the transcription
persona_prompt: The persona's system prompt
user_query: User's question
use_only_transcript: If True, only use transcript content
top_k: Number of relevant chunks to retrieve
Returns:
RAGResponse with response text, citations, and confidence
"""
# Retrieve relevant chunks
relevant_chunks = self.rag.retrieve_relevant_chunks(
transcription_id, user_query, top_k
)
if not relevant_chunks:
if use_only_transcript:
return RAGResponse(
response="I couldn't find any relevant information about that in the transcript. Could you rephrase your question or ask about something discussed in the video?",
citations=[],
confidence=0.0,
used_general_knowledge=False
)
else:
# Fall back to general knowledge
response = self._generate_without_rag(persona_prompt, user_query)
return RAGResponse(
response=response + "\n\n*Note: This response is based on general knowledge, not the video content.*",
citations=[],
confidence=0.5,
used_general_knowledge=True
)
# Build context from retrieved chunks
context_parts = []
for chunk, score in relevant_chunks:
context_parts.append(
f"[{chunk.start_time}] {chunk.text}"
)
context = "\n".join(context_parts)
# Calculate confidence based on relevance scores
avg_score = sum(s for _, s in relevant_chunks) / len(relevant_chunks)
max_score = max(s for _, s in relevant_chunks)
confidence = (avg_score + max_score) / 2
# Build the enhanced prompt
rag_system_prompt = f"""{persona_prompt}
IMPORTANT INSTRUCTIONS FOR CITATIONS:
You have access to specific excerpts from the video transcript below. When answering:
1. Base your response ONLY on the provided transcript excerpts
2. Include timestamp citations in [HH:MM:SS] format for every factual claim
3. If the information is not in the provided excerpts, say "I don't see information about that in the video"
4. Quote briefly from the transcript when relevant
TRANSCRIPT EXCERPTS:
{context}
Remember: Always cite timestamps when referencing information from the video."""
user_prompt = f"""Based on the transcript excerpts provided, please answer this question:
{user_query}
Include [timestamp] citations for any information from the video."""
# Generate response
response = self._generate_completion(rag_system_prompt, user_prompt)
# Extract citations from the response
citations = self._extract_citations(response, relevant_chunks)
return RAGResponse(
response=response,
citations=citations,
confidence=confidence,
used_general_knowledge=False
)
def _generate_without_rag(self, persona_prompt: str, user_query: str) -> str:
"""Generate response without RAG context."""
return self._generate_completion(persona_prompt, user_query)
def _extract_citations(
self,
response: str,
chunks: List[Tuple[TranscriptChunk, float]]
) -> List[Citation]:
"""Extract citations from the response."""
citations = []
seen_timestamps = set()
# Find all timestamp patterns in response
timestamp_pattern = re.compile(r'\[(\d{2}:\d{2}:\d{2})\]')
matches = timestamp_pattern.finditer(response)
for match in matches:
timestamp = match.group(1)
if timestamp in seen_timestamps:
continue
seen_timestamps.add(timestamp)
# Find the chunk that matches this timestamp
seconds = self.rag._parse_timestamp(timestamp)
for chunk, score in chunks:
if chunk.start_seconds <= seconds <= chunk.end_seconds + 10:
citations.append(Citation(
timestamp=timestamp,
seconds=seconds,
text=chunk.text[:200] + "..." if len(chunk.text) > 200 else chunk.text,
relevance_score=score
))
break
# Sort by timestamp
citations.sort(key=lambda c: c.seconds)
return citations
def format_response_with_citations(rag_response: RAGResponse) -> str:
"""
Format RAG response for display with citations section.
Args:
rag_response: RAGResponse object
Returns:
Formatted string with response and citations
"""
output = [rag_response.response]
if rag_response.citations:
output.append("\n\n---\n**📍 Sources:**")
for citation in rag_response.citations:
output.append(f"- **[{citation.timestamp}]** \"{citation.text}\"")
# Add confidence indicator
confidence_level = ""
if rag_response.confidence >= 0.7:
confidence_level = "🟢 High confidence"
elif rag_response.confidence >= 0.4:
confidence_level = "🟡 Medium confidence"
else:
confidence_level = "🔴 Low confidence"
output.append(f"\n\n*{confidence_level}*")
if rag_response.used_general_knowledge:
output.append("\n*⚠️ Response includes general knowledge, not just video content.*")
return "\n".join(output)
def get_transcript_rag(db_path: str = "transcription.db") -> TranscriptRAG:
"""Get or create a TranscriptRAG instance."""
return TranscriptRAG(db_path=db_path)