Skip to main content

Cloudflare Vectorize

如果你将项目部署到 Cloudflare Worker 中,则可以将 Cloudflare Vectorize 与 LangChain.js 结合使用。这是一个强大而便捷的选项,直接内置于 Cloudflare 中。

¥If you're deploying your project in a Cloudflare worker, you can use Cloudflare Vectorize with LangChain.js. It's a powerful and convenient option that's built directly into Cloudflare.

设置

¥Setup

Compatibility

Cloudflare Vectorize 目前处于公开测试阶段,需要付费版 Cloudflare 账户才能使用。

¥Cloudflare Vectorize is currently in open beta, and requires a Cloudflare account on a paid plan to use.

设置你的项目 之后,通过运行以下 Wrangler 命令创建索引:

¥After setting up your project, create an index by running the following Wrangler command:

$ npx wrangler vectorize create <index_name> --preset @cf/baai/bge-small-en-v1.5

你可以查看 vectorize 命令 在官方文档中 的完整选项列表。

¥You can see a full list of options for the vectorize command in the official documentation.

然后,你需要更新 wrangler.toml 文件以包含 [[vectorize]] 的条目:

¥You'll then need to update your wrangler.toml file to include an entry for [[vectorize]]:

[[vectorize]]
binding = "VECTORIZE_INDEX"
index_name = "<index_name>"

最后,你需要安装 LangChain Cloudflare 集成包:

¥Finally, you'll need to install the LangChain Cloudflare integration package:

npm install @langchain/cloudflare @langchain/core

用法

¥Usage

以下示例工作器根据所使用的路径将文档添加到向量存储、对其进行查询或清除。它也使用 Cloudflare Workers AI 嵌入

¥Below is an example worker that adds documents to a vectorstore, queries it, or clears it depending on the path used. It also uses Cloudflare Workers AI Embeddings.

note

如果在本地运行,请务必以 npx wrangler dev --remote 身份运行 wrangler!

¥If running locally, be sure to run wrangler as npx wrangler dev --remote!

name = "langchain-test"
main = "worker.ts"
compatibility_date = "2024-01-10"

[[vectorize]]
binding = "VECTORIZE_INDEX"
index_name = "langchain-test"

[ai]
binding = "AI"
// @ts-nocheck

import type {
VectorizeIndex,
Fetcher,
Request,
} from "@cloudflare/workers-types";

import {
CloudflareVectorizeStore,
CloudflareWorkersAIEmbeddings,
} from "@langchain/cloudflare";

export interface Env {
VECTORIZE_INDEX: VectorizeIndex;
AI: Fetcher;
}

export default {
async fetch(request: Request, env: Env) {
const { pathname } = new URL(request.url);
const embeddings = new CloudflareWorkersAIEmbeddings({
binding: env.AI,
model: "@cf/baai/bge-small-en-v1.5",
});
const store = new CloudflareVectorizeStore(embeddings, {
index: env.VECTORIZE_INDEX,
});
if (pathname === "/") {
const results = await store.similaritySearch("hello", 5);
return Response.json(results);
} else if (pathname === "/load") {
// Upsertion by id is supported
await store.addDocuments(
[
{
pageContent: "hello",
metadata: {},
},
{
pageContent: "world",
metadata: {},
},
{
pageContent: "hi",
metadata: {},
},
],
{ ids: ["id1", "id2", "id3"] }
);

return Response.json({ success: true });
} else if (pathname === "/clear") {
await store.delete({ ids: ["id1", "id2", "id3"] });
return Response.json({ success: true });
}

return Response.json({ error: "Not Found" }, { status: 404 });
},
};

API Reference:

你还可以传递 filter 参数,以便按先前加载的元数据进行筛选。有关所需格式的信息,请参阅 官方文档

¥You can also pass a filter parameter to filter by previously loaded metadata. See the official documentation for information on the required format.

¥Related