*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to reduce retrieval latency

On this page

How to reduce retrieval latency
===============================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [Retrievers](/v0.2/docs/concepts/#retrievers)
*   [Embeddings](/v0.2/docs/concepts/#embedding-models)
*   [Vector stores](/v0.2/docs/concepts/#vectorstores)
*   [Retrieval-augmented generation (RAG)](/v0.2/docs/tutorials/rag)

One way to reduce retrieval latency is through a technique called "Adaptive Retrieval". The [`MatryoshkaRetriever`](https://v02.api.js.langchain.com/classes/langchain_retrievers_matryoshka_retriever.MatryoshkaRetriever.html) uses the Matryoshka Representation Learning (MRL) technique to retrieve documents for a given query in two steps:

*   **First-pass**: Uses a lower dimensional sub-vector from the MRL embedding for an initial, fast, but less accurate search.
    
*   **Second-pass**: Re-ranks the top results from the first pass using the full, high-dimensional embedding for higher accuracy.
    

![Matryoshka Retriever](/v0.2/assets/images/adaptive_retrieval-2abb9f6f280c11a424ae6978d39eb011.png)

It is based on this [Supabase](https://supabase.com/) blog post ["Matryoshka embeddings: faster OpenAI vector search using Adaptive Retrieval"](https://supabase.com/blog/matryoshka-embeddings).

### Setup[​](#setup "Direct link to Setup")

tip

See [this section for general instructions on installing integration packages](/v0.2/docs/how_to/installation#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai @langchain/community

    yarn add @langchain/openai @langchain/community

    pnpm add @langchain/openai @langchain/community

To follow the example below, you need an OpenAI API key:

    export OPENAI_API_KEY=your-api-key

We'll also be using `chroma` for our vector store. Follow the instructions [here](/v0.2/docs/integrations/vectorstores/chroma) to setup.

    import { MatryoshkaRetriever } from "langchain/retrievers/matryoshka_retriever";import { Chroma } from "@langchain/community/vectorstores/chroma";import { OpenAIEmbeddings } from "@langchain/openai";import { Document } from "@langchain/core/documents";import { faker } from "@faker-js/faker";const smallEmbeddings = new OpenAIEmbeddings({  model: "text-embedding-3-small",  dimensions: 512, // Min number for small});const largeEmbeddings = new OpenAIEmbeddings({  model: "text-embedding-3-large",  dimensions: 3072, // Max number for large});const vectorStore = new Chroma(smallEmbeddings, {  numDimensions: 512,});const retriever = new MatryoshkaRetriever({  vectorStore,  largeEmbeddingModel: largeEmbeddings,  largeK: 5,});const irrelevantDocs = Array.from({ length: 250 }).map(  () =>    new Document({      pageContent: faker.lorem.word(7), // Similar length to the relevant docs    }));const relevantDocs = [  new Document({    pageContent: "LangChain is an open source github repo",  }),  new Document({    pageContent: "There are JS and PY versions of the LangChain github repos",  }),  new Document({    pageContent: "LangGraph is a new open source library by the LangChain team",  }),  new Document({    pageContent: "LangChain announced GA of LangSmith last week!",  }),  new Document({    pageContent: "I heart LangChain",  }),];const allDocs = [...irrelevantDocs, ...relevantDocs];/** * IMPORTANT: * The `addDocuments` method on `MatryoshkaRetriever` will * generate the small AND large embeddings for all documents. */await retriever.addDocuments(allDocs);const query = "What is LangChain?";const results = await retriever.invoke(query);console.log(results.map(({ pageContent }) => pageContent).join("\n"));/**  I heart LangChain  LangGraph is a new open source library by the LangChain team  LangChain is an open source github repo  LangChain announced GA of LangSmith last week!  There are JS and PY versions of the LangChain github repos*/

#### API Reference:

*   [MatryoshkaRetriever](https://v02.api.js.langchain.com/classes/langchain_retrievers_matryoshka_retriever.MatryoshkaRetriever.html) from `langchain/retrievers/matryoshka_retriever`
*   [Chroma](https://v02.api.js.langchain.com/classes/langchain_community_vectorstores_chroma.Chroma.html) from `@langchain/community/vectorstores/chroma`
*   [OpenAIEmbeddings](https://v02.api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`
*   [Document](https://v02.api.js.langchain.com/classes/langchain_core_documents.Document.html) from `@langchain/core/documents`

note

Due to the constraints of some vector stores, the large embedding metadata field is stringified (`JSON.stringify`) before being stored. This means that the metadata field will need to be parsed (`JSON.parse`) when retrieved from the vector store.

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You've now learned a technique that can help speed up your retrieval queries.

Next, check out the [broader tutorial on RAG](/v0.2/docs/tutorials/rag), or this section to learn how to [create your own custom retriever over any data source](/v0.2/docs/how_to/custom_retriever/).

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to recursively split text by characters

](/v0.2/docs/how_to/recursive_text_splitter)[

Next

How to route execution within a chain

](/v0.2/docs/how_to/routing)

*   [Setup](#setup)
*   [Next steps](#next-steps)