📖 Scenario: You are building a document processing tool using LangChain. You have a document with text and metadata. You want to split the document into smaller chunks but keep the metadata attached to each chunk.
🎯 Goal: Create a Python script that uses LangChain's CharacterTextSplitter to split a document while preserving its metadata in each chunk.
📋 What You'll Learn
Create a Document object with specific text and metadata
Create a CharacterTextSplitter with a chunk size of 10
Use the splitter to split the document into chunks
Ensure each chunk keeps the original metadata
💡 Why This Matters
🌍 Real World
When processing large documents for search or analysis, splitting text into smaller parts while keeping metadata helps maintain context and source information.
💼 Career
This skill is useful for developers working on document processing, search engines, chatbots, or any application that handles large text data with metadata.
Progress0 / 4 steps
1
Create the initial Document with text and metadata
Create a Document object named doc with the text 'Hello world! This is a test document.' and metadata {'source': 'test_source'}.
LangChain
Hint
Use Document(page_content=..., metadata=...) to create the document.
2
Create a CharacterTextSplitter with chunk size 10
Create a CharacterTextSplitter object named splitter with chunk_size=10.
LangChain
Hint
Import CharacterTextSplitter from langchain.text_splitter and set chunk_size=10.
3
Split the document into chunks preserving metadata
Use splitter.split_documents with a list containing doc to create a variable chunks.
LangChain
Hint
Call split_documents on a list with doc inside.
4
Verify each chunk preserves the original metadata
Add a for loop iterating over chunks with variable chunk. Inside the loop, assign chunk.metadata to a variable meta.
LangChain
Hint
Use for chunk in chunks: and inside assign meta = chunk.metadata.
Practice
(1/5)
1. What is the main benefit of preserving metadata when splitting documents in Langchain?
easy
A. It speeds up the splitting process.
B. It removes unnecessary text from documents.
C. It reduces the size of each chunk.
D. It keeps important context linked to each chunk.
Solution
Step 1: Understand what metadata means in this context
Metadata is extra information about the document, like source or tags, that helps keep context.
Step 2: Recognize why preserving metadata is useful
When splitting, keeping metadata ensures each chunk still knows where it came from and what it relates to.
Final Answer:
It keeps important context linked to each chunk. -> Option D
Quick Check:
Metadata preservation = keeps context [OK]
Hint: Metadata keeps context linked to chunks [OK]
Common Mistakes:
Thinking metadata speeds splitting
Confusing metadata with chunk size
Believing metadata removes text
2. Which method in Langchain is used to split documents while preserving metadata?
easy
A. split_documents
B. preserve_metadata
C. chunk_text
D. split_text
Solution
Step 1: Recall the method name for splitting with metadata
The method designed to split documents and keep metadata is called split_documents.
Step 2: Eliminate incorrect options
Other options like preserve_metadata, chunk_text, and split_text are not valid Langchain methods for this purpose.
Final Answer:
split_documents -> Option A
Quick Check:
Method to split with metadata = split_documents [OK]
Hint: Remember method name: split_documents [OK]
Common Mistakes:
Using split_text instead of split_documents
Assuming preserve_metadata is a method
Confusing chunk_text with splitting
3. Given this code snippet, what will be the metadata of the first chunk after splitting?