RAG Backend

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.

  • HNSW index with Product Quantization for 8-16x compression
  • Hybrid queries: combine vector similarity with SQL filters
  • Works with OpenAI, Cohere, or any embedding model
  • Sub-millisecond retrieval at scale
Python
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}
            ]
        )

AI Agent Memory

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.

  • Session-scoped conversation history
  • Automatic token counting per message
  • Summarize when context exceeds thresholds
  • Built-in REST API — no custom tables needed
TypeScript
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 */);

Multi-Tenant SaaS

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 policies enforce isolation automatically
  • Tenant-scoped JWT tokens via the auth API
  • Per-tenant resource quotas and connection limits
  • Zero risk of cross-tenant data leakage
SQL + Python
-- 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

Event Analytics

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.

  • JSONB properties for flexible event payloads
  • Time-series aggregation with date_trunc and window functions
  • Time-travel queries: AS OF TIMESTAMP for historical analysis
  • Indexed by user + timestamp for efficient range scans
TypeScript
// 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
`);

Feature Flags

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.

  • Boolean flags with percentage-based rollouts
  • Target specific users or subscription plans
  • Deterministic hashing for consistent evaluation
  • Cacheable with instant invalidation on change
SQL + Python
-- 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()

Start Building

Try these examples in the SQL Playground or get started with HeliosDB in minutes.