Skip to main content

Momento 向量索引(MVI)

¥Momento Vector Index (MVI)

MVI:最高效、最易于使用、无服务器的数据向量索引。要开始使用 MVI,只需注册一个账户即可。无需处理基础设施、管理服务器或担心扩展问题。MVI 是一项可自动扩展以满足你需求的服务。无论是在 Node.js、浏览器还是 Edge 中,Momento 都能满足你的需求。

¥MVI: the most productive, easiest to use, serverless vector index for your data. To get started with MVI, simply sign up for an account. There's no need to handle infrastructure, manage servers, or be concerned about scaling. MVI is a service that scales automatically to meet your needs. Whether in Node.js, browser, or edge, Momento has you covered.

要注册并访问 MVI,请访问 Momento 控制台

¥To sign up and access MVI, visit the Momento Console.

设置

¥Setup

  1. Momento 控制台 注册 API 密钥。

    ¥Sign up for an API key in the Momento Console.

  2. 根据你的环境安装 SDK。

    ¥Install the SDK for your environment.

    2.1.对于 Node.js:

    ¥2.1. For Node.js:

    npm install @gomomento/sdk

    2.2.对于浏览器或边缘环境:

    ¥2.2. For browser or edge environments:

    npm install @gomomento/sdk-web
  3. 运行代码前,请为 Momento 设置环境变量

    ¥Setup Env variables for Momento before running the code

    3.1 OpenAI

    export OPENAI_API_KEY=YOUR_OPENAI_API_KEY_HERE

    3.2 Momento

    export MOMENTO_API_KEY=YOUR_MOMENTO_API_KEY_HERE # https://console.gomomento.com

用法

¥Usage

npm install @langchain/openai @langchain/community @langchain/core

¥Index documents using fromTexts and search

本示例演示如何使用 fromTexts 方法实例化向量存储和索引文档。如果索引不存在,则会创建索引。如果索引已存在,则文档将添加到现有索引中。

¥This example demonstrates using the fromTexts method to instantiate the vector store and index documents. If the index does not exist, then it will be created. If the index already exists, then the documents will be added to the existing index.

ids 是可选的;如果你省略它们,Momento 将为你生成 UUID。

¥The ids are optional; if you omit them, then Momento will generate UUIDs for you.

import { MomentoVectorIndex } from "@langchain/community/vectorstores/momento_vector_index";
// For browser/edge, adjust this to import from "@gomomento/sdk-web";
import {
PreviewVectorIndexClient,
VectorIndexConfigurations,
CredentialProvider,
} from "@gomomento/sdk";
import { OpenAIEmbeddings } from "@langchain/openai";
import { sleep } from "langchain/util/time";

const vectorStore = await MomentoVectorIndex.fromTexts(
["hello world", "goodbye world", "salutations world", "farewell world"],
{},
new OpenAIEmbeddings(),
{
client: new PreviewVectorIndexClient({
configuration: VectorIndexConfigurations.Laptop.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: "MOMENTO_API_KEY",
}),
}),
indexName: "langchain-example-index",
},
{ ids: ["1", "2", "3", "4"] }
);

// because indexing is async, wait for it to finish to search directly after
await sleep();

const response = await vectorStore.similaritySearch("hello", 2);

console.log(response);

/*
[
Document { pageContent: 'hello world', metadata: {} },
Document { pageContent: 'salutations world', metadata: {} }
]
*/

API Reference:

¥Index documents using fromDocuments and search

与上述类似,此示例演示了如何使用 fromDocuments 方法实例化向量存储并索引文档。如果索引不存在,则会创建索引。如果索引已存在,则文档将添加到现有索引中。

¥Similar to the above, this example demonstrates using the fromDocuments method to instantiate the vector store and index documents. If the index does not exist, then it will be created. If the index already exists, then the documents will be added to the existing index.

使用 fromDocuments 允许你无缝地将各种文档加载器与索引链接起来。

¥Using fromDocuments allows you to seamlessly chain the various document loaders with indexing.

import { MomentoVectorIndex } from "@langchain/community/vectorstores/momento_vector_index";
// For browser/edge, adjust this to import from "@gomomento/sdk-web";
import {
PreviewVectorIndexClient,
VectorIndexConfigurations,
CredentialProvider,
} from "@gomomento/sdk";
import { OpenAIEmbeddings } from "@langchain/openai";
import { TextLoader } from "langchain/document_loaders/fs/text";
import { sleep } from "langchain/util/time";

// Create docs with a loader
const loader = new TextLoader("src/document_loaders/example_data/example.txt");
const docs = await loader.load();

const vectorStore = await MomentoVectorIndex.fromDocuments(
docs,
new OpenAIEmbeddings(),
{
client: new PreviewVectorIndexClient({
configuration: VectorIndexConfigurations.Laptop.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: "MOMENTO_API_KEY",
}),
}),
indexName: "langchain-example-index",
}
);

// because indexing is async, wait for it to finish to search directly after
await sleep();

// Search for the most similar document
const response = await vectorStore.similaritySearch("hello", 1);

console.log(response);
/*
[
Document {
pageContent: 'Foo\nBar\nBaz\n\n',
metadata: { source: 'src/document_loaders/example_data/example.txt' }
}
]
*/

API Reference:

从现有集合中搜索

¥Search from an existing collection

import { MomentoVectorIndex } from "@langchain/community/vectorstores/momento_vector_index";
// For browser/edge, adjust this to import from "@gomomento/sdk-web";
import {
PreviewVectorIndexClient,
VectorIndexConfigurations,
CredentialProvider,
} from "@gomomento/sdk";
import { OpenAIEmbeddings } from "@langchain/openai";

const vectorStore = new MomentoVectorIndex(new OpenAIEmbeddings(), {
client: new PreviewVectorIndexClient({
configuration: VectorIndexConfigurations.Laptop.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: "MOMENTO_API_KEY",
}),
}),
indexName: "langchain-example-index",
});

const response = await vectorStore.similaritySearch("hello", 1);

console.log(response);
/*
[
Document {
pageContent: 'Foo\nBar\nBaz\n\n',
metadata: { source: 'src/document_loaders/example_data/example.txt' }
}
]
*/

API Reference:

¥Related