> ## 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.

# InMemoryStore集成

> 使用 LangChain JavaScript 集成 InMemoryStore 存储。

这将帮助您开始使用 [InMemoryStore](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/stores.md)。有关所有 `InMemoryStore` 功能和配置的详细文档，请访问 [API reference](https://reference.langchain.com/javascript/langchain-core/stores/InMemoryStore)。

`InMemoryStore` 允许为存储中的值分配通用类型。我们将分配类型 `BaseMessage` 作为我们的值类型，遵循聊天历史存储的主题。

## 概述

### 集成细节

| 类 | 包 | 本地 | [PY 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/stores/in_memory.md) | 下载 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`InMemoryStore`](https://reference.langchain.com/javascript/langchain-core/stores/InMemoryStore) | [`@langchain/core`](https://www.npmjs.com/package/@langchain/core) | ✅ | ✅ | ![NPM - 下载](https://img.shields.io/npm/dm/@langchain/core?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/core?style=flat-square\&label=%20&) |

## 设置

### 安装

LangChain InMemoryStore 集成位于 `@langchain/core` 包中：

```bash npm theme={null}
npm install @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/core
```

## 实例化

现在我们可以实例化我们的字节存储：

```typescript theme={null}
import { InMemoryStore } from "@langchain/core/stores"
import { BaseMessage } from "@langchain/core/messages";

const kvStore = new InMemoryStore<BaseMessage>();
```

## 使用

您可以使用 `mset` 方法像这样在键下设置数据：

```typescript theme={null}
import { AIMessage, HumanMessage } from "@langchain/core/messages";

await kvStore.mset(
  [
    ["key1", new HumanMessage("value1")],
    ["key2", new AIMessage("value2")],
  ]
)

await kvStore.mget(
  [
    "key1",
    "key2",
  ]
)
```

```json theme={null}
[
  HumanMessage {
    "content": "value1",
    "additional_kwargs": {},
    "response_metadata": {}
  },
  AIMessage {
    "content": "value2",
    "additional_kwargs": {},
    "response_metadata": {},
    "tool_calls": [],
    "invalid_tool_calls": []
  }
]
```

并且您可以使用 `mdelete` 方法删除数据：

```typescript theme={null}
await kvStore.mdelete(
  [
    "key1",
    "key2",
  ]
)

await kvStore.mget(
  [
    "key1",
    "key2",
  ]
)
```

```text theme={null}
[ undefined, undefined ]
```

## 获取值

如果您想获取所有键，可以调用 `yieldKeys` 方法。可选地，您可以传递一个键前缀，仅获取与该前缀匹配的键。

```typescript theme={null}
import { InMemoryStore } from "@langchain/core/stores"
import { AIMessage, BaseMessage, HumanMessage } from "@langchain/core/messages";

const kvStoreForYield = new InMemoryStore<BaseMessage>();

// Add some data to the store
await kvStoreForYield.mset(
  [
    ["message:id:key1", new HumanMessage("value1")],
    ["message:id:key2", new AIMessage("value2")],
  ]
)

const yieldedKeys = [];
for await (const key of kvStoreForYield.yieldKeys("message:id:")) {
  yieldedKeys.push(key);
}

console.log(yieldedKeys);
```

```python theme={null}
[ 'message:id:key1', 'message:id:key2' ]
```

***

## API 参考

有关所有 `InMemoryStore` 功能和配置的详细文档，请访问 [API reference](https://reference.langchain.com/javascript/langchain-core/stores/InMemoryStore)

***

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


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