AI Hints
Home›LangChain Tutorial›RAG Foundations
LangChain · Lesson 60 of 120

Loading Files

From disk to documents.

⏱ 2 min read▶ Video at 1:07:20🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Riverside Books keeps its policies in a folder of small text files. Shipping, returns, gift cards, the loyalty club, and the shop itself. Let's load them all as documents.

Loading Files
From the video: Loading Files
  • A folder of policies
  • Five small files
  • Load them as documents

Loading a folder

Path glob finds every markdown file in the folder. For each one, we read the text, and make a document, with the file name as its source. Run it. We get five documents, one per file, and we print each source and its first line.

load_files.pypython
from pathlib import Path
from langchain_core.documents import Document

docs = [Document(p.read_text(), metadata={"source": p.name})
        for p in sorted(Path("kb").glob("*.md"))]

print(len(docs), "documents")
for d in docs:
    print(f"{d.metadata['source']:<14} {d.page_content.splitlines()[0]}")
Output
5 documents
gift_cards.md  # Gift cards
loyalty.md     # Riverside Readers loyalty club
returns.md     # Returns and refunds
shipping.md    # Shipping
shop.md        # About the shop
Also uses kb/shipping.md
# Shipping
Standard delivery in the UK takes 3 to 5 working days and costs £2.99.
Delivery is free on orders over £25.
Express delivery arrives the next working day if you order before 2pm, and costs £5.99.
We ship to Ireland and France in 5 to 8 working days for £7.50.
Every order gets a tracking link by email once it leaves our warehouse.
Also uses kb/returns.md
# Returns and refunds
You can return any book within 30 days of delivery for a full refund.
Books must be unread and in the condition you received them.
Damaged or faulty books can be returned at any time, and we pay the return postage.
Refunds go back to the original payment method within 5 working days of us receiving the book.
To start a return, reply to your order confirmation email.
Also uses kb/gift_cards.md
# Gift cards
Riverside Books gift cards come in values from £10 to £100.
Gift cards never expire and can be used online or in our shop.
A gift card can be used across several orders until the balance is spent.
Gift cards cannot be exchanged for cash.
Also uses kb/loyalty.md
# Riverside Readers loyalty club
Joining the Riverside Readers club is free.
Members earn one point for every pound they spend.
100 points can be swapped for a £5 voucher.
Members get early access to signed editions and a birthday discount of 10 percent.
Also uses kb/shop.md
# About the shop
Riverside Books has one physical shop, at 12 Mill Lane, Bath.
The shop is open from 9am to 6pm Monday to Saturday, and 11am to 4pm on Sunday.
We sell new and second-hand books, and run a children's story time every Saturday at 10am.
You can contact us at hello@riverside-books.example or on 01225 000 000.

Document loaders

For other formats, LangChain has many ready made document loaders, in separate packages. There are loaders for PDFs, web pages, Word files, spreadsheets, Notion, Google Drive, and many more. They all return the same thing, a list of documents, so the rest of your pipeline doesn't change.

keyvalue
PDFPyPDFLoader
Web pagesWebBaseLoader
CSVCSVLoader
All returna list of Documents

Summary

Loading files means reading each one into a document, with its source in the metadata. Loaders exist for almost every format. Next, splitting documents into chunks.

Key takeaways
  • Read each file
  • Document per file
  • Loaders for other formats
Next up: Splitting Text — chunks