# Fix Chat Assistant 500 Error

## Issue
Getting 500 Internal Server Error when calling `/api/v1/chat` endpoint.

## Root Causes

1. **Model Name Mismatch** ✅ FIXED
   - Code was using `gemma3:27b` but entrypoint pulls `gemma3:270M`
   - **Fixed**: Updated code to use `gemma3:270M`

2. **Ollama Not Running**
   - Ollama service might not be started in the container
   - Network connectivity issues

3. **Model Not Available**
   - Model might not be pulled yet
   - Model name incorrect

## Solutions

### Solution 1: Restart the Container

```bash
# Stop and restart the backend container
docker-compose down
docker-compose up -d be0

# Wait for Ollama to start (check logs)
docker-compose logs -f be0
```

### Solution 2: Check Ollama Status

```bash
# Check if container is running
docker ps | grep be0

# Check Ollama inside container
docker exec be0 ollama list

# If Ollama is not running, start it
docker exec be0 ollama serve &
```

### Solution 3: Pull the Model

```bash
# Pull the required model
docker exec be0 ollama pull gemma3:270M

# Verify it's available
docker exec be0 ollama list | grep gemma3
```

### Solution 4: Test the Health Endpoint

```bash
# Check health endpoint (includes Ollama status)
curl http://localhost:4402/health

# Should show:
# {
#   "status": "healthy",
#   "ollama": {
#     "status": "connected",
#     "available_models": ["gemma3:270M", ...]
#   }
# }
```

### Solution 5: Check Backend Logs

```bash
# View recent logs
docker-compose logs be0 | tail -50

# View ChatAssistant specific logs
tail -f be0/logs/ChatAssistant.log
```

## Quick Fix Commands

```bash
# 1. Restart everything
docker-compose restart be0

# 2. Check Ollama
docker exec be0 ollama list

# 3. Test health
curl http://localhost:4402/health

# 4. Test chat endpoint
curl -X POST http://localhost:4402/api/v1/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello"}'
```

## What Was Fixed

1. ✅ Model name changed from `gemma3:27b` to `gemma3:270M`
2. ✅ Added better error handling with specific error messages
3. ✅ Added Ollama connection check on initialization
4. ✅ Added health endpoint with Ollama status
5. ✅ Improved logging for debugging

## Expected Behavior After Fix

1. Container starts and Ollama service runs
2. Model `gemma3:270M` is available
3. Health endpoint shows Ollama as "connected"
4. Chat endpoint returns 200 with AI response

## If Still Not Working

1. **Check container logs:**
   ```bash
   docker-compose logs be0
   ```

2. **Check if Ollama is accessible:**
   ```bash
   docker exec be0 curl http://localhost:11434/api/tags
   ```

3. **Manually start Ollama:**
   ```bash
   docker exec -d be0 ollama serve
   sleep 2
   docker exec be0 ollama list
   ```

4. **Rebuild container:**
   ```bash
   docker-compose down
   docker-compose build be0
   docker-compose up be0
   ```
