Python / Python Modern Generative AI and Agents Interview Questions
What document loaders does LangChain provide, and how do you handle different file types in a RAG pipeline?
A RAG system is only as good as the documents it can ingest. LangChain provides over 100 document loaders for web pages, PDFs, Word files, databases, code repositories, spreadsheets, and cloud storage. Every loader returns a list of Document objects with page_content (the text) and metadata (source, page number, etc.).
from langchain_community.document_loaders import ( PyPDFLoader, UnstructuredWordDocumentLoader, WebBaseLoader, CSVLoader, DirectoryLoader, GitLoader, ) # ââ PDF (page-by-page) pdf_loader = PyPDFLoader('report.pdf') pdf_docs = pdf_loader.load() # list of Document, one per page print(pdf_docs[0].page_content[:200]) print(pdf_docs[0].metadata) # {'source': 'report.pdf', 'page': 0} # ââ Web page web_loader = WebBaseLoader( web_paths=['https://lilianweng.github.io/posts/2023-06-23-agent/'], bs_kwargs={'features': 'html.parser'}, ) web_docs = web_loader.load() # ââ CSV with custom column for content csv_loader = CSVLoader( file_path='products.csv', content_columns=['description'], metadata_columns=['id', 'category'], ) csv_docs = csv_loader.load() # ââ Load an entire directory (auto-detect file types) dir_loader = DirectoryLoader( './docs', glob='**/*.pdf', # only PDF files loader_cls=PyPDFLoader, show_progress=True, use_multithreading=True, ) all_docs = dir_loader.load() # ââ Code repository git_loader = GitLoader( repo_path='/local/path/to/repo', branch='main', file_filter=lambda path: path.endswith('.py'), ) code_docs = git_loader.load() # After loading, split all docs the same way regardless of source from langchain_text_splitters import RecursiveCharacterTextSplitter splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) chunks = splitter.split_documents(all_docs) print(f'Total chunks: {len(chunks)}')
More Related questions...