55
66import sys
77from pathlib import Path
8- from typing import List
98
109# Add parent directory to path for imports
1110sys .path .insert (0 , str (Path (__file__ ).parent ))
1615
1716class DocumentRAG (BaseRAGExample ):
1817 """RAG example for document processing (PDF, TXT, MD, etc.)."""
19-
18+
2019 def __init__ (self ):
2120 super ().__init__ (
2221 name = "Document" ,
2322 description = "Process and query documents (PDF, TXT, MD, etc.) with LEANN" ,
24- default_index_name = "test_doc_files" # Match original main_cli_example.py default
23+ default_index_name = "test_doc_files" , # Match original main_cli_example.py default
2524 )
26-
25+
2726 def _add_specific_arguments (self , parser ):
2827 """Add document-specific arguments."""
29- doc_group = parser .add_argument_group (' Document Parameters' )
28+ doc_group = parser .add_argument_group (" Document Parameters" )
3029 doc_group .add_argument (
3130 "--data-dir" ,
3231 type = str ,
3332 default = "examples/data" ,
34- help = "Directory containing documents to index (default: examples/data)"
33+ help = "Directory containing documents to index (default: examples/data)" ,
3534 )
3635 doc_group .add_argument (
3736 "--file-types" ,
3837 nargs = "+" ,
3938 default = [".pdf" , ".txt" , ".md" ],
40- help = "File types to process (default: .pdf .txt .md)"
39+ help = "File types to process (default: .pdf .txt .md)" ,
4140 )
4241 doc_group .add_argument (
43- "--chunk-size" ,
44- type = int ,
45- default = 256 ,
46- help = "Text chunk size (default: 256)"
42+ "--chunk-size" , type = int , default = 256 , help = "Text chunk size (default: 256)"
4743 )
4844 doc_group .add_argument (
49- "--chunk-overlap" ,
50- type = int ,
51- default = 128 ,
52- help = "Text chunk overlap (default: 128)"
45+ "--chunk-overlap" , type = int , default = 128 , help = "Text chunk overlap (default: 128)"
5346 )
54-
55- async def load_data (self , args ) -> List [str ]:
47+
48+ async def load_data (self , args ) -> list [str ]:
5649 """Load documents and convert to text chunks."""
5750 print (f"Loading documents from: { args .data_dir } " )
5851 print (f"File types: { args .file_types } " )
59-
52+
6053 # Check if data directory exists
6154 data_path = Path (args .data_dir )
6255 if not data_path .exists ():
6356 raise ValueError (f"Data directory not found: { args .data_dir } " )
64-
57+
6558 # Load documents
6659 documents = SimpleDirectoryReader (
6760 args .data_dir ,
6861 recursive = True ,
6962 encoding = "utf-8" ,
7063 required_exts = args .file_types ,
7164 ).load_data (show_progress = True )
72-
65+
7366 if not documents :
7467 print (f"No documents found in { args .data_dir } with extensions { args .file_types } " )
7568 return []
76-
69+
7770 print (f"Loaded { len (documents )} documents" )
78-
71+
7972 # Convert to text chunks
8073 all_texts = create_text_chunks (
81- documents ,
82- chunk_size = args .chunk_size ,
83- chunk_overlap = args .chunk_overlap
74+ documents , chunk_size = args .chunk_size , chunk_overlap = args .chunk_overlap
8475 )
85-
76+
8677 # Apply max_items limit if specified
8778 if args .max_items > 0 and len (all_texts ) > args .max_items :
8879 print (f"Limiting to { args .max_items } chunks (from { len (all_texts )} )" )
89- all_texts = all_texts [:args .max_items ]
90-
80+ all_texts = all_texts [: args .max_items ]
81+
9182 return all_texts
9283
9384
9485if __name__ == "__main__" :
9586 import asyncio
96-
87+
9788 # Example queries for document RAG
9889 print ("\n 📄 Document RAG Example" )
9990 print ("=" * 50 )
@@ -102,6 +93,6 @@ async def load_data(self, args) -> List[str]:
10293 print ("- 'Summarize the key findings in these papers'" )
10394 print ("- 'What is the storage reduction achieved by LEANN?'" )
10495 print ("\n Or run without --query for interactive mode\n " )
105-
96+
10697 rag = DocumentRAG ()
107- asyncio .run (rag .run ())
98+ asyncio .run (rag .run ())
0 commit comments