Momento 向量索引 (MVI)
MVI: 为您的数据提供最有效率、最易于使用的无服务器向量索引。要开始使用 MVI,只需注册一个帐户。无需处理基础设施、管理服务器,也无需担心扩展问题。MVI 是一项自动扩展以满足您需求的服务。无论是在 Node.js、浏览器还是边缘,Momento 都可以满足您的需求。
要注册并访问 MVI,请访问 Momento 控制台.
设置
在 Momento 控制台 中注册 API 密钥。
安装适合您的环境的 SDK。
2.1. 适用于 **Node.js**
- npm
- Yarn
- pnpm
npm install @gomomento/sdk
yarn add @gomomento/sdk
pnpm add @gomomento/sdk
2.2. 适用于 **浏览器或边缘环境**
- npm
- Yarn
- pnpm
npm install @gomomento/sdk-web
yarn add @gomomento/sdk-web
pnpm add @gomomento/sdk-web
在运行代码之前为 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
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core
yarn add @langchain/openai @langchain/community @langchain/core
pnpm add @langchain/openai @langchain/community @langchain/core
使用 fromTexts
索引文档并进行搜索
此示例演示了使用 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 参考
- MomentoVectorIndex 来自
@langchain/community/vectorstores/momento_vector_index
- OpenAIEmbeddings 来自
@langchain/openai
- sleep 来自
langchain/util/time
使用 fromDocuments
索引文档并进行搜索
与上面类似,此示例演示了使用 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 参考
- MomentoVectorIndex 来自
@langchain/community/vectorstores/momento_vector_index
- OpenAIEmbeddings 来自
@langchain/openai
- TextLoader 来自
langchain/document_loaders/fs/text
- sleep 来自
langchain/util/time
从现有集合中搜索
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 参考
- MomentoVectorIndex 来自
@langchain/community/vectorstores/momento_vector_index
- OpenAIEmbeddings 来自
@langchain/openai