A Cloudflare Worker that generates vector embeddings from text using Cloudflare Workers AI and stores them in Cloudflare Vectorize for efficient vector search and retrieval.
This service provides an API endpoint for generating and optionally storing text embeddings. It uses the @cf/baai/bge-base-en-v1.5 model provided by Cloudflare Workers AI to generate high-quality embeddings, which can then be stored in a Vectorize index for fast vector-based similarity search.
The worker is particularly useful for:
- Converting document chunks or text fragments into vector embeddings
- Organizing embeddings by collection, user, and file
- Efficiently managing batch processing of large documents
- Building search applications, RAG (Retrieval Augmented Generation) systems, or AI-powered document analysis tools
- Generate embeddings from text strings or arrays of text
- Optional storage of embeddings in Cloudflare Vectorize
- Batch processing support with configurable batch sizes
- Detailed metadata storage including collection ID, file information, and chunk indexes
- Error handling and logging
POST /
{
"text": "String or array of strings to embed",
"collection_id": "your-collection-identifier",
"file_key": "user-id/collection-id/file-id/filename",
"start_index": 0,
"batch_size": 10,
"total_chunks": 100,
"store": true
}text: A string or array of strings to generate embeddings forcollection_id: Identifier for the collection this embedding belongs tofile_key: String in the format"user-id/collection-id/file-id/filename"start_index: Starting chunk index for this batchbatch_size: Number of chunks in this batch (maximum 100)total_chunks: Total number of chunks in the entire documentstore: Boolean flag whether to store embeddings in Vectorize
{
"status": "completed",
"stored": true
}{
"status": "completed",
"stored": false,
"embeddings": {
"data": [
[...embedding vector values...]
]
}
}{
"status": "error",
"message": "Error message details"
}- Node.js (v16 or later)
- Wrangler CLI
- Cloudflare account with Workers AI and Vectorize enabled
-
Clone the repository:
git clone [repository-url] cd embedding-generator -
Install dependencies:
npm install
-
Configure your Vectorize index in the Cloudflare Dashboard or using Wrangler.
Run the worker locally:
npm run devDeploy to Cloudflare Workers:
npm run deployConfiguration is managed through the wrangler.jsonc file:
- Maximum batch size is 100 text chunks at a time
- Text encoding is determined by the
@cf/baai/bge-base-en-v1.5model - File path structure must follow the format:
user-id/collection-id/file-id/filename
MIT
{ "name": "embedding-generator-and-store", "main": "src/index.ts", "compatibility_date": "2025-02-11", "compatibility_flags": ["nodejs_compat"], "ai": { "binding": "AI" }, "vectorize": [ { "binding": "VECTORIZE", "index_name": "files-1" } ], "observability": { "enabled": true, "head_sampling_rate": 1 } }