-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_rag_index.py
More file actions
615 lines (495 loc) · 24.3 KB
/
Copy pathbuild_rag_index.py
File metadata and controls
615 lines (495 loc) · 24.3 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
"""
One-shot ingestion script: PDF → chunks → embeddings → Qdrant.
Run this script once to build (or incrementally update) the local Qdrant index
from FIA regulation PDFs. Subsequent runs are idempotent — each chunk is hashed
and skipped if it already exists in the collection, so adding a new PDF only
indexes the new content without rebuilding from scratch.
Usage:
python scripts/build_rag_index.py
python scripts/build_rag_index.py --docs-dir data/rag/documents
python scripts/build_rag_index.py --force-rebuild
PDF naming convention (required):
<doc_type>_<year>.pdf
e.g. sporting_regs_2025.pdf technical_regs_2024.pdf
Supported doc_types : sporting_regs, technical_regs
Supported years : 2023, 2024, 2025
"""
from __future__ import annotations
import argparse
import hashlib
import logging
import re
import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import Iterator
import numpy as np
import pypdf
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, PointStruct, VectorParams
from sentence_transformers import SentenceTransformer
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
@dataclass
class IndexConfig:
"""Centralised configuration for the PDF ingestion pipeline.
Grouping all tunable parameters here means changing the embedding model,
chunk strategy, or storage paths requires editing exactly one place.
Attributes:
collection_name: Name of the Qdrant collection to populate. Must match
``RagConfig.collection_name`` in ``retriever.py`` —
a mismatch means the retriever queries an empty collection.
embedding_model: Sentence-transformers model used to encode chunks. Must
be the same model used at query time in ``retriever.py``
— incompatible models produce meaningless similarity scores.
embedding_dim: Output vector size of the embedding model. BGE-M3 produces
1024-dim vectors; changing the model requires updating this
value or Qdrant will reject the upsert silently.
chunk_size: Sliding window size in characters. 512 chars ≈ 80–120 words,
fitting comfortably inside BGE-M3's 512-token limit while
keeping each chunk semantically coherent (one or two articles).
chunk_overlap: Characters repeated at the start of each new window so that
sentences at chunk boundaries appear complete in at least one
chunk and are not truncated mid-article.
embed_batch_size: Number of chunks embedded in a single encoder call. Larger
batches saturate the GPU better but consume more VRAM; 64 is
a safe default for an 8 GB card with BGE-M3.
"""
collection_name: str = "fia_regulations"
embedding_model: str = "BAAI/bge-m3" # MTEB ~67, 1024-dim, ~2 GB VRAM on RTX 5070
embedding_dim: int = 1024
chunk_size: int = 512
chunk_overlap: int = 64
embed_batch_size: int = 64
def __post_init__(self) -> None:
self._repo_root = Path(__file__).resolve().parent.parent
@property
def docs_dir(self) -> Path:
"""Directory where FIA PDFs are stored, scanned at index build time."""
return self._repo_root / "data" / "rag" / "documents"
@property
def qdrant_path(self) -> Path:
"""On-disk Qdrant storage directory; created automatically if absent."""
return self._repo_root / "data" / "rag" / "qdrant_local"
CFG = IndexConfig()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Data classes
# ---------------------------------------------------------------------------
@dataclass
class PDFDocument:
"""Represents a single FIA PDF file before chunking.
Carries the raw extracted text alongside the metadata derived from the
filename so that every chunk created from this document inherits the
correct ``doc_type`` and ``year`` without re-parsing the filename.
Attributes:
path: Absolute path to the source PDF, kept for error messages and
logging so failures can be traced back to a specific file.
doc_type: Regulatory domain of the document — either ``"sporting_regs"``
or ``"technical_regs"``. Derived from the filename prefix and
stored on every chunk so downstream agents can filter by domain.
year: Season the document applies to (2023–2025). F1 regulations
change annually, so the year determines which rule version is
authoritative for a given race.
text: Full plain-text content extracted from the PDF. May contain
artefacts from PDF rendering (hyphenation, ligatures) that are
cleaned during chunking.
"""
path: Path
doc_type: str
year: int
text: str = field(default="", repr=False)
@dataclass
class TextChunk:
"""A single chunk of regulation text ready for embedding and indexing.
Produced by splitting a ``PDFDocument`` into overlapping windows. Each chunk
is self-contained enough to be returned as a retrieval result without the
surrounding context, which is why the source metadata (doc_type, year,
article reference) is duplicated here rather than kept only on the parent
document.
Attributes:
text: The regulation passage itself, trimmed and normalised.
This is the string that gets embedded and stored as the
Qdrant payload — what the RAG agent returns to the LLM.
doc_type: Inherited from the parent ``PDFDocument``. Lets callers
filter retrieval results by regulatory domain without
parsing the text.
year: Inherited from the parent ``PDFDocument``. Determines
which season's rules apply — critical when regulations
changed between years (e.g. cost-cap rules 2023 vs 2025).
article: Article or section reference extracted by regex from the
chunk text (e.g. ``"Article 48.3"``). Empty string when
no reference is found. Stored in the Qdrant payload so
the LLM can cite the exact article without re-parsing.
section_title: Nearest section heading found above this chunk in the
document, when available. Provides coarse context about
which part of the regulations the chunk belongs to.
chunk_hash: SHA-256 of the normalised text, used for idempotent
upserts — chunks already present in Qdrant are skipped
so re-running the script only indexes new content.
"""
text: str
doc_type: str
year: int
article: str = ""
section_title: str = ""
chunk_hash: str = ""
# ---------------------------------------------------------------------------
# PDF extraction
# ---------------------------------------------------------------------------
_FILENAME_RE = re.compile(r"^(?P<doc_type>sporting_regs|technical_regs)_(?P<year>20\d{2})\.pdf$")
def parse_pdf_filename(path: Path) -> tuple[str, int] | None:
"""Extract doc_type and year from a PDF filename following the naming convention.
Returns ``None`` for files that do not match the expected pattern so the
caller can skip them with a warning rather than raising an exception — this
lets the script process a directory that may contain unrelated files without
aborting the whole run.
Args:
path: Path to the PDF file. Only the filename (not the full path) is
matched against the pattern, so the file does not need to exist.
"""
match = _FILENAME_RE.match(path.name)
if match is None:
return None
return match.group("doc_type"), int(match.group("year"))
def extract_text_from_pdf(path: Path) -> str:
"""Extract all plain text from a PDF file using PyMuPDF.
Concatenates text from every page separated by a newline so that page
boundaries do not create artificial word splits during chunking. pypdf
handles the simple linear layout of FIA regulation documents well without
requiring native C dependencies.
Args:
path: Path to the PDF file to read. Raises ``FileNotFoundError`` if
the file does not exist — callers should validate the path first.
"""
reader = pypdf.PdfReader(str(path))
pages = [page.extract_text() or "" for page in reader.pages]
return "\n".join(pages)
def load_pdf_documents(docs_dir: Path) -> list[PDFDocument]:
"""Discover and load all FIA PDFs in a directory into ``PDFDocument`` objects.
Skips files whose names do not match the naming convention and logs a
warning for each skipped file so the operator knows what was ignored.
Also logs an error (without raising) if a PDF cannot be opened, allowing
the rest of the batch to continue.
Args:
docs_dir: Directory to scan for PDF files. Non-PDF files are silently
ignored; only the naming convention check produces a warning.
"""
documents: list[PDFDocument] = []
for pdf_path in sorted(docs_dir.glob("*.pdf")):
parsed = parse_pdf_filename(pdf_path)
if parsed is None:
log.warning("Skipping %s — does not match naming convention", pdf_path.name)
continue
doc_type, year = parsed
try:
text = extract_text_from_pdf(pdf_path)
documents.append(PDFDocument(path=pdf_path, doc_type=doc_type, year=year, text=text))
log.info("Loaded %s (%d chars)", pdf_path.name, len(text))
except Exception as exc:
log.error("Failed to read %s: %s", pdf_path.name, exc)
return documents
# ---------------------------------------------------------------------------
# Text cleaning + chunking
# ---------------------------------------------------------------------------
_ARTICLE_RE = re.compile(r"Article\s+\d+[\.\d]*", re.IGNORECASE)
_SECTION_HEAD_RE = re.compile(r"^\s{0,4}(\d+[\.\d]*\s+[A-Z][A-Z\s]{4,})\s*$", re.MULTILINE)
def clean_text(text: str) -> str:
"""Normalise raw PDF text for embedding.
Collapses runs of whitespace and removes hyphenation artefacts introduced
by PDF line-wrapping (``word-\\nnext`` → ``wordnext``). Does not strip
newlines entirely because the section-heading regex relies on line structure.
Args:
text: Raw text as returned by ``extract_text_from_pdf``.
"""
text = re.sub(r"-\n", "", text) # dehyphenate wrapped words
text = re.sub(r"[ \t]{2,}", " ", text) # collapse horizontal whitespace
text = re.sub(r"\n{3,}", "\n\n", text) # collapse blank lines
return text.strip()
def extract_article_reference(text: str) -> str:
"""Find the first FIA article reference in a chunk of text.
Searches for patterns like ``Article 48``, ``Article 48.3``, or
``ARTICLE 28.6`` (case-insensitive). Returns only the first match because
a 512-character chunk rarely spans more than one article, and having a
single authoritative reference is more useful for citation than a list.
Returns an empty string when no reference is found so the field is always
a valid string and never ``None``.
Args:
text: The regulation chunk to search. Typically 512 characters but can
be shorter for the last chunk of a document section.
"""
match = _ARTICLE_RE.search(text)
return " ".join(match.group(0).strip().split()) if match else ""
def extract_section_title(text: str) -> str:
"""Find the nearest section heading inside a chunk of text.
Matches lines that look like numbered section headings in FIA documents:
a number followed by all-caps words, e.g. ``"48 SAFETY CAR PROCEDURE"``.
Returns the first match found, or an empty string when none is present.
This is a best-effort heuristic — not every chunk will have a heading.
Args:
text: The regulation chunk to search.
"""
match = _SECTION_HEAD_RE.search(text)
return match.group(1).strip() if match else ""
def compute_hash(text: str) -> str:
"""Compute a stable SHA-256 hash of a text string for idempotent indexing.
The hash is computed on the UTF-8 encoded text before any further
processing so that two identical passages from different PDFs produce the
same hash and are deduplicated in Qdrant. This prevents the collection
from growing with duplicate content if the same article appears in both
the sporting and technical regulations.
Args:
text: Normalised chunk text. Must be the same string that will be
stored in the payload, otherwise the hash check will miss
already-indexed chunks.
"""
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def iter_chunks(
document: PDFDocument,
chunk_size: int | None = None,
chunk_overlap: int | None = None,
) -> Iterator[TextChunk]:
"""Yield overlapping text chunks from a ``PDFDocument``.
Uses a sliding window over the cleaned document text. The overlap ensures
that regulation sentences which fall at a chunk boundary appear in full in
at least one chunk, preventing the retriever from returning a truncated
article mid-sentence. Each chunk carries its own article reference and
section title extracted by regex so retrieval results are self-contained.
Args:
document: The source document to chunk. Its ``doc_type`` and
``year`` are inherited by every produced chunk.
chunk_size: Window size in characters. Smaller windows give more
precise retrieval but require more Qdrant storage and
more embedding calls; 512 chars is a good default.
chunk_overlap: Number of characters to repeat at the start of each
new window. Must be smaller than ``chunk_size``.
"""
chunk_size = chunk_size or CFG.chunk_size
chunk_overlap = chunk_overlap or CFG.chunk_overlap
text = clean_text(document.text)
start = 0
stride = chunk_size - chunk_overlap
while start < len(text):
end = min(start + chunk_size, len(text))
chunk_text = text[start:end].strip()
if chunk_text:
yield TextChunk(
text=chunk_text,
doc_type=document.doc_type,
year=document.year,
article=extract_article_reference(chunk_text),
section_title=extract_section_title(chunk_text),
chunk_hash=compute_hash(chunk_text),
)
start += stride
# ---------------------------------------------------------------------------
# Qdrant management
# ---------------------------------------------------------------------------
def ensure_collection(client: QdrantClient, name: str, dim: int) -> None:
"""Create the Qdrant collection if it does not already exist.
Uses cosine distance to match the L2-normalised embeddings produced by
``all-MiniLM-L6-v2``. Does nothing if the collection already exists, making
this function safe to call on every script run without risk of wiping the
existing index.
Args:
client: An initialised ``QdrantClient`` pointing to the local storage.
name: Name of the collection to create. Must match ``COLLECTION_NAME``
used in ``retriever.py`` or queries will hit the wrong collection.
dim: Embedding dimension. Must match the output size of the model
used during indexing — mismatches cause silent wrong results.
"""
existing = {c.name for c in client.get_collections().collections}
if name not in existing:
client.create_collection(
collection_name=name,
vectors_config=VectorParams(size=dim, distance=Distance.COSINE),
)
log.info("Created collection '%s' (dim=%d, distance=COSINE)", name, dim)
else:
log.info("Collection '%s' already exists — skipping creation", name)
def get_existing_hashes(client: QdrantClient, name: str) -> set[str]:
"""Retrieve all chunk hashes currently stored in a Qdrant collection.
Used to determine which chunks are new before embedding — skipping already-
indexed chunks avoids redundant embedding calls and prevents duplicates.
Scrolls through the full collection in pages of 1000 to handle large indexes
without loading everything into memory at once.
Args:
client: An initialised ``QdrantClient`` pointing to the local storage.
name: Collection name to scroll. Must exist before calling this function.
"""
hashes: set[str] = set()
offset = None
while True:
results, next_offset = client.scroll(
collection_name=name,
scroll_filter=None,
limit=1000,
offset=offset,
with_payload=["chunk_hash"],
with_vectors=False,
)
for point in results:
h = point.payload.get("chunk_hash")
if h:
hashes.add(h)
if next_offset is None:
break
offset = next_offset
return hashes
# ---------------------------------------------------------------------------
# Embedding + upsert
# ---------------------------------------------------------------------------
def embed_chunks(
chunks: list[TextChunk],
encoder: SentenceTransformer,
) -> np.ndarray:
"""Embed a list of chunks in batches and return the embedding matrix.
Processes chunks in batches of ``EMBED_BATCH_SIZE`` to keep GPU/CPU memory
usage bounded. Normalisation is applied so cosine similarity equals dot
product, consistent with how the retriever queries the collection.
Args:
chunks: The chunks to embed. Their ``text`` field is used as input;
all other fields are preserved separately as Qdrant payload.
encoder: A loaded ``SentenceTransformer`` instance. Passed explicitly
rather than re-loaded here so the caller controls when the
model is loaded (typically once at script startup).
"""
texts = [c.text for c in chunks]
return encoder.encode(
texts,
batch_size=CFG.embed_batch_size,
normalize_embeddings=True,
show_progress_bar=len(texts) > CFG.embed_batch_size,
)
def upsert_chunks(
client: QdrantClient,
name: str,
chunks: list[TextChunk],
embeddings: np.ndarray,
id_offset: int = 0,
) -> int:
"""Upsert a list of chunks and their embeddings into a Qdrant collection.
Each chunk becomes one Qdrant point whose payload mirrors the ``TextChunk``
fields exactly, making the stored payload compatible with the ``RegulationChunk``
dataclass in ``retriever.py`` without any transformation at query time.
Uses upsert (not insert) so re-running the script after a partial failure
overwrites incomplete points rather than creating duplicates.
Args:
client: An initialised ``QdrantClient`` pointing to the local storage.
name: Collection name to upsert into.
chunks: List of ``TextChunk`` objects to store. Order must match
``embeddings`` row order.
embeddings: Float32 matrix of shape ``(len(chunks), EMBEDDING_DIM)``.
id_offset: Integer offset added to the chunk's list index to produce
a unique point ID. Pass the current collection size to avoid
ID collisions when adding new documents incrementally.
Returns:
Number of points successfully upserted.
"""
points = [
PointStruct(
id=id_offset + i,
vector=embeddings[i].tolist(),
payload={
"text": chunk.text,
"doc_type": chunk.doc_type,
"year": chunk.year,
"article": chunk.article,
"section_title": chunk.section_title,
"chunk_hash": chunk.chunk_hash,
},
)
for i, chunk in enumerate(chunks)
]
client.upsert(collection_name=name, points=points)
return len(points)
# ---------------------------------------------------------------------------
# Main orchestrator
# ---------------------------------------------------------------------------
def build_index(
docs_dir: Path | None = None,
qdrant_path: Path | None = None,
force_rebuild: bool = False,
) -> None:
"""Orchestrate the full PDF → Qdrant pipeline.
Loads all PDFs, chunks them, skips already-indexed chunks (unless
``force_rebuild`` is set), embeds the new ones, and upserts them into
Qdrant. Prints a summary at the end with the total number of indexed,
skipped, and failed chunks.
Args:
docs_dir: Directory containing the FIA PDFs. Must exist and contain
at least one file matching the naming convention.
qdrant_path: On-disk Qdrant storage directory. Created automatically
if it does not exist.
force_rebuild: When ``True``, deletes and recreates the collection before
indexing so all chunks are re-embedded from scratch. Use
this when the embedding model changes or the chunking
parameters are modified.
"""
docs_dir = docs_dir or CFG.docs_dir
qdrant_path = qdrant_path or CFG.qdrant_path
if not docs_dir.exists() or not any(docs_dir.glob("*.pdf")):
log.error("No PDFs found in %s — add regulation PDFs and retry", docs_dir)
sys.exit(1)
qdrant_path.mkdir(parents=True, exist_ok=True)
client = QdrantClient(path=str(qdrant_path))
encoder = SentenceTransformer(CFG.embedding_model)
if force_rebuild:
existing = {c.name for c in client.get_collections().collections}
if CFG.collection_name in existing:
client.delete_collection(CFG.collection_name)
log.info("Deleted existing collection '%s' (--force-rebuild)", CFG.collection_name)
ensure_collection(client, CFG.collection_name, CFG.embedding_dim)
existing_hashes = get_existing_hashes(client, CFG.collection_name)
log.info("Existing indexed chunks: %d", len(existing_hashes))
documents = load_pdf_documents(docs_dir)
if not documents:
log.error("No valid PDFs loaded — check naming convention")
sys.exit(1)
all_chunks: list[TextChunk] = []
for doc in documents:
for chunk in iter_chunks(doc):
if chunk.chunk_hash not in existing_hashes:
all_chunks.append(chunk)
skipped = sum(
1 for doc in documents for chunk in iter_chunks(doc) if chunk.chunk_hash in existing_hashes
)
log.info("New chunks to index: %d | skipped (already indexed): %d", len(all_chunks), skipped)
if not all_chunks:
log.info("Nothing to do — index is up to date")
return
log.info("Embedding %d chunks with '%s'...", len(all_chunks), CFG.embedding_model)
embeddings = embed_chunks(all_chunks, encoder)
id_offset = client.get_collection(CFG.collection_name).points_count or 0
n_upserted = upsert_chunks(client, CFG.collection_name, all_chunks, embeddings, id_offset)
total = client.get_collection(CFG.collection_name).points_count or 0
log.info("Done. Upserted: %d | Total in collection: %d", n_upserted, total)
def main() -> None:
"""Parse CLI arguments and run the ingestion pipeline."""
parser = argparse.ArgumentParser(
description="Build the FIA regulation Qdrant index from PDF documents."
)
parser.add_argument(
"--docs-dir",
type=Path,
default=CFG.docs_dir,
help=f"Directory containing FIA PDFs (default: {CFG.docs_dir})",
)
parser.add_argument(
"--force-rebuild",
action="store_true",
help="Delete and recreate the collection before indexing",
)
args = parser.parse_args()
build_index(
docs_dir=args.docs_dir,
force_rebuild=args.force_rebuild,
)
if __name__ == "__main__":
main()