การสืบค้นและสร้างคำตอบ
พัฒนาตรรกะสำหรับประมวลผลคำค้นของผู้ใช้ เรียกคืนบริบทที่เกี่ยวข้อง และสังเคราะห์คำตอบด้วย LLM
การสืบค้นและสร้างคำตอบ เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LangChain / RAG / Vector DBs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Querying RAG: The Answer Flow
After integrating RAG components, the next step is to use them to answer user questions. This lesson covers the full process from a user's query to a generated answer.
We'll focus on the 'query-time' logic: how your system takes a question, finds relevant context, and synthesizes a coherent response using an LLM.
Understanding the User Query
A RAG system starts with a user's question, just like a search engine. This raw input is the trigger for the entire process.
- It defines what information needs to be retrieved.
- It guides the LLM on what kind of answer to generate.
No special formatting is typically needed at this initial stage; it's just plain text.
Setting Up Your Retriever
To find relevant documents, you need a retriever. This component knows how to query your vector store. You typically obtain it from your VectorStore instance.
The as_retriever() method creates this component, and you can configure parameters like k (number of top documents to fetch).
from langchain_community.vectorstores import InMemoryVectorStore
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from typing import List
# A simple mock for embeddings
class MockEmbeddings(Embeddings):
def embed_documents(self, texts: List[str]) -> List[List[float]]:
return [[i * 0.1] * 10 for i in range(len(texts))]
def embed_query(self, text: str) -> List[float]:
return [0.5] * 10
def main():
# Create a dummy vector store with some content
embeddings = MockEmbeddings()
docs = [
Document(page_content="The capital of France is Paris.", metadata={"source": "wiki"}),
Document(page_content="Eiffel Tower is in Paris, France.", metadata={"source": "travel"})
]
vectorstore = InMemoryVectorStore.from_documents(docs, embeddings)
# Create a retriever from the vector store
retriever = vectorstore.as_retriever(search_kwargs={"k": 1})
print("Retriever created successfully!")
if __name__ == "__main__":
main()Fetching Contextual Documents
Once you have a retriever, you can invoke it with the user's query. It will perform a similarity search in your vector store and return the most relevant Document objects.
These documents form the context that will be passed to the LLM.
from langchain_community.vectorstores import InMemoryVectorStore
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from typing import List
class MockEmbeddings(Embeddings):
def embed_documents(self, texts: List[str]) -> List[List[float]]:
return [[i * 0.1] * 10 for i in range(len(texts))]
def embed_query(self, text: str) -> List[float]:
return [0.5] * 10
def main():
embeddings = MockEmbeddings()
docs = [
Document(page_content="The capital of France is Paris.", metadata={"source": "wiki"}),
Document(page_content="Eiffel Tower is in Paris, France.", metadata={"source": "travel"}),
Document(page_content="London is the capital of the UK.", metadata={"source": "wiki"})
]
vectorstore = InMemoryVectorStore.from_documents(docs, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
user_query = "What is the capital of France?"
retrieved_docs = retriever.invoke(user_query)
print(f"Retrieved {len(retrieved_docs)} documents:")
for doc in retrieved_docs:
print(f"- {doc.page_content[:50]}...")
if __name__ == "__main__":
main()Preparing Context for the LLM
LLMs usually prefer a single string of text as context. The retrieved Document objects need to be combined into a coherent format.
A common approach is to concatenate their page_content fields, perhaps with separators, and include source metadata if desired.
- Ensures all context fits within the LLM's token window.
- Presents a clean input for the LLM to reason over.
Crafting the RAG Prompt
The prompt template is crucial. It instructs the LLM on how to use the provided context to answer the user's question. It typically includes placeholders for both the context and the question.
A well-designed prompt guides the LLM to be factual and avoid hallucination.
from langchain_core.prompts import ChatPromptTemplate
def main():
# Define a RAG-specific prompt template
rag_prompt = ChatPromptTemplate.from_messages([
("system", "You are an AI assistant for Q&A. Use the context to answer. If you don't know, say that you don't know."),
("human", "Context: {context}\nQuestion: {question}")
])
print("RAG Prompt Template created!")
# Example of how it formats:
# print(rag_prompt.format(context="some info", question="a query"))
if __name__ == "__main__":
main()Connecting the Generation Engine
The final step in generating an answer is to pass the prepared context and the user's question to a Large Language Model. LangChain allows you to easily plug in various LLM providers.
For this example, we'll use a mock LLM to demonstrate the integration without needing an API key.
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import BaseMessage, AIMessage
from typing import List, Any
# A simple mock LLM
class MockChatLLM(BaseChatModel):
def invoke(self, input: Any, config: Any = None) -> BaseMessage:
# Simulate LLM response based on input
if "Paris" in str(input):
return AIMessage(content="Paris is the capital of France.")
elif "London" in str(input):
return AIMessage(content="London is the capital of the UK.")
else:
return AIMessage(content="I don't have enough info to answer.")
async def ainvoke(self, input: Any, config: Any = None) -> BaseMessage:
return self.invoke(input, config) # Simple async pass-through
@property
def _llm_type(self) -> str:
return "mock-chat-llm"
def main():
llm = MockChatLLM()
print("Mock LLM initialized!")
# Example invocation (not part of the RAG chain yet)
response = llm.invoke("Tell me about Paris.")
print(f"LLM Response: {response.content}")
if __name__ == "__main__":
main()Assembling the End-to-End RAG Chain
Now, we combine the retriever, prompt template, and LLM using LangChain Expression Language (LCEL) to create a powerful, flexible RAG chain. This chain handles the entire flow.
We'll use RunnablePassthrough to manage inputs and StrOutputParser to extract the final text answer.
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.documents import Document
from langchain_community.vectorstores import InMemoryVectorStore
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import BaseMessage, AIMessage
from typing import List, Any
# Mock Embeddings
class MockEmbeddings(Embeddings):
def embed_documents(self, texts: List[str]) -> List[List[float]]:
return [[i * 0.1] * 10 for i in range(len(texts))]
def embed_query(self, text: str) -> List[float]:
return [0.5] * 10
# Mock LLM
class MockChatLLM(BaseChatModel):
def invoke(self, input: Any, config: Any = None) -> BaseMessage:
input_str = str(input)
if "Paris" in input_str and "capital of France" in input_str:
return AIMessage(content="Based on context, Paris is the capital of France.")
elif "Eiffel Tower" in input_str and "Paris" in input_str:
return AIMessage(content="The Eiffel Tower is in Paris, France.")
else:
return AIMessage(content="I don't have enough info in the context.")
async def ainvoke(self, input: Any, config: Any = None) -> BaseMessage:
return self.invoke(input, config)
@property
def _llm_type(self) -> str:
return "mock-chat-llm"
def main():
# 1. Setup Retriever
embeddings = MockEmbeddings()
docs = [
Document(page_content="The capital of France is Paris.", metadata={"source": "wiki"}),
Document(page_content="Eiffel Tower is in Paris, France.", metadata={"source": "travel"})
]
vectorstore = InMemoryVectorStore.from_documents(docs, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 1})
# 2. Setup Prompt
rag_prompt = ChatPromptTemplate.from_messages([
("system", "You are an AI assistant. Use the following context to answer: {context}. If you don't know, say 'I don't know.'"),
("human", "Question: {question}")
])
# 3. Setup LLM
llm = MockChatLLM()
# 4. Define how to format retrieved documents
def format_docs(docs: List[Document]) -> str:
return "\n\n".join(doc.page_content for doc in docs)
# 5. Build the RAG chain
rag_chain = (
{"context": retriever | RunnableLambda(format_docs),
"question": RunnablePassthrough()}
| rag_prompt
| llm
| StrOutputParser()
)
print("RAG chain assembled!")
if __name__ == "__main__":
main()Querying Your RAG Application
With the RAG chain fully constructed, you can now invoke it with a user's question. The chain will internally handle retrieval, context formatting, prompting, and LLM generation, returning a direct answer.
This is the final step in getting a response from your RAG system.
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.documents import Document
from langchain_community.vectorstores import InMemoryVectorStore
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import BaseMessage, AIMessage
from typing import List, Any
# Mock Embeddings
class MockEmbeddings(Embeddings):
def embed_documents(self, texts: List[str]) -> List[List[float]]:
return [[i * 0.1] * 10 for i in range(len(texts))]
def embed_query(self, text: str) -> List[float]:
return [0.5] * 10
# Mock LLM
class MockChatLLM(BaseChatModel):
def invoke(self, input: Any, config: Any = None) -> BaseMessage:
input_str = str(input)
if "Paris" in input_str and "capital of France" in input_str:
return AIMessage(content="Based on context, Paris is the capital of France.")
elif "Eiffel Tower" in input_str and "Paris" in input_str:
return AIMessage(content="The Eiffel Tower is in Paris, France.")
else:
return AIMessage(content="I don't have enough info in the context.")
async def ainvoke(self, input: Any, config: Any = None) -> BaseMessage:
return self.invoke(input, config)
@property
def _llm_type(self) -> str:
return "mock-chat-llm"
def main():
# Setup Retriever
embeddings = MockEmbeddings()
docs = [
Document(page_content="The capital of France is Paris.", metadata={"source": "wiki"}),
Document(page_content="Eiffel Tower is in Paris, France.", metadata={"source": "travel"})
]
vectorstore = InMemoryVectorStore.from_documents(docs, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 1})
# Setup Prompt
rag_prompt = ChatPromptTemplate.from_messages([
("system", "You are an AI assistant. Use the following context to answer: {context}. If you don't know, say 'I don't know.'"),
("human", "Question: {question}")
])
# Setup LLM
llm = MockChatLLM()
def format_docs(docs: List[Document]) -> str:
return "\n\n".join(doc.page_content for doc in docs)
# Build the RAG chain
rag_chain = (
{"context": retriever | RunnableLambda(format_docs),
"question": RunnablePassthrough()}
| rag_prompt
| llm
| StrOutputParser()
)
# Invoke the RAG chain with a query
query = "Where is the Eiffel Tower?"
result = rag_chain.invoke(query)
print(f"Query: {query}")
print(f"Answer: {result}")
query_no_context = "What is the capital of Japan?"
result_no_context = rag_chain.invoke(query_no_context)
print(f"\nQuery: {query_no_context}")
print(f"Answer: {result_no_context}")
if __name__ == "__main__":
main()Test Your RAG Flow Knowledge
Consider a LangChain RAG application designed to answer questions from a knowledge base.
Recap: Querying RAG
In this lesson, you learned how to bring all the RAG components together to process user queries and generate answers:
- We transformed a user's question into a query for the retriever.
- We instantiated a retriever from a vector store to fetch relevant documents.
- We crafted a prompt template to guide the LLM.
- We integrated an LLM to synthesize the final answer.
- Finally, we assembled and invoked an end-to-end RAG chain using LangChain Expression Language (LCEL).
You can now build a functional RAG system that delivers grounded, factual answers!
คำถามที่พบบ่อย
บทเรียน “การสืบค้นและสร้างคำตอบ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสืบค้นและสร้างคำตอบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสืบค้นและสร้างคำตอบ”
พัฒนาตรรกะสำหรับประมวลผลคำค้นของผู้ใช้ เรียกคืนบริบทที่เกี่ยวข้อง และสังเคราะห์คำตอบด้วย LLM คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LangChain / RAG / Vector DBs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน LangChain / RAG / Vector DBs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การสืบค้นและสร้างคำตอบ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน LangChain / RAG / Vector DBs นี้ได้ไหม
ได้ บทเรียน LangChain / RAG / Vector DBs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผสานรวมองค์ประกอบ RAG ทั้งหมด
- การสืบค้นและสร้างคำตอบ
- การประเมินประสิทธิภาพระบบ RAG
- การสร้างชุดทดสอบมาตรฐานสำหรับ RAG