Skip to content

Latest commit

 

History

History
351 lines (278 loc) · 8.66 KB

File metadata and controls

351 lines (278 loc) · 8.66 KB

AI-Powered Search - Ready to Deploy

What Changed

1. Search Icon Updated

  • Changed from magnifying glass (🔍) to brain icon (🧠)
  • Indicates AI-powered capabilities
  • Mobile-friendly positioning fixed (proper spacing between icons)

2. Hybrid Search Intelligence

The search now automatically detects query type:

Traditional Search (fast, direct DB):

  • "John Smith" → finds voter by name
  • "123 Main St" → finds voters at address
  • "1234567890" → finds voter by VUID

AI Search (natural language):

  • "Show me voters in TX-15 who voted in 2024 but not 2026"
  • "How many voters switched from Republican to Democratic?"
  • "Find new voters in Hidalgo County"

3. Google-Style AI Response

When AI detects a question, it shows:

  • AI Response section at top (like Google's AI Overview)
  • Natural language explanation of results
  • Collapsible SQL query (for transparency)
  • Follow-up question suggestions
  • Data results below (tables or voter cards)

4. Mobile Layout Fixed

  • Bottom icons now properly spaced on mobile
  • No more overlapping buttons
  • Touch-friendly sizing (44px)

5. Admin Dashboard Management

  • New 🧠 AI Assistant tab in admin dashboard
  • Check Ollama service status
  • Check for and install updates with one click
  • Manage installed models (pull, delete, test)
  • View performance statistics
  • Action log for all operations

Files Modified

Frontend

  • public/search.js - Added hybrid search logic with AI detection
  • public/index.html - Added llm-chat.js script tag
  • public/styles.css - Added AI response styles + fixed mobile icon spacing

Backend

  • backend/app.py - Added /api/llm/query, /api/llm/status, and Ollama management endpoints
  • backend/llm_query.py - Already created (QueryAssistant class)
  • backend/llm_api_endpoint.py - Reference implementation (integrated into app.py)

Admin Dashboard

  • backend/admin/dashboard.html - Added AI Assistant tab
  • backend/admin/dashboard.js - Added Ollama management functions

Deployment

  • deploy/setup_llm_assistant.sh - Automated setup script

Documentation

  • AI_SEARCH_IMPLEMENTATION.md - Complete technical documentation
  • AI_SEARCH_READY.md - This file

Deployment Steps

Option 1: Automated (Recommended)

# 1. Copy setup script to server
scp -i WhoVoted/deploy/whovoted-key.pem WhoVoted/deploy/setup_llm_assistant.sh ubuntu@politiquera.com:/tmp/

# 2. Run setup script
ssh -i WhoVoted/deploy/whovoted-key.pem ubuntu@politiquera.com
sudo bash /tmp/setup_llm_assistant.sh

The script will:

  1. Install Ollama (LLM runtime)
  2. Download Llama 3.2 3B model (~2GB, takes 2-5 minutes)
  3. Install Python ollama package
  4. Restart gunicorn with new code
  5. Verify everything is working

Option 2: Manual

ssh -i WhoVoted/deploy/whovoted-key.pem ubuntu@politiquera.com

# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh

# Start service
sudo systemctl enable ollama
sudo systemctl start ollama

# Pull model (this takes a few minutes)
ollama pull llama3.2:3b-instruct

# Install Python package
/opt/whovoted/venv/bin/pip install ollama

# Restart gunicorn
pkill gunicorn
cd /opt/whovoted
PYTHONPATH=/opt/whovoted/backend /opt/whovoted/venv/bin/gunicorn -w 5 -b 127.0.0.1:5000 'app:app' --daemon

Testing

1. Check AI Service Status

curl http://localhost/api/llm/status

Should return:

{
  "available": true,
  "models": ["llama3.2:3b-instruct"],
  "recommended": "llama3.2:3b-instruct"
}

2. Test Admin Dashboard

  1. Go to https://politiquera.com/admin
  2. Click 🧠 AI Assistant tab
  3. Should show:
    • Service status (Running/Not Installed)
    • Update checker
    • Installed models list
    • Performance stats
  4. If not installed, click Install Ollama button
  5. Once installed, click ⬆️ Update Now to check for updates
  6. Test a model by clicking 🧪 Test button

3. Test Traditional Search

  1. Click brain icon (🧠) at bottom-left
  2. Type: "John Smith"
  3. Press Enter
  4. Should show voter cards (fast, <200ms)

4. Test AI Search

  1. Click brain icon (🧠)
  2. Type: "Show me voters in TX-15 who voted in 2024 but not 2026"
  3. Press Enter
  4. Should show:
    • AI Response section with explanation
    • SQL query (collapsible)
    • Follow-up suggestions
    • Results table below

5. Test Mobile Layout

  1. Open on mobile device or resize browser to <768px
  2. Check bottom icons are properly spaced
  3. Brain icon should be at left, other icons at right
  4. No overlapping

What Users See

Desktop

  • Brain icon (🧠) at bottom-left corner
  • Click to open search modal
  • Type question or name
  • Get instant results

Mobile

  • Brain icon (🧠) at bottom-left
  • Properly spaced from other icons
  • Full-screen modal on tap
  • Touch-friendly buttons

Example Queries to Try

Simple (Traditional Search)

  • "Maria Garcia"
  • "123 Main Street"
  • "McAllen"

Complex (AI Search)

  • "Show me voters in TX-15 who voted in 2024 but not 2026"
  • "How many voters switched from Republican to Democratic?"
  • "Find new voters in Hidalgo County"
  • "What's the turnout rate by age group?"
  • "Show me voters who voted early in 2026"
  • "Find voters in precinct 123"

Technical Details

Model

  • Name: Llama 3.2 3B Instruct
  • Size: 2GB RAM
  • Speed: ~50 tokens/sec on CPU (2-5 sec per query)
  • Cost: $0/month (runs locally)

Performance

  • Traditional search: 50-200ms
  • AI search: 2-5 seconds
    • LLM generation: 1-3 sec
    • SQL execution: 50-500ms
    • Explanation: 500ms-1s

Security

  • Requires authentication
  • SQL injection prevention
  • Read-only database access
  • Query validation

Cost Savings

Before (if using OpenAI)

  • $0.045 per query
  • 1000 queries/month = $45/month
  • Annual: $540

After (local LLM)

  • $0 per query
  • Unlimited queries
  • Annual: $0
  • Savings: $540/year

Troubleshooting

"AI service not available"

# Check Ollama is running
sudo systemctl status ollama

# Check model is downloaded
ollama list

# Restart if needed
sudo systemctl restart ollama

Slow first query

  • Normal - model loads into memory on first use
  • Subsequent queries are faster
  • Model stays in memory

SQL errors

  • LLM may occasionally generate invalid SQL
  • User sees error with SQL query shown
  • Can refine question and retry
  • Report persistent issues

Monitoring

# Check Ollama status
sudo systemctl status ollama

# View Ollama logs
sudo journalctl -u ollama -f

# Check available models
ollama list

# Test LLM directly
curl http://localhost:11434/api/generate -d '{
  "model": "llama3.2:3b-instruct",
  "prompt": "Convert to SQL: Show all voters in TX-15"
}'

Next Steps

After deployment:

  1. Test with various queries
  2. Monitor performance and errors
  3. Collect user feedback
  4. Consider enhancements:
    • Query history
    • Saved queries
    • Export to CSV
    • Chart generation

Support

If issues arise:

  1. Check logs: sudo journalctl -u ollama -f
  2. Verify model: ollama list
  3. Test API: curl http://localhost/api/llm/status
  4. Restart services:
    sudo systemctl restart ollama
    pkill gunicorn
    cd /opt/whovoted
    PYTHONPATH=/opt/whovoted/backend /opt/whovoted/venv/bin/gunicorn -w 5 -b 127.0.0.1:5000 'app:app' --daemon

Summary

✅ Search icon changed to brain (🧠)
✅ Hybrid search detects traditional vs AI queries
✅ Google-style AI response section
✅ Mobile icon layout fixed
✅ Admin dashboard for Ollama management
✅ One-click updates from admin panel
✅ Model management (pull, delete, test)
✅ Zero API costs (local LLM)
✅ Ready to deploy with automated script

Ready to go live!

Admin Dashboard Features

AI Assistant Tab

Access at: https://politiquera.com/admin🧠 AI Assistant

Service Status Section:

  • Ollama service status (Running/Stopped)
  • Version information
  • API availability
  • Number of installed models
  • One-click refresh

Updates Section:

  • Automatic update checker
  • Shows current vs latest version
  • ⬆️ Update Now button (one-click update)
  • Requires superadmin role for updates

Models Section:

  • List of all installed models
  • Model size and last modified date
  • ➕ Pull New Model button
  • Per-model actions:
    • 🧪 Test - Test model with sample query
    • 🗑️ Delete - Remove model (superadmin only)

Performance Stats:

  • Total queries processed
  • Average response time
  • Success rate
  • Memory usage

Action Log:

  • Real-time log of all operations
  • Timestamps for each action
  • Success/failure indicators

Security

  • All Ollama management requires authentication
  • Install/Update/Delete operations require superadmin role
  • Pull and Test operations available to all authenticated users
  • Action log tracks all operations