VOOZH about

URL: https://dev.to/0012303/supabase-vector-has-a-free-api-build-ai-search-in-minutes-ckh

⇱ Supabase Vector Has a Free API — Build AI Search in Minutes - DEV Community


TL;DR

Supabase now includes pgvector out of the box — giving you a free vector database alongside your PostgreSQL database. Build semantic search, RAG pipelines, and recommendation engines without a separate vector DB.

What Is Supabase Vector?

Supabase is the open-source Firebase alternative built on PostgreSQL. With the addition of pgvector, every Supabase project becomes an AI-ready database:

  • pgvector extension — vector similarity search natively in PostgreSQL
  • Free tier — 500 MB database with vector support included
  • REST API — auto-generated API for all your tables (including vector columns)
  • Edge Functions — run embedding generation at the edge
  • Auth + RLS — secure your vector data with Row Level Security

Setting Up Vector Search

1. Enable pgvector

-- Enable the vector extension (one-time setup)
CREATE EXTENSION IF NOT EXISTS vector;

-- Create a table with embeddings
CREATE TABLE documents (
 id BIGSERIAL PRIMARY KEY,
 title TEXT NOT NULL,
 content TEXT NOT NULL,
 embedding VECTOR(1536) -- OpenAI ada-002 dimensions
);

-- Create an index for fast similarity search
CREATE INDEX ON documents
 USING ivfflat (embedding vector_cosine_ops)
 WITH (lists = 100);

2. Store Embeddings via API

import { createClient } from "@supabase/supabase-js";

const supabase = createClient(
 "https://YOUR_PROJECT.supabase.co",
 "YOUR_ANON_KEY"
);

// Generate embedding with OpenAI
const embeddingResponse = await fetch(
 "https://api.openai.com/v1/embeddings",
 {
 method: "POST",
 headers: {
 Authorization: `Bearer ${OPENAI_KEY}`,
 "Content-Type": "application/json",
 },
 body: JSON.stringify({
 model: "text-embedding-ada-002",
 input: "Your document text here",
 }),
 }
);

const { data } = await embeddingResponse.json();

// Store in Supabase
await supabase.from("documents").insert({
 title: "My Document",
 content: "Your document text here",
 embedding: data[0].embedding,
});

3. Semantic Search with RPC

-- Create a search function
CREATE OR REPLACE FUNCTION match_documents(
 query_embedding VECTOR(1536),
 match_threshold FLOAT,
 match_count INT
)
RETURNS TABLE (
 id BIGINT,
 title TEXT,
 content TEXT,
 similarity FLOAT
)
LANGUAGE SQL STABLE
AS $$
 SELECT
 id, title, content,
 1 - (embedding <=> query_embedding) AS similarity
 FROM documents
 WHERE 1 - (embedding <=> query_embedding) > match_threshold
 ORDER BY (embedding <=> query_embedding)
 LIMIT match_count;
$$;
// Call from your app
const { data: results } = await supabase.rpc("match_documents", {
 query_embedding: queryVector,
 match_threshold: 0.78,
 match_count: 10,
});

RAG Pipeline with Supabase + OpenAI

async function askQuestion(question) {
 // 1. Generate embedding for the question
 const embedding = await generateEmbedding(question);

 // 2. Find relevant documents
 const { data: docs } = await supabase.rpc("match_documents", {
 query_embedding: embedding,
 match_threshold: 0.7,
 match_count: 5,
 });

 // 3. Build context from matched documents
 const context = docs.map((d) => d.content).join("\n\n");

 // 4. Ask GPT with context
 const response = await openai.chat.completions.create({
 model: "gpt-4",
 messages: [
 {
 role: "system",
 content: `Answer based on this context:\n${context}`,
 },
 { role: "user", content: question },
 ],
 });

 return response.choices[0].message.content;
}

Supabase Vector vs Alternatives

Feature Supabase Pinecone Weaviate Qdrant
Free Tier 500 MB 100K vectors Self-host Self-host
SQL Support ✅ Full PostgreSQL
Auth Built-in ✅ RLS
REST API ✅ Auto-generated ✅ REST+GraphQL
Hybrid Search ✅ (SQL + vector)
Managed ✅ Cloud ✅ Cloud

Why Supabase Vector?

  1. No separate service — vectors live alongside your regular data
  2. SQL + vectors — combine traditional filters with semantic search
  3. Built-in auth — RLS policies apply to vector queries too
  4. Free forever tier — 500 MB is enough for millions of embeddings
  5. Open source — self-host if you need more control

Resources


Building an AI app that needs web data? My Apify scraping tools can extract data from any website — feed it into your Supabase vector store for powerful semantic search. Questions? Email spinov001@gmail.com