AI & Vectors

Google Colab

Use Google Colab to manage your Supabase Vector store.


Google Colab is a hosted Jupyter Notebook service. It provides free access to computing resources, including GPUs and TPUs, and is well-suited to machine learning, data science, and education. We can use Colab to manage collections using Supabase Vecs.

In this tutorial we'll connect to a database running on the Supabase platform. If you don't already have a database, you can create one here: database.new.

Create a new notebook

Start by visiting colab.research.google.com. There you can create a new notebook.

Google Colab new notebook

Install Vecs

We'll use the Supabase Vector client, Vecs, to manage our collections.

At the top of the notebook add the notebook paste the following code and hit the "execute" button (ctrl+enter):


_10
pip install vecs

Install vecs

Connect to your database

Find the Postgres pooler connection string for your Supabase project in the database settings of the dashboard. Copy the "URI" format, which should look something like postgres://postgres.xxxx:password@xxxx.pooler.supabase.com:6543/postgres

Create a new code block below the install block (ctrl+m b) and add the following code using the Postgres URI you copied above:


_10
import vecs
_10
_10
DB_CONNECTION = "postgres://postgres.xxxx:password@xxxx.pooler.supabase.com:6543/postgres"
_10
_10
# create vector store client
_10
vx = vecs.create_client(DB_CONNECTION)

Execute the code block (ctrl+enter). If no errors were returned then your connection was successful.

Create a collection

Now we're going to create a new collection and insert some documents.

Create a new code block below the install block (ctrl+m b). Add the following code to the code block and execute it (ctrl+enter):


_16
collection = vx.get_or_create_collection(name="colab_collection", dimension=3)
_16
_16
collection.upsert(
_16
vectors=[
_16
(
_16
"vec0", # the vector's identifier
_16
[0.1, 0.2, 0.3], # the vector. list or np.array
_16
{"year": 1973} # associated metadata
_16
),
_16
(
_16
"vec1",
_16
[0.7, 0.8, 0.9],
_16
{"year": 2012}
_16
)
_16
]
_16
)

This will create a table inside your database within the vecs schema, called colab_collection. You can view the inserted items in the Table Editor, by selecting the vecs schema from the schema dropdown.

Colab documents

Query your documents

Now we can search for documents based on their similarity. Create a new code block and execute the following code:


_10
collection.query(
_10
query_vector=[0.4,0.5,0.6], # required
_10
limit=5, # number of records to return
_10
filters={}, # metadata filters
_10
measure="cosine_distance", # distance measure to use
_10
include_value=False, # should distance measure values be returned?
_10
include_metadata=False, # should record metadata be returned?
_10
)

You will see that this returns two documents in an array ['vec1', 'vec0']:

Colab results

It also returns a warning:


_10
Query does not have a covering index for cosine_distance.

You can lean more about creating indexes in the Vecs documentation.

Resources