Supabase
Supabase provides an open source toolkit for developing AI applications using Postgres and pgvector. With Portkey integration, you can seamlessly generate embeddings using AI models like OpenAI and store them in Supabase, enabling efficient data retrieval. Portkey’s unified API supports over 250 models, making AI management more streamlined and secure.
Prerequisites
- Supabase project API Key and
- Portkey AI API key
Setting up your environment
First, let’s set up our Python environment with the necessary libraries:
Preparing your database
- Create a Supabase account
- Enable
pgvector
, an extension for PostgreSQL that allows you to both store and query vector embeddings within your database. Let’s try it out.
First we’ll enable the Vector extension. In Supabase, this can be done from the web portal through Database → Extensions. You can also do this in SQL by running:
- Next let’s create a table to store our documents and their embeddings:
pgvector
introduces a new data type calledvector
. In the code above, we create a column namedembedding
with thevector
data type. The size of the vector defines how many dimensions the vector holds. OpenAI’stext-embedding-ada-002
model outputs 1536 dimensions, so we will use that for our vector size. We also create atext
column namedcontent
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
Next, we’ll import the required libraries and set up our Supabase and Portkey clients:
Replace the placeholder values with your actual Supabase and Portkey credentials.
Generating and storing embeddings
Now, let’s create a function to generate embeddings using Portkey and OpenAI, and store them in Supabase:
This function takes a text input, generates an embedding using through Portkey, and then stores both the original text and its embedding in the 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 you will need to make a new table with
1024
dimensions instead of1536
dimensions for Cohere’sembed-english-v3.0
model.