*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to create a time-weighted retriever

On this page

How to create a time-weighted retriever
=======================================

Prerequisites

This guide assumes familiarity with the following concepts:

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

This guide covers the [`TimeWeightedVectorStoreRetriever`](https://v02.api.js.langchain.com/classes/langchain_retrievers_time_weighted.TimeWeightedVectorStoreRetriever.html), which uses a combination of semantic similarity and a time decay.

The algorithm for scoring them is:

    semantic_similarity + (1.0 - decay_rate) ^ hours_passed

Notably, `hours_passed` refers to the hours passed since the object in the retriever **was last accessed**, not since it was created. This means that frequently accessed objects remain "fresh."

    let score = (1.0 - this.decayRate) ** hoursPassed + vectorRelevance;

`this.decayRate` is a configurable decimal number between 0 and 1. A lower number means that documents will be "remembered" for longer, while a higher number strongly weights more recently accessed documents.

Note that setting a decay rate of exactly 0 or 1 makes `hoursPassed` irrelevant and makes this retriever equivalent to a standard vector lookup.

It is important to note that due to required metadata, all documents must be added to the backing vector store using the `addDocuments` method on the **retriever**, not the vector store itself.

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

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { TimeWeightedVectorStoreRetriever } from "langchain/retrievers/time_weighted";import { MemoryVectorStore } from "langchain/vectorstores/memory";import { OpenAIEmbeddings } from "@langchain/openai";const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());const retriever = new TimeWeightedVectorStoreRetriever({  vectorStore,  memoryStream: [],  searchKwargs: 2,});const documents = [  "My name is John.",  "My name is Bob.",  "My favourite food is pizza.",  "My favourite food is pasta.",  "My favourite food is sushi.",].map((pageContent) => ({ pageContent, metadata: {} }));// All documents must be added using this method on the retriever (not the vector store!)// so that the correct access history metadata is populatedawait retriever.addDocuments(documents);const results1 = await retriever.invoke("What is my favourite food?");console.log(results1);/*[  Document { pageContent: 'My favourite food is pasta.', metadata: {} }] */const results2 = await retriever.invoke("What is my favourite food?");console.log(results2);/*[  Document { pageContent: 'My favourite food is pasta.', metadata: {} }] */

#### API Reference:

*   [TimeWeightedVectorStoreRetriever](https://v02.api.js.langchain.com/classes/langchain_retrievers_time_weighted.TimeWeightedVectorStoreRetriever.html) from `langchain/retrievers/time_weighted`
*   [MemoryVectorStore](https://v02.api.js.langchain.com/classes/langchain_vectorstores_memory.MemoryVectorStore.html) from `langchain/vectorstores/memory`
*   [OpenAIEmbeddings](https://v02.api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`

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

You've now learned how to use time as a factor when performing retrieval.

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 stream

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

Next

How to use a chat model to call tools

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

*   [Next steps](#next-steps)