> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 时间加权集成

> 使用 LangChain JavaScript 集成 Time-weighted 检索器。

时间加权检索器是一种在考虑相似度的同时，还会考虑时效性的检索器。其评分算法如下：

```typescript theme={null}
let score = (1.0 - this.decayRate) ** hoursPassed + vectorRelevance;
```

值得注意的是，上述`hoursPassed`指的是检索器中对象最后一次被访问的时间，而不是其创建时间。这意味着经常被访问的对象会保持"新鲜"状态，并获得更高的分数。

`this.decayRate`是一个可配置的介于0和1之间的十进制数。数值越小，文档会被"记住"的时间越长；数值越大，则更侧重于最近被访问的文档。

请注意，将衰减率设置为0或1会使`hoursPassed`变得无关紧要，并使这种检索器等同于标准的向量查找。

## 使用方法

本示例展示了如何使用向量存储初始化一个`TimeWeightedVectorStoreRetriever`。
需要注意的是，由于需要元数据，所有文档都必须通过**检索器**上的`addDocuments`方法添加到后端向量存储中，而不是直接通过向量存储本身添加。

```typescript theme={null}
import { TimeWeightedVectorStoreRetriever } from "@langchain/classic/retrievers/time_weighted";
import { MemoryVectorStore } from "@langchain/classic/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 populated
await 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: {} }
]
 */
```

## 相关

* [检索指南](https://langchain-docs-zh.mintlify.site/langchain/oss/deepagents/retrieval.md)

***

> 官方原文：[src/oss/javascript/integrations/retrievers/time-weighted-retriever.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/javascript/integrations/retrievers/time-weighted-retriever.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.