Spaces:
Running
Running
File size: 6,796 Bytes
010c963 | 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | # π Groq Integration Setup - COMPLETE GUIDE
## β
What Was Done:
### 1. **Added FREE Groq Fallback**
- Created `backend/groq_fallback.py` - Handles Groq API integration
- Updated `backend/main.py` - Integrated fallback logic
- Updated `requirements.txt` - Added `groq>=0.4.0`
- **NO BREAKING CHANGES** - System works with or without Groq!
### 2. **How It Works:**
```
User Question
β
ALU Knowledge Base Search
β
Score >= 50? β Use ALU Answer β
β
Score < 50? β Use Groq Fallback π€
β
Groq Failed? β Use ALU Answer anyway β
```
**Priority:**
1. **ALU Knowledge Base** (always first!)
2. **Groq API** (if ALU match is weak)
3. **ALU fallback** (if Groq fails)
---
## π Setup Instructions:
### **Step 1: Get FREE Groq API Key** (2 minutes)
1. Go to: **https://console.groq.com/**
2. Click **"Sign Up"** (free account)
3. Verify your email
4. Go to **"API Keys"** section
5. Click **"Create API Key"**
6. Copy the key (starts with `gsk_...`)
**IMPORTANT:** Save this key - you won't see it again!
---
### **Step 2: Add API Key to Hugging Face** (1 minute)
1. Go to: **https://huggingface.co/spaces/Ngum/alu-student-companion**
2. Click **"Settings"** tab (top right)
3. Scroll down to **"Repository secrets"**
4. Click **"New secret"** button
5. Fill in:
- **Name:** `GROQ_API_KEY`
- **Value:** Paste your Groq API key (gsk_...)
6. Click **"Add secret"**
---
### **Step 3: Wait for Rebuild** (2-3 minutes)
The space will automatically:
- β
Detect the new secret
- β
Rebuild the container
- β
Load Groq integration
- β
Start serving requests
**You'll see:** "Building" β "Running" in the space status
---
## π§ͺ Testing:
### **Test 1: Check Groq Status**
```bash
curl https://ngum-alu-student-companion.hf.space/health
```
**Expected Response:**
```json
{
"status": "healthy",
"components": {
"groq_fallback": {
"enabled": true,
"api_key_set": true,
"client_initialized": true,
"model": "llama-3.1-70b-versatile",
"free_tier": "14,400 requests/day"
}
}
}
```
**If `"enabled": false`:**
- Check API key is added correctly
- Check space has restarted
- Check logs for errors
---
### **Test 2: ALU Question (Should use ALU KB)**
```bash
curl -X POST https://ngum-alu-student-companion.hf.space/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "Who founded ALU?"}'
```
**Expected:**
- Should return: "Fred Swaniker founded ALU in 2015"
- Source: ALU Knowledge Base
- **NOT using Groq** (ALU KB has high score)
---
### **Test 3: Non-ALU Question (Should use Groq)**
```bash
curl -X POST https://ngum-alu-student-companion.hf.space/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "What is the capital of France?"}'
```
**Expected:**
- Should return: "Paris is the capital of France..."
- Source: `groq_llm`
- Model: `llama-3.1-70b-versatile`
---
### **Test 4: Weak ALU Match (Should use Groq)**
```bash
curl -X POST https://ngum-alu-student-companion.hf.space/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "Tell me about university life in Africa"}'
```
**Expected:**
- Should use Groq (weak ALU match)
- But will mention ALU in context
- Comprehensive answer about African university life
---
## π What Questions Use What:
### **ALU Knowledge Base (Primary):**
- β
"Who founded ALU?"
- β
"Where is ALU located?"
- β
"What programs does ALU offer?"
- β
"How do I apply to ALU?"
- β
"What is the tuition at ALU?"
- β
Any specific ALU question
### **Groq Fallback (Secondary):**
- π€ "What is the capital of France?"
- π€ "Explain quantum physics"
- π€ "How do I learn Python?"
- π€ "What is climate change?"
- π€ Weak ALU matches (score < 50)
---
## π° Cost Analysis:
### **Groq FREE Tier:**
- β
**14,400 requests/day**
- β
**500+ tokens/second**
- β
**No credit card required**
- β
**No expiration**
### **Estimated Usage:**
- **ALU questions:** ~80% (use ALU KB, FREE)
- **Groq fallback:** ~20% (use Groq, FREE)
- **Total cost:** **$0/month** π
### **If you exceed 14,400/day:**
- System falls back to ALU KB
- No errors or downtime
- Consider upgrading Groq plan (optional)
---
## π§ Troubleshooting:
### **Problem: Groq not enabled**
**Check:**
```bash
curl https://ngum-alu-student-companion.hf.space/health
```
**If `"enabled": false`:**
1. Verify API key in Hugging Face secrets
2. Check key starts with `gsk_`
3. Restart space manually
4. Check logs for initialization errors
---
### **Problem: Groq API errors**
**Symptoms:**
- Responses say "I don't have information..."
- Logs show "Groq API error"
**Solutions:**
1. Check API key is valid at https://console.groq.com/
2. Check you haven't exceeded 14,400 requests/day
3. System will automatically fall back to ALU KB
---
### **Problem: All questions use Groq**
**This shouldn't happen!** ALU questions should use ALU KB first.
**Debug:**
1. Check semantic search is working
2. Check ALU knowledge base is loaded
3. Check logs for "ALU score" values
4. If scores are low, knowledge base may need updating
---
## π Monitoring:
### **Check Logs:**
In Hugging Face Space:
1. Go to **"Logs"** tab
2. Look for:
- `β
Groq fallback enabled (FREE tier)`
- `π€ Using Groq fallback - ALU score too low: XX.X`
- `[OK] Groq API works!`
### **Response Metadata:**
Every response includes:
```json
{
"response": "...",
"source": "groq_llm" or "alu_kb",
"model": "llama-3.1-70b-versatile",
"engine": "groq_fallback",
"alu_score": 35.2
}
```
Use this to track which questions use Groq!
---
## π― Success Criteria:
β
**Groq Status Check:**
- Health endpoint shows `"enabled": true`
- API key is set
- Client initialized
β
**ALU Questions Work:**
- "Who founded ALU?" β Returns Fred Swaniker
- Uses ALU Knowledge Base (not Groq)
- Fast and accurate
β
**Non-ALU Questions Work:**
- "What is the capital of France?" β Returns Paris
- Uses Groq fallback
- Comprehensive answer
β
**No Breaking Changes:**
- All existing functionality works
- System handles Groq failures gracefully
- Falls back to ALU KB when needed
---
## π Support:
If you have issues:
1. **Check health endpoint first**
2. **Check Hugging Face logs**
3. **Verify API key is correct**
4. **Test with curl commands above**
**Remember:** The system works perfectly WITHOUT Groq! It's just an enhancement.
---
## π You're Done!
Your ALU Chatbot now has:
- β
**Smart ALU Knowledge Base** (primary)
- β
**FREE Groq Fallback** (secondary)
- β
**Semantic Search** (finds Fred Swaniker!)
- β
**Professional Responses** (concise and accurate)
- β
**No Breaking Changes** (everything still works!)
**Total Cost: $0/month** π
Enjoy your enhanced chatbot! π
|