-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_vector_db.py
More file actions
88 lines (63 loc) · 2.73 KB
/
Copy pathcreate_vector_db.py
File metadata and controls
88 lines (63 loc) · 2.73 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
"""Utility script to build a vector database (embeddings + FAISS index)
from the documents stored in *docs* directory.
It re-uses the existing ingestion & retrieval utilities from the
``hackrx_llm`` package and simply wraps them in a small CLI so that you can
run it independently of the Typer CLI defined in ``hackrx_llm.cli``.
Usage
-----
# Build index from default documents folder and store under backend_index/
python create_vector_db.py
# Build index from a custom docs dir and output location
python create_vector_db.py --docs ./my_docs --index ./vector_store/my_index
Two files will be produced at the *index* prefix location:
* ``<index>.faiss`` – the FAISS index with L2-normalised embedding vectors
* ``<index>.meta.pkl`` – pickled list[Clause] containing texts + metadata
"""
from __future__ import annotations
import argparse
from pathlib import Path
from hackrx_llm.ingestion import ingest_dir
from hackrx_llm.retriever import Retriever
DEFAULT_DOCS_DIR = Path("documents") # relative to repo root
DEFAULT_INDEX_PREFIX = Path("backend_index/store") # will create parent dirs if missing
def build_vector_db(docs_path: Path, index_prefix: Path):
"""Ingest *docs_path* directory and save vector DB to *index_prefix*.
The *index_prefix* is the path *without* extension. The function will
create ``<prefix>.faiss`` and ``<prefix>.meta.pkl`` under the same parent
directory.
"""
docs_path = docs_path.expanduser().resolve()
index_prefix = index_prefix.expanduser().with_suffix("") # ensure no ext
if not docs_path.is_dir():
raise FileNotFoundError(f"Documents directory not found: {docs_path}")
# Ensure output directory exists
index_prefix.parent.mkdir(parents=True, exist_ok=True)
print(f"[1/3] Loading documents from {docs_path} …")
clauses = ingest_dir(docs_path)
print(f" → Loaded {len(clauses)} clauses")
print("[2/3] Building embeddings & FAISS index …")
retriever = Retriever()
retriever.fit(clauses)
print(f"[3/3] Saving index to {index_prefix}.* …")
retriever.save(index_prefix)
print("✓ Vector database created successfully.")
def parse_args():
parser = argparse.ArgumentParser(description="Create vector DB (FAISS) from documents")
parser.add_argument(
"--docs",
type=Path,
default=DEFAULT_DOCS_DIR,
help="Path to documents directory (default: ./documents)",
)
parser.add_argument(
"--index",
type=Path,
default=DEFAULT_INDEX_PREFIX,
help="Output index *prefix* (default: ./backend_index/store)",
)
return parser.parse_args()
def main():
args = parse_args()
build_vector_db(args.docs, args.index)
if __name__ == "__main__":
main()