Production-quality code examples showing how to build with HeliosDB. Copy, adapt, and ship.
Build a complete Retrieval-Augmented Generation system with HeliosDB as the vector store. Chunk documents, generate embeddings, store them with HNSW + Product Quantization, and retrieve context for LLM generation.
class RAGBackend:
def search(self, query: str, limit=5):
embedding = self.embed(query)
return requests.post(
f"{self.url}/api/v1/vectors/search",
json={
"table": "documents",
"column": "embedding",
"query_vector": embedding,
"limit": limit,
"metric": "cosine"
}
).json()["data"]["results"]
def answer(self, question: str):
context = self.search(question)
return openai.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system",
"content": f"Context:\n{context}"},
{"role": "user",
"content": question}
]
)
Implement persistent memory for AI agents with automatic context summarization. Store conversation history, track token counts, and auto-summarize long sessions to stay within context windows.
const memory = new AgentMemory(url, token);
// Start conversation
const session = await memory.createSession(
"support-bot", "user-123"
);
// Store messages
await memory.addMessage(session, "user",
"How do I reset my password?");
await memory.addMessage(session, "assistant",
"To reset your password...");
// Get context for next response
const history = await memory.getHistory(session);
// Auto-summarize long conversations
await memory.summarizeIfNeeded(session,
4000 /* max tokens */);
Build a SaaS platform with automatic tenant isolation using Row-Level Security. Each tenant sees only their own data, enforced at the database level — no application-level filtering required.
-- Row-Level Security Policy
CREATE POLICY tenant_isolation
ON projects
USING (tenant_id = current_setting(
'app.tenant_id')::uuid);
ALTER TABLE projects
ENABLE ROW LEVEL SECURITY;
# Each tenant only sees their own data
acme_token = db.get_tenant_token(acme_id)
db.query_as_tenant(acme_token,
"SELECT * FROM projects")
# Returns: only Acme's projects
startup_token = db.get_tenant_token(startup_id)
db.query_as_tenant(startup_token,
"SELECT * FROM projects")
# Returns: only Startup's projects
Implement natural language product search by combining vector similarity with traditional SQL filters. Users search by intent, not keywords — "headphones for working from home" finds noise-canceling headphones even without exact keyword matches.
-- Semantic search with filters
SELECT name, price,
embedding <=> $query_vec AS distance
FROM products
WHERE category = 'Electronics'
AND price < 100
ORDER BY embedding <=> $query_vec
LIMIT 10;
-- Create optimized index
CREATE INDEX products_emb_idx
ON products
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 200);
Track user behavior with time-series event storage and aggregate with SQL. JSONB properties support flexible event schemas, and time-travel queries let you analyze historical state without separate audit tables.
date_trunc and window functionsAS OF TIMESTAMP for historical analysis// Track events
await tracker.track("user-123", "page_view",
{ page: "/products", referrer: "google" });
await tracker.track("user-123", "purchase",
{ order_id: "ord-789", total: 29.99 });
// Daily aggregation
const metrics = await query(`
SELECT date_trunc('day', timestamp) AS period,
COUNT(*) AS events,
COUNT(DISTINCT user_id) AS users
FROM events
WHERE event_type = 'purchase'
AND timestamp >= NOW() - INTERVAL '30 days'
GROUP BY period
ORDER BY period DESC
`);
Implement database-backed feature flags with percentage-based rollouts, user targeting, and plan-based access control. Deterministic bucket assignment ensures consistent flag evaluation per user.
-- Feature flags table
CREATE TABLE feature_flags (
id TEXT PRIMARY KEY,
enabled BOOLEAN DEFAULT false,
rollout_percentage INTEGER DEFAULT 0,
target_plans TEXT[]
);
INSERT INTO feature_flags VALUES
('new_checkout', true, 50,
ARRAY['pro', 'enterprise']);
# Check flag in application
if flags.is_enabled("new_checkout",
user_id="user-123",
plan="pro"):
show_new_checkout()
else:
show_old_checkout()
Try these examples in the SQL Playground or get started with HeliosDB in minutes.