Build an Agentic RAG AI App in Days with RDS Supabase & LangChain
This article demonstrates how to rapidly create a full‑stack Agentic Retrieval‑Augmented Generation (RAG) application using Alibaba Cloud RDS PostgreSQL‑based Supabase, covering data preparation, vector storage, real‑time communication, authentication, deployment steps, performance optimizations, and code examples with LangChain and large language models.
Introduction
RDS PostgreSQL‑based Supabase provides a fully managed Backend‑as‑a‑Service (BaaS) platform that accelerates AI application development by integrating database, authentication, storage, and real‑time communication capabilities.
Advantages over Traditional Development
Development & Deployment Efficiency : Integrated services reduce the development cycle from months to days.
Cost Optimization : Fully managed resources cut both infrastructure and personnel costs by more than 50%.
Scalability : Serverless elasticity and PostgreSQL plugins enable seamless scaling for traffic spikes.
What is Supabase?
Supabase is an open‑source BaaS built on PostgreSQL that offers API‑driven database management, user authentication, object storage, and real‑time messaging, allowing developers to build high‑performance, scalable applications without maintaining backend infrastructure.
Core Capabilities
Authentication & User Management (Auth)
User registration, logout, role management, and anonymous users.
Multiple authentication methods (email/password, phone, SSO, OAuth) via GoTrue.
Session management with JWT for fine‑grained permission control and Row‑Level Security (RLS) for data isolation.
Database Management (REST & GraphQL)
RESTful API via PostgREST with built‑in connection pooling.
GraphQL support through pg_graphql plugin.
Automatic endpoint generation (e.g., /rest/v1/messages) for CRUD operations.
Real‑time Communication (Realtime)
WebSocket‑based real‑time messaging leverages PostgreSQL logical replication to broadcast table changes instantly to connected clients, ideal for chat or collaborative tools.
Storage (S3‑compatible Object Store)
Provides S3‑compatible object storage with access control enforced by Auth’s RLS policies.
RDS Supabase Enhancements
Combines Alibaba Cloud RDS PostgreSQL’s enterprise‑grade performance, elasticity, and automated operations with Supabase’s BaaS features, delivering a fully managed backend solution.
Performance
Traditional Serverless: auto‑scales for development, testing, and bursty workloads.
Committed Serverless: fast‑up scaling + slow‑down scaling with local expansion to avoid cross‑zone latency, guaranteeing business continuity, high I/O throughput (up to 4 GB/s), and intelligent resource scheduling.
Disaster Recovery
One‑click cross‑Region replication and PITR recovery via physical replication and cloud‑disk snapshots, meeting diverse RTO/RPO requirements.
AI Enhancements
Over 100 plugins (e.g., pgvector, Apache Age, RUM) enable vector search, graph queries, and full‑text search, allowing rapid construction of GraphRAG, hybrid knowledge‑base search, and other AI workloads. Specialized vector indexes (ivfflat, HNSW, disk_ann) improve query speed and memory usage.
Overall Architecture
The architecture integrates Supabase services (Auth, Database, Realtime, Storage) on top of RDS PostgreSQL, providing a seamless development experience from data ingestion to AI‑driven decision making.
Practical Walkthrough: Building a Simple Agentic RAG App
Step 1 – Create RDS Supabase Instance
Log in to the RDS console and provision an RDS Supabase instance.
Step 2 – Enable Vector Extension and Create Tables
-- Enable the pgvector extension to work with embedding vectors
create extension if not exists vector;
-- Create a table to store your documents
create table documents(
id uuid primary key,
content text, -- Document text
metadata jsonb, -- Document metadata
embedding vector(1536) -- 1536‑dimensional embedding (OpenAI compatible)
);
-- Function to search for similar documents
create function match_documents(
query_embedding vector(1536),
filter jsonb default '{}'
) returns table(
id uuid,
content text,
metadata jsonb,
similarity float
) language plpgsql as $$
-- implementation omitted for brevity
$$;Step 3 – Set Environment Variables
DASHSCOPE_API_KEY="sk-*****"
SUPABASE_URL="http://<host>:<port>"
SUPABASE_SERVICE_KEY="eyJhbG..."Step 4 – Prepare RAG Data and Embed Documents
import os
from dotenv import load_dotenv
from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import SupabaseVectorStore
from langchain_community.embeddings.dashscope import DashScopeEmbeddings
from supabase import create_client, Client
load_dotenv()
supabase: Client = create_client(os.getenv("SUPABASE_URL"), os.getenv("SUPABASE_SERVICE_KEY"))
embeddings = DashScopeEmbeddings(model="text-embedding-v2", dashscope_api_key=os.getenv("DASHSCOPE_API_KEY"))
loader = PyPDFDirectoryLoader("documents")
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
chunks = text_splitter.split_documents(documents)
vector_store = SupabaseVectorStore.from_documents(
chunks,
embeddings,
client=supabase,
table_name="documents",
query_name="match_documents",
chunk_size=1000,
)Step 5 – Build the Agentic RAG Agent
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain import hub
from langchain_core.tools import tool
from langchain_community.vectorstores import SupabaseVectorStore
from langchain_community.embeddings.dashscope import DashScopeEmbeddings
from langchain_community.chat_models.tongyi import ChatTongyi
@tool(response_format="content_and_artifact")
def retrieve(query: str):
"""Retrieve information related to a query."""
docs = vector_store.similarity_search(query, k=2)
serialized = "
".join(
f"Source: {doc.metadata}
Content: {doc.page_content}" for doc in docs
)
return serialized, docs
tools = [retrieve]
llm = ChatTongyi(streaming=True)
prompt = hub.pull("hwchase17/openai-functions-agent")
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
response = agent_executor.invoke({"input": "why is agentic rag better than naive rag?"})
print(response["output"])Step 6 – Deploy with Streamlit for Multi‑turn Conversation
import streamlit as st
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_community.vectorstores import SupabaseVectorStore
from langchain_community.embeddings.dashscope import DashScopeEmbeddings
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.tools import tool
from supabase import create_client, Client
from dotenv import load_dotenv
import os
load_dotenv()
supabase: Client = create_client(os.getenv("SUPABASE_URL"), os.getenv("SUPABASE_SERVICE_KEY"))
embeddings = DashScopeEmbeddings(model="text-embedding-v2", dashscope_api_key=os.getenv("DASHSCOPE_API_KEY"))
vector_store = SupabaseVectorStore(embedding=embeddings, client=supabase, table_name="documents", query_name="match_documents")
@tool(response_format="content_and_artifact")
def retrieve(query: str):
docs = vector_store.similarity_search(query, k=2)
serialized = "
".join(f"Source: {doc.metadata}
Content: {doc.page_content}" for doc in docs)
return serialized, docs
tools = [retrieve]
llm = ChatTongyi(streaming=True)
prompt = hub.pull("hwchase17/openai-functions-agent")
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
st.set_page_config(page_title="Agentic RAG Chatbot", page_icon="🦜")
st.title("🦜 Agentic RAG Chatbot")
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
if isinstance(message, HumanMessage):
with st.chat_message("user"):
st.markdown(message.content)
elif isinstance(message, AIMessage):
with st.chat_message("assistant"):
st.markdown(message.content)
user_question = st.chat_input("How are you?")
if user_question:
with st.chat_message("user"):
st.markdown(user_question)
st.session_state.messages.append(HumanMessage(user_question))
result = agent_executor.invoke({"input": user_question, "chat_history": st.session_state.messages})
ai_message = result["output"]
with st.chat_message("assistant"):
st.markdown(ai_message)
st.session_state.messages.append(AIMessage(ai_message))Conclusion
By tightly integrating Alibaba Cloud RDS PostgreSQL with Supabase, developers can build next‑generation AI applications at lower cost and higher speed, truly realizing the "code as infrastructure" vision.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
