การผสานรวมองค์ประกอบ RAG ทั้งหมด
ประกอบตัวโหลดเอกสาร เวกเตอร์แทนความหมาย ที่จัดเก็บเวกเตอร์ และ LLM ให้เป็นแอปพลิเคชัน RAG ของ LangChain ที่ทำงานสอดคล้องกัน
การผสานรวมองค์ประกอบ RAG ทั้งหมด เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LangChain / RAG / Vector DBs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to RAG Integration
Welcome to this lesson on integrating all the components of a Retrieval Augmented Generation (RAG) system using LangChain!
So far, you've learned about individual pieces: document loaders, text splitters, embedding models, vector stores, LLMs, and prompts. Now, it's time to bring them all together.
The RAG Pipeline Flow
A RAG application follows a clear pipeline to answer questions using external knowledge. It generally involves these steps:
- Load: Ingest data from various sources.
- Split: Break documents into manageable chunks.
- Embed: Convert text chunks into numerical vectors.
- Store: Save these vectors in a searchable database.
- Retrieve: Find relevant chunks based on a user query.
- Generate: Use an LLM to answer the query, referencing the retrieved context.
Essential LangChain Imports
To build our RAG application, we'll need several key classes from LangChain. These help us manage documents, create embeddings, interact with vector stores, and connect to LLMs.
We'll use components like Chroma for the vector store, OllamaEmbeddings and ChatOllama for local models, and core LangChain Expression Language (LCEL) tools.
Preparing Documents for RAG
The first step in any RAG system is preparing your knowledge base. This involves loading your data and then splitting it into smaller, manageable chunks.
- Loading: Fetching content from files, databases, or web pages.
- Splitting: Breaking large texts into smaller, semantically coherent pieces to fit LLM context windows and improve retrieval accuracy.
For our runnable example, we'll use simple in-memory Document objects for quick setup.
Embedding & Vector Store Setup
Once documents are split, they need to be converted into numerical representations called embeddings. These embeddings capture the semantic meaning of the text.
The embeddings are then stored in a vector store (like Chroma), which is specialized for efficient similarity search. This allows us to quickly find document chunks related to a user's query.
Setting Up the LLM & Retriever
The next crucial steps are setting up our Large Language Model (LLM) and preparing the retriever:
- LLM: We'll initialize an LLM (e.g.,
ChatOllama) that will take the retrieved context and user question to generate an answer. - Retriever: Our vector store is converted into a
retriever. This component's job is to take the user's query, embed it, search the vector store, and return the most relevant document chunks.
Crafting the Prompt Template
A well-designed prompt template is key to guiding the LLM to generate accurate and relevant answers. It instructs the LLM on how to use the provided context.
Our template will clearly define placeholders for the context (retrieved documents) and the user's question, ensuring the LLM focuses on the provided information.
LangChain Expression Language
LangChain Expression Language (LCEL) is a powerful way to compose complex chains from simple components. It uses the | operator to chain runnables together.
RunnableParallel: Allows multiple branches to run concurrently, useful for fetching context and passing the question.RunnablePassthrough: Passes its input through, often used to keep the original question available in a parallel branch.
LCEL makes our RAG pipeline flexible and modular.
Assembling the RAG Chain
Now, let's put it all together. The RAG chain conceptually flows like this:
- The user's question enters the chain.
RunnableParallelsends the question to two places: to the retriever (to get context) and also passes the original question directly.- The retrieved context and the original question are then fed into the prompt template.
- The filled prompt goes to the LLM for generation.
- Finally, an
StrOutputParserextracts the plain text answer from the LLM's response.
Complete RAG Application Example
Try running this complete LangChain RAG application. Make sure you have Ollama installed and models like nomic-embed-text and llama2 pulled (e.g., ollama pull nomic-embed-text).
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.chat_models import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough, RunnableParallel
from langchain_core.output_parsers import StrOutputParser
from langchain_core.documents import Document
# 1. Prepare Documents (simple in-memory for runnable example)
documents = [
Document(page_content="LangChain is a framework for developing applications powered by large language models."),
Document(page_content="It simplifies the process of building complex LLM workflows."),
Document(page_content="Retrieval Augmented Generation (RAG) combines LLMs with external data retrieval."),
Document(page_content="Ollama allows running open-source LLMs locally.")
]
# 2. Generate Embeddings and Store in Vector DB
embeddings = OllamaEmbeddings(model="nomic-embed-text")
vectorstore = Chroma.from_documents(documents=documents, embedding=embeddings)
retriever = vectorstore.as_retriever()
# 3. Initialize LLM
llm = ChatOllama(model="llama2")
# 4. Define Prompt Template
prompt = ChatPromptTemplate.from_template("""
Answer the question based ONLY on the following context:
{context}
Question: {question}
""")
# 5. Assemble the RAG Chain
rag_chain = (
RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
| prompt
| llm
| StrOutputParser()
)
# 6. Invoke the Chain with a question
question = "What is LangChain?"
print(f"Question: {question}")
response = rag_chain.invoke(question)
print(f"Answer: {response}")
question_2 = "What is RAG?"
print(f"\nQuestion: {question_2}")
response_2 = rag_chain.invoke(question_2)
print(f"Answer: {response_2}")RAG Pipeline Component Check
Which of the following components are typically part of a LangChain RAG pipeline for answering user questions?
RAG Integration Recap
Fantastic work! You've successfully learned how to integrate all the core components into a cohesive LangChain RAG application.
- We walked through the full RAG pipeline from loading documents to generating answers.
- You saw how LangChain's modular components and LCEL allow for flexible and powerful chain construction.
- You now have a runnable example demonstrating a complete RAG system.
Next, we'll explore how to query your RAG system effectively and generate high-quality answers.
เรียนรู้ LangChain / RAG / Vector DBs ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การผสานรวมองค์ประกอบ RAG ทั้งหมด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การผสานรวมองค์ประกอบ RAG ทั้งหมด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การผสานรวมองค์ประกอบ RAG ทั้งหมด”
ประกอบตัวโหลดเอกสาร เวกเตอร์แทนความหมาย ที่จัดเก็บเวกเตอร์ และ LLM ให้เป็นแอปพลิเคชัน RAG ของ LangChain ที่ทำงานสอดคล้องกัน คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LangChain / RAG / Vector DBs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน LangChain / RAG / Vector DBs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การผสานรวมองค์ประกอบ RAG ทั้งหมด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน LangChain / RAG / Vector DBs นี้ได้ไหม
ได้ บทเรียน LangChain / RAG / Vector DBs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผสานรวมองค์ประกอบ RAG ทั้งหมด
- การสืบค้นและสร้างคำตอบ
- การประเมินประสิทธิภาพระบบ RAG
- การสร้างชุดทดสอบมาตรฐานสำหรับ RAG