# 🧠 How to Train Your MFA Hydra Bot

Your bot uses **RAG (Retrieval-Augmented Generation)** — it searches your knowledge base before answering, making responses accurate and business-specific.

---

## 5 Ways to Train Your Bot

### 1️⃣ Knowledge Base (Fastest — Use This First)

**What it is:** A database of Q&A pairs the bot searches before using AI.

**How to use:**
1. Go to **Admin Panel → 🧠 AI Training**
2. Click **"Add Training Data"**
3. Fill in:
   - **Category:** Pick a topic (pricing, delivery, etc.)
   - **Question:** What customers ask (e.g., "Netflix price?")
   - **Answer:** Exact response bot should give
   - **Keywords:** Related words (e.g., `netflix, price, cost, how much`)
   - **Priority:** 10 = most important, 1 = least
4. Click **Save**

**Example Entry:**
```
Category: pricing
Question: How much is Netflix?
Answer: 🎬 Netflix Premium 4K is just ₹299/month (original ₹649). 
         Includes 4 screens + Ultra HD. 30-day warranty!
Keywords: netflix, price, cost, how much, ₹
Priority: 10
```

**When user asks:** *"How much for Netflix?"*
**Bot finds:** Keywords match → Injects answer into AI prompt → AI gives accurate, formatted response

---

### 2️⃣ System Prompt (AI Personality)

**What it is:** The "brain instructions" that define how your bot behaves.

**How to edit:**
1. Admin Panel → 🧠 AI Training
2. Scroll to **"AI Personality (System Prompt)"**
3. Edit the text box
4. Click **Update AI Brain**

**What to include in system prompt:**
```
- Your business name and description
- Complete product list with prices
- Payment methods and UPI ID
- Delivery timeframes
- Warranty policy
- Reseller commission details
- Support contact info
- Tone rules (friendly, professional, emoji usage)
- Language preferences (Hinglish, English)
- What to do when unsure (escalate to human)
```

**Example System Prompt:**
```
You are the AI assistant for Team Hydra Shop - India's #1 digital product store.

PRODUCTS & PRICES:
- Netflix Premium 4K: ₹299/month (orig ₹649)
- ChatGPT Plus: ₹399/month (orig ₹799)
- CapCut Pro Lifetime: ₹149 (orig ₹299)
- Spotify Premium 3mo: ₹349 (orig ₹599)
- YouTube Premium: ₹199/month (orig ₹399)

PAYMENT:
- UPI ID: teamhydra@kotak
- Accept: GPay, PhonePe, Paytm, any UPI app
- Send screenshot after payment for verification

DELIVERY:
- Auto: 2-5 minutes after payment confirm
- Manual: 15-30 minutes
- Credentials sent directly in chat

RULES:
1. Always be friendly, use emojis
2. Never make up prices
3. If unsure, say "Let me connect you to support"
4. Keep responses under 150 words
5. Respond in user's language (English/Hindi/Hinglish)
```

---

### 3️⃣ Chat History Memory

**What it is:** The bot remembers last 20 messages per user for context.

**How it works automatically:**
- Every conversation is saved in `chat_history` table
- AI sees previous context when replying
- No action needed — it just works

**To clear a user's memory:**
```sql
DELETE FROM chat_history WHERE user_id = [USER_ID];
```

---

### 4️⃣ Product Database Training

**What it is:** Bot learns from your product catalog automatically.

**How to improve:**
1. Admin Panel → 📦 Products
2. Make sure every product has:
   - **Detailed description**
   - **Short description** (used in quick replies)
   - **Accurate price** (original + sale)
   - **Warranty days**
   - **Delivery type** (auto/manual/license)
   - **Delivery content** (for auto-delivery)

**When user asks about products, bot pulls live data from database.**

---

### 5️⃣ Real-World Learning (Advanced)

**What it is:** Analyze what users ask and add missing answers.

**How to do it:**
1. Check **error.log** and chat logs regularly
2. Look for questions bot answered poorly
3. Add those Q&A pairs to Knowledge Base
4. Update system prompt if pattern emerges

**Pro tip:** Every week, run this SQL to see most common unanswered questions:
```sql
SELECT message, COUNT(*) as count 
FROM chat_history 
WHERE role = 'user' 
  AND message NOT IN (
    SELECT question FROM knowledge_base
  )
GROUP BY message 
ORDER BY count DESC 
LIMIT 20;
```

---

## 🎯 Training Best Practices

| Do This | Avoid This |
|---------|-----------|
| ✅ Use specific keywords | ❌ Vague keywords like "help" |
| ✅ Write natural questions | ❌ Robotic phrasing |
| ✅ Include prices in answers | ❌ "Check website for prices" |
| ✅ Add emoji to answers | ❌ Plain text only |
| ✅ Set priority 10 for common questions | ❌ All entries same priority |
| ✅ Update system prompt monthly | ❌ Set-and-forget |
| ✅ Deactivate outdated entries | ❌ Delete old entries (lose data) |
| ✅ Test bot after each addition | ❌ Bulk add without testing |

---

## 📊 Measuring Training Success

**Check these metrics in Admin Panel:**

1. **Knowledge Base Usage** — See which answers are used most
2. **Chat History** — Review actual conversations
3. **Ticket Volume** — Should decrease as bot gets smarter
4. **Order Conversion** — Better answers = more sales

**SQL to check bot performance:**
```sql
-- Most used knowledge base entries (good answers)
SELECT question, usage_count 
FROM knowledge_base 
ORDER BY usage_count DESC 
LIMIT 10;

-- Users who created tickets (bot failed them)
SELECT DISTINCT user_id, COUNT(*) as tickets 
FROM tickets 
WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY user_id 
ORDER BY tickets DESC;
```

---

## 🚀 Quick Training Checklist

**Week 1 — Foundation:**
- [ ] Add 20 common Q&A to Knowledge Base
- [ ] Write detailed system prompt with all products
- [ ] Test bot with 10 real customer questions
- [ ] Fix any wrong answers

**Week 2 — Refinement:**
- [ ] Review chat logs for missed questions
- [ ] Add 10 more knowledge base entries
- [ ] Update product descriptions
- [ ] Check AI response quality

**Week 3 — Optimization:**
- [ ] Analyze most-used knowledge entries
- [ ] Improve low-performing answers
- [ ] Add seasonal/promotional responses
- [ ] Train on refund/complaint handling

**Ongoing:**
- [ ] Weekly: Review 20 chat logs
- [ ] Monthly: Update system prompt
- [ ] Monthly: Add new product Q&A
- [ ] Quarterly: Full knowledge base audit

---

## 🎓 Example: Training for a New Product

**Scenario:** You add "Disney+ Hotstar" to your store

**Step 1:** Add Product in Admin
- Name: Disney+ Hotstar Premium
- Price: ₹199
- Description: Full details

**Step 2:** Add Knowledge Base Entries
```
Entry 1:
Q: Do you have Disney Plus?
A: ✅ Yes! Disney+ Hotstar Premium is ₹199/month. 
    Includes Marvel, Star Wars, Disney originals + Hotstar sports.
Keywords: disney, hotstar, plus, marvel, streaming
Priority: 10

Entry 2:
Q: Disney Hotstar price
A: 🎬 Disney+ Hotstar Premium: ₹199/month (original ₹299)
    ✓ 4K streaming ✓ 4 screens ✓ Download available
Keywords: disney, hotstar, price, cost
Priority: 10

Entry 3:
Q: Is Disney+ shared or private?
A: We offer both types! Private account: ₹249. 
    Shared account: ₹199. Both have full access.
Keywords: disney, shared, private, account type
Priority: 8
```

**Step 3:** Update System Prompt
Add to products list:
```
- Disney+ Hotstar Premium: ₹199/month (orig ₹299)
```

**Step 4:** Test
Message bot: *"How much is Disney+?"*
Expected: Accurate price with details

---

## 🔥 Pro Tips

**1. Use Hinglish for Indian Customers**
```
Q: Bhai Netflix kitne ka hai?
A: Bhai Netflix Premium 4K sirf ₹299/month! 
    4 screens + Ultra HD ke saath. 🔥
```

**2. Handle Complaints Gracefully**
```
Q: Account not working! Scam!
A: 😟 Sorry for the trouble! No worries, we got you.

    Please share:
    1️⃣ Your Order ID
    2️⃣ Screenshot of error

    We'll replace it within 24 hours OR full refund.
    WhatsApp: 917038146526
```

**3. Upsell Naturally**
```
Q: I want Netflix
A: 🎬 Great choice! Netflix Premium 4K: ₹299

    💡 Combo Deal: Netflix + Spotify = ₹599 only!
    (Save ₹49) 

    Want to add Spotify? 🎵
```

**4. Escalation Path**
Always include in answers:
```
❓ Still have questions? 
📱 WhatsApp: 917038146526
📢 Telegram: @teamhydrashops
```

---

## 📁 Files Related to Training

| File | Purpose |
|------|---------|
| `admin/training.php` | Add/edit knowledge base, system prompt |
| `includes/GeminiAI.php` | AI engine with RAG logic |
| `database.sql` | Knowledge base table schema |
| `admin/products.php` | Product catalog training data |

---

**Your bot learns from every interaction. The more you train it, the smarter it gets!** 🧠✨
