-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·369 lines (322 loc) · 10.4 KB
/
start.sh
File metadata and controls
executable file
·369 lines (322 loc) · 10.4 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/bash
#
# Dendrite - One-command idempotent startup script
#
# Usage:
# ./start.sh # Auto-detect GPU, use if available
# ./start.sh cpu # Force CPU mode
# ./start.sh gpu # Force GPU mode (fails if no GPU)
# ./start.sh api # HTTP API (auto-detect GPU)
# ./start.sh stop # Stop all services
# ./start.sh status # Show service status
# ./start.sh logs # Show logs
# ./start.sh test # Run tests (auto-detect GPU)
#
# Requirements:
# - Docker
# - Docker Compose (v2)
#
# This script is idempotent - run it as many times as you want.
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log() { echo -e "${GREEN}[dendrite]${NC} $1"; }
warn() { echo -e "${YELLOW}[dendrite]${NC} $1"; }
error() { echo -e "${RED}[dendrite]${NC} $1"; exit 1; }
# =============================================================================
# GPU Detection
# =============================================================================
detect_gpu() {
# Check if nvidia-smi exists and works
if ! command -v nvidia-smi &> /dev/null; then
echo "cpu"
return
fi
# Check if NVIDIA driver is working
if ! nvidia-smi &> /dev/null; then
echo "cpu"
return
fi
# Check if Docker has nvidia runtime
if docker info 2>/dev/null | grep -q "Runtimes:.*nvidia"; then
echo "gpu"
return
fi
# Fallback: try running a GPU container (slower)
if timeout 30 docker run --rm --gpus all nvidia/cuda:12.0-base nvidia-smi &> /dev/null 2>&1; then
echo "gpu"
return
fi
echo "cpu"
}
# Detect mode from argument
MODE="${1:-auto}"
# =============================================================================
# Command handlers
# =============================================================================
cmd_stop() {
log "Stopping all Dendrite services..."
docker compose --profile cpu --profile gpu down 2>/dev/null || true
log "✅ All services stopped"
}
cmd_status() {
log "Service status:"
docker compose ps
}
cmd_logs() {
docker compose logs -f --tail=50
}
cmd_test() {
# Auto-detect GPU for tests
local hw_mode=$(detect_gpu)
log "Running tests (hardware: $hw_mode)..."
# Use tests or tests-gpu based on detected hardware
local test_service="tests"
[ "$hw_mode" = "gpu" ] && test_service="tests-gpu"
docker compose --profile "$hw_mode" run --rm "$test_service" pytest "$@"
}
cmd_help() {
echo "Dendrite - Neural Engine"
echo ""
echo "Usage: ./start.sh [command]"
echo ""
echo "Commands:"
echo " (none), auto Start with auto-detected hardware (GPU if available)"
echo " cpu Force CPU mode"
echo " gpu Force GPU mode (fails if no GPU)"
echo " api Start HTTP API server (auto-detect hardware)"
echo " goal \"...\" Run a single goal and exit"
echo " scheduler Run the scheduler daemon (uses goals.yaml)"
echo " stop Stop all services"
echo " status Show service status"
echo " logs Follow logs"
echo " test Run tests (auto-detect hardware)"
echo " help Show this help"
echo ""
echo "Environment variables:"
echo " RAM_PROFILE Model size: 8gb, 16gb (default), 32gb, 64gb"
echo " FORCE_CPU=1 Force CPU mode even if GPU detected"
echo ""
echo "Examples:"
echo " ./start.sh # Auto-detect, use GPU if available"
echo " ./start.sh cpu # Force CPU mode"
echo " RAM_PROFILE=8gb ./start.sh # Start with smaller model"
echo " ./start.sh api # Start HTTP API (auto-detect)"
echo " ./start.sh goal \"Get my Strava activities\" # Run one goal"
echo " ./start.sh scheduler # Run scheduler daemon"
echo " ./start.sh test -k 'test_foo' # Run specific tests"
}
# =============================================================================
# Handle commands
# =============================================================================
case "$MODE" in
stop)
cmd_stop
exit 0
;;
status)
cmd_status
exit 0
;;
logs)
cmd_logs
exit 0
;;
test)
shift
cmd_test "$@"
exit 0
;;
goal)
shift
GOAL_TEXT="$*"
if [ -z "$GOAL_TEXT" ]; then
error "Usage: ./start.sh goal \"Your goal here\""
fi
log "Running single goal: $GOAL_TEXT"
hw_mode=$(detect_gpu)
# Ensure dependencies are running first
docker compose --profile "$hw_mode" up -d postgres redis llama-$hw_mode
docker compose --profile "$hw_mode" run --rm app-$hw_mode python main.py --goal "$GOAL_TEXT"
exit 0
;;
scheduler)
log "Starting scheduler daemon..."
hw_mode=$(detect_gpu)
# Ensure dependencies are running first
docker compose --profile "$hw_mode" up -d postgres redis llama-$hw_mode
docker compose --profile "$hw_mode" run --rm app-$hw_mode python main.py --daemon --config goals.yaml
exit 0
;;
help|--help|-h)
cmd_help
exit 0
;;
auto|cpu|gpu|api)
# Continue to startup
;;
*)
error "Unknown command: $MODE. Use './start.sh help' for usage."
;;
esac
# =============================================================================
# Startup
# =============================================================================
# Handle FORCE_CPU override
if [ "${FORCE_CPU:-0}" = "1" ]; then
DETECTED_HW="cpu"
log "🔧 FORCE_CPU=1 set, using CPU mode"
else
DETECTED_HW=$(detect_gpu)
fi
# Determine profiles to use based on mode
case "$MODE" in
auto)
# Auto-detect hardware
LLAMA_MODE="$DETECTED_HW"
PROFILES="--profile $LLAMA_MODE"
API_ENABLED=false
;;
cpu)
PROFILES="--profile cpu"
LLAMA_MODE="cpu"
API_ENABLED=false
;;
gpu)
# Verify GPU is available when explicitly requested
if [ "$DETECTED_HW" != "gpu" ]; then
error "GPU mode requested but no working GPU detected. Use './start.sh' for auto-detect."
fi
PROFILES="--profile gpu"
LLAMA_MODE="gpu"
API_ENABLED=false
;;
api)
# Auto-detect hardware for API mode
LLAMA_MODE="$DETECTED_HW"
if [ "$LLAMA_MODE" = "gpu" ]; then
PROFILES="--profile gpu --profile gpu-api"
else
PROFILES="--profile cpu --profile api"
fi
API_ENABLED=true
;;
esac
log "🧠 Dendrite Neural Engine"
log " Mode: $MODE (hardware: $LLAMA_MODE)"
log " RAM Profile: ${RAM_PROFILE:-16gb}"
if [ "$LLAMA_MODE" = "gpu" ]; then
GPU_NAME=$(nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null | head -1)
log " GPU: $GPU_NAME"
fi
if [ "$API_ENABLED" = true ]; then
log " HTTP API: Enabled (port 8000)"
fi
echo ""
# Check Docker
if ! command -v docker &> /dev/null; then
error "Docker not found. Please install Docker first: https://docs.docker.com/get-docker/"
fi
# Check Docker Compose
if ! docker compose version &> /dev/null; then
error "Docker Compose v2 not found. Please update Docker."
fi
# Check if Docker daemon is running
if ! docker info &> /dev/null; then
error "Docker daemon not running. Please start Docker."
fi
# Export RAM profile for docker-compose
export RAM_PROFILE="${RAM_PROFILE:-16gb}"
export RAM_PROFILE="${RAM_PROFILE:-16gb}"
# Stop any existing containers (idempotent)
log "Stopping any existing services..."
docker compose --profile cpu --profile gpu --profile api --profile gpu-api down 2>/dev/null || true
# Pull images (idempotent - skips if already present)
log "Pulling images (this may take a while on first run)..."
docker compose $PROFILES pull 2>/dev/null || true
# Start services
log "Starting services..."
docker compose $PROFILES up -d
# Wait for llama.cpp to be ready
log "Waiting for LLM to load model..."
LLAMA_CONTAINER="dendrite-llama-$LLAMA_MODE"
MAX_WAIT=300
WAITED=0
while true; do
if docker exec "$LLAMA_CONTAINER" curl -sf http://localhost:8080/health > /dev/null 2>&1; then
break
fi
if [ $WAITED -ge $MAX_WAIT ]; then
error "Timeout waiting for LLM. Check logs: docker compose logs llama-$MODE"
fi
sleep 5
WAITED=$((WAITED + 5))
echo -n "."
done
echo ""
log "✅ LLM ready!"
# Wait for API server if enabled
if [ "$API_ENABLED" = true ]; then
log "Waiting for API server..."
API_CONTAINER="dendrite-api"
[ "$LLAMA_MODE" = "gpu" ] && API_CONTAINER="dendrite-api-gpu"
MAX_WAIT=120
WAITED=0
while true; do
if curl -sf http://localhost:8000/health > /dev/null 2>&1; then
break
fi
if [ $WAITED -ge $MAX_WAIT ]; then
warn "⚠️ API server taking longer than expected. Check logs: docker compose logs api"
break
fi
sleep 3
WAITED=$((WAITED + 3))
echo -n "."
done
echo ""
if curl -sf http://localhost:8000/health > /dev/null 2>&1; then
log "✅ API server ready!"
fi
fi
# Show status
echo ""
log "Service status:"
docker compose $PROFILES ps
# Quick health check
echo ""
log "Testing LLM connection..."
RESPONSE=$(curl -s http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Say OK"}],"max_tokens":5}' \
2>/dev/null | grep -o '"content":"[^"]*"' | head -1 || echo "")
if [ -n "$RESPONSE" ]; then
log "✅ LLM responding: $RESPONSE"
else
warn "⚠️ Could not verify LLM response. Check logs if issues persist."
fi
echo ""
log "🎉 Dendrite is ready!"
echo ""
echo " LLM endpoint: http://localhost:8080/v1/chat/completions"
echo " LLM health: http://localhost:8080/health"
if [ "$API_ENABLED" = true ]; then
echo ""
echo " API endpoint: http://localhost:8000/api/v1/goals"
echo " API health: http://localhost:8000/health"
echo " API docs: http://localhost:8000/docs"
fi
echo ""
echo " Commands:"
echo " ./start.sh status # Check status"
echo " ./start.sh logs # View logs"
echo " ./start.sh stop # Stop all"
echo " ./start.sh test # Run tests"
echo ""