Spaces:
Sleeping
Sleeping
File size: 7,103 Bytes
0f77bc1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# ποΈ 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
1. Go to https://supabase.com
2. Sign up (free tier available)
3. Create new project
4. Wait for database initialization (~2 min)
### 2. Enable pgvector
```sql
-- In Supabase SQL Editor:
CREATE EXTENSION IF NOT EXISTS vector;
```
### 3. Create Documents Table
```sql
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
```sql
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:
1. Go to **Settings β API**
2. Copy:
- `Project URL` β `SUPABASE_URL`
- `anon public` key β `SUPABASE_ANON_KEY` (for Streamlit app)
- `service_role` key β `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)
```bash
# 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:
1. Settings β Secrets and variables β Actions
2. Add new secrets:
- `SUPABASE_URL` = your Supabase URL
- `SUPABASE_SERVICE_ROLE_KEY` = service role key (for ingestion)
### 8. Configure HF Space Secrets
In HuggingFace Space Settings β Secrets:
- `HF_API_TOKEN` = your HF token
- `SUPABASE_URL` = your Supabase URL
- `SUPABASE_ANON_KEY` = anon public key (safe to expose)
- `EMBEDDING_MODEL` = (optional) embedding model ID
- `RESULTS_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
1. **GitHub**: Push code to `main` branch
2. **GitHub Actions**:
- Runs `ingest.py` with `SUPABASE_SERVICE_ROLE_KEY`
- Ingests documents into Supabase
- Workflow completes
3. **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:
1. Update `data/sap_dataset.json` with new documents
2. Push to GitHub
3. GitHub Actions auto-runs ingestion
4. New documents available in Supabase
5. 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-transformers` for 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)
```python
EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2" # 768 dims
```
### Recreate table with new dimensions
```sql
ALTER TABLE documents ALTER COLUMN embedding TYPE vector(768);
```
### Update app.py
```python
EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2"
```
---
## Next Steps
1. β
Create Supabase project
2. β
Enable pgvector and create table
3. β
Add GitHub Actions secrets
4. β
Push code (triggers ingestion)
5. β
Configure HF Space secrets
6. β
Test: "How do I monitor SAP jobs?"
7. β
Share with team!
---
## Resources
- π [Supabase Docs](https://supabase.com/docs)
- π¦ [pgvector Docs](https://github.com/pgvector/pgvector)
- π€ [HF Inference API](https://huggingface.co/docs/api-inference)
- π [Supabase Security Best Practices](https://supabase.com/docs/guides/api-keys)
---
**Your production-grade SAP chatbot is ready! π**
|