Google Vertex AI Matching Engine
仅在 Node.js 上可用。
Google Vertex AI Matching Engine “提供业界领先的高规模低延迟向量数据库。这些向量数据库通常被称为向量相似度匹配或近似最近邻 (ANN) 服务。”
设置
此模块需要预先创建的端点和已部署的索引,因为创建时间接近一小时。要了解更多信息,请参阅 LangChain Python 文档 创建索引并将其部署到端点。
在运行此代码之前,您应该确保在您的 Google Cloud 仪表板中为相关项目启用了 Vertex AI API,并且您已使用以下方法之一通过身份验证连接到 Google Cloud
- 您已登录到允许访问该项目的帐户(使用
gcloud auth application-default login
)。 - 您正在使用允许访问该项目的服务帐户的机器上运行。
- 您已下载允许访问该项目的服务帐户的凭据,并将
GOOGLE_APPLICATION_CREDENTIALS
环境变量设置为此文件的路径。
使用以下命令安装身份验证库
- npm
- Yarn
- pnpm
npm install @langchain/community @langchain/core google-auth-library
yarn add @langchain/community @langchain/core google-auth-library
pnpm add @langchain/community @langchain/core google-auth-library
Matching Engine 不存储实际的文档内容,仅存储嵌入。因此,您需要一个文档存储。以下示例使用 Google Cloud Storage,这需要以下条件
- npm
- Yarn
- pnpm
npm install @google-cloud/storage
yarn add @google-cloud/storage
pnpm add @google-cloud/storage
使用方法
初始化引擎
创建 MatchingEngine
对象时,您需要一些关于 Matching Engine 配置的信息。您可以从 Matching Engine 的 Cloud Console 获取此信息
- 索引的 ID
- 索引端点的 ID
您还需要一个文档存储。虽然 InMemoryDocstore
对于初始测试来说还可以,但您会希望使用类似 GoogleCloudStorageDocstore 的东西来更永久地存储它。
import { MatchingEngine } from "@langchain/community/vectorstores/googlevertexai";
import { Document } from "langchain/document";
import { SyntheticEmbeddings } from "langchain/embeddings/fake";
import { GoogleCloudStorageDocstore } from "@langchain/community/stores/doc/gcs";
const embeddings = new SyntheticEmbeddings({
vectorSize: Number.parseInt(
process.env.SYNTHETIC_EMBEDDINGS_VECTOR_SIZE ?? "768",
10
),
});
const store = new GoogleCloudStorageDocstore({
bucket: process.env.GOOGLE_CLOUD_STORAGE_BUCKET!,
});
const config = {
index: process.env.GOOGLE_VERTEXAI_MATCHINGENGINE_INDEX!,
indexEndpoint: process.env.GOOGLE_VERTEXAI_MATCHINGENGINE_INDEXENDPOINT!,
apiVersion: "v1beta1",
docstore: store,
};
const engine = new MatchingEngine(embeddings, config);
添加文档
const doc = new Document({ pageContent: "this" });
await engine.addDocuments([doc]);
文档中的任何元数据都将转换为 Matching Engine “允许列表”值,这些值可以在查询期间用于筛选。
const documents = [
new Document({
pageContent: "this apple",
metadata: {
color: "red",
category: "edible",
},
}),
new Document({
pageContent: "this blueberry",
metadata: {
color: "blue",
category: "edible",
},
}),
new Document({
pageContent: "this firetruck",
metadata: {
color: "red",
category: "machine",
},
}),
];
// Add all our documents
await engine.addDocuments(documents);
文档假定具有可用的“id”参数。如果未设置此参数,则将分配一个 ID 并作为文档的一部分返回。
查询文档
使用任何标准方法都可以完成直接的 k-最近邻搜索,该搜索返回所有结果
const results = await engine.similaritySearch("this");
使用过滤器/限制查询文档
我们可以根据为文档设置的元数据来限制返回哪些文档。因此,如果我们只想将结果限制为那些具有红色的文档,我们可以这样做
import { Restriction } from `langchain/vectorstores/googlevertexai`;
const redFilter: Restriction[] = [
{
namespace: "color",
allowList: ["red"],
},
];
const redResults = await engine.similaritySearch("this", 4, redFilter);
如果我们想要做更复杂的事情,比如红色的,但不可食用的东西
const filter: Restriction[] = [
{
namespace: "color",
allowList: ["red"],
},
{
namespace: "category",
denyList: ["edible"],
},
];
const results = await engine.similaritySearch("this", 4, filter);
删除文档
删除文档是使用 ID 完成的。
import { IdDocument } from `langchain/vectorstores/googlevertexai`;
const oldResults: IdDocument[] = await engine.similaritySearch("this", 10);
const oldIds = oldResults.map( doc => doc.id! );
await engine.delete({ids: oldIds});