0Pricing
Prompt Engineering & LLM Optimization for Developers · บทเรียน

พื้นฐาน LangChain และ LlamaIndex

เริ่มต้นใช้งานเฟรมเวิร์กทรงพลังอย่าง LangChain และ LlamaIndex เพื่อสร้างแอปพลิเคชัน LLM ที่ซับซ้อนได้ง่ายขึ้น

พื้นฐาน LangChain และ LlamaIndex เป็นบทเรียน Prompt Engineering & LLM Optimization for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Prompt Engineering & LLM Optimization for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Prompt Engineering & LLM Optimization for Developers มีบทเรียนทั้งหมด 4 บทเรียน

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

Why Use LLM Frameworks?

Working directly with Large Language Model (LLM) APIs can be like building with raw LEGO bricks. It's powerful, but complex for bigger projects.

Frameworks like LangChain and LlamaIndex provide pre-built tools and structures. They simplify common tasks, making it much easier to build sophisticated LLM applications.

LangChain: Orchestrating LLMs

LangChain is a framework designed to help you build applications with LLMs by "chaining" together different components.

  • It provides abstractions for working with various LLMs.
  • It enables complex workflows involving multiple steps.
  • Think of it as the glue for your LLM-powered application logic.

Core LangChain Components

LangChain organizes functionality into key modules:

  • LLMs: Wrappers for different LLM providers (e.g., OpenAI, Anthropic).
  • Prompts: Templates to easily construct dynamic prompts.
  • Chains: Sequences of calls to LLMs or other utilities.
  • Agents: LLMs that decide which tools to use and in what order.
  • Memory: For persistent state in conversational applications.

Your First LangChain Idea

Let's see how a simple prompt and a mock LLM can work together. This example shows how LangChain conceptualizes connecting a prompt template to an LLM, even with a simulated model.

from langchain_core.prompts import PromptTemplate
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import BaseMessage, HumanMessage, AIMessage

# A simple mock LLM for demonstration
class MockChatModel(BaseChatModel):
    def _generate(self, messages, stop=None, run_manager=None):
        last_message_content = messages[-1].content
        response = f"Mock response for: '{last_message_content}'"
        return AIMessage(content=response)

    @property
    def _llm_type(self) -> str:
        return "mock_chat_model"

def main():
    # 1. Define a Prompt Template
    template = "Tell me a fun fact about {topic}."
    prompt = PromptTemplate(template=template, input_variables=["topic"])

    # 2. Instantiate our Mock LLM
    llm = MockChatModel()

    # 3. Format the prompt with input
    formatted_prompt = prompt.format(topic="penguins")
    print("--- Formatted Prompt ---")
    print(formatted_prompt)

    # 4. Invoke the LLM (simulated)
    # In a real LangChain app, you'd use a chain, but this shows the core interaction.
    response_message = llm.invoke([HumanMessage(content=formatted_prompt)])
    print("\n--- Mock LLM Response ---")
    print(response_message.content)

if __name__ == "__main__":
    main()

LlamaIndex: Data & LLMs

LlamaIndex is a data framework designed to connect your custom data sources to LLMs. Its primary goal is to make it easy to build applications that can query and understand your own private or domain-specific data.

  • Focuses on data ingestion, indexing, and retrieval.
  • Powers Retrieval Augmented Generation (RAG) applications.
  • Helps LLMs answer questions beyond their training data.

Core LlamaIndex Concepts

LlamaIndex streamlines the RAG pipeline with these components:

  • Documents: Raw data inputs (e.g., text files, PDFs, database records).
  • Nodes: Chunks of text derived from Documents, often with associated metadata.
  • Indexes: Data structures (like vector stores) that organize Nodes for efficient retrieval.
  • Query Engines: Interfaces for querying an index, often combining retrieval and LLM synthesis.

Querying Custom Data (Simulated)

Here's a simplified look at how LlamaIndex connects data to a query. We simulate ingesting a document and then querying it, demonstrating the core retrieval idea.

class Document:
    def __init__(self, text, doc_id=None):
        self.text = text
        self.doc_id = doc_id if doc_id else f"doc_{hash(text)}"

class MockVectorStore:
    def __init__(self):
        self.store = {}
        self.documents = []

    def add(self, docs):
        for doc in docs:
            self.store[doc.doc_id] = doc.text
            self.documents.append(doc)

    def query(self, query_text):
        for doc in self.documents:
            if query_text.lower() in doc.text.lower():
                return [doc.text]
        return []

class MockQueryEngine:
    def __init__(self, vector_store):
        self.vector_store = vector_store

    def query(self, query_text):
        retrieved_texts = self.vector_store.query(query_text)
        if retrieved_texts:
            return f"Based on available info, for '{query_text}': {retrieved_texts[0]}"
        return f"Could not find specific information for '{query_text}'."

def main():
    # 1. Simulate Documents
    documents = [
        Document("The quick brown fox jumps over the lazy dog."),
        Document("Cats love to nap in sunny spots."),
        Document("Python is a popular programming language.")
    ]

    # 2. Simulate building a Vector Store and Index
    mock_store = MockVectorStore()
    mock_store.add(documents)

    # 3. Create a Query Engine
    query_engine = MockQueryEngine(mock_store)

    # 4. Query the data
    response1 = query_engine.query("What is Python?")
    print("--- Query 1 Response ---")
    print(response1)

    response2 = query_engine.query("What about foxes?")
    print("\n--- Query 2 Response ---")
    print(response2)

    response3 = query_engine.query("What is the capital of France?")
    print("\n--- Query 3 Response ---")
    print(response3)

if __name__ == "__main__":
    main()

LangChain vs. LlamaIndex

While both frameworks help with LLMs, their primary focus differs:

  • LangChain: Best for orchestrating complex LLM workflows, building agents, conversational bots, and integrating various tools.
  • LlamaIndex: Ideal for data-intensive LLM applications, especially when you need to connect LLMs to your private or domain-specific data for RAG.

Often, you'll find them complementing each other in real-world applications.

Synergy: LangChain + LlamaIndex

It's common to use LangChain and LlamaIndex together for powerful applications:

  • Use LlamaIndex to ingest, index, and retrieve relevant information from your data sources.
  • Pass the retrieved context to LangChain, which then uses its "chains" or "agents" to process this context with an LLM, refine answers, or interact with other tools.

This combines LlamaIndex's data prowess with LangChain's orchestration capabilities.

Framework Roles

Consider the core strengths of LangChain and LlamaIndex. Which of the following statements accurately describe their primary roles or common use cases?

Lesson Recap

In this lesson, we explored LangChain and LlamaIndex, two powerful frameworks for building LLM applications.

  • LangChain helps orchestrate complex LLM workflows, chains, and agents.
  • LlamaIndex specializes in connecting LLMs to your custom data for efficient retrieval and RAG.
  • These frameworks often work together, with LlamaIndex providing data context to LangChain's reasoning.

Understanding their distinct roles will help you choose the right tool for your next LLM project!

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

บทเรียน “พื้นฐาน LangChain และ LlamaIndex” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พื้นฐาน LangChain และ LlamaIndex” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Prompt Engineering & LLM Optimization for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Prompt Engineering & LLM Optimization for Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐาน LangChain และ LlamaIndex”

เริ่มต้นใช้งานเฟรมเวิร์กทรงพลังอย่าง LangChain และ LlamaIndex เพื่อสร้างแอปพลิเคชัน LLM ที่ซับซ้อนได้ง่ายขึ้น คุณปฏิบัติ Prompt Engineering & LLM Optimization for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Prompt Engineering & LLM Optimization for Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Prompt Engineering & LLM Optimization for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “พื้นฐาน LangChain และ LlamaIndex” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Prompt Engineering & LLM Optimization for Developers นี้ได้ไหม

ได้ บทเรียน Prompt Engineering & LLM Optimization for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การโต้ตอบกับ API ของ LLM (OpenAI, Anthropic)
  2. พื้นฐาน LangChain และ LlamaIndex
  3. การจัดการและการกำหนดเวอร์ชันพรอมต์
  4. พื้นฐานการสร้างแบบเสริมด้วยการค้นคืน (RAG)
← กลับไปที่ Prompt Engineering & LLM Optimization for Developers