Calling Vector Database Functions via RPC
Once a database function is registered on your PostgreSQL server, you can trigger it from your frontend application using the Supabase JavaScript client SDK.
1. Using the .rpc() Method
The Supabase client provides an .rpc() method designed to execute database stored procedures.
Here is the configuration for querying our registered match_documents function:
import { supabase } from "../lib/supabase";
async function findSimilarDocuments(searchVector: number[]) {
const { data, error } = await supabase.rpc("match_documents", {
// Parameter keys must match the SQL function parameter names exactly
query_embedding: searchVector,
match_threshold: 0.78, // Only match similarity scores above 78%
match_count: 5, // Return top 5 matches
});
if (error) {
console.error("RPC execution failed:", error.message);
return [];
}
// Returns rows containing the fields: id, content, similarity
console.log("Found matches:", data);
return data;
}2. Complete Frontend Integration Flow
In a typical AI search setup (such as a documentation semantic search bar):
- The user types a query string in your search input field.
- The application sends the text to an embedding model API (such as OpenAI Embeddings API) to generate a search vector.
- The application calls
supabase.rpc()passing the search vector. - The database calculates similarity scores, filters rows based on RLS and your threshold parameters, and returns matching sections.
- The application displays the text results to the user.
Published on Last updated: