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 official Cloudflare AI SDK:
- npm
- Yarn
- pnpm
npm install @cloudflare/ai
yarn add @cloudflare/ai
pnpm add @cloudflare/ai
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 = "2023-09-22"
[[vectorize]]
binding = "VECTORIZE_INDEX"
index_name = "langchain-test"
[ai]
binding = "AI"
// @ts-nocheck
import type {
VectorizeIndex,
Fetcher,
Request,
} from "@cloudflare/workers-types";
import { CloudflareVectorizeStore } from "langchain/vectorstores/cloudflare_vectorize";
import { CloudflareWorkersAIEmbeddings } from "langchain/embeddings/cloudflare_workersai";
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,
modelName: "@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/vectorstores/cloudflare_vectorize
- CloudflareWorkersAIEmbeddings from
langchain/embeddings/cloudflare_workersai