AI Hints
Home›RAG Tutorial›Loading Documents
RAG · Lesson 14 of 120

Loading Files

Markdown and text.

⏱ 2 min read▶ Video at 13:35🎓 Free
Watch this lesson in the RAG courseWatch on YouTube ↗Full RAG course

Our help centre pages are markdown files. Loading them is easy. We read each file, and make a document with its text and some metadata.

Loading Files
From the video: Loading Files
  • Markdown files
  • Read each one
  • Make documents

Loading the help centre

We use LangChain's Document class, which holds page content and metadata. For each markdown file, the source is the file name, and the title is the first line, without the hash sign. Run it. Fourteen documents, each with a source and a title.

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

docs = []
for path in sorted(Path("data/rag/northwind").glob("*.md")):
    text = path.read_text()
    title = text.splitlines()[0].lstrip("# ")
    docs.append(Document(page_content=text, metadata={"source": path.name, "title": title}))

print(len(docs), "documents")
for d in docs[:4]:
    print(d.metadata)
Output
14 documents
{'source': 'ebike_battery.md', 'title': 'E-bike battery guide'}
{'source': 'ebike_errors.md', 'title': 'E-bike display error codes'}
{'source': 'finance.md', 'title': 'Paying monthly'}
{'source': 'membership.md', 'title': 'Northwind Riders club'}

Other sources

Real knowledge bases come from many places. Web pages, wikis, PDFs, spreadsheets, and databases. Each needs its own loader, but they all end the same way, as documents with text and metadata.

keyvalue
Web pagesstrip the HTML
PDFspage by page
Spreadsheetsrows as text
All end asDocuments

Summary

Loading means reading each file into a document, with its source and title as metadata. Next, loading PDFs.

Key takeaways
  • Document class
  • page_content
  • metadata
Next up: Loading PDFs — page by page