带有可选向量搜索功能的内存键值存储。

一个使用 JavaScript Map 实现的轻量级存储。在配置了嵌入(embeddings)时,支持基本的键值操作和向量搜索。

示例

// Basic key-value storage
const store = new InMemoryStore();
await store.put(["users", "123"], "prefs", { theme: "dark" });
const item = await store.get(["users", "123"], "prefs");

// Vector search with embeddings
import { OpenAIEmbeddings } from "@langchain/openai";
const store = new InMemoryStore({
index: {
dims: 1536,
embeddings: new OpenAIEmbeddings({ modelName: "text-embedding-3-small" }),
}
});

// Store documents
await store.put(["docs"], "doc1", { text: "Python tutorial" });
await store.put(["docs"], "doc2", { text: "TypeScript guide" });

// Search by similarity
const results = await store.search(["docs"], { query: "python programming" });

警告

该存储将所有数据保存在内存中。进程退出时数据将丢失。如需持久化,请使用由数据库支持的存储。

层级结构 (查看完整)

构造函数

访问器

  • get indexConfig(): undefined | IndexConfig
  • 返回 undefined | IndexConfig

方法

  • 在单个批处理中执行多个操作。这比单独执行操作更高效。

    类型参数

    参数

    • operations: Op

      要执行的操作数组

    返回 Promise<OperationResults<Op>>

    解析为与操作相匹配结果的 Promise

  • 从存储中删除一个项目。

    参数

    • namespace: string[]

      项目的分层路径

    • key: string

      命名空间内的唯一标识符

    返回 Promise<void>

  • 通过命名空间和键检索单个项目。

    参数

    • namespace: string[]

      项目的分层路径

    • key: string

      命名空间内的唯一标识符

    返回 Promise<null | Item>

    解析为项目或在未找到时解析为 null 的 Promise

  • 列出并筛选存储中的命名空间。用于探索数据组织和导航命名空间层级。

    参数

    • 可选 options: {
          limit?: number;
          maxDepth?: number;
          offset?: number;
          prefix?: string[];
          suffix?: string[];
      }

      用于列出命名空间的选项

      • 可选 limit?: number
      • 可选 maxDepth?: number
      • 可选 offset?: number
      • 可选 prefix?: string[]
      • 可选 suffix?: string[]

    返回 Promise<string[][]>

    解析为命名空间路径列表的 Promise

    示例

    // List all namespaces under "documents"
    await store.listNamespaces({
    prefix: ["documents"],
    maxDepth: 2
    });

    // List namespaces ending with "v1"
    await store.listNamespaces({
    suffix: ["v1"],
    limit: 50
    });
  • 存储或更新项目。

    参数

    • namespace: string[]

      项目的分层路径

    • key: string

      命名空间内的唯一标识符

    • value: Record<string, any>

      包含项目数据的对象

    • 可选 index: false | string[]

      可选的索引配置

    返回 Promise<void>

    示例

    // Simple storage
    await store.put(["docs"], "report", { title: "Annual Report" });

    // With specific field indexing
    await store.put(
    ["docs"],
    "report",
    {
    title: "Q4 Report",
    chapters: [{ content: "..." }, { content: "..." }]
    },
    ["title", "chapters[*].content"]
    );
  • 在命名空间前缀内搜索项目。支持元数据筛选和向量相似性搜索。

    参数

    • namespacePrefix: string[]

      要搜索的分层路径前缀

    • 可选 options: {
          filter?: Record<string, any>;
          limit?: number;
          offset?: number;
          query?: string;
      }

      用于筛选和分页的搜索选项

      • 可选 filter?: Record<string, any>
      • 可选 limit?: number
      • 可选 offset?: number
      • 可选 query?: string

    返回 Promise<SearchItem[]>

    解析为带有相关性分数的匹配项目列表的 Promise

    示例

    // Search with filters
    await store.search(["documents"], {
    filter: { type: "report", status: "active" },
    limit: 5,
    offset: 10
    });

    // Vector similarity search
    await store.search(["users", "content"], {
    query: "technical documentation about APIs",
    limit: 20
    });
  • 启动存储。如果需要初始化,请重写此方法。

    返回 void | Promise<void>

  • 停止存储。如果需要清理,请重写此方法。

    返回 void | Promise<void>