Skip to main content

键值存储

¥Key-value stores

概述

¥Overview

LangChain 提供了一个用于存储和检索数据的键值存储接口。

¥LangChain provides a key-value store interface for storing and retrieving data.

LangChain 包含一个 BaseStore 接口,允许存储任意数据。然而,需要键值存储的 LangChain 组件接受更具体的 BaseStore<string, Uint8Array> 实例,该实例存储二进制数据(称为 ByteStore),并在内部根据其特定需求对数据进行编码和解码。

¥LangChain includes a BaseStore interface, which allows for storage of arbitrary data. However, LangChain components that require KV-storage accept a more specific BaseStore<string, Uint8Array> instance that stores binary data (referred to as a ByteStore), and internally take care of encoding and decoding data for their specific needs.

这意味着作为用户,你只需考虑一种存储类型,而无需为不同类型的数据考虑不同的存储类型。

¥This means that as a user, you only need to think about one type of store rather than different ones for different types of data.

用法

¥Usage

LangChain 中的键值存储接口主要用于:

¥The key-value store interface in LangChain is used primarily for:

  1. 通过 CachedBackedEmbeddings 缓存 embeddings,以避免在重复查询或重新索引内容时重新计算嵌入。

    ¥Caching embeddings via CachedBackedEmbeddings to avoid recomputing embeddings for repeated queries or when re-indexing content.

  2. 在某些检索器中,作为简单的 文档 持久层。

    ¥As a simple Document persistence layer in some retrievers.

请参阅以下操作指南了解更多信息:

¥Please see these how-to guides for more information:

接口

¥Interface

所有 BaseStore 都支持以下接口。请注意,该接口允许一次修改多个键值对:

¥All BaseStores support the following interface. Note that the interface allows for modifying multiple key-value pairs at once:

  • mget(keys: string[]): Promise<(Uint8Array | undefined)[]>:获取多个键的内容,如果键不存在则返回 undefined

    ¥mget(keys: string[]): Promise<(Uint8Array | undefined)[]>: get the contents of multiple keys, returning undefined if the key does not exist

  • mset(keyValuePairs: [string, Uint8Array][]): Promise<void>:设置多个键的内容

    ¥mset(keyValuePairs: [string, Uint8Array][]): Promise<void>: set the contents of multiple keys

  • mdelete(keys: string[]): Promise<void>:删除多个键

    ¥mdelete(keys: string[]): Promise<void>: delete multiple keys

  • yieldKeys(prefix?: string): AsyncIterator<string>:返回存储中的所有键,并可选择按前缀进行筛选。

    ¥yieldKeys(prefix?: string): AsyncIterator<string>: yield all keys in the store, optionally filtering by a prefix

集成

¥Integrations

请参阅 存储集成页面 获取可用的键值存储集成列表。

¥Please reference the stores integration page for a list of available key-value store integrations.