# AI Setup Guide - No API Keys Required!

## Overview

Your chatbot service now supports **3 AI providers** with automatic fallback:

1. **OpenAI** (GPT-4, GPT-3.5) - Cloud, requires API key
2. **Anthropic** (Claude) - Cloud, requires API key
3. **Ollama** (Llama, Mistral, etc.) - **LOCAL, NO API KEY NEEDED!** ✨

## Quick Start - Use Local AI (No Cost!)

### Option 1: Just Start Docker (Easiest)

```bash
# Start all services including Ollama
docker-compose up -d

# Wait for Ollama to download the model (first time only)
docker-compose logs -f ollama
```

**That's it!** Your chatbot will automatically use Ollama (local AI) if no API keys are configured.

### Option 2: Add API Keys for Better Performance

Edit `.env.docker`:
```env
OPENAI_API_KEY=sk-your-key-here
# OR
ANTHROPIC_API_KEY=sk-ant-your-key-here
```

Then restart:
```bash
docker-compose restart backend
```

## How Automatic Fallback Works

```
Priority Order:
1. OpenAI (if API key configured)
   ↓ (if fails or not configured)
2. Anthropic (if API key configured)
   ↓ (if fails or not configured)
3. Ollama (Local - ALWAYS AVAILABLE)
```

**Benefits:**
- ✓ Zero cost with Ollama
- ✓ Works offline
- ✓ No API rate limits
- ✓ Automatic failover if cloud APIs are down
- ✓ Data privacy (runs locally)

## Available Local Models (Ollama)

### Recommended for Chatbots

| Model | Size | Speed | Quality | Best For |
|-------|------|-------|---------|----------|
| **llama3.2:3b** | 2GB | ⚡⚡⚡ | ⭐⭐⭐ | General chat (DEFAULT) |
| llama3.2:1b | 1GB | ⚡⚡⚡⚡ | ⭐⭐ | Simple Q&A, very fast |
| phi3:3.8b | 2.3GB | ⚡⚡⚡ | ⭐⭐⭐⭐ | Customer service |
| mistral:7b | 4GB | ⚡⚡ | ⭐⭐⭐⭐ | Complex conversations |
| gemma2:9b | 5GB | ⚡⚡ | ⭐⭐⭐⭐⭐ | Advanced reasoning |

### Install Additional Models

```bash
# List available models
docker-compose exec ollama ollama list

# Pull a new model
docker-compose exec ollama ollama pull phi3:3.8b

# Remove unused model
docker-compose exec ollama ollama rm llama3.2:1b
```

## Training Custom Models

You can train custom models on your client's data!

### Example: Train Real Estate Chatbot

```python
from app.services.ai.training import ModelTrainingService, TrainingDataFormat

# Initialize training service
training_service = ModelTrainingService()

# Prepare training data (conversations from your client)
training_data = [
    {
        "messages": [
            {"role": "user", "content": "Show me 3BHK apartments in Mumbai"},
            {"role": "assistant", "content": "I found 5 excellent 3BHK apartments in Mumbai. Here are the top options..."}
        ]
    },
    {
        "messages": [
            {"role": "user", "content": "What's the EMI for a 50 lakh home loan?"},
            {"role": "assistant", "content": "For a ₹50 lakh home loan at 8.5% interest for 20 years, your EMI would be approximately ₹43,391 per month."}
        ]
    },
    # Add 50-100+ examples for better results
]

# Create training job
job = await training_service.create_training_job(
    client_id=123,
    model_name="real_estate_assistant",
    base_model="llama3.2:3b",
    training_data=training_data,
    data_format=TrainingDataFormat.CONVERSATIONS
)

# Start training
system_prompt = """You are a professional real estate assistant.
You help customers find properties, calculate EMI, and schedule viewings.
Always be friendly, professional, and provide accurate information."""

success = await training_service.train_ollama_model(
    job_id=job.job_id,
    system_prompt=system_prompt
)

# Check status
status = await training_service.get_training_status(job.job_id)
print(status)
```

### Use Custom Trained Model

```python
from app.services.ai.chat_service import get_chat_service

chat_service = await get_chat_service()

response = await chat_service.chat(
    user_message="Show me properties near Bandra",
    system_prompt=system_prompt,
    context={
        "business_name": "Dream Homes Realty",
        "business_type": "real_estate",
        "capabilities": ["property search", "EMI calculation", "tour scheduling"]
    }
)

print(response["response"])
print(f"Provider used: {response['provider']}")  # Will show which AI was used
```

## Web Interface for Ollama

Access Ollama Web UI at: **http://localhost:3001**

Features:
- Chat with any local model
- Test different models
- View model information
- Download new models

## Cost Comparison

### Cloud APIs (Per 1M Tokens)

| Provider | Input | Output | Est. Monthly Cost* |
|----------|-------|--------|-------------------|
| OpenAI (GPT-4o-mini) | $0.15 | $0.60 | $50-200 |
| Anthropic (Claude Haiku) | $0.25 | $1.25 | $80-300 |

### Local (Ollama)

| Provider | Input | Output | Est. Monthly Cost |
|----------|-------|--------|-------------------|
| **Ollama** | **$0** | **$0** | **$0** ✨ |

*Based on moderate usage (100K messages/month)

**Note:** Cloud APIs are faster and higher quality, but Ollama is excellent for:
- Development/testing
- Cost-sensitive projects
- Privacy-critical applications
- Backup/fallback

## System Requirements for Ollama

### Minimum
- **RAM:** 8GB (for 3B models)
- **Disk:** 5GB free space
- **CPU:** Any modern processor

### Recommended
- **RAM:** 16GB (for 7B+ models)
- **GPU:** NVIDIA GPU with 8GB+ VRAM (10x faster)
- **Disk:** 20GB free space

### Enable GPU Support

Edit `docker-compose.yml`, uncomment:

```yaml
ollama:
  # Uncomment for GPU support
  deploy:
    resources:
      reservations:
        devices:
          - driver: nvidia
            count: 1
            capabilities: [gpu]
```

Then restart:
```bash
docker-compose down
docker-compose up -d
```

## API Endpoints

### Check AI Provider Status

```bash
GET /api/v1/ai/status

Response:
{
  "available": ["ollama"],
  "configured": ["ollama"],
  "priority_order": ["openai", "anthropic", "ollama"],
  "recommendations": [
    "ℹ️ Using local Ollama only. Add OpenAI/Anthropic API key for better performance."
  ]
}
```

### Send Chat Message

```bash
POST /api/v1/chat
{
  "message": "Hello, I need help finding a property",
  "client_id": 123,
  "preferred_provider": "ollama"  // Optional: force specific provider
}

Response:
{
  "response": "Hello! I'd be happy to help you find a property...",
  "provider": "ollama",
  "tokens_used": 45,
  "latency_ms": 823,
  "success": true
}
```

## Troubleshooting

### "No AI providers available"

**Solution 1:** Wait for Ollama to start
```bash
docker-compose logs -f ollama
# Wait for: "Ollama is running"
```

**Solution 2:** Manually pull model
```bash
docker-compose exec ollama ollama pull llama3.2:3b
```

### Slow Response Times

**Solutions:**
1. Use smaller model (llama3.2:1b instead of mistral:7b)
2. Enable GPU support
3. Add cloud API key for faster responses

### Out of Memory

**Solution:** Use smaller model
```bash
docker-compose exec ollama ollama pull llama3.2:1b
```

Update `.env.docker`:
```env
OLLAMA_DEFAULT_MODEL=llama3.2:1b
```

## Best Practices

### For Production

1. **Use Cloud API as Primary**
   - Better quality and speed
   - Set OpenAI or Anthropic API key

2. **Keep Ollama as Fallback**
   - Ensures uptime even if cloud API fails
   - Zero additional cost

3. **Train Custom Models**
   - Better responses for specific use cases
   - Improved accuracy with domain knowledge

### For Development

1. **Use Ollama Only**
   - No API costs during development
   - Faster iteration

2. **Test with Multiple Models**
   - Compare quality vs speed
   - Find the right balance

## Migration Path

### Start Free, Scale as Needed

```
Phase 1: Development
└─ Use Ollama (Free, local)

Phase 2: Testing
├─ Add OpenAI API (paid)
└─ Keep Ollama as fallback

Phase 3: Production
├─ Primary: OpenAI/Anthropic
├─ Fallback: Ollama
└─ Custom: Trained models for specific clients
```

## Monitoring

View real-time AI provider usage:

```bash
# View backend logs
docker-compose logs -f backend | grep "AI"

# You'll see:
# ✓ ollama is available
# Attempting chat with ollama
# ✓ Successfully got response from ollama
```

## Need Help?

1. Check logs: `docker-compose logs -f backend ollama`
2. View Ollama status: http://localhost:11434
3. Check provider status: GET /api/v1/ai/status
4. Review this guide: AI_SETUP_GUIDE.md

---

**Summary:** Your chatbot works out-of-the-box with Ollama (free, local AI). Add API keys later for better performance. Automatic fallback ensures it always works! 🚀
