0Pricing
AI Agents with LangChain & Autonomous Workflows · Lekcja

Retrievery i kompresja kontekstowa

Przekształć vector store w konfigurowalny retriever, kontroluj liczbę zwracanych dokumentów i używaj kompresji kontekstowej do usuwania nieistotnego tekstu, zanim dotrze on do LLM-a.

Retrievery i kompresja kontekstowa to bezpłatna lekcja AI Agents with LangChain & Autonomous Workflows na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej AI Agents with LangChain & Autonomous Workflows, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs AI Agents with LangChain & Autonomous Workflows zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Retrievery i kompresja kontekstowa” jest bezpłatna?

Tak — pełny tekst „Retrievery i kompresja kontekstowa” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu AI Agents with LangChain & Autonomous Workflows, przejdź na CoddyKit PRO. Kurs AI Agents with LangChain & Autonomous Workflows zawiera 4 lekcji w sumie.

Co nauczysz się w „Retrievery i kompresja kontekstowa”?

Przekształć vector store w konfigurowalny retriever, kontroluj liczbę zwracanych dokumentów i używaj kompresji kontekstowej do usuwania nieistotnego tekstu, zanim dotrze on do LLM-a. Ćwiczysz AI Agents with LangChain & Autonomous Workflows z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć AI Agents with LangChain & Autonomous Workflows?

Nie wymagamy żadnego doświadczenia. AI Agents with LangChain & Autonomous Workflows w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Retrievery i kompresja kontekstowa”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji AI Agents with LangChain & Autonomous Workflows?

Tak. Każda lekcja AI Agents with LangChain & Autonomous Workflows zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wyjaśnienie document loaderów
  2. Text splittery i embeddings
  3. Vector stores na potrzeby wyszukiwania
  4. Retrievery i kompresja kontekstowa
← Powrót do AI Agents with LangChain & Autonomous Workflows