#!/bin/bash # A script to automatically start the server and run an interactive chat session. # # REQUIREMENTS: # - 'jq' must be installed (e.g., sudo apt-get install jq). BASE_URL="http://127.0.0.1:8000" # --- 1. Check for Dependencies --- if ! command -v jq &> /dev/null then echo "❌ 'jq' is not installed. Please install it to run this script." exit 1 fi # --- 2. Start the FastAPI Server in the Background --- echo "--- Starting AI Hub Server ---" uvicorn app.main:app --host 127.0.0.1 --port 8000 & SERVER_PID=$! # Define a cleanup function to kill the server on exit cleanup() { echo "" echo "--- Shutting Down Server (PID: $SERVER_PID) ---" kill $SERVER_PID } # Register the cleanup function to run when the script exits (e.g., Ctrl+C or typing 'exit') trap cleanup EXIT echo "Server started with PID: $SERVER_PID. Waiting for it to initialize..." sleep 5 # Wait for the server to be ready # --- 3. Create a New Conversation Session --- echo "--- Starting a new conversation session... ---" SESSION_DATA=$(curl -s -X POST "$BASE_URL/sessions" \ -H "Content-Type: application/json" \ -d '{"user_id": "local_user", "model": "deepseek"}') SESSION_ID=$(echo "$SESSION_DATA" | jq '.id') if [ -z "$SESSION_ID" ] || [ "$SESSION_ID" == "null" ]; then echo "❌ Failed to create a session. Server might not have started correctly." exit 1 fi echo "✅ Session created with ID: $SESSION_ID. Type 'exit' or 'quit' to end." echo "--------------------------------------------------" # --- 4. Start the Interactive Chat Loop --- while true; do read -p "You: " user_input if [[ "$user_input" == "exit" || "$user_input" == "quit" ]]; then break fi json_payload=$(jq -n --arg prompt "$user_input" '{"prompt": $prompt}') ai_response=$(curl -s -X POST "$BASE_URL/sessions/$SESSION_ID/chat" \ -H "Content-Type: application/json" \ -d "$json_payload" | jq -r '.answer') echo "AI: $ai_response" done # The 'trap' will automatically call the cleanup function when the loop breaks.