Spaces:
Running
Running
π 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- Addedgroq>=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:
- ALU Knowledge Base (always first!)
- Groq API (if ALU match is weak)
- ALU fallback (if Groq fails)
π Setup Instructions:
Step 1: Get FREE Groq API Key (2 minutes)
- Go to: https://console.groq.com/
- Click "Sign Up" (free account)
- Verify your email
- Go to "API Keys" section
- Click "Create API Key"
- 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)
- Go to: https://huggingface.co/spaces/Ngum/alu-student-companion
- Click "Settings" tab (top right)
- Scroll down to "Repository secrets"
- Click "New secret" button
- Fill in:
- Name:
GROQ_API_KEY - Value: Paste your Groq API key (gsk_...)
- Name:
- 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
curl https://ngum-alu-student-companion.hf.space/health
Expected Response:
{
"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)
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)
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)
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:
curl https://ngum-alu-student-companion.hf.space/health
If "enabled": false:
- Verify API key in Hugging Face secrets
- Check key starts with
gsk_ - Restart space manually
- Check logs for initialization errors
Problem: Groq API errors
Symptoms:
- Responses say "I don't have information..."
- Logs show "Groq API error"
Solutions:
- Check API key is valid at https://console.groq.com/
- Check you haven't exceeded 14,400 requests/day
- 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:
- Check semantic search is working
- Check ALU knowledge base is loaded
- Check logs for "ALU score" values
- If scores are low, knowledge base may need updating
π Monitoring:
Check Logs:
In Hugging Face Space:
- Go to "Logs" tab
- 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:
{
"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:
- Check health endpoint first
- Check Hugging Face logs
- Verify API key is correct
- 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! π