การโหลดเอกสารหลากหลายประเภท
สำรวจตัวโหลดเอกสารของ LangChain สำหรับ PDF หน้าเว็บ ฐานข้อมูล และอื่น ๆ เพื่อสกัดเนื้อหาสำหรับระบบ RAG
การโหลดเอกสารหลากหลายประเภท เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LangChain / RAG / Vector DBs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Document Loaders: The First Step
Welcome to the world of LangChain! Before an LLM can answer questions about your data, it needs to 'read' it. This is where Document Loaders come in.
Document loaders are tools that help LangChain ingest data from various sources like text files, PDFs, web pages, or databases. They convert raw data into a standardized format called Document objects.
Loading Simple Text Files
The simplest way to load data is from a plain text file. LangChain's TextLoader is perfect for this. It reads the content and wraps it into a Document object.
Try running this example to see how it works:
import os
from langchain_community.document_loaders import TextLoader
# Create a dummy text file for demonstration
file_content = "Hello CoddyKit learners!\nThis is a sample text file."
file_path = "sample.txt"
with open(file_path, "w") as f:
f.write(file_content)
# Initialize the TextLoader with the file path
loader = TextLoader(file_path)
# Load the documents
documents = loader.load()
# Print the content of the first document
if documents:
print(f"Loaded content:\n{documents[0].page_content}")
print(f"Source: {documents[0].metadata.get('source')}")
# Clean up the dummy file
os.remove(file_path)Understanding Document Objects
When a loader processes data, it creates one or more Document objects. These objects are fundamental in LangChain.
page_content: This is the main text extracted from your source.metadata: A dictionary containing additional information like the source file path, page number (for PDFs), URL (for web pages), etc. This metadata is super useful for tracking and filtering!
Loaders standardize diverse data into this consistent format.
Loading PDF Documents
PDFs are common sources of information. LangChain provides the PyPDFLoader to extract text from PDF files. It uses the pypdf library under the hood.
Each page of a PDF typically becomes a separate Document object, with metadata indicating the page number.
from langchain_community.document_loaders import PyPDFLoader
# To use PyPDFLoader, you'd typically have a PDF file.
# For example, let's imagine 'my_report.pdf' exists.
# loader = PyPDFLoader("my_report.pdf")
# documents = loader.load()
print("PyPDFLoader is used to load text from PDF files.")
print("It often creates one Document per PDF page.")
print("Requires 'pypdf' library (pip install pypdf).")Extracting Web Page Content
Need to get content from a website? The WebBaseLoader is your friend! It can fetch HTML content from URLs and extract the main text.
This is extremely useful for RAG systems that need to query up-to-date information from the internet.
from langchain_community.document_loaders import WebBaseLoader
# To use WebBaseLoader, you provide a list of URLs.
# loader = WebBaseLoader(["https://www.example.com"])
# documents = loader.load()
print("WebBaseLoader fetches content from specified URLs.")
print("It's great for pulling information from websites.")
print("Requires 'bs4' and 'requests' (pip install beautifulsoup4 requests).")Loading from Directories
What if you have many files in a folder? The DirectoryLoader can process an entire directory of documents at once!
You can specify a glob pattern to filter for specific file types (e.g., *.txt, *.md). It can also use a specific loader for the files it finds.
import os
import shutil
from langchain_community.document_loaders import DirectoryLoader, TextLoader
# Create a dummy directory and files
dir_path = "temp_docs"
os.makedirs(dir_path, exist_ok=True)
with open(os.path.join(dir_path, "doc1.txt"), "w") as f:
f.write("Content of document 1.")
with open(os.path.join(dir_path, "doc2.txt"), "w") as f:
f.write("Content of document 2.")
# Initialize DirectoryLoader for .txt files
loader = DirectoryLoader(
dir_path, glob="*.txt", loader_cls=TextLoader
)
# Load documents from the directory
documents = loader.load()
print(f"Found {len(documents)} documents in '{dir_path}'.")
for i, doc in enumerate(documents):
print(f"Doc {i+1}: {doc.page_content}")
# Clean up the dummy directory
shutil.rmtree(dir_path)Database and API Loaders
LangChain isn't just for files! It also offers loaders for various databases and APIs:
- SQL Databases: Load data directly from tables using
SQLDatabaseLoader. - NoSQL Databases: Load from MongoDB, Cassandra, and more.
- APIs: Fetch data from REST APIs, Notion, Jira, Confluence, etc.
These loaders allow you to integrate live, structured data into your RAG pipeline, keeping your LLM's knowledge up-to-date.
More Common Document Loaders
LangChain supports an extensive list of document types. Here are a few more popular ones:
- CSVLoader: For comma-separated value files.
- JSONLoader: For structured JSON data.
- MarkdownLoader: For Markdown files, respecting their structure.
- EvernoteLoader, GoogleDriveLoader, S3DirectoryLoader: For cloud storage and productivity apps.
The flexibility of document loaders means you can pull data from almost anywhere!
Choose the Right Loader
You're building a RAG system and need to ingest data from three sources: a local folder with multiple text files, a PDF user manual, and a company's public documentation website. Which LangChain loaders would be most appropriate for each source?
Recap: Loading Diverse Data
In this lesson, you learned about the crucial role of Document Loaders in LangChain. These tools are the first step in bringing your data into an LLM-powered application.
- We explored loaders for text files, PDFs, and web pages.
- You saw how
Documentobjects standardize data withpage_contentandmetadata. - We also touched upon loading from directories, databases, and other diverse sources.
Mastering document loading is key to building powerful RAG systems that can access and understand a wide range of information!
คำถามที่พบบ่อย
บทเรียน “การโหลดเอกสารหลากหลายประเภท” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การโหลดเอกสารหลากหลายประเภท” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การโหลดเอกสารหลากหลายประเภท”
สำรวจตัวโหลดเอกสารของ LangChain สำหรับ PDF หน้าเว็บ ฐานข้อมูล และอื่น ๆ เพื่อสกัดเนื้อหาสำหรับระบบ RAG คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LangChain / RAG / Vector DBs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน LangChain / RAG / Vector DBs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การโหลดเอกสารหลากหลายประเภท” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน LangChain / RAG / Vector DBs นี้ได้ไหม
ได้ บทเรียน LangChain / RAG / Vector DBs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การโหลดเอกสารหลากหลายประเภท
- ทำความเข้าใจกลยุทธ์การแบ่งข้อความ
- การปรับแต่งการแบ่งเอกสาร
- การจัดการข้อมูลกำกับเอกสารและการกรอง