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

Documents

Text plus metadata.

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

In LangChain, every piece of text is a document. A document has two parts. The page content, which is the text, and metadata, a dictionary of extra facts, like where the text came from.

Documents
From the video: Documents
  • Everything is a Document
  • page_content
  • metadata

Making documents

We create two documents by hand. Each has some text, and metadata with a source and a topic. Run it. We can read the text, and the metadata, separately. Metadata is how we'll later cite sources, and filter searches by topic.

documents.pypython
from langchain_core.documents import Document

docs = [
    Document("You can return any book within 30 days.",
             metadata={"source": "returns.md", "topic": "returns"}),
    Document("Gift cards never expire.",
             metadata={"source": "gift_cards.md", "topic": "gifts"}),
]
for d in docs:
    print(d.metadata["source"], "|", d.page_content)
Output
returns.md | You can return any book within 30 days.
gift_cards.md | Gift cards never expire.

Useful metadata

Good metadata pays off later. Store the source file or web address, so answers can cite it. A title or section heading. A date, so you can prefer recent documents. And a category, so you can search within one topic.

keyvalue
sourcefor citations
title / sectioncontext
dateprefer recent
categoryfilter searches

Summary

A document is text plus metadata. Metadata powers citations and filters later. Next, loading documents from files.

Key takeaways
  • Document(text, metadata)
  • page_content
  • metadata dict
Next up: Loading Files — from disk to documents