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

ตัวดึงข้อมูลและการบีบอัดตามบริบท

เปลี่ยนคลังเวกเตอร์ให้เป็นตัวดึงข้อมูลที่ปรับแต่งได้ ควบคุมจำนวนเอกสารที่ส่งกลับ และใช้การบีบอัดตามบริบทเพื่อตัดข้อความที่ไม่เกี่ยวข้องก่อนส่งต่อไปยัง LLM

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

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

From Vector Store to Retriever

A vector store knows how to search, but agents talk to a retriever — a thin interface with one job: given a query, return relevant documents.

Any vector store exposes as_retriever() to produce one.

retriever = vectorstore.as_retriever()
docs = retriever.invoke('How do I reset my password?')

Controlling k

The k parameter sets how many documents to return. Too few misses context; too many wastes tokens and adds noise.

retriever = vectorstore.as_retriever(
    search_kwargs={'k': 4}
)

Similarity Score Thresholds

Instead of a fixed count, you can return only documents above a relevance score. This avoids forcing irrelevant chunks when nothing good matches.

retriever = vectorstore.as_retriever(
    search_type='similarity_score_threshold',
    search_kwargs={'score_threshold': 0.7}
)

Maximal Marginal Relevance

MMR balances relevance with diversity, avoiding near-duplicate chunks. It is great when documents repeat similar text.

retriever = vectorstore.as_retriever(
    search_type='mmr',
    search_kwargs={'k': 4, 'fetch_k': 20}
)

Metadata Filtering

Documents carry metadata (source, date, category). You can filter retrieval to a subset, e.g. only the current product version.

retriever = vectorstore.as_retriever(
    search_kwargs={'filter': {'version': 'v2'}}
)

The Noise Problem

Even relevant chunks often contain unrelated sentences. Sending that noise to the LLM dilutes the answer and burns tokens.

Contextual compression shrinks each retrieved document to only the parts that matter for the query.

ContextualCompressionRetriever

This wrapper sits in front of a base retriever and post-processes its results with a compressor.

from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor

compressor = LLMChainExtractor.from_llm(llm)
compression_retriever = ContextualCompressionRetriever(
    base_compressor=compressor,
    base_retriever=retriever
)

How Extraction Works

LLMChainExtractor asks the LLM to pull only the sentences from each document that are relevant to the query, discarding the rest before it reaches the final prompt.

docs = compression_retriever.invoke(
    'What is the refund window?'
)

Cheaper Filters

LLM extraction costs tokens. EmbeddingsFilter is a faster, cheaper alternative that drops documents below a similarity threshold using embeddings only — no extra LLM call.

from langchain.retrievers.document_compressors import EmbeddingsFilter

compressor = EmbeddingsFilter(
    embeddings=embeddings,
    similarity_threshold=0.76
)

Chaining Compressors

Combine steps in a DocumentCompressorPipeline: first a cheap embeddings filter, then LLM extraction on what survives. This keeps quality high while controlling cost.

from langchain.retrievers.document_compressors import DocumentCompressorPipeline

pipeline = DocumentCompressorPipeline(
    transformers=[embeddings_filter, extractor]
)

Plugging Into RAG

Because a compression retriever has the same interface as any retriever, you swap it into your RAG chain without changing the rest of the pipeline. Cleaner context usually means better, cheaper answers.

Quick Check

Test your retriever knowledge.

Recap

You learned to tune and refine retrieval:

  • Convert a store with as_retriever() and tune k
  • Use score thresholds, MMR, and metadata filters
  • Contextual compression removes noisy text
  • EmbeddingsFilter is a cheap alternative to LLM extraction
  • Chain compressors for quality plus efficiency

Better retrieval is often the biggest lever for RAG quality.

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

บทเรียน “ตัวดึงข้อมูลและการบีบอัดตามบริบท” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ตัวดึงข้อมูลและการบีบอัดตามบริบท”

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

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

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