Build AI-powered real-time collaborative applications with PartyKit AI. Powered by Cloudflare AI, PartyKit brings you models for a variety of use cases, including: text and image generation, translation, text-to-speech, and more. It also includes a vector database (powered by Cloudflare Vectorize) to build search engines and RAG applications.
Models: Text, Images, and more
To get started with using an AI model, first install the partykit-ai package in your PartyKit project:
Then, import the partykit-ai package in your server code:
You can also use the package for non-party, regular api endpoints with onFetch / onSocket / onCron:
The partykit-ai package is a wrapper on top of @cloudflare/ai, so you can use the same API to access the models. For example, you could use the text-generation model to build a chatbot as described here. Learn more at the Workers AI documentation.
As an example, hereβs a fetch handler that uses the text-generation model to get a description for a word:
Vectorize: Build your own search engine
PartyKit AI includes a vector database (powered by Cloudflare Vectorize) to build search engines and RAG applications.
Commands
You can list all available commands by running npx partykit vectorize in your terminal.
create
Create a vectorize index
delete
Delete a vectorize index
get
Get a vectorize indexβ details by name
list
List all vectorize indexes
insert
Insert vectors into a Vectorize index
API
You can also interact with the vectorize index from your server code. After configuring your index in partykit.json like so:
You can access it from your server code like so:
Vectors
A vector represents the vector embedding output from a machine learning model.
id - a unique string identifying the vector in the index. This should map back to the ID of the document, object or database identifier that the vector values were generated from.
namespace - an optional partition key within a index. Operations are performed per-namespace, so this can be used to create isolated segments within a larger index.
values - an array of number, Float32Array, or Float64Array as the vector embedding itself. This must be a dense array, and the length of this array must match the dimensions configured on the index.
metadata - an optional set of key-value pairs that can be used to store additional metadata alongside a vector.
Retrieves the configuration of a given index directly, including its configured dimensions and distance metric.
Metadata Filtering
In addition to providing an input vector to your query, you can also filter by vector metadata associated with every vector. Query results only include vectors that match filter criteria, meaning that filter is applied first, and topK results are taken from the filtered set.
By using metadata filtering to limit the scope of a query, you can filter by specific customer IDs, tenant, product category or any other metadata you associate with your vectors.
Supported operations
Optional filter property on query() method specifies metadata filter:
Operator
Description
$eq
Equals
$ne
Not equals
filter must be non-empty object whose compact JSON representation must be less than 2048 bytes.
filter object keys cannot be empty, contain " | . (dot is reserved for nesting), start with $, or be longer than 512 characters.
filter object non-nested values can be string, number, boolean, or null values.
Namespace versus metadata filtering
Both namespaces and metadata filtering narrow the vector search space for a query. Consider the following when evaluating both filter types:
A namespace filter is applied before metadata filter(s).
A vector can only be part of a single namespace. Vector metadata can contain multiple key-value pairs. Metadata values support different types (string, boolean, and others), therefore offering more flexibility.