跳至主要内容

Momento 向量索引 (MVI)

MVI: 为您的数据提供最具生产力、最易于使用的无服务器向量索引。要开始使用 MVI,只需注册一个帐户即可。无需处理基础设施、管理服务器或担心扩展问题。MVI 是一种服务,可以根据您的需求自动扩展。无论是在 Node.js、浏览器还是边缘,Momento 都能满足您的需求。

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

设置

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

  2. 为您的环境安装 SDK。

    2.1. 适用于 Node.js

    npm install @gomomento/sdk

    2.2. 适用于 浏览器或边缘环境

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

    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

使用

npm install @langchain/openai @langchain/community

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

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

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 参考

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

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

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 参考

从现有集合中搜索

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 参考


此页面是否有帮助?


您也可以在 GitHub 上留下详细的反馈 on GitHub.