Cloudflare Workers AI
If you're deploying your project in a Cloudflare worker, you can use Cloudflare's built-in Workers AI embeddings with LangChain.js.
Setup
First, follow the official docs to set up your worker.
You'll also need to install the LangChain Cloudflare integration package:
- npm
- Yarn
- pnpm
npm install @langchain/cloudflare
yarn add @langchain/cloudflare
pnpm add @langchain/cloudflare
Usage
Below is an example worker that uses Workers AI embeddings with a Cloudflare Vectorize vectorstore.
note
If running locally, be sure to run wrangler as npx wrangler dev --remote
!
name = "langchain-test"
main = "worker.js"
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:
- CloudflareVectorizeStore from
@langchain/cloudflare
- CloudflareWorkersAIEmbeddings from
@langchain/cloudflare