-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Cloudflare Vectorize and Workers AI embeddings (#2740)
* Add Cloudflare Vectorize and Workers AI embeddings * Add id options * Make params mandatory in CloudflareWorkersAIEmbeddings constructor * Add Vectorize/WorkersAI example to CF test exports * Small interface tweaks, add docs * Revert optional dep listing * Fix typo * Fix typo * Update docs, fix CI * Fix build --------- Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
- Loading branch information
1 parent
bb6204a
commit 03911cc
Showing
15 changed files
with
744 additions
and
29 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
docs/extras/modules/data_connection/text_embedding/integrations/cloudflare_ai.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
hide_table_of_contents: true | ||
--- | ||
|
||
# Cloudflare Workers AI | ||
|
||
If you're deploying your project in a Cloudflare worker, you can use Cloudflare's [built-in Workers AI embeddings](https://developers.cloudflare.com/workers-ai/) with LangChain.js. | ||
|
||
## Setup | ||
|
||
First, [follow the official docs](https://developers.cloudflare.com/workers-ai/get-started/workers-wrangler/) to set up your worker. | ||
|
||
You'll also need to install the official Cloudflare AI SDK: | ||
|
||
```bash npm2yarn | ||
npm install @cloudflare/ai | ||
``` | ||
|
||
## Usage | ||
|
||
Below is an example worker that uses Workers AI embeddings with a [Cloudflare Vectorize](/docs/modules/data_connection/vectorstores/integrations/cloudflare_vectorize) vectorstore. | ||
|
||
:::note | ||
If running locally, be sure to run wrangler as `npx wrangler dev --remote`! | ||
::: | ||
|
||
```toml | ||
name = "langchain-test" | ||
main = "worker.js" | ||
compatibility_date = "2023-09-22" | ||
|
||
[[vectorize]] | ||
binding = "VECTORIZE_INDEX" | ||
index_name = "langchain-test" | ||
|
||
[ai] | ||
binding = "AI" | ||
``` | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/indexes/vector_stores/cloudflare_vectorize/example.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
57 changes: 57 additions & 0 deletions
57
...tras/modules/data_connection/vectorstores/integrations/cloudflare_vectorize.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
hide_table_of_contents: true | ||
--- | ||
|
||
# Cloudflare Vectorize | ||
|
||
If you're deploying your project in a Cloudflare worker, you can use [Cloudflare Vectorize](https://developers.cloudflare.com/vectorize/) with LangChain.js. | ||
It's a powerful and convenient option that's built directly into Cloudflare. | ||
|
||
## Setup | ||
|
||
:::tip Compatibility | ||
Cloudflare Vectorize is currently in open beta, and requires a Cloudflare account on a paid plan to use. | ||
::: | ||
|
||
After [setting up your project](https://developers.cloudflare.com/vectorize/get-started/intro/#prerequisites), | ||
create an index by running the following Wrangler command: | ||
|
||
```bash | ||
$ npx wrangler vectorize create <index_name> --preset @cf/baai/bge-small-en-v1.5 | ||
``` | ||
|
||
You can see a full list of options for the `vectorize` command [in the official documentation](https://developers.cloudflare.com/workers/wrangler/commands/#vectorize). | ||
|
||
You'll then need to update your `wrangler.toml` file to include an entry for `[[vectorize]]`: | ||
|
||
```toml | ||
[[vectorize]] | ||
binding = "VECTORIZE_INDEX" | ||
index_name = "<index_name>" | ||
``` | ||
|
||
## Usage | ||
|
||
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](/docs/modules/data_connection/text_embedding/integrations/cloudflare_ai). | ||
|
||
:::note | ||
If running locally, be sure to run wrangler as `npx wrangler dev --remote`! | ||
::: | ||
|
||
```toml | ||
name = "langchain-test" | ||
main = "worker.js" | ||
compatibility_date = "2023-09-22" | ||
|
||
[[vectorize]] | ||
binding = "VECTORIZE_INDEX" | ||
index_name = "langchain-test" | ||
|
||
[ai] | ||
binding = "AI" | ||
``` | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/indexes/vector_stores/cloudflare_vectorize/example.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
name = "test-exports-cf" | ||
main = "src/index.ts" | ||
compatibility_date = "2023-04-05" | ||
compatibility_date = "2023-09-22" | ||
|
||
[[vectorize]] | ||
binding = "VECTORIZE_INDEX" | ||
index_name = "langchain-test" | ||
|
||
[ai] | ||
binding = "AI" |
56 changes: 56 additions & 0 deletions
56
examples/src/indexes/vector_stores/cloudflare_vectorize/example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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 }); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
03911cc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
langchainjs-docs – ./
langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-langchain.vercel.app
js.langchain.com