-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
322 lines (288 loc) · 13.8 KB
/
Copy pathsetup.sh
File metadata and controls
322 lines (288 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env bash
# ──────────────────────────────────────────────────────────────────────────────
# NPR RAG — First-time setup script for Linux / macOS
#
# Usage:
# ./setup.sh # Full setup
# ./setup.sh --skip-docker # Skip docker compose (remote infra)
# ./setup.sh --openai-key=sk-... # Non-interactive API key
#
# Every step is idempotent — safe to re-run.
# ──────────────────────────────────────────────────────────────────────────────
set -euo pipefail
# ── Paths ────────────────────────────────────────────────────────────────────
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$ROOT_DIR/backend"
FRONTEND_DIR="$ROOT_DIR/frontend"
ENV_EXAMPLE="$BACKEND_DIR/.env.example"
ENV_FILE="$BACKEND_DIR/.env"
VENV_DIR="$BACKEND_DIR/venv"
VENV_PYTHON="$VENV_DIR/bin/python"
REQUIREMENTS="$BACKEND_DIR/requirements.txt"
NODE_MODULES="$FRONTEND_DIR/node_modules"
# ── Arguments ────────────────────────────────────────────────────────────────
SKIP_DOCKER=false
OPENAI_KEY=""
for arg in "$@"; do
case $arg in
--skip-docker) SKIP_DOCKER=true ;;
--openai-key=*) OPENAI_KEY="${arg#*=}" ;;
*)
echo "Unknown argument: $arg"
echo "Usage: ./setup.sh [--skip-docker] [--openai-key=sk-...]"
exit 1
;;
esac
done
# ── Helpers ──────────────────────────────────────────────────────────────────
step() { printf '\n\033[36m[%s/8] %s\033[0m\n' "$1" "$2"; }
ok() { printf ' \033[32m[OK]\033[0m %s\n' "$1"; }
warn() { printf ' \033[33m[WARN]\033[0m %s\n' "$1"; }
err() { printf ' \033[31m[ERROR]\033[0m %s\n' "$1"; }
info() { printf ' %s\n' "$1"; }
# Detect docker compose command (v2 plugin vs standalone v1)
get_docker_compose() {
if docker compose version &>/dev/null; then
echo "docker compose"
elif docker-compose version &>/dev/null; then
echo "docker-compose"
else
echo ""
fi
}
# Detect python command (python3 preferred, then python)
get_python_cmd() {
if command -v python3 &>/dev/null; then
local ver
ver=$(python3 --version 2>&1 | awk '{print $2}' | cut -d. -f1-2)
echo "python3"
elif command -v python &>/dev/null; then
local ver
ver=$(python --version 2>&1)
if [[ "$ver" == *"Python 3"* ]]; then
echo "python"
else
echo ""
fi
else
echo ""
fi
}
# Test minimum version: test_min_version "3.10" 3 10
test_min_version() {
local ver="$1" major_min="$2" minor_min="$3"
local major minor
major=$(echo "$ver" | cut -d. -f1)
minor=$(echo "$ver" | cut -d. -f2)
if (( major > major_min )) || { (( major == major_min )) && (( minor >= minor_min )); }; then
return 0
fi
return 1
}
# sed -i that works on both macOS and Linux
sed_inplace() {
if [[ "$(uname)" == "Darwin" ]]; then
sed -i '' "$@"
else
sed -i "$@"
fi
}
# ── Banner ───────────────────────────────────────────────────────────────────
echo ""
echo "========================================================"
printf '\033[36m NPR — Near-Perfect RAG | First-Time Setup\033[0m\n'
echo "========================================================"
# ═════════════════════════════════════════════════════════════════════════════
# Step 1: Check prerequisites
# ═════════════════════════════════════════════════════════════════════════════
step 1 "Checking prerequisites"
errors=()
# Docker
if [[ "$SKIP_DOCKER" == false ]]; then
dc=$(get_docker_compose)
if [[ -z "$dc" ]]; then
errors+=("Docker not found. Install Docker: https://docs.docker.com/get-docker/")
else
ok "Docker Compose: $dc"
fi
else
info "Docker check skipped (--skip-docker)"
fi
# Python
PYTHON_CMD=$(get_python_cmd)
if [[ -z "$PYTHON_CMD" ]]; then
errors+=("Python 3.10+ not found. Install from https://www.python.org/downloads/")
else
py_ver=$($PYTHON_CMD --version 2>&1 | awk '{print $2}' | cut -d. -f1-2)
if ! test_min_version "$py_ver" 3 10; then
errors+=("Python 3.10+ required, found $py_ver")
else
ok "Python: $py_ver (command: $PYTHON_CMD)"
fi
fi
# Node.js
if command -v node &>/dev/null; then
node_ver=$(node --version | tr -d 'v' | cut -d. -f1-2)
if ! test_min_version "$node_ver" 18 0; then
errors+=("Node.js 18+ required, found $node_ver. Install from https://nodejs.org/")
else
ok "Node.js: $node_ver"
fi
else
errors+=("Node.js not found. Install from https://nodejs.org/")
fi
# npm
if command -v npm &>/dev/null; then
npm_ver=$(npm --version)
ok "npm: $npm_ver"
else
errors+=("npm not found. It should come with Node.js — reinstall Node.")
fi
if [[ ${#errors[@]} -gt 0 ]]; then
echo ""
for e in "${errors[@]}"; do err "$e"; done
echo ""
err "Fix the above issues and re-run ./setup.sh"
exit 1
fi
# ═════════════════════════════════════════════════════════════════════════════
# Step 2: Start infrastructure
# ═════════════════════════════════════════════════════════════════════════════
step 2 "Starting infrastructure (Docker)"
if [[ "$SKIP_DOCKER" == true ]]; then
info "Skipped (--skip-docker flag)"
else
dc=$(get_docker_compose)
info "Running: $dc up -d"
$dc -f "$ROOT_DIR/docker-compose.yml" up -d
# Poll container health
containers=("rag-postgres" "rag-milvus" "rag-minio" "rag-redis")
timeout=180
interval=5
info "Waiting for containers to become healthy (timeout: ${timeout}s)..."
for ctr in "${containers[@]}"; do
elapsed=0
healthy=false
while [[ "$healthy" == false ]] && (( elapsed < timeout )); do
status=$(docker inspect --format='{{.State.Health.Status}}' "$ctr" 2>/dev/null || echo "not_found")
if [[ "$status" == "healthy" ]]; then
healthy=true
ok "$ctr is healthy"
break
fi
sleep "$interval"
elapsed=$((elapsed + interval))
done
if [[ "$healthy" == false ]]; then
warn "$ctr did not become healthy within ${timeout}s (may still be starting)"
fi
done
fi
# ═════════════════════════════════════════════════════════════════════════════
# Step 3: Configure environment (.env)
# ═════════════════════════════════════════════════════════════════════════════
step 3 "Configuring environment"
if [[ -f "$ENV_FILE" ]]; then
ok ".env already exists — skipping copy"
else
if [[ ! -f "$ENV_EXAMPLE" ]]; then
err ".env.example not found at $ENV_EXAMPLE"
exit 1
fi
cp "$ENV_EXAMPLE" "$ENV_FILE"
ok "Copied .env.example -> .env"
fi
# Check / set OPENAI_API_KEY
placeholder="your-openai-api-key-here"
if grep -qE "OPENAI_API_KEY\s*=\s*${placeholder}" "$ENV_FILE" || grep -qE "OPENAI_API_KEY\s*=\s*$" "$ENV_FILE"; then
if [[ -n "$OPENAI_KEY" ]]; then
sed_inplace "s|OPENAI_API_KEY=.*|OPENAI_API_KEY=$OPENAI_KEY|" "$ENV_FILE"
ok "Set OPENAI_API_KEY from --openai-key parameter"
else
warn "OPENAI_API_KEY is still the placeholder value."
printf ' Enter your OpenAI API key (or press Enter to skip): '
read -r key
if [[ -n "$key" ]]; then
sed_inplace "s|OPENAI_API_KEY=.*|OPENAI_API_KEY=$key|" "$ENV_FILE"
ok "OPENAI_API_KEY updated"
else
warn "Skipped — you'll need to edit backend/.env before embeddings work"
fi
fi
else
ok "OPENAI_API_KEY is already configured"
fi
# ═════════════════════════════════════════════════════════════════════════════
# Step 4: Python virtual environment
# ═════════════════════════════════════════════════════════════════════════════
step 4 "Setting up Python virtual environment"
if [[ -f "$VENV_PYTHON" ]]; then
ok "venv already exists at $VENV_DIR"
else
info "Creating venv..."
$PYTHON_CMD -m venv "$VENV_DIR"
ok "Created venv at $VENV_DIR"
fi
info "Installing Python dependencies..."
"$VENV_PYTHON" -m pip install --upgrade pip --quiet 2>&1 | tail -n 0 || true
"$VENV_PYTHON" -m pip install -r "$REQUIREMENTS" --quiet
ok "Python dependencies installed"
# ═════════════════════════════════════════════════════════════════════════════
# Step 5: Database / collection / bucket setup
# ═════════════════════════════════════════════════════════════════════════════
step 5 "Running database setup (migrations, collections, buckets)"
cd "$BACKEND_DIR"
"$VENV_PYTHON" -m scripts.setup.setup_all || warn "setup_all.py reported issues (see above)"
cd "$ROOT_DIR"
# ═════════════════════════════════════════════════════════════════════════════
# Step 6: Frontend dependencies
# ═════════════════════════════════════════════════════════════════════════════
step 6 "Installing frontend dependencies"
if [[ -d "$NODE_MODULES" ]]; then
ok "node_modules already exists — skipping npm install"
else
info "Running npm install in frontend/..."
cd "$FRONTEND_DIR"
npm install
cd "$ROOT_DIR"
ok "Frontend dependencies installed"
fi
# ═════════════════════════════════════════════════════════════════════════════
# Step 7: Verify connections
# ═════════════════════════════════════════════════════════════════════════════
step 7 "Verifying service connections"
cd "$BACKEND_DIR"
"$VENV_PYTHON" -m scripts.setup.verify_connections || warn "Some connections failed (see above)"
cd "$ROOT_DIR"
# ═════════════════════════════════════════════════════════════════════════════
# Step 8: Summary
# ═════════════════════════════════════════════════════════════════════════════
step 8 "Setup complete!"
echo ""
printf '\033[32m========================================================\033[0m\n'
printf '\033[32m Setup finished. Next steps:\033[0m\n'
printf '\033[32m========================================================\033[0m\n'
echo ""
echo " Start everything with:"
echo " python run.py"
echo ""
echo " Or start services manually on Linux/macOS:"
echo ""
printf '\033[33m Terminal 1 (Backend):\033[0m\n'
echo " cd backend && source venv/bin/activate"
echo " uvicorn main:app --reload --host 0.0.0.0 --port 8000"
echo ""
printf '\033[33m Terminal 2 (Celery worker):\033[0m\n'
echo " cd backend && source venv/bin/activate"
echo " celery -A app.worker worker --loglevel=info --pool=prefork --concurrency=4"
echo " # On Windows use: --pool=solo"
echo ""
printf '\033[33m Terminal 3 (Frontend):\033[0m\n'
echo " cd frontend && npm run dev"
echo ""
echo " Then open:"
printf '\033[33m http://localhost:3000 \033[0m(App UI)\n'
printf '\033[33m http://localhost:8000/docs \033[0m(API docs)\n'
echo ""
echo " Upload a document and start chatting!"
echo ""