alu-chatbot / GROQ_SETUP_INSTRUCTIONS.md
Ngum's picture
Add FREE Groq fallback integration - NO BREAKING CHANGES
010c963
|
Raw
History Blame Contribute Delete
6.8 kB

πŸš€ 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

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:

  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:

{
  "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! 🎊