new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Supabase · all subjects

ai-tools

59 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

AI provider integrations with Supabase

Supabase provides integrations with the following AI providers: OpenAI, Amazon Bedrock, Hugging Face, LangChain, and LlamaIndex.

Search types available in Supabase

Supabase supports three types of search features: semantic search (search by meaning rather than exact keywords), keyword search (search by words or phrases), and hybrid search (combine semantic search with keyword search).

Supabase AI toolkit components

Supabase provides an open source toolkit for developing AI applications using Postgres and pgvector. The toolkit includes: a vector store and embeddings support using Postgres and pgvector; a Python client for managing unstructured embeddings; an embedding generation process using open source models directly in Edge Functions; database migrations for managing structured embeddings; and integrations with popular AI providers including OpenAI, Hugging Face, LangChain, Amazon Bedrock, and LlamaIndex.

Vector store and embeddings in Supabase

Supabase uses Postgres and pgvector to provide vector store capabilities and embeddings support. These enable storing, indexing, and querying vector embeddings at scale.

Three steps to build similarity search in documentation

There are three steps to build similarity search inside documentation: 1) Prepare your database, 2) Ingest your documentation, and 3) Add a search interface.

Database setup for Headless Vector Search

To set up the database: 1) Create a new Supabase project and store database and API credentials from project settings, 2) Clone the Headless Vector Search repo with `git clone git@github.com:supabase/headless-vector-search.git`, 3) Link the repo to your remote project with `supabase link --project-ref XXX`, 4) Apply database migrations with `supabase db push`, 5) Set your OpenAI key as a secret with `supabase secrets set OPENAI_API_KEY=sk-xxx`, 6) Deploy Edge Functions with `supabase functions deploy --no-verify-jwt`, 7) Expose the `docs` schema via API in Supabase Dashboard settings > API Settings > Exposed schemas.

GitHub Action for embedding documentation

Use the Supabase Embeddings Generator GitHub Action (https://github.com/marketplace/actions/supabase-embeddings-generator) to automatically update your database when Pull Requests are made. Create a workflow file at `.github/workflows/generate_embeddings.yml` that runs on main branch changes.

GitHub Action embeddings workflow configuration

The GitHub Action workflow file `.github/workflows/generate_embeddings.yml` should contain: name 'generate_embeddings', trigger on push to main branch, run ubuntu-latest, checkout v3 action, then supabase/embeddings-generator@v0.0.x action with parameters: supabase-url (your project URL), supabase-secret-key (repository secret), openai-key (repository secret), and docs-root-path (path to markdown files). Store SUPABASE_SECRET_KEY and OPENAI_API_KEY as repository secrets in settings > secrets > actions.

Headless search interface implementation requirement

The search interface is language-agnostic and headless. The only requirement is to send the user query to the `query` Edge Function, which streams an answer back from OpenAI.

Streaming search results with EventSource

Use EventSource API to stream results from the query Edge Function. Send the user query as URLSearchParams to the Edge Function endpoint at `https://your-project-ref.supabase.co/functions/v1/{query}`. Listen for 'message' events which contain JSON responses with completion text, and '[DONE]' signal when streaming is complete. Handle 'error' events for error cases.

Query Edge Function streaming implementation example

Example JavaScript implementation for streaming search: ```js const onSubmit = (e: Event) => { e.preventDefault() answer.value = "" isLoading.value = true const query = new URLSearchParams({ query: inputRef.current!.value }) const projectUrl = `https://your-project-ref.supabase.co/functions/v1` const queryURL = `${projectUrl}/${query}` const eventSource = new EventSource(queryURL) eventSource.addEventListener("error", (err) => { isLoading.value = false console.error(err) }) eventSource.addEventListener("message", (e: MessageEvent) => { isLoading.value = false if (e.data === "[DONE]") { eventSource.close() return } const completionResponse: CreateCompletionResponse = JSON.parse(e.data) const text = completionResponse.choices[0].text answer.value += text }); isLoading.value = true } ``` This example shows how to submit a search query to the Edge Function, handle streaming responses, accumulate text, and detect when streaming is complete.

Headless Vector Search tech stack

The tech stack consists of Supabase for database and Edge Functions, OpenAI for embeddings and completions, and GitHub Actions for ingesting markdown documentation.

Headless Vector Search Toolkit overview

Supabase provides a Headless Search Toolkit for adding generative Q&A to documentation. The toolkit is headless, allowing integration into existing websites with custom styling. It enables ChatGPT-style documentation search powered by vector embeddings.

Headless Vector Search toolkit components

The toolkit consists of two parts: the Headless Vector Search template (available at https://github.com/supabase/headless-vector-search) that you can deploy in your organization, and a GitHub Action (https://github.com/supabase/embeddings-generator) that ingests markdown files, converts them to embeddings, and stores them in the database.

Hugging Face model selection for text-to-image

Hugging Face recommends using the stabilityai/stable-diffusion-2 model for text-to-image generation. Other text-to-image models are available and can be found by filtering on the Hugging Face models page at https://huggingface.co/models?pipeline_tag=text-to-image.

Three ways to use Hugging Face models with Supabase

There are three approaches to integrate Hugging Face models: (1) Use the Transformers Python library for inference in a Python backend, (2) Generate embeddings directly in Edge Functions using Transformers.js, (3) Use Hugging Face's hosted Inference API to execute AI tasks remotely on Hugging Face servers.

Hugging Face AI task categories

Hugging Face supports natural language tasks (summarization, text classification, text generation, translation, fill-in-the-blank), computer vision tasks (image-to-text, text-to-image, image classification, video classification, object detection, image segmentation), and audio tasks (text-to-speech, speech-to-text, audio classification).

Creating a Hugging Face access token

Generate a Hugging Face access token at https://huggingface.co/settings/tokens. Name tokens based on the app and environment (e.g., 'Image Generator (Dev)' and 'Image Generator (Prod)'). For the Inference API, select the read role. While the Inference API can work without a token, using a token prevents rate limiting and unexpected downtime.

use_cache parameter in Hugging Face inference

The use_cache parameter in Hugging Face inference controls response caching. Set use_cache to false to ensure repeat queries with the same input produce new results (useful for generative tasks like image generation). Set use_cache to true for deterministic tasks that always produce the same output from the same input, which will provide faster responses through caching.

RRF smoothing constant k

To prevent extremely high scores for items ranked first in RRF, a smoothing constant k is added to the denominator, using the formula 1/(k+rank) instead of 1/rank. This constant is typically a small positive number. For example, with k=1, a record ranked first scores 1/(1+1) = 0.5 instead of 1. This adjustment helps balance the influence of items ranked very high in individual lists when creating the final combined list.

Hybrid search definition

Hybrid search combines full text search (searching by keyword) with semantic search (searching by meaning) to identify results that are both directly and contextually relevant to the user's query.

Hybrid search use cases

Hybrid search is useful for applications where users may search for specific keywords and also benefit from contextually related suggestions. For a code repository where developers need exact lines of code, keyword search alone is better. In a mental health forum where semantic meaning matters, semantic search alone is better. For a shopping app where customers search for specific product names yet are open to related suggestions, hybrid search combines both strengths.

Hybrid search fusion process

Hybrid search works by executing keyword search and semantic search separately to produce two result lists. These lists are then combined through a fusion process that merges results together based on a ranking or scoring system, prioritizing results based on factors like relevance and ranking in individual lists. The result is a unified list integrating the strengths of both search methods.

Reciprocal Ranked Fusion (RRF) scoring

Reciprocal Ranked Fusion (RRF) assigns a score to each record by calculating 1 divided by the record's rank in each list, summed together. For example, a record ranked 3rd in keyword search and 9th in semantic search receives a score of 1/3 + 1/9 = 0.444. Records found in only one list receive 0 for the other list. Records are then sorted by score, with highest scores ranked first. This method ensures items ranked high in multiple lists receive high final rank, while items ranked high in only a few lists but low in others do not receive high rank.

LangChain hybrid search combines similarity and full text search

LangChain supports hybrid search that combines Similarity Search with Full Text Search. The Supabase Hybrid Search function can be installed through the database.dev package manager at langchain/hybrid_search.

LangChain vector store integration with Supabase

LangChain is a framework for working with AI, vectors, and embeddings that supports using Supabase as a vector store through the pgvector extension.

LangChain Supabase documents table schema

The documents table for LangChain integration has the following columns: id (bigserial primary key), content (text, corresponds to Document.pageContent), metadata (jsonb, corresponds to Document.metadata), and embedding (extensions.vector with 1536 dimensions for OpenAI embeddings, adjustable if needed).

LangChain match_documents PostgreSQL function

The match_documents function takes parameters: query_embedding (extensions.vector, 1536 dimensions), match_count (int, default null), and filter (jsonb, default '{}'). It returns a table with columns: id (bigint), content (text), metadata (jsonb), and similarity (float). The function calculates similarity as 1 - (documents.embedding <=> query_embedding), filters by metadata using JSONB containment operator @>, and orders results by embedding distance.

Enable pgvector extension for LangChain

Create the pgvector extension with the command: create extension vector with schema extensions;

LangChain SupabaseVectorStore initialization with texts

Example code showing how to initialize SupabaseVectorStore from texts: const vectorStore = await SupabaseVectorStore.fromTexts(['Hello world', 'Bye bye', "What's this?"], [{ id: 2 }, { id: 1 }, { id: 3 }], new OpenAIEmbeddings(), { client, tableName: 'documents', queryName: 'match_documents' });

LangChain similarity search with metadata filtering

The SupabaseVectorStore.similaritySearch method accepts a third parameter for metadata filtering. The filter parameter is a JSON object that uses the Postgres JSONB Containment operator @> to filter documents by metadata field values. Example: vectorStore.similaritySearch('Hello world', 1, { user_id: 3 });

LangChain advanced filtering with SupabaseFilterRPCCall

Advanced metadata filtering can use query builder-style filtering via SupabaseFilterRPCCall. Filter properties in the metadata column use arrow operators: -> for integer values and ->> for text values, with data type casting (e.g., metadata->b::int). Example: const funcFilter = (rpc) => rpc.filter('metadata->b::int', 'lt', 3).filter('metadata->>stuff', 'eq', 'right');

LangChain SupabaseVectorStore requires environment variables

LangChain integration requires SUPABASE_SECRET_KEY and SUPABASE_URL environment variables to create a Supabase client for use in SupabaseVectorStore operations.

LangChain addDocuments method with metadata

The SupabaseVectorStore.addDocuments method accepts an array of documents with pageContent and metadata properties. Example structure: { pageContent: 'hello', metadata: { b: 1, c: 9, stuff: 'right' } }

vecs collection parameters

When creating or getting a vecs collection, the name parameter specifies the collection identifier and the dimension parameter specifies the vector dimensionality (number of elements in each vector).

Create vecs client and collection example

To create a vector store client and collection named 'docs' with 3 dimensions: import vecs; vx = vecs.create_client("postgresql://postgres:postgres@localhost:54322/postgres"); docs = vx.get_or_create_collection(name="docs", dimension=3)

vecs Python client for vector management

Supabase provides a Python client called vecs for managing unstructured vector stores in Postgres. The vecs client provides tools for creating and querying collections in Postgres using the pgvector extension.

Upsert vectors into vecs collection example

To insert embeddings into a vecs collection: docs = vecs.get_or_create_collection(name="docs", dimension=3); vectors=[("vec0", [0.1, 0.2, 0.3], {"year": 1973}), ("vec1", [0.7, 0.8, 0.9], {"year": 2012})]; docs.upsert(vectors=vectors). Each vector is a tuple of (id, embedding array, metadata dict).

Query vecs collection with filters example

To query a vecs collection: docs = vecs.get_or_create_collection(name="docs", dimension=3); docs.query(data=[0.4,0.5,0.6], limit=1, filters={"year": {"$eq": 2012}}). The data parameter is required and specifies the query vector. The limit parameter specifies number of records to return. The filters parameter uses metadata filtering syntax.

Sentry JavaScript SDK version requirement for Supabase integration

The built-in Supabase integration in Sentry requires Sentry JavaScript SDK version 9.14.0 or later. If you are using an older SDK version (including v7), you must use the community package @supabase/sentry-js-integration instead, which the built-in integration is based on.

Sentry Supabase integration capabilities

The Sentry JavaScript SDK has a built-in integration for Supabase that instruments database queries and authentication calls made through supabase-js, creating spans for performance monitoring and capturing errors. It supports browser, Node, and edge environments.

Enable Sentry Supabase integration via Sentry.init

To enable the integration when your Sentry.init call and Supabase client live in the same place, add supabaseIntegration to the integrations list when initializing Sentry. Example: Sentry.init({ dsn: SENTRY_DSN, tracesSampleRate: 1.0, integrations: [Sentry.browserTracingIntegration(), Sentry.supabaseIntegration({ supabaseClient })]})

Enable Sentry Supabase integration via instrumentSupabaseClient

Call Sentry.instrumentSupabaseClient where you create the client. This approach is better for frameworks like Next.js where Sentry.init runs in a separate config file. You must instrument each client you create because it patches database calls per runtime (browser, server, edge) and auth calls per client instance. Setups that create a client per request (like @supabase/ssr) must instrument each one.

Sentry Supabase integration redaction of query data by default

By default, query filters and mutation bodies are redacted from spans and breadcrumbs in Sentry. To capture them, pass sendOperationData: true when setting up instrumentation (either Sentry.supabaseIntegration({ supabaseClient, sendOperationData: true }) or Sentry.instrumentSupabaseClient(client, { sendOperationData: true })), or enable dataCollection: { userInfo: true } in your Sentry.init options, which applies to every client in that runtime.

Deduplicating Sentry spans for Supabase REST calls

Sentry's HTTP and Fetch tracing integrations are enabled by default in the Node and Next.js SDKs, so Supabase REST calls are traced as http.client spans in addition to db spans from the Supabase integration. To avoid duplicate spans, configure the HTTP/Fetch integration to skip Supabase REST requests by checking if the URL starts with ${SUPABASE_URL}/rest using shouldCreateSpanForRequest (for browser/Next.js) or ignoreOutgoingRequests (for Node) or shouldCreateSpanForRequest (for winterCGFetchIntegration in Proxy & Edge Functions).

Sentry configuration for Next.js with Supabase

For Next.js applications using Supabase, run the Sentry Next.js wizard to set up base configuration, then add Sentry.instrumentSupabaseClient to each client factory (server client with @supabase/ssr, browser client with createBrowserClient, and middleware client) since Next.js runs Sentry across browser, server, and edge runtimes and auth-aware setups create a client per request. To include query filters and mutation bodies, enable dataCollection: { userInfo: true } in each runtime's Sentry config or pass sendOperationData: true at the call site. Build and run your application with npm run build && npm run start to see Supabase queries as db spans in Sentry traces.

Amazon Bedrock integration overview

Amazon Bedrock is a fully managed service offering high-performing foundation models from AI21 Labs, Anthropic, Cohere, Meta, Mistral AI, Stability AI, and Amazon. Each model is accessible through a common API with features for security, privacy, and responsible AI. Supabase can integrate with Amazon Bedrock to create embeddings and store them in Postgres using vecs.

Amazon Bedrock environment setup requirements

To set up Amazon Bedrock with Supabase, you need Python 3.7 or higher, the vecs and boto3 libraries installed via pip, AWS account credentials configured, and a Postgres database with the pgvector extension enabled.

Amazon Bedrock boto3 client configuration

The boto3 bedrock-runtime client requires the following parameters: region_name (e.g., 'us-east-1'), aws_access_key_id, aws_secret_access_key, and aws_session_token for AWS credentials.

Amazon Bedrock invoke_model API parameters

The boto3 invoke_model method for bedrock-runtime requires: body (JSON string with inputText), modelId (e.g., 'amazon.titan-embed-text-v1'), accept (e.g., 'application/json'), and contentType (e.g., 'application/json').

Use Session pooler connection string with Google Colab

Google Colab does not support IPv6, so the Session pooler connection string must be used when connecting to Supabase from Colab notebooks instead of other connection types.

Supabase vector_hello_world Colab notebook location

The example notebook is available at https://github.com/supabase/supabase/blob/master/examples/ai/vector_hello_world.ipynb and can be launched directly in Google Colab. Copy the notebook to Google Drive using the 'Copy to Drive' button at the top of the notebook.

Connection string format must start with postgresql:// for SQLAlchemy

SQLAlchemy requires the connection string to start with 'postgresql://' instead of 'postgres://'. When copying the connection string from the Supabase dashboard, rename the protocol prefix if necessary.

MCP (Model Context Protocol) definition and capabilities

MCP is a live connection between an AI agent and a Supabase project. Once connected, an agent can call tools to query data, run migrations, deploy Edge Functions, and more.

Agent Skills definition and purpose

Agent Skills are portable, on-demand instructions that agents load when they need Supabase or Postgres-specific procedural knowledge. Skills do not require a live connection and work across different agents.

Plugin definition in Supabase AI tools

A Plugin is a single install that bundles the MCP server and Agent Skills together for a specific agent.

Supabase Evals benchmark for AI coding agents

Supabase Evals is an open-source benchmark that tests how AI coding agent tools perform on real Supabase tasks.

Prompts for AI agents without native support

Prompts are static prompt files that can be copied into a project for agents that do not support MCP, plugins, or skills natively.

Supabase provides for connecting AI coding agents

Supabase provides MCP (a live connection to database and platform), Agent Skills (portable instructions), Plugins (one-step bundle of MCP and skills), and copy-paste prompts for tools that do not support the above.

Give your agent this brain