0Pricing
AI Agents with LangChain & Autonomous Workflows · บทเรียน

อธิบายตัวโหลดเอกสาร

ค้นพบวิธีโหลดข้อมูลจากแหล่งที่หลากหลาย เช่น PDF หน้าเว็บ และฐานข้อมูล ให้อยู่ในรูปแบบที่ LangChain นำไปใช้ได้

อธิบายตัวโหลดเอกสาร เป็นบทเรียน AI Agents with LangChain & Autonomous Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents with LangChain & Autonomous Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Data for Smarter LLMs

Large Language Models (LLMs) are powerful, but their knowledge is limited to their training data. To make AI agents truly useful, they often need access to fresh, external information.

  • Imagine an agent that needs to answer questions about today's news.
  • Or one that summarizes a specific document from your company's internal drive.

This is where data loading comes in.

What are Document Loaders?

Document Loaders are LangChain's way of bringing external data into your agent's workflow. They act as bridges, converting raw data from various sources into a standardized format that LLMs can understand.

Think of them as specialized data connectors. They handle the messy details of reading files, fetching web pages, or querying databases, and then present the data cleanly to LangChain.

The LangChain Document Object

When a loader fetches data, it wraps it into a Document object. This is LangChain's universal container for data.

Each Document typically has two main parts:

  • page_content: The actual text content extracted from the source (e.g., the text from a PDF page, a web article).
  • metadata: A dictionary containing extra information about the document (e.g., source URL, page number, file path, creation date).

Loading from Local Text Files

One of the simplest ways to load data is from a plain text file on your computer. LangChain provides the TextLoader for this purpose.

It takes a file path, reads the content, and creates a Document object. Let's see how it works by creating a temporary file and loading it.

TextLoader in Action

This example creates a temporary text file, writes some content, then uses TextLoader to read it and prints the result.

from langchain_community.document_loaders import TextLoader
import os
import tempfile

if __name__ == "__main__":
    # Create a temporary file
    with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as temp_file:
        temp_file.write("Hello CoddyKit!\nThis is a test document.")
        temp_file_path = temp_file.name

    print(f"Created temp file: {temp_file_path}")

    try:
        # Load the document using TextLoader
        loader = TextLoader(temp_file_path)
        documents = loader.load()

        # Print the content of the loaded document
        for doc in documents:
            print("--- Document Content ---")
            print(doc.page_content)
            print("--- Metadata ---")
            print(doc.metadata)
    finally:
        # Clean up the temporary file
        os.remove(temp_file_path)
        print(f"Cleaned up temp file: {temp_file_path}")

Web Content with WebBaseLoader

What if your data is on the internet? The WebBaseLoader is perfect for fetching content directly from web pages.

It's smart enough to extract the main text content, ignoring navigation, ads, and other irrelevant elements. You just provide the URL!

WebBaseLoader Example

Here's how to fetch content from a simple example web page. Note: This loader typically requires beautifulsoup4 and lxml to be installed.

from langchain_community.document_loaders import WebBaseLoader

if __name__ == "__main__":
    # Define the URL to load
    url = "https://www.google.com/search?q=hello"
    print(f"Loading content from: {url}")

    # Initialize the WebBaseLoader
    loader = WebBaseLoader(url)

    # Load the documents
    documents = loader.load()

    # Print the content of the first loaded document
    if documents:
        print("--- Extracted Content (first 200 chars) ---")
        print(documents[0].page_content[:200] + "...")
        print("--- Metadata ---")
        print(documents[0].metadata)
    else:
        print("No documents loaded.")

PDFs and Structured Documents

Loading data from PDFs is a common requirement. LangChain offers loaders like PyPDFLoader (which uses the pypdf library) to handle these.

These loaders are designed to extract text from complex formats, often preserving some structure. For PDFs, each page might become a separate Document object, with metadata indicating its page number.

There are also loaders for other formats like CSV, JSON, Markdown, and even specific data structures like Notion databases or Confluence pages.

Beyond Files: Database Loaders

Your data might live in databases. LangChain supports various database loaders to connect directly to your data sources:

  • SQL Database Loader: Connects to relational databases (e.g., PostgreSQL, MySQL) to fetch data based on queries.
  • MongoDB Loader: For NoSQL document databases.
  • Elasticsearch Loader: To pull data from Elasticsearch indices.

These loaders allow your agents to dynamically query and retrieve data from your existing data infrastructure.

Quick Check

Which of the following is NOT a primary purpose of a LangChain Document Loader?

Recap: Document Loaders

In this lesson, we explored LangChain's Document Loaders, essential tools for bringing external data into your AI agents.

  • Loaders convert diverse data sources (text files, web pages, PDFs, databases) into LangChain's universal Document object.
  • Each Document contains page_content and metadata.
  • We saw practical examples with TextLoader for local files and WebBaseLoader for web content.

Next, we'll learn how to handle large documents by splitting them into manageable chunks.

คำถามที่พบบ่อย

บทเรียน “อธิบายตัวโหลดเอกสาร” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “อธิบายตัวโหลดเอกสาร” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents with LangChain & Autonomous Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “อธิบายตัวโหลดเอกสาร”

ค้นพบวิธีโหลดข้อมูลจากแหล่งที่หลากหลาย เช่น PDF หน้าเว็บ และฐานข้อมูล ให้อยู่ในรูปแบบที่ LangChain นำไปใช้ได้ คุณปฏิบัติ AI Agents with LangChain & Autonomous Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents with LangChain & Autonomous Workflows หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents with LangChain & Autonomous Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “อธิบายตัวโหลดเอกสาร” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AI Agents with LangChain & Autonomous Workflows นี้ได้ไหม

ได้ บทเรียน AI Agents with LangChain & Autonomous Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. อธิบายตัวโหลดเอกสาร
  2. ตัวแบ่งข้อความและเวกเตอร์ฝังตัว
  3. คลังเวกเตอร์สำหรับการดึงข้อมูล
  4. ตัวดึงข้อมูลและการบีบอัดตามบริบท
← กลับไปที่ AI Agents with LangChain & Autonomous Workflows