RTX 3050 - Order Now
Home / Blog / Tutorials / Connect Supabase to Self-Hosted AI on GPU
Tutorials

Connect Supabase to Self-Hosted AI on GPU

Add AI capabilities to your Supabase backend using a self-hosted LLM on GPU. This guide covers Edge Functions that call your inference endpoint, database triggers for AI processing, and building AI-powered features with Supabase + GPU infrastructure.

What You’ll Connect

After this guide, your Supabase project will have AI features powered by your own GPU server — no API costs, no rate limits. Supabase Edge Functions call your vLLM or Ollama endpoint on dedicated GPU hardware, and database triggers automatically process new rows through your self-hosted LLM.

This integration is ideal for applications that store data in Supabase and need AI enrichment — chat applications, content platforms, or analytics dashboards. The AI processing runs on your GPU while Supabase handles authentication, storage, and real-time subscriptions.

Supabase Edge Function –> fetch() to GPU Server –> vLLM Inference (web/mobile) /functions/v1/ai-chat /v1/chat/completions on dedicated GPU | | Real-time <-- Supabase Realtime <-- Edge Function writes <-- Completion subscription pushes updates result to database returned | DB Trigger --> pg_net HTTP call –> GPU Server (alternative path) –>

Prerequisites

  • A GigaGPU server with a running LLM on an OpenAI-compatible API (self-host guide)
  • A Supabase project with the CLI installed (npm install -g supabase)
  • Deno runtime for local Edge Function development
  • HTTPS access to your GPU server (Nginx proxy guide)
  • GPU API key stored as a Supabase secret (security guide)

Integration Steps

Create a Supabase Edge Function: supabase functions new ai-chat. This generates a Deno-based function in supabase/functions/ai-chat/. The function receives a request body with a user prompt, calls your GPU server’s OpenAI-compatible API via fetch(), and returns the completion.

Set your GPU API key as a Supabase secret: supabase secrets set GPU_API_KEY=sk-your-key GPU_API_URL=https://your-gpu-server.gigagpu.com. Deploy the function: supabase functions deploy ai-chat. Your client application can now invoke the function via Supabase’s client library.

For automated AI processing, create a database table with a trigger. When a new row is inserted (e.g., a user message), a PostgreSQL trigger calls the pg_net extension to POST to your Edge Function, which processes the text through the GPU model and writes the result back to the database.

Code Example

This Supabase Edge Function calls your GPU inference server and returns a streamed response:

// supabase/functions/ai-chat/index.ts
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";

serve(async (req) => {
  const { messages } = await req.json();
  const GPU_URL = Deno.env.get("GPU_API_URL")!;
  const API_KEY = Deno.env.get("GPU_API_KEY")!;

  const response = await fetch(`${GPU_URL}/v1/chat/completions`, {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "meta-llama/Llama-3-70b-chat-hf",
      messages: [
        { role: "system", content: "You are a helpful assistant." },
        ...messages,
      ],
      max_tokens: 1024,
      stream: true,
    }),
  });

  return new Response(response.body, {
    headers: { "Content-Type": "text/event-stream" },
  });
});

// Client-side usage with Supabase JS:
// const { data } = await supabase.functions.invoke('ai-chat', {
//   body: { messages: [{ role: 'user', content: 'Hello!' }] }
// });

Testing Your Integration

Test locally with supabase functions serve ai-chat --env-file .env.local. Send a POST request to the local function URL with a messages array. Verify the response streams tokens from your GPU server. Deploy and test the production function via your Supabase project URL.

Test the database trigger path by inserting a row into your messages table and verifying the AI response column populates automatically. Check the Supabase Edge Function logs for any errors or timeout issues.

Production Tips

Supabase Edge Functions run on Deno Deploy with a 150-second execution limit. Streaming responses keep the connection alive as long as tokens flow, so long completions work well. For non-streaming use cases, keep max_tokens reasonable to complete within the timeout.

Use Supabase’s Row Level Security (RLS) to control which users can invoke AI functions. Pair the Edge Function with Supabase Auth so each request is authenticated and rate-limited per user. This prevents abuse without building custom auth middleware.

The Supabase + GPU architecture works well for building full-stack AI applications. Supabase handles auth, database, storage, and real-time features while your open-source model on dedicated GPU handles inference. Browse more tutorials or get started with GigaGPU to power your Supabase AI features.

Need a Dedicated GPU Server?

Deploy from RTX 3050 to RTX 5090. Full root access, NVMe storage, 1Gbps — UK datacenter.

Browse GPU Servers

admin

We benchmark, deploy, and optimise GPU infrastructure for AI workloads. All data in our guides comes from real-world testing on our UK-based dedicated GPU servers.

Ready to deploy your AI workload?

Dedicated GPU servers from our UK datacenter. NVMe storage, 1Gbps networking, full root access.

Browse GPU Servers Contact Sales

Have a question? Need help?