#!/bin/sh # Wait for PostgreSQL to be ready until pg_isready -h localhost -U db-user do echo "Waiting for PostgreSQL to be ready..." sleep 1 done # Start nginx with explicit configuration echo "Starting nginx..." /usr/sbin/nginx -c /etc/nginx/nginx.conf if [ $? -ne 0 ]; then echo "Failed to start nginx" exit 1 fi # Verify nginx is running if ! ps aux | grep nginx | grep -v grep > /dev/null; then echo "Nginx failed to start" exit 1 fi echo "Nginx started successfully" # Start llamafiler instances echo "Starting llamafiler services..." # Create TMPDIR if it doesn't exist mkdir -p /tmp/llamafiler echo "Starting chat model..." TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model /base/gemma-2b.gguf --listen 0.0.0.0:8082 & GEMMA_PID=$! echo "Starting embedding model..." TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model /base/embeddings.gguf --listen 0.0.0.0:8081 & EMBEDDINGS_PID=$! # Wait for the models to start loading echo "Waiting for initial model loading..." sleep 15 # Give more time for the models to load into memory echo 'Waiting for servers to start...' until curl -s --fail -X POST http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{"model": "gemma-2b", "messages":[{"role":"user","content":"hi"}]}' >/dev/null 2>&1 && \ curl -s --fail -X POST http://localhost:8080/v1/embeddings \ -H "Content-Type: application/json" \ -d '{"input":"test"}' >/dev/null 2>&1 do # Check if processes are still running if ! kill -0 $GEMMA_PID 2>/dev/null; then echo "Gemma model process died." exit 1 fi if ! kill -0 $EMBEDDINGS_PID 2>/dev/null; then echo "Embeddings model process died." exit 1 fi sleep 2 echo "Waiting for services... (PIDs: Gemma=$GEMMA_PID, Embeddings=$EMBEDDINGS_PID)" done echo 'Servers are ready!' # Start the AI assistant application from the correct directory cd /ai-assistant/base && \ NODE_PATH=/ai-assistant/base/node_modules \ PATH=/ai-assistant/base/node_modules/.bin:/ai-assistant/base/bin:$PATH \ exec /ai-assistant/base/bin/entrypoint.sh node app/main.bundle.js