Spaces:
Sleeping
Sleeping
A newer version of the Streamlit SDK is available:
1.52.2
ποΈ Supabase Vector Database Setup
Your SAP Chatbot now uses Supabase + pgvector for production-grade vector search!
Architecture
GitHub Actions (Ingestion)
β (SUPABASE_SERVICE_ROLE_KEY)
ingest.py
ββ Load SAP documents
ββ Compute embeddings (sentence-transformers)
ββ Insert into Supabase (pgvector)
β
HuggingFace Spaces (Streamlit App)
ββ User asks question
ββ HF Inference API computes embedding
ββ Supabase RPC search_documents()
ββ Retrieve top-k results
ββ Generate answer with HF Inference API
Quick Setup
1. Create Supabase Project
- Go to https://supabase.com
- Sign up (free tier available)
- Create new project
- Wait for database initialization (~2 min)
2. Enable pgvector
-- In Supabase SQL Editor:
CREATE EXTENSION IF NOT EXISTS vector;
3. Create Documents Table
CREATE TABLE documents (
id BIGSERIAL PRIMARY KEY,
source TEXT,
url TEXT,
title TEXT,
content TEXT,
chunk_id INT,
embedding VECTOR(384),
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX ON documents USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
4. Create Search Function
CREATE OR REPLACE FUNCTION search_documents(query_embedding VECTOR, k INT DEFAULT 5)
RETURNS TABLE(id BIGINT, source TEXT, url TEXT, title TEXT, content TEXT, chunk_id INT, distance FLOAT8) AS $$
BEGIN
RETURN QUERY
SELECT
documents.id,
documents.source,
documents.url,
documents.title,
documents.content,
documents.chunk_id,
1 - (documents.embedding <=> query_embedding) AS distance
FROM documents
ORDER BY documents.embedding <=> query_embedding
LIMIT k;
END;
$$ LANGUAGE plpgsql;
5. Get Credentials
In Supabase dashboard:
- Go to Settings β API
- Copy:
Project URLβSUPABASE_URLanon publickey βSUPABASE_ANON_KEY(for Streamlit app)service_rolekey βSUPABASE_SERVICE_ROLE_KEY(for GitHub Actions only!)
β οΈ NEVER put service_role key in Space Secrets! Only in GitHub Actions.
6. Run Local Ingestion (Optional)
# Set env vars locally
export SUPABASE_URL="https://your-project.supabase.co"
export SUPABASE_SERVICE_ROLE_KEY="your-service-role-key"
export EMBEDDING_MODEL="sentence-transformers/all-MiniLM-L6-v2"
# Run ingestion
python ingest.py
7. Configure GitHub Actions Secrets
In your GitHub repo:
- Settings β Secrets and variables β Actions
- Add new secrets:
SUPABASE_URL= your Supabase URLSUPABASE_SERVICE_ROLE_KEY= service role key (for ingestion)
8. Configure HF Space Secrets
In HuggingFace Space Settings β Secrets:
HF_API_TOKEN= your HF tokenSUPABASE_URL= your Supabase URLSUPABASE_ANON_KEY= anon public key (safe to expose)EMBEDDING_MODEL= (optional) embedding model IDRESULTS_K= (optional) number of results (default: 5)
File Structure
sap-chatbot/
βββ app.py # Streamlit UI (uses HF API + Supabase RPC)
βββ ingest.py # Ingestion script (uses sentence-transformers)
βββ Dockerfile # Docker config for HF Spaces
βββ requirements.txt # Python dependencies (includes supabase, sentence-transformers)
βββ .github/
β βββ workflows/
β βββ deploy.yml # GitHub Actions: ingest + deploy
βββ data/
βββ sap_dataset.json # Source documents
Deployment Flow
First Deployment
- GitHub: Push code to
mainbranch - GitHub Actions:
- Runs
ingest.pywithSUPABASE_SERVICE_ROLE_KEY - Ingests documents into Supabase
- Workflow completes
- Runs
- HF Spaces:
- Auto-syncs from GitHub (Linked Repository)
- Launches Streamlit app
- App connects to Supabase with
SUPABASE_ANON_KEY
Update Knowledge Base
To add more SAP documents:
- Update
data/sap_dataset.jsonwith new documents - Push to GitHub
- GitHub Actions auto-runs ingestion
- New documents available in Supabase
- HF Spaces app immediately sees new data
API Endpoints
Streamlit App (HF Spaces)
- Uses HF Inference API for embeddings
- Calls Supabase RPC
search_documents(query_embedding, k) - Generates answers with HF Inference API
ingest.py (GitHub Actions)
- Uses local
sentence-transformersfor embeddings - Inserts directly to Supabase with service role key
- Runs on schedule or manual trigger
Performance
| Operation | Time | Notes |
|---|---|---|
| Compute embedding | 50-100ms | Local sentence-transformers |
| Vector search | 10-50ms | pgvector with IVFFlat index |
| HF Inference (answer) | 10-30s | Cloud API |
| Total response | 10-30s | Dominated by LLM generation |
Cost Analysis
| Component | Cost | Notes |
|---|---|---|
| Supabase (free tier) | FREE | 500MB DB + 2GB file storage |
| Supabase (paid) | $25+/mo | More storage, more API calls |
| HF Inference API | FREE | Rate limited, generous |
| GitHub Actions | FREE | 2000 min/month |
| HF Spaces | FREE | 5+ concurrent users |
| TOTAL | $0-25/mo | Scales with usage |
Upgrade to paid Supabase when:
- Dataset grows beyond 500MB
- Vector searches become slow
- Need higher API rate limits
Troubleshooting
"pgvector not found"
- Enable pgvector extension in Supabase SQL Editor
- Run:
CREATE EXTENSION IF NOT EXISTS vector;
"RPC function not found"
- Copy search_documents SQL function into Supabase
- Run in SQL Editor
- Wait for function to compile
"Embedding dimension mismatch"
- Model uses 384 dims:
sentence-transformers/all-MiniLM-L6-v2 - If changing model, recreate VECTOR(new_dim) in table
"Ingestion too slow"
- Increase BATCH_SIZE in ingest.py
- Run on larger GitHub Actions runner
- Consider async ingestion
"Search results irrelevant"
- Check embedding model matches
- Verify documents chunked correctly
- Try different chunk_size/overlap in ingest.py
Advanced: Custom Embeddings
To use different embedding model:
Local (ingest.py)
EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2" # 768 dims
Recreate table with new dimensions
ALTER TABLE documents ALTER COLUMN embedding TYPE vector(768);
Update app.py
EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2"
Next Steps
- β Create Supabase project
- β Enable pgvector and create table
- β Add GitHub Actions secrets
- β Push code (triggers ingestion)
- β Configure HF Space secrets
- β Test: "How do I monitor SAP jobs?"
- β Share with team!
Resources
- π Supabase Docs
- π¦ pgvector Docs
- π€ HF Inference API
- π Supabase Security Best Practices
Your production-grade SAP chatbot is ready! π