#!/bin/bash # GLAM Heritage RAG API Startup Script # # Usage: ./scripts/start_api.sh [port] [log_level] # port: API port (default: 8765) # log_level: uvicorn log level (default: info) # # Prerequisites: # - .env file with OPENAI_API_KEY # - Qdrant running on localhost:6333 # - Valkey/Redis running on localhost:6379 # - Oxigraph running on localhost:7878 (optional) set -e # Navigate to project root SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" cd "$PROJECT_ROOT" # Configuration PORT="${1:-8765}" LOG_LEVEL="${2:-info}" API_LOG="/tmp/glam_api_${PORT}.log" echo "=== GLAM Heritage RAG API Startup ===" echo "Project root: $PROJECT_ROOT" echo "Port: $PORT" echo "Log level: $LOG_LEVEL" echo "Log file: $API_LOG" # Check .env file exists if [ ! -f ".env" ]; then echo "ERROR: .env file not found in $PROJECT_ROOT" echo "Create .env with at least OPENAI_API_KEY" exit 1 fi # Load environment variables echo "" echo "Loading environment variables from .env..." set -a # automatically export all variables source .env set +a # Verify critical environment variables if [ -z "$OPENAI_API_KEY" ]; then echo "ERROR: OPENAI_API_KEY not set in .env" exit 1 fi echo " OPENAI_API_KEY: set (${#OPENAI_API_KEY} chars)" # Check optional services echo "" echo "Checking services..." # Qdrant if curl -s http://localhost:6333/collections >/dev/null 2>&1; then QDRANT_STATUS="✓ running" QDRANT_COLLECTIONS=$(curl -s http://localhost:6333/collections | python3 -c "import sys,json; print(len(json.load(sys.stdin)['result']['collections']))" 2>/dev/null || echo "?") echo " Qdrant: $QDRANT_STATUS ($QDRANT_COLLECTIONS collections)" else echo " Qdrant: ✗ not running (RAG search will fail)" fi # Valkey/Redis if redis-cli ping >/dev/null 2>&1; then echo " Valkey/Redis: ✓ running" else echo " Valkey/Redis: ✗ not running (caching disabled)" fi # Oxigraph if curl -s http://localhost:7878/query -H "Accept: application/json" --data-urlencode "query=SELECT (1 as ?x) WHERE {}" >/dev/null 2>&1; then TRIPLE_COUNT=$(curl -s http://localhost:7878/query -H "Accept: application/sparql-results+json" --data-urlencode "query=SELECT (COUNT(*) as ?c) WHERE { ?s ?p ?o }" | python3 -c "import sys,json; print(json.load(sys.stdin)['results']['bindings'][0]['c']['value'])" 2>/dev/null || echo "?") echo " Oxigraph: ✓ running ($TRIPLE_COUNT triples)" else echo " Oxigraph: ✗ not running (SPARQL queries will fail)" fi # Kill any existing API on the same port echo "" echo "Checking for existing API on port $PORT..." if pgrep -f "uvicorn.*$PORT" >/dev/null 2>&1; then echo " Stopping existing API..." pkill -f "uvicorn.*$PORT" || true sleep 2 fi # Start the API echo "" echo "Starting API..." nohup python -m uvicorn glam_extractor.api.main:app \ --port "$PORT" \ --log-level "$LOG_LEVEL" \ > "$API_LOG" 2>&1 & API_PID=$! echo " PID: $API_PID" # Wait for startup echo " Waiting for startup..." for i in {1..30}; do if curl -s "http://localhost:$PORT/api/rag/health" >/dev/null 2>&1; then break fi sleep 1 done # Verify startup if curl -s "http://localhost:$PORT/api/rag/health" >/dev/null 2>&1; then echo "" echo "=== API Started Successfully ===" echo " Health: http://localhost:$PORT/api/rag/health" echo " RAG Query: POST http://localhost:$PORT/api/rag/query" echo " Docs: http://localhost:$PORT/docs" echo "" echo "Test query:" echo " curl -X POST http://localhost:$PORT/api/rag/query \\" echo " -H 'Content-Type: application/json' \\" echo " -d '{\"question\": \"museums in Amsterdam\", \"k\": 5}'" echo "" echo "Tail logs: tail -f $API_LOG" else echo "" echo "=== API Failed to Start ===" echo "Check logs: tail -50 $API_LOG" tail -20 "$API_LOG" exit 1 fi