File size: 7,602 Bytes
c59d808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Embedding Compatibility Guide

## πŸ” Understanding Embedding Dimensions

When working with vector databases and embeddings, **dimension compatibility** is crucial for successful similarity searches. This guide helps you understand and troubleshoot embedding dimension issues.

## πŸ“Š Common Embedding Models & Their Dimensions

| Provider | Model | Dimensions | Use Case |
|----------|-------|------------|----------|
| **HuggingFace** | `sentence-transformers/all-MiniLM-L6-v2` | **384** | Fast, lightweight, good for most tasks |
| **HuggingFace** | `sentence-transformers/all-mpnet-base-v2` | **768** | Higher quality, larger model |
| **Ollama** | `nomic-embed-text:v1.5` | **768** | Local inference, privacy-focused |
| **Ollama** | `mxbai-embed-large` | **1024** | High-quality local embeddings |
| **OpenAI** | `text-embedding-3-small` | **1536** | Commercial API, good performance |
| **OpenAI** | `text-embedding-3-large` | **3072** | Highest quality, expensive |
| **Google** | `models/embedding-001` | **768** | Google AI integration |

## ⚠️ Common Error: Dimension Mismatch

### Symptoms
```
WARNING - [custom_mongo_vector.py:103] - ⚠️ Error processing document: shapes (768,) and (384,) not aligned
```

### Root Cause
Your **query embeddings** and **stored embeddings** have different dimensions:
- Query: Generated with Model A (e.g., 768 dimensions)
- Stored: Created with Model B (e.g., 384 dimensions)

### Why This Happens
1. You changed embedding models after creating your database
2. Your database was created with a different embedding provider
3. Environment configuration doesn't match the original setup

## πŸ”§ Solution Strategies

### Strategy 1: Match Your Current Database (Recommended)

**Step 1: Identify stored embedding dimensions**
```bash
# Check your MongoDB collection to see stored embedding dimensions
# Look at the 'ingredients_emb' field length
```

**Step 2: Update .env to match**
```bash
# If stored embeddings are 384-dimensional (common with all-MiniLM-L6-v2)
EMBEDDING_PROVIDER=huggingface
HUGGINGFACE_EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2

# If stored embeddings are 768-dimensional
EMBEDDING_PROVIDER=ollama
OLLAMA_EMBEDDING_MODEL=nomic-embed-text:v1.5
```

### Strategy 2: Regenerate Database with New Model

**Step 1: Choose your preferred embedding model**
```bash
# Example: Use Ollama for local inference
EMBEDDING_PROVIDER=ollama
OLLAMA_EMBEDDING_MODEL=nomic-embed-text:v1.5
```

**Step 2: Enable database refresh**
```bash
DB_REFRESH_ON_START=true
```

**Step 3: Restart application**
```bash
uvicorn app:app --reload
```

**Step 4: Disable refresh (Important!)**
```bash
DB_REFRESH_ON_START=false
```

## πŸ” Debugging Embedding Issues

### Check Current Configuration
```bash
# View your current embedding setup
grep -E "EMBEDDING_PROVIDER|_EMBEDDING_MODEL" .env
```

### Monitor Embedding Dimensions
The custom MongoDB vector store now logs dimension information:
```
πŸ”’ Query embedding dimensions: 768
⚠️ Dimension mismatch: query=768D, stored=384D
πŸ’‘ Consider changing EMBEDDING_PROVIDER to match stored embeddings
```

### Verify Database Content
```python
# Check stored embedding dimensions in MongoDB
collection.find_one({"ingredients_emb": {"$exists": True}})["ingredients_emb"]
# Count the array length to get dimensions
```

## πŸ“‹ Environment Configuration Examples

### Example 1: HuggingFace (384D) - Most Common
```bash
# .env configuration for 384-dimensional embeddings
EMBEDDING_PROVIDER=huggingface
HUGGINGFACE_EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
HUGGINGFACE_API_TOKEN=your_token_here
```

### Example 2: Ollama (768D) - Local Inference
```bash
# .env configuration for 768-dimensional embeddings
EMBEDDING_PROVIDER=ollama
OLLAMA_EMBEDDING_MODEL=nomic-embed-text:v1.5
OLLAMA_BASE_URL=http://localhost:11434
```

### Example 3: OpenAI (1536D) - Premium Quality
```bash
# .env configuration for 1536-dimensional embeddings
EMBEDDING_PROVIDER=openai
OPENAI_EMBEDDING_MODEL=text-embedding-3-small
OPENAI_API_KEY=your_api_key_here
```

## 🚨 Common Pitfalls

### 1. Mixed Providers
❌ **Don't do this:**
```bash
# Database created with HuggingFace
EMBEDDING_PROVIDER=huggingface  # Original

# Later changed to Ollama without refreshing DB
EMBEDDING_PROVIDER=ollama  # New - causes dimension mismatch!
```

### 2. Forgetting to Disable Refresh
❌ **Don't forget:**
```bash
# After refreshing database, always disable refresh
DB_REFRESH_ON_START=false  # SET THIS BACK TO FALSE!
```

### 3. Model Name Typos
❌ **Watch out for:**
```bash
# Typo in model name will cause failures
OLLAMA_EMBEDDING_MODEL=nomic-embed-text:v1.5  βœ…
OLLAMA_EMBEDDING_MODEL=nomic-embed-text       ❌ (missing version)
```

## πŸ“Š Performance Comparison

| Model | Speed | Quality | Dimensions | Local/API | Cost |
|-------|-------|---------|------------|-----------|------|
| `all-MiniLM-L6-v2` | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | 384 | Both | Free |
| `nomic-embed-text:v1.5` | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | 768 | Local | Free |
| `text-embedding-3-small` | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | 1536 | API | $$$ |

## πŸ”§ Troubleshooting Steps

### Step 1: Check Current Setup
```bash
# 1. Check your environment configuration
cat .env | grep EMBEDDING

# 2. Check vector store provider
cat .env | grep VECTOR_STORE_PROVIDER
```

### Step 2: Test Embedding Generation
```python
# Test script to check embedding dimensions
from services.vector_store import vector_store_service

# Generate a test embedding
test_embedding = vector_store_service.embeddings.embed_query("test")
print(f"Current embedding dimensions: {len(test_embedding)}")
```

### Step 3: Check Database Content
For MongoDB users:
```javascript
// MongoDB shell command to check stored embedding dimensions
db.your_collection.findOne({"ingredients_emb": {"$exists": true}})
```

### Step 4: Apply Fix
Choose one of the strategies above based on your needs.

## πŸ“ Best Practices

### 1. Document Your Embedding Model
Keep a record of which embedding model you used:
```bash
# Add comments to your .env file
# Database created on 2025-08-27 with all-MiniLM-L6-v2 (384D)
EMBEDDING_PROVIDER=huggingface
HUGGINGFACE_EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
```

### 2. Version Control Your Configuration
```bash
# Commit your .env changes with descriptive messages
git add .env
git commit -m "Update embedding model to match database (384D)"
```

### 3. Test After Changes
```bash
# After changing embedding configuration, test a query
curl -X POST "http://localhost:8080/chat" \
  -H "Content-Type: application/json" \
  -d '{"message": "test query"}'
```

## πŸ†˜ Quick Reference

### Error Pattern Recognition
```
shapes (768,) and (384,) not aligned  β†’ Query=768D, Stored=384D
shapes (384,) and (768,) not aligned  β†’ Query=384D, Stored=768D
shapes (1536,) and (384,) not aligned β†’ Query=1536D, Stored=384D
```

### Quick Fixes
| Stored Dimensions | Set EMBEDDING_PROVIDER to |
|-------------------|-------------------------|
| 384 | `huggingface` with `all-MiniLM-L6-v2` |
| 768 | `ollama` with `nomic-embed-text:v1.5` |
| 1536 | `openai` with `text-embedding-3-small` |

---

## πŸ“ž Need Help?

If you're still experiencing issues:

1. Check the application logs for detailed error messages
2. Verify your embedding model is properly installed/accessible
3. Ensure your database connection is working
4. Consider regenerating your vector database if switching models permanently

Remember: **Consistency is key** - your query embeddings and stored embeddings must use the same model and dimensions!