liveBook · Manning

Similarity search

As explained in Transformers in Action

Retrieval is the process of finding and returning relevant information from a collection or database in response to a query.

Similarity search finds relevant passages by comparing representations of a user query with stored document representations. In retrieval-augmented generation (RAG), a retriever selects passages from a vector database; a language model then uses those passages as context for its answer. This supplies information at inference time without changing the model’s weights. section 5.4 section 5.4.3

A typical retrieval path has four stages:

  • Chunk documents, encode them with an embedding model, and store the resulting vectors in a vector database. section 5.4.3
  • Encode the incoming query and retrieve top-matching passages based on similarity. section 5.4.3 section 3.4
  • Optionally rerank or compress the retrieved passages before passing the selected context and original query to the generator. section 5.4.3

Retrieval quality depends on more than the search step. The embedding model must represent semantic meaning well, while chunk size, the number of results retrieved (top-k), and metadata affect what can be found. Exact match, hybrid BM25-plus-vector retrieval, and rerankers are design options. Poor chunking, missing metadata, or ineffective reranking can leave the generator with inadequate context. section 5.4.3

Figure 5.2 Overview of a RAG system. In the preproduction phase, documents from a knowledge base are chunked and embedded into a vector database. During the production phase, a user query is embedded and used to retrieve top-matching documents based on similarity. An optional reranker refines these results to improve relevance. The final ranked context is then passed, together with the original query, to the language model for response generation.
figure

Follow the diagram from document chunking and embedding through query-time matching, optional reranking, and response generation. section 5.4.3

Listing 5.24 Adding a reranker to your RAG system with Flashrank
compressor = FlashrankRerank() #1

compression_retriever = ContextualCompressionRetriever( #2
                base_compressor=compressor,
                base_retriever=retriever
                )

compressed_docs = compression_retriever.invoke( #3
            "What was Amazon's net income in 2023?"
            )
print([doc.metadata["id"] for doc in compressed_docs])

chain = RetrievalQA.from_chain_type( #4
              llm=llm,
              retriever=compression_retriever
              )

chain.invoke("What was Amazon's net income in 2023?") #5

The Flashrank example wraps an existing retriever in a contextual-compression retriever, checks the returned document IDs for a financial question, and supplies that retriever to a RetrievalQA chain. section 5.4.3

How Transformers in Action covers it

How deep it goes
Traces document indexing, query matching, and optional reranking, then shows how to attach a reranker to a retrieval chain.
Examples
Illustrates a document-to-answer pipeline and a Flashrank retrieval chain queried about Amazon’s 2023 net income.
What it assumes
Introduces the roles of embeddings, retrievers, and vector databases while assuming basic familiarity with language-model queries and responses.
What you can do after
Apply it via Flashrank and LangChain’s ContextualCompressionRetriever to rerank retrieved documents before generation.
How it connects
Links embedding models and vector databases to retrievers, rerankers, and retrieval-augmented generation.

Also explained in

Compare how 22 books explain similarity search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.