LangChain / RAG / Vector DBs · บทเรียน

การพัฒนาตัวโหลดเอกสารแบบกำหนดเอง

สร้างตัวโหลดเอกสารเฉพาะทางเพื่อรับข้อมูลจากแหล่งข้อมูลที่มีลักษณะเฉพาะหรือเป็นกรรมสิทธิ์ ซึ่ง LangChain ยังไม่รองรับโดยตรง

บทเรียน 1 จาก 411 ขั้นตอน

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

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

Why Custom Document Loaders?

LangChain offers many built-in document loaders for common formats like PDFs, web pages, and databases. But what if your data is unique?

Sometimes, you'll encounter:

  • Proprietary file formats
  • Internal APIs or data sources
  • Complex data structures needing custom parsing

This is where custom document loaders shine!

Meet LangChain's BaseLoader

To create your own loader, you'll inherit from LangChain's BaseLoader class. This is an abstract class, meaning it provides a template for what your loader needs to do.

The most important method you'll implement is load(). This method is responsible for fetching your data and transforming it into a list of Document objects.

The LangChain Document Object

All data processed by LangChain, especially for RAG, is standardized into Document objects. Each Document has two main parts:

  • page_content: The actual text content.
  • metadata: A dictionary of key-value pairs describing the document (e.g., source file, page number, author).

Your custom loader's job is to create these Document objects from your unique data.

Basic Custom Loader Structure

Let's start with a very simple custom loader that just returns a fixed text string as a document. This shows the basic structure of inheriting from BaseLoader and implementing load().

from langchain_core.documents import Document
from langchain_core.document_loaders import BaseLoader

class MySimpleTextLoader(BaseLoader):
    def load(self):
        content = "This is a custom text document from MySimpleTextLoader."
        doc = Document(page_content=content)
        return [doc]

# Example usage:
if __name__ == "__main__":
    loader = MySimpleTextLoader()
    documents = loader.load()
    for doc in documents:
        print(f"Content: {doc.page_content}")
        print(f"Metadata: {doc.metadata}")

Making Your Loader Dynamic

A fixed string loader isn't very useful! Real-world loaders need to take parameters, like a file path, a URL, or API credentials.

You can achieve this by adding an __init__ method to your custom loader class. This allows you to pass arguments when you create an instance of your loader.

Loading a 'Custom' Log File

Imagine you have a simple application log file (app.log) where each line is an event. Let's create a custom loader to read this file, treating each line as a separate document.

We'll create a dummy app.log file content directly in the code for simplicity.

from langchain_core.documents import Document
from langchain_core.document_loaders import BaseLoader

# Simulate a log file content
log_file_content = (
    "[INFO] User logged in: user123\n"
    "[ERROR] Database connection failed\n"
    "[DEBUG] Processing request for /api/data\n"
    "[INFO] Data retrieved successfully"
)

class CustomLogLoader(BaseLoader):
    def __init__(self, log_data: str):
        self.log_data = log_data.split('\n')

    def load(self):
        documents = []
        for line in self.log_data:
            if line.strip(): # Avoid empty lines
                doc = Document(page_content=line)
                documents.append(doc)
        return documents

# Example usage:
if __name__ == "__main__":
    loader = CustomLogLoader(log_file_content)
    documents = loader.load()
    for i, doc in enumerate(documents):
        print(f"Doc {i+1}: {doc.page_content[:40]}...")

Adding Rich Metadata

Metadata is incredibly useful! It helps the LLM understand the context of the text and can be used for filtering or improving retrieval. For our log file example, knowing the original log line number or the source file could be very helpful.

You can add any relevant information as key-value pairs to the metadata dictionary of a Document.

Log Loader with Metadata

Let's enhance our CustomLogLoader to include metadata like the original source and the line number for each log entry. This makes the retrieved information much richer!

from langchain_core.documents import Document
from langchain_core.document_loaders import BaseLoader

# Simulate a log file content
log_file_content = (
    "[INFO] User logged in: user123\n"
    "[ERROR] Database connection failed\n"
    "[DEBUG] Processing request for /api/data\n"
    "[INFO] Data retrieved successfully"
)

class CustomLogLoaderWithMeta(BaseLoader):
    def __init__(self, log_data: str, source_name: str = "app.log"):
        self.log_data = log_data.split('\n')
        self.source_name = source_name

    def load(self):
        documents = []
        for i, line in enumerate(self.log_data):
            if line.strip():
                metadata = {
                    "source": self.source_name,
                    "line_number": i + 1
                }
                doc = Document(page_content=line, metadata=metadata)
                documents.append(doc)
        return documents

# Example usage:
if __name__ == "__main__":
    loader = CustomLogLoaderWithMeta(log_file_content, "my_custom_app_logs")
    documents = loader.load()
    for i, doc in enumerate(documents):
        print(f"Doc {i+1}:")
        print(f"  Content: {doc.page_content[:40]}...")
        print(f"  Metadata: {doc.metadata}")

Integrating Custom Documents

Once your custom loader produces Document objects, they behave just like documents loaded by any other LangChain loader.

You can then pass them into subsequent steps of your RAG pipeline:

  • Text Splitting: Break large documents into smaller chunks.
  • Embeddings: Convert text chunks into numerical vectors.
  • Vector Stores: Store these embeddings for efficient similarity search.

Your custom data is now ready for advanced LLM applications!

Quick Check: Custom Loaders

You're building a custom document loader for LangChain. Which of the following statements is TRUE about the Document object you must return?

Recap: Custom Document Loaders

Great job! You've learned how to develop custom document loaders in LangChain.

  • You inherit from BaseLoader and implement the load() method.
  • Your loader converts unique data into a list of Document objects.
  • Document objects contain page_content and a flexible metadata dictionary.
  • Custom loaders are essential for integrating proprietary data sources into your RAG applications.

Next, we'll explore how to integrate custom embedding models!

เริ่มต้นได้ฟรี

เรียนรู้ LangChain / RAG / Vector DBs ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

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

บทเรียน “การพัฒนาตัวโหลดเอกสารแบบกำหนดเอง” ฟรีหรือไม่

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

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

สร้างตัวโหลดเอกสารเฉพาะทางเพื่อรับข้อมูลจากแหล่งข้อมูลที่มีลักษณะเฉพาะหรือเป็นกรรมสิทธิ์ ซึ่ง LangChain ยังไม่รองรับโดยตรง คุณปฏิบัติ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การพัฒนาตัวโหลดเอกสารแบบกำหนดเอง
  2. การผสานรวมโมเดลการฝังแบบกำหนดเอง
  3. การขยายสายโซ่การค้นคืนด้วยตรรกะแบบกำหนดเอง
  4. การสร้างตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเอง
← กลับไปที่ LangChain / RAG / Vector DBs