Files
hydro-site-final/scripts/CHAT_API_README.md
Dr. Jones d9961a76ad Initial commit: site code, layouts, content (images added separately)
Fixes duplicate placeholder cover image on the 3 newest blog posts,
broken aspect-ratio classes on the albums listing page, and a
non-responsive fixed sidebar on the chat page.
2026-07-08 06:18:17 +00:00

2.6 KiB

Chat API Server

This Flask-based API server provides a secure interface for the chat feature, connecting users to the Ollama model.

Features

  • Secure: Input sanitization, rate limiting, CORS protection
  • Rate Limited: 10 requests per minute per IP address
  • Context Aware: Maintains conversation history (last 5 messages)
  • Auto-restart: Runs as systemd service with automatic restart

Configuration

Environment Variables:

  • OLLAMA_HOST: Ollama endpoint (default: http://10.30.20.110:11434)
  • OLLAMA_MODEL: Model name (default: drjones-posts-to-much)
  • PORT: API server port (default: 5000)
  • CHAT_API_KEY: Optional API key for additional security (not set by default)

Service Management

# Start service
sudo systemctl start hugo-chat-api.service

# Stop service
sudo systemctl stop hugo-chat-api.service

# Enable at boot
sudo systemctl enable hugo-chat-api.service

# Check status
sudo systemctl status hugo-chat-api.service

# View logs
sudo journalctl -u hugo-chat-api.service -f

API Endpoints

POST /api/chat

Send a chat message and get AI response.

Request:

{
  "message": "How do I maintain sterile conditions?",
  "history": [
    {"role": "user", "content": "Previous message"},
    {"role": "assistant", "content": "Previous response"}
  ]
}

Response:

{
  "response": "AI response text...",
  "model": "drjones-posts-to-much"
}

GET /api/health

Health check endpoint.

Response:

{
  "status": "healthy",
  "model": "drjones-posts-to-much",
  "ollama_host": "http://10.30.20.110:11434"
}

Security Features

  1. Rate Limiting: 10 requests per minute per IP address
  2. Input Sanitization: Removes dangerous characters, limits length to 1000 chars
  3. CORS Protection: Only allows requests from configured origins
  4. Error Handling: Graceful error handling with user-friendly messages

Installation

  1. Install dependencies:

    pip3 install flask flask-cors ollama
    
  2. Enable and start service:

    sudo systemctl daemon-reload
    sudo systemctl enable hugo-chat-api.service
    sudo systemctl start hugo-chat-api.service
    

Troubleshooting

Service won't start:

  • Check logs: sudo journalctl -u hugo-chat-api.service -n 50
  • Verify Ollama is accessible: curl http://10.30.20.110:11434/api/tags
  • Check port 5000 is available: sudo lsof -i :5000

Rate limit errors:

  • Normal behavior - users are limited to 10 requests per minute
  • Rate limit resets after 1 minute

Connection errors:

  • Verify Ollama endpoint is correct and accessible
  • Check firewall rules allow connections to port 5000