#!/bin/bash # Stop any existing service pkill -f "uvicorn ai_service.api:app" || true pkill -f "run_ai_service.py" || true # Create data directory if it doesn't exist mkdir -p ai_service/data # Create empty files for local storage if they don't exist touch ai_service/data/document_metadata.json touch ai_service/data/chats.json # Start the AI service echo "Starting AI Service on port 5252..." VENV_PATH="./venv" PYTHON_PATH="$VENV_PATH/bin/python" # Check if the virtual environment exists if [ -f "$PYTHON_PATH" ]; then echo "Using Python from virtual environment: $PYTHON_PATH" nohup $PYTHON_PATH run_ai_service.py > ai_service.log 2>&1 & else echo "Virtual environment not found at $VENV_PATH, using system Python" nohup python run_ai_service.py > ai_service.log 2>&1 & fi # Wait a moment for the service to start sleep 2 # Check if the service is running if pgrep -f "run_ai_service.py" > /dev/null; then echo "AI Service started successfully on port 5252" echo "Check ai_service.log for output" echo "To stop the service, run: pkill -f \"run_ai_service.py\"" # Test the health endpoint echo -e "\nTesting health endpoint..." curl -s http://localhost:5252/health echo -e "\n" # Test creating a chat and sending a message echo "Testing chat creation and message sending..." if [ -f "$PYTHON_PATH" ]; then # Create a simple test script cat > test_api.py << 'EOF' import requests import json # Create a chat response = requests.post( "http://localhost:5252/chats", json={ "user_id": "test_user", "title": "Test Chat", "model_id": "llama3.1" } ) if response.status_code == 200: chat_id = response.json()["id"] print(f"Chat created with ID: {chat_id}") # Send a message with parameters response = requests.post( f"http://localhost:5252/chats/{chat_id}/messages", json={ "message": "Hello, AI!", "user_id": "test_user", "temperature": 0.7, "max_tokens": 100 } ) if response.status_code == 200: print("Message sent successfully") print(f"Response: {response.json()['content'][:100]}...") else: print(f"Error sending message: {response.status_code}") print(response.text) else: print(f"Error creating chat: {response.status_code}") print(response.text) EOF # Run the test script with the virtual environment's Python $PYTHON_PATH test_api.py rm test_api.py else echo "Skipping API test as virtual environment Python is not available" fi else echo "Failed to start AI Service. Check ai_service.log for errors." exit 1 fi