Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Supabase 11 min read How-to mediumAdept

Storing OpenAI embeddings in Postgres with pgvector

pgvector adds a vector data type and distance operators (Euclidean <->, negative inner product <#>, cosine <=>) to Postgres, so OpenAI text-embedding-ada-002 embeddings (1536 dimensions) can be stored and queried for similarity directly in the database. The walkthrough creates a documents table, a match_documents SQL function that filters by a cosine-similarity threshold, and an IVFFlat index sized at roughly 4 * sqrt(rows) lists. It then generates embeddings in JavaScript and builds Deno Edge Functions for semantic search and for retrieval-augmented answers with text-davinci-003, keeping injected context under about 1500 tokens to fit the 4000-token limit.

Related posts

  • Building a RAG application with Llama 3.1 and pgvector

    Neon mediumAdept

    A small RAG pipeline built on Neon Postgres with pgvector as the vector store, OctoAI's gte-large embedding model producing 1024-dimensional vectors, and Llama 3.1 405B generating answers. The walkthrough enables the vector extension, creates a table with a VECTOR(1024) column, and embeds and inserts a CSV of fake quotes from Python via psycopg2. At query time it embeds the user input, retrieves the two nearest quotes with a cosine-distance ORDER BY ... LIMIT 2 query, and injects them into the system prompt. Embedding dimension directly drives storage cost, e.g. 1024 for gte-large versus up to 3072 for OpenAI's model.

  • Semantic search using OpenAI, pg_embedding and Neon

    Neon mediumAdept

    A YC idea-matcher app implements semantic search by embedding ~4,400 YCombinator company descriptions with OpenAI's text-embedding-ada-002 (1536 dimensions) and storing them as real[] columns in Neon Postgres via the pg_embedding extension. Queries embed the user's idea and order rows by cosine distance (<=>) to return the top five matches from a Next.js edge function, which is colocated with the database region and uses the Neon serverless driver, Upstash rate limiting and Zod validation. The full ingestion script and API route are shown. Sequential scan is fine for this small dataset, but larger tables need an HNSW index. pg_embedding has since been deprecated in favor of pgvector.

  • ChatGPT plugins now support Postgres & Supabase

    Supabase mediumAdept

    Supabase contributed Postgres and Supabase datastore providers to OpenAI's ChatGPT Retrieval Plugin, storing text-embedding-ada-002 embeddings in pgvector. The Postgres provider connects directly via psycopg2, while the Supabase provider goes through PostgREST to enable Row Level Security. Both share the same documents schema and use a plpgsql match function that filters by source, author and date and ranks results by inner product distance. A walkthrough ingests the PostgreSQL 15 docs (about 7,904 chunks), adds an approximate ivfflat index with lists ≈ rows/1000, and registers the local plugin with ChatGPT to answer documentation questions.

  • Hugging Face is now supported in Supabase

    Supabase mediumAdept

    Supabase adds first-class Hugging Face support for generating embeddings stored in pgvector, as an open-source, self-hostable alternative to OpenAI's text-embedding-ada-002. The Python vecs client gains "adapters" that chunk text and embed it automatically on upsert and query. Edge Functions can run Transformers.js inference in Deno, and small models can even run in the browser and query pgvector through PostgREST and RLS. Supabase recommends lower-dimension models such as gte-small for cheaper storage and faster retrieval, and notes current limits: 2-6s cold starts from model downloads, and no turnkey audio/image decoding in Deno.

  • How to Generate & Store OpenAI Vector Embeddings with Turso

    Turso mediumAdept

    Turso's beta vector support stores OpenAI text-embedding-ada-002 embeddings in a libSQL F32_BLOB(1536) column, whose size matches the model's 1536 dimensions. A DiskANN index with a cosine metric, declared through libsql_vector_idx, handles similarity search. The walkthrough covers creating a vector-enabled group and database with the Turso CLI, generating embeddings with the OpenAI Node SDK, and inserting them as Float32Array buffers in batches of 50 via db.batch transactions. Larger documents such as PDFs need to be chunked before embedding to stay within API limits.

The quest for software engineering knowledge.

Look:
Layout: