Introduction to Vectorize
Vectorize is Cloudflareβs vector database. Vector databases allow you to use machine learning (ML) models to perform semantic search, recommendation, classification and anomaly detection tasks, as well as provide context to LLMs (Large Language Models).
This guide will instruct you through:
- Creating your first Vectorize index.
- Connecting a Cloudflare Worker to your index.
- Inserting and performing a similarity search by querying your index.
Prerequisites
To continue, you will need:
- Sign up for a Cloudflare account β if you have not already.
- Install
npm
β. - Install
Node.js
β. Use a Node version manager like Volta β or nvm β to avoid permission issues and change Node.js versions. Wrangler requires a Node version of16.17.0
or later.
1. Create a Worker
You will create a new project that will contain a Worker, which will act as the client application for your Vectorize index.
Create a new project named vectorize-tutorial
by running:
For setup, select the following options:
- For What would you like to start with?, choose
Hello World example
. - For Which template would you like to use?, choose
Hello World Worker
. - For Which language do you want to use?, choose
TypeScript
. - For Do you want to use git for version control?, choose
Yes
. - For Do you want to deploy your application?, choose
No
(we will be making some changes before deploying).
This will create a new vectorize-tutorial
directory. Your new vectorize-tutorial
directory will include:
- A
"Hello World"
Worker atsrc/index.ts
. - A
wrangler.toml
configuration file.wrangler.toml
is how yourvectorize-tutorial
Worker will access your index.
2. Create an index
A vector database is distinct from a traditional SQL or NoSQL database. A vector database is designed to store vector embeddings, which are representations of data, but not the original data itself.
To create your first Vectorize index, change into the directory you just created for your Workers project:
To create an index, you will need to use the wrangler vectorize create
command and provide a name for the index. A good index name is:
- A combination of lowercase and/or numeric ASCII characters, shorter than 32 characters, starts with a letter, and uses dashes (-) instead of spaces.
- Descriptive of the use-case and environment. For example, βproduction-doc-searchβ or βdev-recommendation-engineβ.
- Only used for describing the index, and is not directly referenced in code.
In addition, you will need to define both the dimensions
of the vectors you will store in the index, as well as the distance metric
used to determine similar vectors when creating the index. A metric
can be euclidean, cosine, or dot product. This configuration cannot be changed later, as a vector database is configured for a fixed vector configuration.
Run the following wrangler vectorize
command:
The command above will create a new vector database, and output the binding configuration needed in the next step.
3. Bind your Worker to your index
You must create a binding for your Worker to connect to your Vectorize index. Bindings allow your Workers to access resources, like Vectorize or R2, from Cloudflare Workers. You create bindings by updating the workerβs wrangler.toml
file.
To bind your index to your Worker, add the following to the end of your wrangler.toml
file:
Specifically:
- The value (string) you set for
<BINDING_NAME>
will be used to reference this database in your Worker. In this tutorial, name your bindingVECTORIZE
. - The binding must be a valid JavaScript variable name β. For example,
binding = "MY_INDEX"
orbinding = "PROD_SEARCH_INDEX"
would both be valid names for the binding. - Your binding is available in your Worker at
env.<BINDING_NAME>
and the Vectorize client API is exposed on this binding for use within your Workers application.
4. [Optional] Create metadata indexes
Vectorize allows you to add up to 10KiB of metadata per vector into your index, and also provides the ability to filter on that metadata while querying vectors. To do so you would need to specify a metadata field as a βmetadata indexβ for your Vectorize index.
To enable vector filtering on a metadata field during a query, use a command like:
Here url
is the metadata field on which filtering would be enabled. The --type
parameter defines the data type for the metadata field; string
, number
and boolean
types are supported.
It typically takes a few seconds for the metadata index to be created. You can check the list of metadata indexes for your Vectorize index by running:
You can create up to 10 metadata indexes per Vectorize index.
For metadata indexes of type number
, the indexed number precision is that of float64.
For metadata indexes of type string
, each vector indexes the first 64B of the string data truncated on UTF-8 character boundaries to the longest well-formed UTF-8 substring within that limit, so vectors are filterable on the first 64B of their value for each indexed property.
See Vectorize Limits for a complete list of limits.
5. Insert vectors
Before you can query a vector database, you need to insert vectors for it to query against. These vectors would be generated from data (such as text or images) you pass to a machine learning model. However, this tutorial will define static vectors to illustrate how vector search works on its own.
First, go to your vectorize-tutorial
Worker and open the src/index.ts
file. The index.ts
file is where you configure your Workerβs interactions with your Vectorize index.
Clear the content of index.ts
, and paste the following code snippet into your index.ts
file. On the env
parameter, replace <BINDING_NAME>
with VECTORIZE
:
In the code above, you:
- Define a binding to your Vectorize index from your Workers code. This binding matches the
binding
value you set inwrangler.toml
under[[vectorize]]
. - Specify a set of example vectors that you will query against in the next step.
- Insert those vectors into the index and confirm it was successful.
In the next step, you will expand the Worker to query the index and the vectors you insert.
6. Query vectors
In this step, you will take a vector representing an incoming query and use it to search your index.
First, go to your vectorize-tutorial
Worker and open the src/index.ts
file. The index.ts
file is where you configure your Workerβs interactions with your Vectorize index.
Clear the content of index.ts
. Paste the following code snippet into your index.ts
file. On the env
parameter, replace <BINDING_NAME>
with VECTORIZE
:
You can also use the Vectorize queryById()
operation to search for vectors similar to a vector that is already present in the index.
7. Deploy your Worker
Before deploying your Worker globally, log in with your Cloudflare account by running:
You will be directed to a web page asking you to log in to the Cloudflare dashboard. After you have logged in, you will be asked if Wrangler can make changes to your Cloudflare account. Scroll down and select Allow to continue.
From here, you can deploy your Worker to make your project accessible on the Internet. To deploy your Worker, run:
Once deployed, preview your Worker at https://vectorize-tutorial.<YOUR_SUBDOMAIN>.workers.dev
.
8. Query your index
To insert vectors and then query them, use the URL for your deployed Worker, such as https://vectorize-tutorial.<YOUR_SUBDOMAIN>.workers.dev/
. Open your browser and:
- Insert your vectors first by visiting
/insert
. This should return the below JSON:
The mutationId here refers to a unique identifier that corresponds to this asynchronous insert operation. Typically it takes a few seconds for inserted vectors to be available for querying.
You can use the index info operation to check the last processed mutation:
Subsequent inserts using the same vector ids will return a mutation id, but it would not change the index vector count since the same vector ids cannot be inserted twice. You will need to use an upsert
operation instead to update the vector values for an id that already exists in an index.
- Query your index - expect your query vector of
[0.13, 0.25, 0.44, ...]
to be closest to vector ID4
by visiting the root path of/
. This query will return the three (topK: 3
) closest matches, as well as their vector values and metadata.
You will notice that id: 4
has a score
of 0.46348256
. Because you are using euclidean
as our distance metric, the closer the score to 0.0
, the closer your vectors are.
From here, experiment by passing a different queryVector
and observe the results: the matches and the score
should change based on the change in distance between the query vector and the vectors in our index.
In a real-world application, the queryVector
would be the vector embedding representation of a query from a user or system, and our sampleVectors
would be generated from real content. To build on this example, read the vector search tutorial that combines Workers AI and Vectorize to build an end-to-end application with Workers.
By finishing this tutorial, you have successfully created and queried your first Vectorize index, a Worker to access that index, and deployed your project globally.
Related resources
- Build an end-to-end vector search application using Workers AI and Vectorize.
- Learn more about how vector databases work.
- Read examples on how to use the Vectorize API from Cloudflare Workers.
- Euclidean Distance vs Cosine Similarity β.
- Dot product β.