Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·355 lines (312 loc) · 13.5 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·355 lines (312 loc) · 13.5 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
#!/bin/bash
# LLM Router: Complete Setup and Run Script
# This script automates the entire workflow from setup to Streamlit dashboard
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
DATA_FILE="data/routerbench_raw.pkl"
EMBEDDINGS_FILE="data/prompt_embeddings_all-MiniLM-L6-v2.pkl"
VENV_DIR=".venv"
PYTHON_CMD="python3"
SKIP_DOWNLOAD=false
SKIP_EMBEDDINGS=false
SKIP_TRAINING=false
SKIP_EVALUATION=false
NUM_EPISODES=1000
EVAL_EPISODES=100
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--skip=*)
SKIP_LIST="${1#*=}"
IFS=',' read -ra SKIP_ITEMS <<< "$SKIP_LIST"
for item in "${SKIP_ITEMS[@]}"; do
case "${item,,}" in
download)
SKIP_DOWNLOAD=true
;;
training)
SKIP_TRAINING=true
;;
embedding)
SKIP_EMBEDDINGS=true
;;
evaluation)
SKIP_EVALUATION=true
;;
*)
echo -e "${RED}Unknown skip option: $item${NC}"
echo "Valid options: download, embedding, training, evaluation"
echo "Example: --skip=download,embedding,training"
exit 1
;;
esac
done
shift
;;
--episodes)
NUM_EPISODES="$2"
shift 2
;;
--eval-episodes)
EVAL_EPISODES="$2"
shift 2
;;
--help)
echo "Usage: ./run.sh [OPTIONS]"
echo ""
echo "Options:"
echo " --skip=LIST Skip steps (comma-separated: download,embedding,training,evaluation)"
echo " Example: --skip=download,embedding,training"
echo " --episodes N Number of training episodes (default: 1000)"
echo " --eval-episodes N Number of evaluation episodes (default: 100)"
echo " --help Show this help message"
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Use --help for usage information"
exit 1
;;
esac
done
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}LLM Router - Complete Setup & Run${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# Step 1: Check Python
echo -e "${YELLOW}[1/9] Checking Python installation...${NC}"
if ! command -v python3 &> /dev/null; then
if ! command -v python &> /dev/null; then
echo -e "${RED}Error: Python 3 is not installed. Please install Python 3.8+${NC}"
exit 1
else
PYTHON_CMD="python"
fi
fi
PYTHON_VERSION=$($PYTHON_CMD --version 2>&1 | awk '{print $2}')
echo -e "${GREEN}✓ Found Python $PYTHON_VERSION${NC}"
echo ""
# Step 2: Setup Virtual Environment
echo -e "${YELLOW}[2/9] Setting up virtual environment...${NC}"
if [ ! -d "$VENV_DIR" ]; then
echo "Creating virtual environment..."
$PYTHON_CMD -m venv $VENV_DIR
echo -e "${GREEN}✓ Virtual environment created${NC}"
else
echo -e "${GREEN}✓ Virtual environment already exists${NC}"
fi
# Activate virtual environment
source $VENV_DIR/bin/activate
echo -e "${GREEN}✓ Virtual environment activated${NC}"
echo ""
# Step 3: Install Dependencies
echo -e "${YELLOW}[3/9] Installing dependencies...${NC}"
echo -e "${YELLOW}This might take a while...${NC}"
if [ -f "requirements.txt" ]; then
pip install --upgrade pip -q
pip install -r requirements.txt -q
echo -e "${GREEN}✓ Dependencies installed${NC}"
else
echo -e "${RED}Error: requirements.txt not found${NC}"
exit 1
fi
echo ""
# Step 4: Check and Download Data File (if not skipped)
if [ "$SKIP_DOWNLOAD" = false ]; then
echo -e "${YELLOW}[4/9] Checking and downloading data file...${NC}"
if [ ! -f "$DATA_FILE" ]; then
echo -e "${YELLOW}⚠ Data file not found at $DATA_FILE${NC}"
echo "Attempting to download from Hugging Face..."
# Create data directory if it doesn't exist
mkdir -p data
# Hugging Face direct download URL
HF_URL="https://huggingface.co/datasets/withmartian/routerbench/resolve/main/routerbench_raw.pkl"
# Try to download using available tool
if command -v wget &> /dev/null; then
echo "Downloading using wget (this may take a while, file is ~1.2 GB)..."
wget -O "$DATA_FILE" "$HF_URL" --progress=bar:force 2>&1 | grep -E "([0-9]+%)|saved"
elif command -v curl &> /dev/null; then
echo "Downloading using curl (this may take a while, file is ~1.2 GB)..."
curl -L -o "$DATA_FILE" "$HF_URL" --progress-bar
else
echo -e "${YELLOW}⚠ Neither wget nor curl found. Attempting Python download...${NC}"
$PYTHON_CMD -c "
import urllib.request
import sys
def progress_hook(count, block_size, total_size):
percent = int(count * block_size * 100 / total_size)
sys.stdout.write(f'\rDownloading... {percent}%')
sys.stdout.flush()
try:
urllib.request.urlretrieve('$HF_URL', '$DATA_FILE', reporthook=progress_hook)
print('\n')
except Exception as e:
print(f'\nError: {e}')
sys.exit(1)
"
fi
# Verify download
if [ -f "$DATA_FILE" ]; then
FILE_SIZE=$(du -h "$DATA_FILE" | cut -f1)
echo -e "${GREEN}✓ Data file downloaded successfully ($FILE_SIZE)${NC}"
else
echo -e "${RED}✗ Download failed. Please download manually from:${NC}"
echo " https://huggingface.co/datasets/withmartian/routerbench/blob/main/routerbench_raw.pkl"
echo " And place it in the data/ directory"
exit 1
fi
else
echo -e "${GREEN}✓ Data file found${NC}"
fi
else
echo -e "${YELLOW}[4/9] Skipping download (--skip=download)${NC}"
if [ ! -f "$DATA_FILE" ]; then
echo -e "${RED}✗ Error: Data file not found at $DATA_FILE${NC}"
echo "Please download it manually or run without --skip=download"
exit 1
else
echo -e "${GREEN}✓ Data file found${NC}"
fi
fi
echo ""
# Step 5: Precompute Embeddings (if not skipped)
if [ "$SKIP_EMBEDDINGS" = false ]; then
if [ -f "$DATA_FILE" ] && [ ! -f "$EMBEDDINGS_FILE" ]; then
echo -e "${YELLOW}[5/9] Precomputing embeddings...${NC}"
echo -e "${YELLOW}This might take a while...${NC}"
# Set PYTHONPATH
export PYTHONPATH="$(pwd):$PYTHONPATH"
$PYTHON_CMD scripts/precompute_embeddings.py --model all-MiniLM-L6-v2 || echo -e "${YELLOW}⚠ Embedding precomputation failed or was interrupted${NC}"
if [ -f "$EMBEDDINGS_FILE" ]; then
echo -e "${GREEN}✓ Embeddings precomputed${NC}"
else
echo -e "${YELLOW}⚠ Embeddings not found. Comparison experiments may fail.${NC}"
fi
elif [ -f "$EMBEDDINGS_FILE" ]; then
echo -e "${YELLOW}[5/9] Embeddings already exist, skipping precomputation${NC}"
else
echo -e "${YELLOW}[5/9] Skipping embedding precomputation (data file not found)${NC}"
fi
else
echo -e "${YELLOW}[5/9] Skipping embedding precomputation (--skip=embedding)${NC}"
if [ ! -f "$EMBEDDINGS_FILE" ]; then
echo -e "${YELLOW}⚠ Embeddings not found. Embedding-based training and comparison experiments will be skipped.${NC}"
else
echo -e "${GREEN}✓ Embeddings file found${NC}"
fi
fi
echo ""
# Step 6: Train Agents (if not skipped)
if [ "$SKIP_TRAINING" = false ]; then
echo -e "${YELLOW}[6/9] Training agents...${NC}"
# Check if models already exist
if [ -f "results/models/dqn_agent_final.pt" ] && \
[ -f "results/models/ppo_agent_final.pt" ] && \
[ -f "results/models/linucb_agent_final.npz" ] && \
[ -f "results/models/pickllm_agent_final.npz" ]; then
echo -e "${YELLOW}Models already exist. Skipping training.${NC}"
echo "Use --skip=training to skip this check in the future."
else
# Create results directory
mkdir -p results/models
# Set PYTHONPATH
export PYTHONPATH="$(pwd):$PYTHONPATH"
echo "Training DQN agent (features)..."
$PYTHON_CMD training/train.py --algo dqn --num_episodes $NUM_EPISODES || echo -e "${YELLOW}⚠ DQN training failed or was interrupted${NC}"
echo "Training PPO agent (features)..."
$PYTHON_CMD training/train.py --algo ppo --num_episodes $NUM_EPISODES || echo -e "${YELLOW}⚠ PPO training failed or was interrupted${NC}"
# Train embedding-based models if embeddings exist
if [ -f "$EMBEDDINGS_FILE" ]; then
echo "Training DQN agent (embeddings)..."
$PYTHON_CMD training/train.py --algo dqn --num_episodes $NUM_EPISODES --use_embeddings --embeddings_path "$EMBEDDINGS_FILE" --embedding_model "all-MiniLM-L6-v2" || echo -e "${YELLOW}⚠ DQN (embeddings) training failed or was interrupted${NC}"
echo "Training PPO agent (embeddings)..."
$PYTHON_CMD training/train.py --algo ppo --num_episodes $NUM_EPISODES --use_embeddings --embeddings_path "$EMBEDDINGS_FILE" --embedding_model "all-MiniLM-L6-v2" || echo -e "${YELLOW}⚠ PPO (embeddings) training failed or was interrupted${NC}"
else
echo -e "${YELLOW}⚠ Embeddings not found. Skipping embedding-based model training.${NC}"
echo "Embedding comparison experiments will be skipped."
fi
echo "Training LinUCB agent..."
$PYTHON_CMD training/train.py --algo linucb --num_episodes $NUM_EPISODES || echo -e "${YELLOW}⚠ LinUCB training failed or was interrupted${NC}"
echo "Training PickLLM agent..."
$PYTHON_CMD training/train.py --algo pickllm --num_episodes $NUM_EPISODES || echo -e "${YELLOW}⚠ PickLLM training failed or was interrupted${NC}"
echo "Running Greedy baseline..."
$PYTHON_CMD training/train.py --algo greedy --num_episodes $NUM_EPISODES || echo -e "${YELLOW}⚠ Greedy baseline failed or was interrupted${NC}"
echo -e "${GREEN}✓ Training completed${NC}"
fi
else
echo -e "${YELLOW}[6/9] Skipping training (--skip=training)${NC}"
fi
echo ""
# Step 7: Run Evaluation (if not skipped)
if [ "$SKIP_EVALUATION" = false ]; then
echo -e "${YELLOW}[7/9] Running evaluation...${NC}"
# Set PYTHONPATH
export PYTHONPATH="$(pwd):$PYTHONPATH"
# Check if evaluation results already exist
if [ -f "results/streamlit/evaluation_results.json" ]; then
echo -e "${YELLOW}Evaluation results already exist. Skipping evaluation.${NC}"
echo "Use --skip=evaluation to skip this check in the future."
else
echo "Evaluating all agents..."
$PYTHON_CMD evaluation/evaluate.py --agent_type all --num_episodes $EVAL_EPISODES || echo -e "${YELLOW}⚠ Evaluation failed or was interrupted${NC}"
echo -e "${GREEN}✓ Evaluation completed${NC}"
fi
else
echo -e "${YELLOW}[7/9] Skipping evaluation (--skip=evaluation)${NC}"
fi
echo ""
# Step 8: Run Comparison Experiments
echo -e "${YELLOW}[8/9] Running comparison experiments...${NC}"
# Set PYTHONPATH
export PYTHONPATH="$(pwd):$PYTHONPATH"
# Check if comparison results already exist
if [ -f "results/streamlit/baseline_comparison.json" ] && \
[ -f "results/streamlit/embedding_comparison.json" ]; then
echo -e "${YELLOW}Comparison results already exist. Skipping comparison.${NC}"
echo "Delete the comparison JSON files to regenerate them."
else
# Check if required models exist (feature-based or embedding-based)
if [ -f "results/models/dqn_agent_final.pt" ] || [ -f "results/models/dqn_agent_final_embeddings.pt" ]; then
dqn_exists=true
else
dqn_exists=false
fi
if [ -f "results/models/ppo_agent_final.pt" ] || [ -f "results/models/ppo_agent_final_embeddings.pt" ]; then
ppo_exists=true
else
ppo_exists=false
fi
if [ "$dqn_exists" = false ] || [ "$ppo_exists" = false ]; then
echo -e "${YELLOW}⚠ Required models not found. Skipping comparison experiments.${NC}"
echo "Train agents first (run without --skip=training) to generate comparison results."
else
# Check if embeddings exist (for embedding experiments)
if [ ! -f "$EMBEDDINGS_FILE" ]; then
echo -e "${YELLOW}⚠ Embeddings not found. Embedding experiments will be skipped.${NC}"
echo "Baseline comparison will still run."
fi
echo "Running comparison experiments (this may take a while)..."
$PYTHON_CMD evaluation/compare_embedding_experiments.py --num_episodes $EVAL_EPISODES || echo -e "${YELLOW}⚠ Some comparison experiments may have failed${NC}"
echo -e "${GREEN}✓ Comparison experiments completed${NC}"
fi
fi
echo ""
# Step 9: Launch Streamlit
echo -e "${YELLOW}[9/9] Launching Streamlit dashboard...${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}Launching Streamlit Dashboard...${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo -e "${GREEN}Dashboard will open at: http://localhost:8501${NC}"
echo "Press Ctrl+C to stop the server"
echo ""
# Set PYTHONPATH
export PYTHONPATH="$(pwd):$PYTHONPATH"
# Launch Streamlit
streamlit run evaluation/streamlit_app.py