Prerequisites
- Supabase project API Key
- Portkey API key
Setting up your environment
First, let’s set up our Python environment with the necessary libraries:Preparing your database
1
Create a Supabase Project
Go to Supabase and create a new project.
2
Enable pgvector extension
pgvector
is an extension for PostgreSQL that allows you to both store and query vector embeddings within your database. We can enable it from the web portal through Database → Extensions. You can also do this in SQL by running:3
Create a table to store our documents and their embeddings
pgvector
introduces a new data type called vector
. In the code above, we create a column named embedding
with the vector
data type. The size of the vector defines how many dimensions the vector holds. OpenAI’s text-embedding-ada-002
model outputs 1536 dimensions, so we will use that for our vector size.
We also create a text
column named content
to store the original document text that produced this embedding. Depending on your use case, you might just store a reference (URL or foreign key) to a document here instead.Configuring Supabase and Portkey
Now, let’s import the required libraries and set up our Supabase and Portkey clients:Generating and storing embeddings
Let’s create a function to generate embeddings using Portkey and OpenAI, and store them in Supabase:documents
table.
Portkey supports 250+ Models, you can choose any model just by changing the provider
and virtual_key
Here’s an example on how to use Cohere
with Portkey
Note that you will need to make a new table with
1024
dimensions instead of 1536
dimensions for Cohere’s embed-english-v3.0
model.