File size: 3,265 Bytes
bc9f3e6 | 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 | #!/usr/bin/env python3
"""
index_docs.py — CLI indexing tool for LocalAiLab Assistant (by LocalAiLab).
Indexes documents into the app's ChromaDB knowledge base without launching the UI.
Usage examples:
# Index a PDF
python index_docs.py --pdf path/to/doc.pdf
# Index a text file
python index_docs.py --txt path/to/notes.txt
# Index a whole folder of PDFs + txts
python index_docs.py --dir ./my_docs
# Load a HuggingFace dataset
python index_docs.py --hf-dataset m-ric/huggingface_doc --text-col text --source-col source
# Show stats
python index_docs.py --stats
# Clear the index
python index_docs.py --clear
"""
import argparse
from pathlib import Path
def main():
parser = argparse.ArgumentParser(description="Index documents into the RAG knowledge base.")
parser.add_argument("--pdf", help="Path to a PDF file to index")
parser.add_argument("--txt", help="Path to a TXT/MD file to index")
parser.add_argument("--docx", help="Path to a DOCX file to index")
parser.add_argument("--dir", help="Directory; indexes all .pdf / .txt / .md / .docx files inside")
parser.add_argument("--hf-dataset", help="HuggingFace dataset name")
parser.add_argument("--text-col", default="text", help="Column containing text (for HF datasets)")
parser.add_argument("--source-col", default="source", help="Column containing source name (optional)")
parser.add_argument("--stats", action="store_true", help="Print index stats and exit")
parser.add_argument("--clear", action="store_true", help="Clear the entire index and exit")
args = parser.parse_args()
# Import after argparse so --help is instant
from backend.models import get_embed_model, get_chroma_collection
from backend.knowledge_base import (
get_index_stats,
index_pdf_file, index_txt_file, index_docx_file, index_hf_dataset, clear_index,
)
# Pre-warm
get_embed_model()
get_chroma_collection()
if args.clear:
# clear_index returns ([], message_string)
_, msg = clear_index()
print(msg)
return
if args.stats:
print(get_index_stats())
return
if args.pdf:
# index_pdf_file returns a plain string
print(index_pdf_file(args.pdf))
if args.txt:
# index_txt_file returns a plain string
print(index_txt_file(args.txt))
if args.docx:
# index_docx_file returns a plain string
print(index_docx_file(args.docx))
if args.dir:
folder = Path(args.dir)
for f in sorted(folder.iterdir()):
if f.suffix.lower() == ".pdf":
print(index_pdf_file(str(f)))
elif f.suffix.lower() in (".txt", ".md"):
print(index_txt_file(str(f)))
elif f.suffix.lower() == ".docx":
print(index_docx_file(str(f)))
if args.hf_dataset:
# index_hf_dataset returns (message_string, table_list)
msg, _ = index_hf_dataset(args.hf_dataset, args.text_col, args.source_col)
print(msg)
print(get_index_stats())
if __name__ == "__main__":
main()
|