Datasets:
Add files using upload-large-folder tool
Browse files- .DS_Store +0 -0
- client/.DS_Store +0 -0
- client/Prompts_large/B2/B2_S.txt +17 -0
- client/Prompts_large/B2/B2_r300_n0_Q.txt +0 -0
- client/Prompts_large/B2/B2_r300_n10_Q.txt +0 -0
- client/Prompts_large/B2/B2_r300_n20_Q.txt +0 -0
- client/Prompts_large/B2/B2_r300_n30_Q.txt +0 -0
- client/Prompts_large/B2/B2_r300_n40_Q.txt +0 -0
- client/Prompts_large/B2/B2_r400_n0_Q.txt +0 -0
- client/Prompts_large/B2/B2_r400_n10_Q.txt +0 -0
- client/Prompts_large/B2/B2_r400_n20_Q.txt +0 -0
- client/Prompts_large/B2/B2_r400_n30_Q.txt +0 -0
- client/Prompts_large/B2/B2_r400_n40_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_S.txt +1 -0
- client/Prompts_large/B2/B2_vF_r300_n0_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_r300_n10_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_r300_n20_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_r300_n30_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_r400_n0_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_r400_n10_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_r400_n20_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_r400_n30_Q.txt +0 -0
- client/Prompts_large/B2/B2_vF_r400_n40_Q.txt +0 -0
- client/Prompts_large/B2/placeholder.txt +0 -0
- client/Prompts_large/B5/B5_S.txt +1 -0
- client/Prompts_large/B5/B5_r300_n0_Q.txt +0 -0
- client/Prompts_large/B5/B5_r300_n10_Q.txt +0 -0
- client/Prompts_large/B5/B5_r300_n20_Q.txt +0 -0
- client/Prompts_large/B5/B5_r300_n30_Q.txt +0 -0
- client/Prompts_large/B5/B5_r300_n40_Q.txt +0 -0
- client/Prompts_large/B5/B5_r400_n0_Q.txt +0 -0
- client/Prompts_large/B5/B5_r400_n10_Q.txt +0 -0
- client/Prompts_large/B5/B5_r400_n20_Q.txt +0 -0
- client/Prompts_large/B5/B5_r400_n30_Q.txt +0 -0
- client/Prompts_large/B5/B5_r400_n40_Q.txt +0 -0
- client/Prompts_large/B5/placeholder.txt +0 -0
- client/ask.py +0 -0
- client/call_db.py +128 -0
- client/call_server.py +47 -0
- client/client_requirements.txt +1 -0
- client/eval.py +0 -0
- client/init.py +0 -0
- readme.md +213 -0
- serve_requirements.txt +98 -0
- server/db.py +72 -0
- server/main.py +74 -0
- server/query.py +1311 -0
- server/serve.py +430 -0
- server/setup_ngrok.sh +29 -0
- server/test.py +143 -0
.DS_Store
ADDED
|
Binary file (10.2 kB). View file
|
|
|
client/.DS_Store
ADDED
|
Binary file (90.1 kB). View file
|
|
|
client/Prompts_large/B2/B2_S.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[2.5496 2.5454 2.5411 2.5368 2.5324 2.5281 2.5238 2.5194 2.515 2.5106
|
| 2 |
+
2.5062 2.5018 2.4974 2.493 2.4885 2.484 2.4796 2.4751 2.4706 2.466
|
| 3 |
+
2.4615 2.457 2.4524 2.4478 2.4432 2.4386 2.434 2.4293 2.4247 2.42
|
| 4 |
+
2.4153 2.4106 2.4059 2.4012 2.3964 2.3916 2.3868 2.382 2.3772 2.3724
|
| 5 |
+
2.3675 2.3627 2.3578 2.3528 2.3479 2.343 2.338 2.333 2.328 2.323
|
| 6 |
+
2.3179 2.3129 2.3078 2.3027 2.2976 2.2924 2.2872 2.282 2.2768 2.2716
|
| 7 |
+
2.2663 2.2611 2.2558 2.2504 2.2451 2.2397 2.2343 2.2289 2.2234 2.218
|
| 8 |
+
2.2125 2.2069 2.2014 2.1958 2.1902 2.1846 2.1789 2.1732 2.1675 2.1618
|
| 9 |
+
2.156 2.1502 2.1444 2.1385 2.1326 2.1267 2.1207 2.1147 2.1087 2.1026
|
| 10 |
+
2.0965 2.0904 2.0842 2.078 2.0717 2.0655 2.0591 2.0528 2.0464 2.0399
|
| 11 |
+
2.0334 2.0269 2.0203 2.0137 2.0071 2.0004 1.9936 1.9868 1.9799 1.973
|
| 12 |
+
1.9661 1.9591 1.952 1.9449 1.9377 1.9305 1.9232 1.9158 1.9084 1.9009
|
| 13 |
+
1.8934 1.8858 1.8781 1.8704 1.8625 1.8546 1.8466 1.8386 1.8304 1.8222
|
| 14 |
+
1.8139 1.8055 1.797 1.7884 1.7797 1.7709 1.762 1.753 1.7438 1.7346
|
| 15 |
+
1.7252 1.7157 1.706 1.6962 1.6863 1.6762 1.666 1.6555 1.6449 1.6341
|
| 16 |
+
1.6231 1.6119 1.6004 1.5888 1.5768 1.5646 1.5521 1.5393 1.5261 1.5126
|
| 17 |
+
1.4987 1.4843 1.4695 1.4541 1.4381 1.4214 1.404 1.3857]
|
client/Prompts_large/B2/B2_r300_n0_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r300_n10_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r300_n20_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r300_n30_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r300_n40_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r400_n0_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r400_n10_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r400_n20_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r400_n30_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_r400_n40_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_S.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
-0.05
|
client/Prompts_large/B2/B2_vF_r300_n0_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_r300_n10_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_r300_n20_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_r300_n30_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_r400_n0_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_r400_n10_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_r400_n20_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_r400_n30_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/B2_vF_r400_n40_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B2/placeholder.txt
ADDED
|
File without changes
|
client/Prompts_large/B5/B5_S.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
24.97
|
client/Prompts_large/B5/B5_r300_n0_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r300_n10_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r300_n20_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r300_n30_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r300_n40_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r400_n0_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r400_n10_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r400_n20_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r400_n30_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/B5_r400_n40_Q.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/Prompts_large/B5/placeholder.txt
ADDED
|
File without changes
|
client/ask.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/call_db.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
DATABASE_URL = # your_URL_here
|
| 6 |
+
|
| 7 |
+
def safe_key(text):
|
| 8 |
+
return text.replace('.', '_')
|
| 9 |
+
|
| 10 |
+
# --------------- Save results into local JSONL file ----------------
|
| 11 |
+
|
| 12 |
+
def save_result(title, model, save_file=None):
|
| 13 |
+
key = f"{title}-{safe_key(model)}"
|
| 14 |
+
url = f"{DATABASE_URL}/results/{key}.json"
|
| 15 |
+
response = requests.get(url)
|
| 16 |
+
|
| 17 |
+
if response.status_code != 200:
|
| 18 |
+
raise Exception(f"Failed to fetch result: {response.status_code}, {response.text}")
|
| 19 |
+
|
| 20 |
+
results = response.json()
|
| 21 |
+
res = list(results.values()) if results else []
|
| 22 |
+
|
| 23 |
+
if save_file:
|
| 24 |
+
with open(save_file, "w") as f:
|
| 25 |
+
for result in res:
|
| 26 |
+
f.write(json.dumps(result) + "\n")
|
| 27 |
+
|
| 28 |
+
return res
|
| 29 |
+
|
| 30 |
+
# --------------- Get all results for a list of models ----------------
|
| 31 |
+
|
| 32 |
+
def get_all_results(title, model_list):
|
| 33 |
+
return [{model: save_result(title, model, None)} for model in model_list]
|
| 34 |
+
|
| 35 |
+
# --------------- Save all results into local JSONL files ----------------
|
| 36 |
+
|
| 37 |
+
def save_all_results(titles, models, save_dir):
|
| 38 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 39 |
+
|
| 40 |
+
for title in titles:
|
| 41 |
+
for model in models:
|
| 42 |
+
filename = f"{safe_key(model)}-{title}.jsonl"
|
| 43 |
+
filepath = os.path.join(save_dir, filename)
|
| 44 |
+
save_result(title, model, filepath)
|
| 45 |
+
|
| 46 |
+
# --------------- Get filename for model ----------------
|
| 47 |
+
|
| 48 |
+
def get_filename(titles, models, save_dir):
|
| 49 |
+
for title in titles:
|
| 50 |
+
for model in models:
|
| 51 |
+
filename = f"{safe_key(model)}-{title}.jsonl"
|
| 52 |
+
return filename
|
| 53 |
+
|
| 54 |
+
import os
|
| 55 |
+
|
| 56 |
+
def find_incomplete_prompts(save_dir, n):
|
| 57 |
+
model_to_titles = {}
|
| 58 |
+
|
| 59 |
+
for filename in os.listdir(save_dir):
|
| 60 |
+
if not filename.endswith('.jsonl'):
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
file_base = filename[:-6] # remove '.jsonl'
|
| 64 |
+
|
| 65 |
+
q_idx = file_base.rfind('-Q')
|
| 66 |
+
if q_idx == -1:
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
model_name = file_base[:q_idx]
|
| 70 |
+
title = file_base[q_idx+1:]
|
| 71 |
+
|
| 72 |
+
filepath = os.path.join(save_dir, filename)
|
| 73 |
+
try:
|
| 74 |
+
with open(filepath, 'r') as f:
|
| 75 |
+
num_lines = sum(1 for _ in f)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Error reading {filepath}: {e}")
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
# Each line = one entry
|
| 81 |
+
if num_lines < n:
|
| 82 |
+
if model_name not in model_to_titles:
|
| 83 |
+
model_to_titles[model_name] = []
|
| 84 |
+
model_to_titles[model_name].append(title)
|
| 85 |
+
|
| 86 |
+
for model in model_to_titles:
|
| 87 |
+
model_to_titles[model].sort(key=lambda x: int(x[1:]))
|
| 88 |
+
|
| 89 |
+
return model_to_titles
|
| 90 |
+
|
| 91 |
+
def get_attempts_left(models, titles, save_dir, n):
|
| 92 |
+
attempts_left = []
|
| 93 |
+
|
| 94 |
+
for model in models:
|
| 95 |
+
model_attempts = []
|
| 96 |
+
for title in titles:
|
| 97 |
+
filename = f"{safe_key(model)}-{title}.jsonl"
|
| 98 |
+
filepath = os.path.join(save_dir, filename)
|
| 99 |
+
|
| 100 |
+
# Count number of attempts already saved
|
| 101 |
+
if os.path.exists(filepath):
|
| 102 |
+
try:
|
| 103 |
+
with open(filepath, 'r') as f:
|
| 104 |
+
num_lines = sum(1 for _ in f)
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Error reading {filepath}: {e}")
|
| 107 |
+
num_lines = 0
|
| 108 |
+
else:
|
| 109 |
+
num_lines = 0
|
| 110 |
+
|
| 111 |
+
# Compute how many more are needed
|
| 112 |
+
remaining = max(0, n - num_lines)
|
| 113 |
+
model_attempts.append(remaining)
|
| 114 |
+
|
| 115 |
+
attempts_left.append(model_attempts)
|
| 116 |
+
|
| 117 |
+
return attempts_left
|
| 118 |
+
|
| 119 |
+
#titles=["Q" + str(i) for i in range(1, 136)]
|
| 120 |
+
#models = ['gemini-2.5-pro-preview-03-25']
|
| 121 |
+
# save_all_results(titles, models, '/home/joseph_tennyson/physics/physics-benchmark/client/Responses_small')
|
| 122 |
+
# a = get_attempts_left(
|
| 123 |
+
# models=["gemini-2.5-pro-preview-03-25"],
|
| 124 |
+
# titles=["Q" + str(i) for i in range(1, 136)],
|
| 125 |
+
# save_dir= "/home/joseph_tennyson/physics/physics-benchmark/client/Responses_small",
|
| 126 |
+
# n=10
|
| 127 |
+
# )
|
| 128 |
+
# print(a)
|
client/call_server.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
BASE_URL = # your_URL_here
|
| 5 |
+
|
| 6 |
+
def run(models, prompts, titles, num_elems, num_repeats=1):
|
| 7 |
+
payload = {
|
| 8 |
+
"models": models,
|
| 9 |
+
"prompts": prompts,
|
| 10 |
+
"titles": titles,
|
| 11 |
+
"num_elems": num_elems,
|
| 12 |
+
"num_repeats": num_repeats
|
| 13 |
+
}
|
| 14 |
+
r = requests.post(f"{BASE_URL}/run", json=payload)
|
| 15 |
+
return r.json()
|
| 16 |
+
|
| 17 |
+
def run_with_code_interpreter(models, prompts, titles, num_elems, file_paths_per_question, num_repeats=1):
|
| 18 |
+
"""
|
| 19 |
+
Run queries with code interpreter enabled and file access.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
models: List of model names
|
| 23 |
+
prompts: List of prompts/questions
|
| 24 |
+
titles: List of question titles
|
| 25 |
+
num_elems: List of expected return lengths (0 for scalar, >0 for array)
|
| 26 |
+
file_paths_per_question: List of lists - each inner list contains file paths for that question
|
| 27 |
+
num_repeats: Number of repeats (int or nested list)
|
| 28 |
+
"""
|
| 29 |
+
payload = {
|
| 30 |
+
"models": models,
|
| 31 |
+
"prompts": prompts,
|
| 32 |
+
"titles": titles,
|
| 33 |
+
"num_elems": num_elems,
|
| 34 |
+
"file_paths_per_question": file_paths_per_question,
|
| 35 |
+
"num_repeats": num_repeats
|
| 36 |
+
}
|
| 37 |
+
r = requests.post(f"{BASE_URL}/run_with_code_interpreter", json=payload)
|
| 38 |
+
return r.json()
|
| 39 |
+
|
| 40 |
+
def status():
|
| 41 |
+
r = requests.get(f"{BASE_URL}/status")
|
| 42 |
+
return r.json()
|
| 43 |
+
|
| 44 |
+
def cancel():
|
| 45 |
+
r = requests.post(f"{BASE_URL}/cancel")
|
| 46 |
+
return r.json()
|
| 47 |
+
|
client/client_requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
requests
|
client/eval.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
client/init.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
readme.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# CME-SpectrumBench Arena
|
| 2 |
+
|
| 3 |
+
## Installation and API usage
|
| 4 |
+
|
| 5 |
+
Before using the client:
|
| 6 |
+
|
| 7 |
+
1. Install the required packages:
|
| 8 |
+
```bash
|
| 9 |
+
pip install -r client_requirements.txt
|
| 10 |
+
2. In `server/db.py`, write in your database URL after `databaseURL`. Likewise for the variable `DATABASE_URL` in `client/call_db.py`.
|
| 11 |
+
3. Ensure that the correct API site is reflected in the variable `BASE_URL` of `client/call_server.py`.
|
| 12 |
+
|
| 13 |
+
<br>
|
| 14 |
+
<br>
|
| 15 |
+
|
| 16 |
+
## General notes, and structure of code & data
|
| 17 |
+
|
| 18 |
+
- The benhcmark codebase has two domains: the scientific-facing side, and an API-facing side.
|
| 19 |
+
- Reflecting this dichotomy, the codebase is split into two main folders, `client` (which prepares benchmark questions, sends questions to the server, reads model responses, evaluates/plots the responses, and generates leaderboards), and `server` (which manages the database in which responses are kept, and how models are queried through APIs).
|
| 20 |
+
- The run script with the directory set to the `client` folder.
|
| 21 |
+
- Note the five scripts in the `client` folder. `call_server.py` and `call_db.py` queries the server and pulls results respectively (next two sections). `init.py` contains the bulk of ARPES-specific code and is understandable only by those well-versed in that subfield of condensed matter; contact the authors for more detail. Both `ask.py` and `eval.py` call upon `init.py` for their operation; `ask.py` asks the server questions and the database for responses, while `eval.py` scores these responses. These will be elaborated in later subsections.
|
| 22 |
+
- Let us now go through the various subfolders of the `client` folder. Note that `_small`, `_med`, and `_large` appended after folders refers to the resolution sizes of questions asked; these are folders for which all questions are posed to all models. `_single` refers to single questions posed to single models, while one parameters (noise or resolution) is varied. Note that the leaderboard ranks the scores of models to `small` resolution questions only, as this was the largest approxciamte set of resolutions that fit within the smallest context windows of all models tested (somewhere on the order 100k tokens once three-shot prompting has been taken into account).
|
| 23 |
+
- The folder `evaluation` contains summaries of all results (such as leaderboards) as well as results for individual models.
|
| 24 |
+
- The folder `Evaluation_plots` contains the plots of all results obtained by all models, where rhe numbering of questions runs from `Q1` to `Q190` covering all categories in order (see appendix E) with five noise/convolution levels per question. Its subfolder `Evaluation_single` collects the plots of individual questions, where `_r[...]` refers to the resolution of that dataset and `_n[...]` its noise level (in percent). Here, questions are labelled by category rather than question number.
|
| 25 |
+
- The folder `Evaluation_single` (not to be confused with the prior subfolder) contains plots for single questions with varying noise/resolution.
|
| 26 |
+
- `Plots` contain plots for all questions, listed under their respective categories, in both .txt (spectra) and .png (images of spectra) formats. Note the format of their names, which applies in general elsewhere. Examples given here will suffice. `A1a` refers to question A1, example a (for three-shot questions, examples a, b, c and are given with answers, and d as the actual question to be asked; for one-shot, only one example a is given, and b is the actual question). `_r25` refers to a resolution of 100 pixels along the horizontal axis, `_n20` to a noise level of 20%, `_k5_e3` to a momentum convolution of 0.005 inverse Angstrom and 0.003 electron-Volts (these are the same for all questions in our benchmark, but may in principle be varied). Lastly, note that questions in the D1 category have an additional parameter in their title referring to phononic coupling strength: `_l05`, `_l075`, `_l10`, `_l20`, and `_l50` refer to coupling strengths of 0.5, 0.75, 1.0, 2.0, and 5.0 respectively.
|
| 27 |
+
- The various folders beginning with `Prompts` contain the actual prompts given to the question (.txt files ending with `_Q`, for 'question') and their respective solutions (ending with `_S`, for 'solution'). The previous comments on resolution sizes (`_small`, `_medium`, `_large`) and single prompts (`_single`) applies.
|
| 28 |
+
- Likewise, folders beginning with `Responses` list the responses of various models to all questions.
|
| 29 |
+
- Users wishing to use apply the benchmark to new models may modify code in `client/ask.py` and `client/eval.py`. However, those wishing to extend existing benchmark questions to other domains of physics or science will have to modify `client/init.py`; for this purpose it is best to contact the authors for more complete documentation and an elaboration of their philosophy.
|
| 30 |
+
|
| 31 |
+
<br>
|
| 32 |
+
<br>
|
| 33 |
+
|
| 34 |
+
## Backend Server Setup
|
| 35 |
+
|
| 36 |
+
### Database Setup (Firebase)
|
| 37 |
+
|
| 38 |
+
1. Create a Firebase project and enable Firestore
|
| 39 |
+
2. Download your Firebase service account admin file and save it to `db_admin.json` in the `server/` directory
|
| 40 |
+
|
| 41 |
+
### Server Setup
|
| 42 |
+
|
| 43 |
+
1. Install server dependencies:
|
| 44 |
+
```bash
|
| 45 |
+
cd server
|
| 46 |
+
pip install -r ../serve_requirements.txt
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
2. Set up ngrok for external access:
|
| 50 |
+
```bash
|
| 51 |
+
chmod +x setup_ngrok.sh
|
| 52 |
+
./setup_ngrok.sh
|
| 53 |
+
ngrok config add-authtoken YOUR_AUTHTOKEN_HERE
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
3. Start the server:
|
| 57 |
+
```bash
|
| 58 |
+
uvicorn main:app --host 0.0.0.0 --port 8000
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
4. In a separate terminal, start ngrok:
|
| 62 |
+
```bash
|
| 63 |
+
ngrok http 8000
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
### Environment Variables
|
| 67 |
+
|
| 68 |
+
Create a `.env` file in the root directory with the following variables:
|
| 69 |
+
|
| 70 |
+
#### Example API Keys
|
| 71 |
+
```bash
|
| 72 |
+
# OpenAI API (for GPT models, o1 models)
|
| 73 |
+
OPENAI_API_KEY=your_openai_api_key_here
|
| 74 |
+
|
| 75 |
+
# Anthropic API (for Claude models)
|
| 76 |
+
ANTHROPIC_API_KEY=your_anthropic_api_key_here
|
| 77 |
+
|
| 78 |
+
# Google AI API (for Gemini models)
|
| 79 |
+
GOOGLE_API_KEY=your_google_api_key_here
|
| 80 |
+
|
| 81 |
+
# DeepSeek API (for DeepSeek models)
|
| 82 |
+
DEEPSEEK_API_KEY=your_deepseek_api_key_here
|
| 83 |
+
|
| 84 |
+
# xAI API (for Grok models)
|
| 85 |
+
XAI_API_KEY=your_xai_api_key_here
|
| 86 |
+
|
| 87 |
+
# OpenRouter API (for various models)
|
| 88 |
+
OPENROUTER_API_KEY=your_openrouter_api_key_here
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
#### Required URLs
|
| 92 |
+
```bash
|
| 93 |
+
# Firebase Database URL
|
| 94 |
+
FIREBASE_DB_URL=https://your-project-id.firebaseio.com
|
| 95 |
+
|
| 96 |
+
# Server Base URL (your ngrok URL)
|
| 97 |
+
BASE_URL=https://your-ngrok-url.ngrok.io
|
| 98 |
+
```
|
| 99 |
+
<br>
|
| 100 |
+
<br>
|
| 101 |
+
|
| 102 |
+
## Querying the Server (`client/call_server.py`)
|
| 103 |
+
|
| 104 |
+
### `run(models, prompts, titles, num_elems, num_repeats=1)`
|
| 105 |
+
Starts a new experiment by sending prompts to the server. Only one job can run at once; additional attempts are blocked.
|
| 106 |
+
|
| 107 |
+
- `models`: list of model names to query
|
| 108 |
+
- `prompts`: list of prompts
|
| 109 |
+
- `titles`: list of unique titles for each prompt
|
| 110 |
+
- `num_elems`: list of values to extract per prompt (0 for a scalar, 1+ for an array with the specified length)
|
| 111 |
+
- `num_repeats`: how many times to repeat each query (default = 1, meaning it isn't repeated, only done once)
|
| 112 |
+
- NOTE: can also pass in a 2D array from calling `client/call_db.py` get_attempts_left so that each model and prompt
|
| 113 |
+
pair is repeated until a certain amount of attempts are completed
|
| 114 |
+
|
| 115 |
+
For the ith prompt in prompts, we expect it to have a title of titles[i] and to have an array length of `num_elems[i]` (or to be a scalar if `num_elems[i] == 0`)
|
| 116 |
+
|
| 117 |
+
### `status()`
|
| 118 |
+
Returns the current server status, including whether a job is running and progress info.
|
| 119 |
+
|
| 120 |
+
### `cancel()`
|
| 121 |
+
Sends a request to cancel the currently running job.
|
| 122 |
+
|
| 123 |
+
<br>
|
| 124 |
+
<br>
|
| 125 |
+
|
| 126 |
+
## Pulling Results (`client/call_db.py`)
|
| 127 |
+
|
| 128 |
+
### `save_result(title, model, save_file=None)`
|
| 129 |
+
Fetches all results for a given title and model.
|
| 130 |
+
If `save_file` is provided, saves the results to a `.jsonl` file, with one line per attempt.
|
| 131 |
+
|
| 132 |
+
### `get_all_results(title, model_list)`
|
| 133 |
+
Fetches all results for a given title across multiple models.
|
| 134 |
+
Returns a list of dictionaries, each containing model results.
|
| 135 |
+
|
| 136 |
+
### `save_all_results(titles, models, save_dir)`
|
| 137 |
+
Saves results for **every** `(title, model)` pair into separate `.jsonl` files inside the specified `save_dir`.
|
| 138 |
+
Each file is named as `title-model.jsonl`.
|
| 139 |
+
|
| 140 |
+
### `find_incomplete_prompts(find_incomplete_prompts(save_dir, n)`
|
| 141 |
+
Takes in a save_dir where the output files reside. For every model, find prompts in which the number of recorded entries is less than a specified threshold `n`.
|
| 142 |
+
|
| 143 |
+
### `get_attempts_left(models, titles, save_dir, n):`
|
| 144 |
+
Get the number of attempts left to get n total attempts for each title. Looks within save_directory. Outputs a 2d array where
|
| 145 |
+
output[`i`][`j`] is how many attempts model i has on title j to reach the target number.
|
| 146 |
+
|
| 147 |
+
<br>
|
| 148 |
+
<br>
|
| 149 |
+
|
| 150 |
+
## Asking questions (`client/ask.py`)
|
| 151 |
+
|
| 152 |
+
Skipping past numerous subsidiary functions, these are the ones that matter and the user is expected to call.
|
| 153 |
+
|
| 154 |
+
### `ask_batch(size, retrieve, attempts)`
|
| 155 |
+
|
| 156 |
+
- Asks all questions to all models listed.
|
| 157 |
+
- Models to be called should be listed in the line `models = [...]`.
|
| 158 |
+
- `size` indicates the resolution of the questions given, where 1 refers to small, 2 medium, and 3 large. Recall that the standard leaderboard reflects answers to small resolution questions only (`size=1`).
|
| 159 |
+
- `retrieve` toggles whether previously generated spectra (saved under `Prompts_[...]`) are to be reused for the current set of questions; this frees the program from having to generate all 190 questions again from scratch (which may take several minutes), which is especially useful for debugging. Ça va sans dire that these questions should have been generated beforehand. `retrieve=0` to generate questions from scratch, and `retrieve=1` to reuse previously generated questions.
|
| 160 |
+
- `attempts` indicates the number of times each question to be posed to each model; that is, the sample size. Note that we can 'top up' responses such that if `n` responses have already been collected for some question where `attempts = m > n`, then only `m-n` more responses will be requested for that question. This requires the existing responses to be loaded into the appropriate `client/Responses_[...]` folder using the `save_batch()` function (next point).
|
| 161 |
+
|
| 162 |
+
### `save_batch(size, attempts)`
|
| 163 |
+
|
| 164 |
+
- Saves responses for the listed models (under `model=[...]`) from the database.
|
| 165 |
+
- `size` and `attempts` are the same parameters as before.
|
| 166 |
+
|
| 167 |
+
### `ask_resolution_iter(question_category, resolution_array, noise_ratio, attempts)` and `ask_noise_iter(question_category, resolution, noise_ratio_array, attempts)`
|
| 168 |
+
|
| 169 |
+
- Asks a single question (listed under the text variable `question_category`) for models listed in `models=[...]` for a total number of `attempts` stated.
|
| 170 |
+
- Available text strings for `question_category` are listed within the previous function in the script, `get_single_prompt`.
|
| 171 |
+
- Resolution sizes should be either given as an array (`resolution_array` in the first function) or fixed (`resolution` in the second function). Resolution is defined as the number of pixels along the horizontal axis of spectra.
|
| 172 |
+
- Noise level (as a fraction of the maximum signal intensity) should be fixed (`noise_ratio` in the first function) or given as an array (`noise_ratio_array` in the second).
|
| 173 |
+
|
| 174 |
+
### `save_resolution_iter(question_category, resolution_array, noise_ratio, attempts)` and `save_noise_iter(question_category, resolution, noise_ratio_array, attempts)`
|
| 175 |
+
|
| 176 |
+
- These save responses to questions asked by the previous two functions.
|
| 177 |
+
- Their input variables are equivalent to the ones described above.
|
| 178 |
+
|
| 179 |
+
<br>
|
| 180 |
+
<br>
|
| 181 |
+
|
| 182 |
+
## Evaluating responses and ranking models (`client/eval.py`)
|
| 183 |
+
|
| 184 |
+
Skipping past numerous subsidiary functions, these are the ones that matter and the user is expected to call.
|
| 185 |
+
|
| 186 |
+
### `score_models()`
|
| 187 |
+
|
| 188 |
+
- After testing all 190 questions a list of models in `client/ask.py` using the function `ask_batch()` and saving them from the databased using the function `save_batch()`, this function scores all models in the list `model_list_1` with accompanying names in `model_name_1`.
|
| 189 |
+
- Responses are read off from the relevant `Responses_[...]` folder (in this case, only low resolution questions were scored).
|
| 190 |
+
- Leaderbaords and detailed breakdowns are generated and saved under the relevant folders.
|
| 191 |
+
|
| 192 |
+
### `plot_models()`
|
| 193 |
+
|
| 194 |
+
- Similar function to the above, calls a set of `plot_model_responses(model_name, size)` for various models stated in `model_name` of resolution sizes listed (`size = 1,2,3` for small, medium, and large).
|
| 195 |
+
- Responses are read off from the relevant `Responses_[...]` folder
|
| 196 |
+
|
| 197 |
+
Note that the following functions were written to score a small number of question categories over a range of resolutions/frequencies; the user may extend these functions at will.
|
| 198 |
+
|
| 199 |
+
### `score_A1_B1_single_resolution_iter(model, resolution_array_A1, resolution_array_B1, noise_ratio)`
|
| 200 |
+
|
| 201 |
+
- For a given `model`, plots how scores for question categories A1 and B1 vary with resolution (stated in the two respective resolution arrays), for a fixed `noise_ratio`.
|
| 202 |
+
|
| 203 |
+
### `score_four_single_noise_iter(model, resolution, noise_array)`
|
| 204 |
+
|
| 205 |
+
- For a given `model` and `resolution`, plots how scores for A1, B1, B2, and D1 (with coupling strength 1) vary with noise (stated in `noise_array`).
|
| 206 |
+
|
| 207 |
+
### `plot_noise_responses(model, resolution, noise_array, question_category)`
|
| 208 |
+
|
| 209 |
+
- Analogous to `plot_models()` but applies to noise-dependent questions given by the given function.
|
| 210 |
+
- Input variables are self-explanatory.
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
|
serve_requirements.txt
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==24.1.0
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anthropic==0.50.0
|
| 4 |
+
anyio==4.9.0
|
| 5 |
+
asttokens==3.0.0
|
| 6 |
+
CacheControl==0.14.2
|
| 7 |
+
cachetools==5.5.2
|
| 8 |
+
certifi==2025.1.31
|
| 9 |
+
cffi==1.17.1
|
| 10 |
+
charset-normalizer==3.4.1
|
| 11 |
+
click==8.1.8
|
| 12 |
+
comm==0.2.2
|
| 13 |
+
contourpy==1.3.2
|
| 14 |
+
cryptography==44.0.2
|
| 15 |
+
cycler==0.12.1
|
| 16 |
+
decorator==5.2.1
|
| 17 |
+
distro==1.9.0
|
| 18 |
+
executing==2.2.0
|
| 19 |
+
fastapi==0.115.12
|
| 20 |
+
firebase-admin==6.8.0
|
| 21 |
+
fonttools==4.57.0
|
| 22 |
+
gcloud==0.18.3
|
| 23 |
+
google-ai-generativelanguage==0.6.15
|
| 24 |
+
google-api-core==2.24.2
|
| 25 |
+
google-api-python-client==2.168.0
|
| 26 |
+
google-auth==2.39.0
|
| 27 |
+
google-auth-httplib2==0.2.0
|
| 28 |
+
google-cloud-core==2.4.3
|
| 29 |
+
google-cloud-firestore==2.20.2
|
| 30 |
+
google-cloud-storage==3.1.0
|
| 31 |
+
google-crc32c==1.7.1
|
| 32 |
+
google-genai==1.12.1
|
| 33 |
+
google-generativeai==0.8.5
|
| 34 |
+
google-resumable-media==2.7.2
|
| 35 |
+
googleapis-common-protos==1.70.0
|
| 36 |
+
grpcio==1.71.0
|
| 37 |
+
grpcio-status==1.71.0
|
| 38 |
+
h11==0.16.0
|
| 39 |
+
httpcore==1.0.9
|
| 40 |
+
httplib2==0.22.0
|
| 41 |
+
httpx==0.28.1
|
| 42 |
+
idna==3.10
|
| 43 |
+
ipython==9.2.0
|
| 44 |
+
ipython_pygments_lexers==1.1.1
|
| 45 |
+
ipywidgets==8.1.6
|
| 46 |
+
jedi==0.19.2
|
| 47 |
+
jiter==0.9.0
|
| 48 |
+
jupyterlab_widgets==3.0.14
|
| 49 |
+
jws==0.1.3
|
| 50 |
+
kiwisolver==1.4.8
|
| 51 |
+
matplotlib==3.10.1
|
| 52 |
+
matplotlib-inline==0.1.7
|
| 53 |
+
msgpack==1.1.0
|
| 54 |
+
numpy==2.2.5
|
| 55 |
+
oauth2client==4.1.3
|
| 56 |
+
openai==1.76.0
|
| 57 |
+
packaging==25.0
|
| 58 |
+
pandas==2.2.3
|
| 59 |
+
parso==0.8.4
|
| 60 |
+
pexpect==4.9.0
|
| 61 |
+
pillow==11.2.1
|
| 62 |
+
prompt_toolkit==3.0.51
|
| 63 |
+
proto-plus==1.26.1
|
| 64 |
+
protobuf==5.29.4
|
| 65 |
+
ptyprocess==0.7.0
|
| 66 |
+
pure_eval==0.2.3
|
| 67 |
+
pyasn1==0.6.1
|
| 68 |
+
pyasn1_modules==0.4.2
|
| 69 |
+
pycparser==2.22
|
| 70 |
+
pycryptodome==3.22.0
|
| 71 |
+
pydantic==2.11.3
|
| 72 |
+
pydantic_core==2.33.1
|
| 73 |
+
Pygments==2.19.1
|
| 74 |
+
PyJWT==2.10.1
|
| 75 |
+
pyparsing==3.2.3
|
| 76 |
+
Pyrebase4==4.8.0
|
| 77 |
+
python-dateutil==2.9.0.post0
|
| 78 |
+
python-dotenv==1.1.0
|
| 79 |
+
python-jwt==2.0.1
|
| 80 |
+
pytz==2025.2
|
| 81 |
+
requests==2.32.3
|
| 82 |
+
requests-toolbelt==0.10.1
|
| 83 |
+
rsa==4.9.1
|
| 84 |
+
six==1.17.0
|
| 85 |
+
sniffio==1.3.1
|
| 86 |
+
stack-data==0.6.3
|
| 87 |
+
starlette==0.46.2
|
| 88 |
+
tqdm==4.67.1
|
| 89 |
+
traitlets==5.14.3
|
| 90 |
+
typing-inspection==0.4.0
|
| 91 |
+
typing_extensions==4.13.2
|
| 92 |
+
tzdata==2025.2
|
| 93 |
+
uritemplate==4.1.1
|
| 94 |
+
urllib3==1.26.20
|
| 95 |
+
uvicorn==0.34.2
|
| 96 |
+
wcwidth==0.2.13
|
| 97 |
+
websockets==15.0.1
|
| 98 |
+
widgetsnbextension==4.0.14
|
server/db.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import firebase_admin
|
| 2 |
+
from firebase_admin import credentials, db
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
cred = credentials.Certificate("db_admin.json")
|
| 7 |
+
firebase_admin.initialize_app(cred, {
|
| 8 |
+
'databaseURL': # your_URL_here
|
| 9 |
+
})
|
| 10 |
+
|
| 11 |
+
def safe_key(text):
|
| 12 |
+
return text.replace('.', '_')
|
| 13 |
+
|
| 14 |
+
def append_result(title, model, result):
|
| 15 |
+
key = f"{title}-{safe_key(model)}"
|
| 16 |
+
data = result
|
| 17 |
+
ref = db.reference(f"results/{key}")
|
| 18 |
+
ref.push(data)
|
| 19 |
+
|
| 20 |
+
# save with jsonl format
|
| 21 |
+
def save_result(title, model, save_file=None):
|
| 22 |
+
key = f"{title}-{safe_key(model)}"
|
| 23 |
+
ref = db.reference(f"results/{key}")
|
| 24 |
+
results = ref.get()
|
| 25 |
+
|
| 26 |
+
res = list(results.values()) if results else []
|
| 27 |
+
if save_file:
|
| 28 |
+
with open("results.jsonl", "w") as f:
|
| 29 |
+
for result in res:
|
| 30 |
+
f.write(json.dumps(result) + "\n")
|
| 31 |
+
|
| 32 |
+
return res
|
| 33 |
+
|
| 34 |
+
def get_all_results(title, model_list):
|
| 35 |
+
return [{model: save_result(title, model, None)} for model in model_list]
|
| 36 |
+
|
| 37 |
+
def save_all_results(titles, models, save_dir):
|
| 38 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 39 |
+
|
| 40 |
+
for title in titles:
|
| 41 |
+
for model in models:
|
| 42 |
+
filename = f"{title}-{safe_key(model)}.jsonl"
|
| 43 |
+
filepath = os.path.join(save_dir, filename)
|
| 44 |
+
save_result(title, model, filepath)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def reset_db():
|
| 49 |
+
ref = db.reference("results")
|
| 50 |
+
ref.delete()
|
| 51 |
+
|
| 52 |
+
def delete_model_entries(model):
|
| 53 |
+
ref = db.reference("results")
|
| 54 |
+
all_entries = ref.get()
|
| 55 |
+
|
| 56 |
+
if not all_entries:
|
| 57 |
+
print("No entries found.")
|
| 58 |
+
return
|
| 59 |
+
|
| 60 |
+
target_suffix = safe_key(model)
|
| 61 |
+
keys_to_delete = []
|
| 62 |
+
|
| 63 |
+
for key in all_entries.keys():
|
| 64 |
+
if key.endswith(f"-{target_suffix}"):
|
| 65 |
+
keys_to_delete.append(key)
|
| 66 |
+
|
| 67 |
+
for key in keys_to_delete:
|
| 68 |
+
print(f"Deleting {key}")
|
| 69 |
+
db.reference(f"results/{key}").delete()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
server/main.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List, Optional, Union
|
| 4 |
+
import asyncio
|
| 5 |
+
|
| 6 |
+
from serve import run_model_prompt_queries # your async query runner
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Shared state
|
| 11 |
+
status = {
|
| 12 |
+
"is_running": False,
|
| 13 |
+
"done": 0,
|
| 14 |
+
"total": 0,
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
status_lock = asyncio.Lock()
|
| 18 |
+
lock = asyncio.Lock()
|
| 19 |
+
running_task: Optional[asyncio.Task] = None # Reference to the active task
|
| 20 |
+
|
| 21 |
+
class QueryRequest(BaseModel):
|
| 22 |
+
models: List[str]
|
| 23 |
+
prompts: List[str]
|
| 24 |
+
titles: Optional[List[str]]
|
| 25 |
+
num_elems: List[int]
|
| 26 |
+
num_repeats: Union[int, List[List[int]]]
|
| 27 |
+
|
| 28 |
+
@app.post("/run")
|
| 29 |
+
async def run_queries(req: QueryRequest):
|
| 30 |
+
global running_task
|
| 31 |
+
|
| 32 |
+
if lock.locked():
|
| 33 |
+
raise HTTPException(status_code=400, detail="Server is already processing a request.")
|
| 34 |
+
|
| 35 |
+
async def run_and_track():
|
| 36 |
+
async with lock:
|
| 37 |
+
status["is_running"] = True
|
| 38 |
+
status["done"] = 0
|
| 39 |
+
status["total"] = len(req.models) * len(req.prompts) * req.num_repeats
|
| 40 |
+
|
| 41 |
+
async def progress_callback():
|
| 42 |
+
async with status_lock:
|
| 43 |
+
status["done"] += 1
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
await run_model_prompt_queries(**req.model_dump(), progress_cb=progress_callback)
|
| 47 |
+
except asyncio.CancelledError:
|
| 48 |
+
print("Task was cancelled.")
|
| 49 |
+
raise
|
| 50 |
+
finally:
|
| 51 |
+
status["is_running"] = False
|
| 52 |
+
|
| 53 |
+
# Launch task and store reference
|
| 54 |
+
running_task = asyncio.create_task(run_and_track())
|
| 55 |
+
return {"status": "started"}
|
| 56 |
+
|
| 57 |
+
@app.post("/cancel")
|
| 58 |
+
async def cancel_query():
|
| 59 |
+
global running_task
|
| 60 |
+
|
| 61 |
+
if running_task is None or running_task.done():
|
| 62 |
+
return {"status": "no task running"}
|
| 63 |
+
|
| 64 |
+
running_task.cancel()
|
| 65 |
+
try:
|
| 66 |
+
await running_task
|
| 67 |
+
except asyncio.CancelledError:
|
| 68 |
+
pass
|
| 69 |
+
|
| 70 |
+
return {"status": "cancelled"}
|
| 71 |
+
|
| 72 |
+
@app.get("/status")
|
| 73 |
+
async def get_status():
|
| 74 |
+
return status
|
server/query.py
ADDED
|
@@ -0,0 +1,1311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
import time
|
| 5 |
+
import asyncio
|
| 6 |
+
import httpx
|
| 7 |
+
from typing import Any, Dict
|
| 8 |
+
import datetime
|
| 9 |
+
from openai import AsyncOpenAI
|
| 10 |
+
import math
|
| 11 |
+
import aiofiles
|
| 12 |
+
|
| 13 |
+
from google import genai
|
| 14 |
+
from google.genai import types
|
| 15 |
+
import hashlib
|
| 16 |
+
from datetime import datetime, timezone, timedelta
|
| 17 |
+
|
| 18 |
+
from dotenv import load_dotenv
|
| 19 |
+
# Load .env from server/ so it works whether you run from project root or server/
|
| 20 |
+
_load_env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
|
| 21 |
+
load_dotenv(dotenv_path=_load_env_path)
|
| 22 |
+
|
| 23 |
+
from aiolimiter import AsyncLimiter
|
| 24 |
+
|
| 25 |
+
# Add rate limiters
|
| 26 |
+
openai_semaphore = asyncio.Semaphore(25)
|
| 27 |
+
openai_limiter = AsyncLimiter(10, time_period=60)
|
| 28 |
+
|
| 29 |
+
qwen_semaphore = asyncio.Semaphore(25)
|
| 30 |
+
qwen_limiter = AsyncLimiter(10, time_period=60)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
gemini_flash_semaphore = asyncio.Semaphore(15)
|
| 34 |
+
gemini_flash_limiter = AsyncLimiter(15, time_period=60)
|
| 35 |
+
|
| 36 |
+
gemini_pro_semaphore = asyncio.Semaphore(15)
|
| 37 |
+
gemini_pro_limiter = AsyncLimiter(5, time_period=60)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
claude_semaphore = asyncio.Semaphore(20)
|
| 42 |
+
claude_limiter = AsyncLimiter(10, time_period=60)
|
| 43 |
+
|
| 44 |
+
deepseek_semaphore = asyncio.Semaphore(2)
|
| 45 |
+
deepseek_limiter = AsyncLimiter(1, time_period=60)
|
| 46 |
+
|
| 47 |
+
TIMEOUT = 43200# 4 hours in seconds
|
| 48 |
+
NUM_RETRIES = 2
|
| 49 |
+
_REGISTRY: Dict[str, Any] = {}
|
| 50 |
+
_CODE_EXECUTION_REGISTRY: Dict[str, Any] = {}
|
| 51 |
+
|
| 52 |
+
def register_model(model: str):
|
| 53 |
+
def decorator(fn):
|
| 54 |
+
_REGISTRY[model] = fn
|
| 55 |
+
return fn
|
| 56 |
+
return decorator
|
| 57 |
+
|
| 58 |
+
def register_code_execution(model: str):
|
| 59 |
+
def decorator(fn):
|
| 60 |
+
_CODE_EXECUTION_REGISTRY[model] = fn
|
| 61 |
+
return fn
|
| 62 |
+
return decorator
|
| 63 |
+
|
| 64 |
+
# ------------------------ Extract value ------------------------
|
| 65 |
+
|
| 66 |
+
def parse_answer_reasoning_tags(text: str) -> tuple[str, str]:
|
| 67 |
+
"""
|
| 68 |
+
Parse <answer> and <reasoning> tags from text if they exist.
|
| 69 |
+
Returns (answer_text, reasoning_text).
|
| 70 |
+
If tags don't exist, returns (full_text, empty_string).
|
| 71 |
+
"""
|
| 72 |
+
answer_match = re.search(r'<answer>(.*?)</answer>', text, re.DOTALL | re.IGNORECASE)
|
| 73 |
+
reasoning_match = re.search(r'<reasoning>(.*?)</reasoning>', text, re.DOTALL | re.IGNORECASE)
|
| 74 |
+
|
| 75 |
+
if answer_match:
|
| 76 |
+
answer = answer_match.group(1).strip()
|
| 77 |
+
reasoning = reasoning_match.group(1).strip() if reasoning_match else ""
|
| 78 |
+
return answer, reasoning
|
| 79 |
+
else:
|
| 80 |
+
# No tags found, return full text as answer and empty reasoning
|
| 81 |
+
return text, ""
|
| 82 |
+
|
| 83 |
+
def extract_value(text: str, length: int):
|
| 84 |
+
if text is None:
|
| 85 |
+
raise ValueError("Output is None")
|
| 86 |
+
if not text.strip():
|
| 87 |
+
raise ValueError("Empty output detected")
|
| 88 |
+
|
| 89 |
+
# First, try to parse answer/reasoning tags
|
| 90 |
+
answer_text, reasoning_text = parse_answer_reasoning_tags(text)
|
| 91 |
+
|
| 92 |
+
# Use the answer_text for extraction, but we'll return a dict with reasoning
|
| 93 |
+
text_to_parse = answer_text
|
| 94 |
+
|
| 95 |
+
if length > 0:
|
| 96 |
+
# Match a list of values inside brackets
|
| 97 |
+
match = re.search(r'\[(.*?)\]', text_to_parse, re.DOTALL)
|
| 98 |
+
if not match:
|
| 99 |
+
raise ValueError(f"No bracketed list found in output, value is {text_to_parse[:50]}")
|
| 100 |
+
|
| 101 |
+
raw_items = match.group(1).split(',')
|
| 102 |
+
result = []
|
| 103 |
+
for x in raw_items:
|
| 104 |
+
x = x.strip()
|
| 105 |
+
try:
|
| 106 |
+
val = float(x)
|
| 107 |
+
if math.isnan(val) or math.isinf(val):
|
| 108 |
+
result.append(None)
|
| 109 |
+
else:
|
| 110 |
+
result.append(val)
|
| 111 |
+
except ValueError:
|
| 112 |
+
result.append(None)
|
| 113 |
+
|
| 114 |
+
return {"answer": result, "reasoning": reasoning_text}
|
| 115 |
+
else:
|
| 116 |
+
match = re.search(r'-?\d+(?:\.\d+)?', text_to_parse)
|
| 117 |
+
if not match:
|
| 118 |
+
raise ValueError(f"No numeric value found in output, {text_to_parse}")
|
| 119 |
+
val = float(match.group())
|
| 120 |
+
answer = None if math.isnan(val) or math.isinf(val) else val
|
| 121 |
+
return {"answer": answer, "reasoning": reasoning_text}
|
| 122 |
+
|
| 123 |
+
def check_type(output, length):
|
| 124 |
+
# Handle new dict format with answer and reasoning
|
| 125 |
+
if isinstance(output, dict) and "answer" in output:
|
| 126 |
+
answer = output["answer"]
|
| 127 |
+
cond = True
|
| 128 |
+
if length == 0:
|
| 129 |
+
cond = False
|
| 130 |
+
return isinstance(answer, list) is cond
|
| 131 |
+
|
| 132 |
+
# Legacy format (backward compatibility)
|
| 133 |
+
cond = True
|
| 134 |
+
if length == 0:
|
| 135 |
+
cond = False
|
| 136 |
+
return isinstance(output, list) is cond
|
| 137 |
+
|
| 138 |
+
def _provider_details(model: str):
|
| 139 |
+
"""
|
| 140 |
+
Pick the right endpoint + API-key env-var based on model prefix.
|
| 141 |
+
DeepSeek is OpenAI-compatible, so same format — just different base URL and key.
|
| 142 |
+
"""
|
| 143 |
+
if model.startswith("deepseek"):
|
| 144 |
+
return "https://api.deepseek.com/v1/chat/completions", "OPENAI_API_KEY"
|
| 145 |
+
if model.startswith("grok"): # xAI / Grok models
|
| 146 |
+
return "https://api.x.ai/v1/chat/completions", "XAI_API_KEY"
|
| 147 |
+
return "https://api.openai.com/v1/chat/completions", "OPENAI_API_KEY"
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
# ------------------------ OpenAI (Tool-calling) ------------------------
|
| 151 |
+
|
| 152 |
+
async def _openai_with_tool(model: str, prompt: str, length=0, **kwargs) -> Any:
|
| 153 |
+
async with openai_semaphore:
|
| 154 |
+
async with openai_limiter:
|
| 155 |
+
tools = [{
|
| 156 |
+
"type": "function",
|
| 157 |
+
"function": {
|
| 158 |
+
"name": "return_array",
|
| 159 |
+
"description": f"Return an array of exactly {max(length, 1)} decimal numbers.",
|
| 160 |
+
"parameters": {
|
| 161 |
+
"type": "object",
|
| 162 |
+
"properties": {
|
| 163 |
+
"numbers": {
|
| 164 |
+
"type": "array",
|
| 165 |
+
"items": {"type": "number"},
|
| 166 |
+
"minItems": max(length, 1),
|
| 167 |
+
"maxItems": max(length, 1)
|
| 168 |
+
}
|
| 169 |
+
},
|
| 170 |
+
"required": ["numbers"]
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
}]
|
| 174 |
+
reasoning = model.split("-")[-1]
|
| 175 |
+
model_base = model[: -len(reasoning) - 1] if reasoning in ["low", "medium", "high"] else model
|
| 176 |
+
payload = {
|
| 177 |
+
"model": model_base,
|
| 178 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 179 |
+
"tools": tools,
|
| 180 |
+
"tool_choice": "auto"
|
| 181 |
+
}
|
| 182 |
+
if reasoning in ["low", "medium", "high"]:
|
| 183 |
+
payload["reasoning_effort"] = reasoning
|
| 184 |
+
|
| 185 |
+
url, key_env = _provider_details(model)
|
| 186 |
+
|
| 187 |
+
headers = {
|
| 188 |
+
"Authorization": f"Bearer {os.getenv(key_env)}",
|
| 189 |
+
"Content-Type": "application/json"
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
|
| 193 |
+
s = time.time()
|
| 194 |
+
|
| 195 |
+
resp = await client.post(
|
| 196 |
+
url,
|
| 197 |
+
headers=headers,
|
| 198 |
+
json=payload
|
| 199 |
+
)
|
| 200 |
+
resp.raise_for_status()
|
| 201 |
+
data = resp.json()
|
| 202 |
+
args_str = data["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"]
|
| 203 |
+
res = json.loads(args_str)['numbers']
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
return res if length != 0 else res[0]
|
| 207 |
+
|
| 208 |
+
# ------------------------ OpenAI basic + DeepSeek (dynamic) ------------------------
|
| 209 |
+
@register_model("o3-pro")
|
| 210 |
+
@register_model("gpt-oss-20b")
|
| 211 |
+
@register_model("gpt-oss-120b")
|
| 212 |
+
@register_model("o1-mini")
|
| 213 |
+
@register_model("o1-low")
|
| 214 |
+
@register_model("o1-medium")
|
| 215 |
+
@register_model("o1-high")
|
| 216 |
+
@register_model("o3-low")
|
| 217 |
+
@register_model("o3-medium")
|
| 218 |
+
@register_model("o3-high")
|
| 219 |
+
@register_model("o3-mini-low")
|
| 220 |
+
@register_model("o3-mini-medium")
|
| 221 |
+
@register_model("o3-mini-high")
|
| 222 |
+
@register_model("o4-mini-low")
|
| 223 |
+
@register_model("o4-mini-medium")
|
| 224 |
+
@register_model("o4-mini-high")
|
| 225 |
+
@register_model("gpt-5.4")
|
| 226 |
+
@register_model("gpt-5")
|
| 227 |
+
async def _openai_basic(model: str, prompt: str, length: int = 0, **kwargs) -> Any:
|
| 228 |
+
async with openai_semaphore:
|
| 229 |
+
async with openai_limiter:
|
| 230 |
+
# ── 1. Derive base-model and reasoning effort ──────────────────────────
|
| 231 |
+
reasoning = model.split("-")[-1]
|
| 232 |
+
model_base = (
|
| 233 |
+
model[: -len(reasoning) - 1] if reasoning in {"low", "medium", "high"} else model
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
payload = {
|
| 237 |
+
"model": model_base,
|
| 238 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 239 |
+
}
|
| 240 |
+
if reasoning in {"low", "medium", "high"}:
|
| 241 |
+
payload["reasoning_effort"] = reasoning
|
| 242 |
+
|
| 243 |
+
# ── 2. Resolve provider URL & key dynamically ──────────────────────────
|
| 244 |
+
url, key_env = _provider_details(model)
|
| 245 |
+
headers = {
|
| 246 |
+
"Authorization": f"Bearer {os.getenv(key_env)}",
|
| 247 |
+
"Content-Type": "application/json",
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
# ── 3. Fire request and parse response ─────────────────────────────────
|
| 251 |
+
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
|
| 252 |
+
resp = await client.post(url, headers=headers, json=payload)
|
| 253 |
+
resp.raise_for_status()
|
| 254 |
+
text = resp.json()["choices"][0]["message"]["content"]
|
| 255 |
+
|
| 256 |
+
return extract_value(text, length)
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
# ------------------------ DeepSeek V4 (OpenAI-compatible, own semaphore/limiter) ------------------------
|
| 260 |
+
# Same logic as _openai_basic — DeepSeek uses the OpenAI-compatible format.
|
| 261 |
+
# _provider_details routes deepseek-* to api.deepseek.com with OPENAI_API_KEY.
|
| 262 |
+
# @register_model("deepseek-v4-pro") # temporarily using call_openrouter below
|
| 263 |
+
async def _deepseek_v4_basic(model: str, prompt: str, length: int = 0, **kwargs) -> Any:
|
| 264 |
+
async with deepseek_semaphore:
|
| 265 |
+
async with deepseek_limiter:
|
| 266 |
+
url, key_env = _provider_details(model)
|
| 267 |
+
headers = {
|
| 268 |
+
"Authorization": f"Bearer {os.getenv(key_env)}",
|
| 269 |
+
"Content-Type": "application/json",
|
| 270 |
+
}
|
| 271 |
+
payload = {
|
| 272 |
+
"model": model,
|
| 273 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 274 |
+
}
|
| 275 |
+
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
|
| 276 |
+
resp = await client.post(url, headers=headers, json=payload)
|
| 277 |
+
resp.raise_for_status()
|
| 278 |
+
text = resp.json()["choices"][0]["message"]["content"]
|
| 279 |
+
return extract_value(text, length)
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
@register_model("deepseek-v4-pro") # temporary: testing via openrouter
|
| 283 |
+
@register_model("deepseek-reasoner")
|
| 284 |
+
@register_model("kimi-k2")
|
| 285 |
+
@register_model("glm-4.5")
|
| 286 |
+
@register_model("qwen3-235b-thinking")
|
| 287 |
+
@register_model("grok-4")
|
| 288 |
+
async def call_openrouter(model: str, prompt: str, length=0, **kwargs) -> Any:
|
| 289 |
+
"""
|
| 290 |
+
Universal OpenRouter handler with reasoning effort support for OpenAI models.
|
| 291 |
+
Handles model name mapping and reasoning effort extraction.
|
| 292 |
+
|
| 293 |
+
IMPORTANT: reasoning_effort is ONLY applied to OpenAI models (o1/o3/o4-mini variants).
|
| 294 |
+
Other models ignore this parameter safely.
|
| 295 |
+
"""
|
| 296 |
+
# Model name mapping for custom aliases
|
| 297 |
+
name_map = {
|
| 298 |
+
"deepseek-v4-pro": "deepseek/deepseek-v4-pro",
|
| 299 |
+
"deepseek-reasoner": "deepseek/deepseek-r1",
|
| 300 |
+
"o3-pro": "openai/o3-pro",
|
| 301 |
+
"kimi-k2": "moonshotai/kimi-k2",
|
| 302 |
+
"glm-4.5": "z-ai/glm-4.5",
|
| 303 |
+
"qwen3-235b-thinking": "qwen/qwen3-235b-a22b-thinking-2507",
|
| 304 |
+
"gpt-oss-20b": "openai/gpt-oss-20b",
|
| 305 |
+
"gpt-oss-120b": "openai/gpt-oss-120b",
|
| 306 |
+
"gpt-5": "openai/gpt-5",
|
| 307 |
+
"grok-4": "x-ai/grok-4",
|
| 308 |
+
"claude-3-7-sonnet-20250219": "anthropic/claude-3.7-sonnet",
|
| 309 |
+
"claude-opus-4-5": "anthropic/claude-opus-4.5",
|
| 310 |
+
"claude-sonnet-4-5": "anthropic/claude-sonnet-4.5",
|
| 311 |
+
"gemini-2.0-flash": "google/gemini-2.0-flash-001",
|
| 312 |
+
"gemini-2.0-flash-lite-preview-02-05": "google/gemini-2.0-flash-lite-001",
|
| 313 |
+
"gemini-3-pro-preview": "google/gemini-3-pro-preview",
|
| 314 |
+
"gemini-3-flash-preview": "google/gemini-3-flash-preview",
|
| 315 |
+
"gemini-2.5-pro": "google/gemini-2.5-pro",
|
| 316 |
+
"gemini-2.5-flash": "google/gemini-2.5-flash",
|
| 317 |
+
"gemini-3.1-pro": "google/gemini-3.1-pro-preview",
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
# Handle OpenAI reasoning models (o1, o3, o4-mini variants)
|
| 321 |
+
# These are the ONLY models that support reasoning_effort parameter
|
| 322 |
+
openai_reasoning_models = {
|
| 323 |
+
"o1-mini": "openai/o1",
|
| 324 |
+
"o1-low": "openai/o1",
|
| 325 |
+
"o1-medium": "openai/o1",
|
| 326 |
+
"o1-high": "openai/o1",
|
| 327 |
+
"o3-low": "openai/o3",
|
| 328 |
+
"o3-medium": "openai/o3",
|
| 329 |
+
"o3-high": "openai/o3",
|
| 330 |
+
"o3-mini-low": "openai/o3-mini",
|
| 331 |
+
"o3-mini-medium": "openai/o3-mini",
|
| 332 |
+
"o3-mini-high": "openai/o3-mini",
|
| 333 |
+
"o4-mini-low": "openai/o4-mini",
|
| 334 |
+
"o4-mini-medium": "openai/o4-mini",
|
| 335 |
+
"o4-mini-high": "openai/o4-mini",
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
# Determine OpenRouter model name and reasoning effort
|
| 339 |
+
reasoning_effort = None
|
| 340 |
+
is_openai_reasoning = False
|
| 341 |
+
|
| 342 |
+
if model in openai_reasoning_models:
|
| 343 |
+
openrouter_model = openai_reasoning_models[model]
|
| 344 |
+
is_openai_reasoning = True
|
| 345 |
+
# Extract reasoning effort from suffix
|
| 346 |
+
effort_level = model.split("-")[-1]
|
| 347 |
+
if effort_level in ["low", "medium", "high"]:
|
| 348 |
+
reasoning_effort = effort_level
|
| 349 |
+
elif model in name_map:
|
| 350 |
+
openrouter_model = name_map[model]
|
| 351 |
+
else:
|
| 352 |
+
# Fallback: assume model name is already in OpenRouter format
|
| 353 |
+
openrouter_model = model
|
| 354 |
+
|
| 355 |
+
# Select appropriate rate limiter based on provider
|
| 356 |
+
if "google" in openrouter_model or "gemini" in openrouter_model.lower():
|
| 357 |
+
if "flash" in openrouter_model.lower():
|
| 358 |
+
semaphore = gemini_flash_semaphore
|
| 359 |
+
limiter = gemini_flash_limiter
|
| 360 |
+
else:
|
| 361 |
+
semaphore = gemini_pro_semaphore
|
| 362 |
+
limiter = gemini_pro_limiter
|
| 363 |
+
elif "anthropic" in openrouter_model or "claude" in openrouter_model.lower():
|
| 364 |
+
semaphore = claude_semaphore
|
| 365 |
+
limiter = claude_limiter
|
| 366 |
+
else:
|
| 367 |
+
# OpenAI and all others (grok, qwen, glm, kimi, deepseek)
|
| 368 |
+
semaphore = openai_semaphore
|
| 369 |
+
limiter = openai_limiter
|
| 370 |
+
|
| 371 |
+
async with semaphore:
|
| 372 |
+
async with limiter:
|
| 373 |
+
client = AsyncOpenAI(
|
| 374 |
+
base_url="https://openrouter.ai/api/v1",
|
| 375 |
+
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 376 |
+
timeout=TIMEOUT # Use the same 4-hour timeout
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
# Build request payload
|
| 380 |
+
# Add cache_control for Claude models
|
| 381 |
+
if "anthropic" in openrouter_model or "claude" in openrouter_model.lower():
|
| 382 |
+
request_params = {
|
| 383 |
+
"model": openrouter_model,
|
| 384 |
+
"messages": [{
|
| 385 |
+
"role": "user",
|
| 386 |
+
"content": [{
|
| 387 |
+
"type": "text",
|
| 388 |
+
"text": prompt,
|
| 389 |
+
"cache_control": {"type": "ephemeral"}
|
| 390 |
+
}]
|
| 391 |
+
}]
|
| 392 |
+
}
|
| 393 |
+
else:
|
| 394 |
+
request_params = {
|
| 395 |
+
"model": openrouter_model,
|
| 396 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
# Add reasoning_effort ONLY for OpenAI reasoning models
|
| 400 |
+
# This ensures we never accidentally pass it to non-OpenAI models
|
| 401 |
+
if reasoning_effort and is_openai_reasoning:
|
| 402 |
+
request_params["reasoning_effort"] = reasoning_effort
|
| 403 |
+
|
| 404 |
+
response = await client.chat.completions.create(**request_params)
|
| 405 |
+
|
| 406 |
+
return extract_value(response.choices[0].message.content, length)
|
| 407 |
+
|
| 408 |
+
# ------------------------ Gemini ------------------------
|
| 409 |
+
_cache_lookup = {} # display_name -> [cache_name, expire_time]
|
| 410 |
+
_cache_lock = asyncio.Lock()
|
| 411 |
+
TTL_SECONDS = 300
|
| 412 |
+
TTL_UPDATE = 20
|
| 413 |
+
TTL_STOP_UPDATE = 10
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
def _make_display_name(prompt: str) -> str:
|
| 417 |
+
return "cache-" + hashlib.sha256(prompt.encode()).hexdigest()[:16]
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def to_update(expire_time) -> bool:
|
| 421 |
+
cur_time = time.time()
|
| 422 |
+
return cur_time + TTL_UPDATE > expire_time and cur_time < expire_time - TTL_STOP_UPDATE
|
| 423 |
+
|
| 424 |
+
def to_create(expire_time) -> bool:
|
| 425 |
+
cur_time = time.time()
|
| 426 |
+
return cur_time > expire_time - TTL_STOP_UPDATE
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
@register_code_execution("gemini-3.1-pro")
|
| 430 |
+
@register_code_execution("gemini-3.1-pro-preview")
|
| 431 |
+
@register_code_execution("gemini-2.0-flash")
|
| 432 |
+
@register_code_execution("gemini-2.0-flash-lite-preview-02-05")
|
| 433 |
+
@register_code_execution("gemini-3-pro-preview")
|
| 434 |
+
@register_code_execution("gemini-3-flash-preview")
|
| 435 |
+
@register_code_execution("gemini-2.5-pro")
|
| 436 |
+
@register_code_execution("gemini-2.5-flash")
|
| 437 |
+
async def _gemini_with_code_execution(
|
| 438 |
+
model: str,
|
| 439 |
+
prompt: str,
|
| 440 |
+
file_paths: list,
|
| 441 |
+
file_path_to_id: dict,
|
| 442 |
+
title: str,
|
| 443 |
+
length: int = 0
|
| 444 |
+
) -> Any:
|
| 445 |
+
"""
|
| 446 |
+
Call Gemini model with code execution enabled.
|
| 447 |
+
Accepts file_path_to_id as nested dict with 'gemini' key containing Gemini file objects.
|
| 448 |
+
For gemini-3.1-pro variants the prompt is augmented with a 20-call budget constraint.
|
| 449 |
+
"""
|
| 450 |
+
_gemini_sdk_name_map = {"gemini-3.1-pro": "gemini-3.1-pro-preview"}
|
| 451 |
+
sdk_model = _gemini_sdk_name_map.get(model, model)
|
| 452 |
+
|
| 453 |
+
if model == "gemini-2.0-flash":
|
| 454 |
+
gemini_semaphore = gemini_flash_semaphore
|
| 455 |
+
gemini_limiter = gemini_flash_limiter
|
| 456 |
+
else:
|
| 457 |
+
gemini_semaphore = gemini_pro_semaphore
|
| 458 |
+
gemini_limiter = gemini_pro_limiter
|
| 459 |
+
|
| 460 |
+
_31_pro_models = ("gemini-3.1-pro", "gemini-3.1-pro-preview")
|
| 461 |
+
effective_prompt = (
|
| 462 |
+
prompt + "\n\nConstraint: use at most 20 code execution calls. "
|
| 463 |
+
"If you cannot finish within that limit, return an empty response."
|
| 464 |
+
) if model in _31_pro_models else prompt
|
| 465 |
+
|
| 466 |
+
config = types.GenerateContentConfig(
|
| 467 |
+
tools=[types.Tool(code_execution=types.ToolCodeExecution())]
|
| 468 |
+
)
|
| 469 |
+
|
| 470 |
+
async with gemini_semaphore:
|
| 471 |
+
async with gemini_limiter:
|
| 472 |
+
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 473 |
+
aio = client.aio
|
| 474 |
+
|
| 475 |
+
try:
|
| 476 |
+
contents = []
|
| 477 |
+
if file_paths and file_path_to_id and "gemini" in file_path_to_id:
|
| 478 |
+
gemini_files = file_path_to_id["gemini"]
|
| 479 |
+
for file_path in file_paths:
|
| 480 |
+
if file_path in gemini_files:
|
| 481 |
+
contents.append(gemini_files[file_path])
|
| 482 |
+
contents.append(effective_prompt)
|
| 483 |
+
|
| 484 |
+
response = await aio.models.generate_content(
|
| 485 |
+
model=sdk_model,
|
| 486 |
+
contents=contents,
|
| 487 |
+
config=config,
|
| 488 |
+
)
|
| 489 |
+
|
| 490 |
+
usage = getattr(response, 'usage_metadata', None)
|
| 491 |
+
thoughts_tok = getattr(usage, 'thoughts_token_count', None) if usage else None
|
| 492 |
+
candidates_tok = getattr(usage, 'candidates_token_count', None) if usage else None
|
| 493 |
+
total_tok = getattr(usage, 'total_token_count', None) if usage else None
|
| 494 |
+
|
| 495 |
+
cand = response.candidates[0] if (hasattr(response, 'candidates') and response.candidates) else None
|
| 496 |
+
finish_reason = getattr(cand, 'finish_reason', None) if cand else None
|
| 497 |
+
safety_ratings = getattr(cand, 'safety_ratings', None) if cand else None
|
| 498 |
+
safety_str = ""
|
| 499 |
+
if safety_ratings:
|
| 500 |
+
blocked = [f"{r.category.name}:{r.probability.name}" for r in safety_ratings
|
| 501 |
+
if hasattr(r, 'blocked') and r.blocked]
|
| 502 |
+
if blocked:
|
| 503 |
+
safety_str = f" SAFETY_BLOCKED={blocked}"
|
| 504 |
+
|
| 505 |
+
_raw_parts = cand.content.parts if cand and cand.content else None
|
| 506 |
+
parts = _raw_parts if _raw_parts is not None else []
|
| 507 |
+
n_code = sum(1 for p in parts if getattr(p, 'executable_code', None))
|
| 508 |
+
n_result = sum(1 for p in parts if getattr(p, 'code_execution_result', None))
|
| 509 |
+
n_plot = sum(1 for p in parts if getattr(p, 'inline_data', None))
|
| 510 |
+
n_text = sum(1 for p in parts if getattr(p, 'text', None))
|
| 511 |
+
|
| 512 |
+
print(f"Gemini {model} [{title}] finish={finish_reason} "
|
| 513 |
+
f"tokens(thoughts={thoughts_tok} out={candidates_tok} total={total_tok}) "
|
| 514 |
+
f"parts(code={n_code} result={n_result} plot={n_plot} text={n_text})"
|
| 515 |
+
f"{safety_str}")
|
| 516 |
+
|
| 517 |
+
_fr_str = str(finish_reason)
|
| 518 |
+
if "TOO_MANY_TOOL_CALLS" in _fr_str:
|
| 519 |
+
raise ValueError(f"TOO_MANY_TOOL_CALLS for {model} on {title}")
|
| 520 |
+
if finish_reason is not None and "STOP" not in _fr_str:
|
| 521 |
+
raise ValueError(f"Non-STOP finish reason for {model} on {title}: {finish_reason}")
|
| 522 |
+
|
| 523 |
+
text = response.text if (hasattr(response, 'text') and response.text) else None
|
| 524 |
+
if text is None and cand:
|
| 525 |
+
for part in parts:
|
| 526 |
+
part_text = getattr(part, 'text', None)
|
| 527 |
+
if part_text:
|
| 528 |
+
text = part_text
|
| 529 |
+
|
| 530 |
+
if not text:
|
| 531 |
+
parts_dump = []
|
| 532 |
+
if hasattr(response, 'candidates') and response.candidates:
|
| 533 |
+
finish_reason = getattr(response.candidates[0], 'finish_reason', None)
|
| 534 |
+
for i, part in enumerate(response.candidates[0].content.parts):
|
| 535 |
+
if getattr(part, 'text', None):
|
| 536 |
+
parts_dump.append(f" [{i}] text: {part.text[:120]!r}")
|
| 537 |
+
elif getattr(part, 'executable_code', None):
|
| 538 |
+
parts_dump.append(f" [{i}] executable_code: {(getattr(part.executable_code, 'code', '') or '')[:80]!r}")
|
| 539 |
+
elif getattr(part, 'code_execution_result', None):
|
| 540 |
+
parts_dump.append(f" [{i}] code_execution_result output: {(getattr(part.code_execution_result, 'output', '') or '')[:80]!r}")
|
| 541 |
+
elif getattr(part, 'inline_data', None):
|
| 542 |
+
parts_dump.append(f" [{i}] inline_data (e.g. plot image)")
|
| 543 |
+
else:
|
| 544 |
+
parts_dump.append(f" [{i}] unknown part: {part}")
|
| 545 |
+
print(f"No text in Gemini response for {model} on {title}. finish_reason={finish_reason}, parts ({len(parts_dump)}):\n" + "\n".join(parts_dump))
|
| 546 |
+
raise ValueError(f"No text in response for model {model}")
|
| 547 |
+
|
| 548 |
+
print(f"Gemini {model} code execution response for {title}: {text[:200]}")
|
| 549 |
+
return extract_value(text, length)
|
| 550 |
+
|
| 551 |
+
except Exception as e:
|
| 552 |
+
print(f"Error in Gemini code execution for {model} on {title}: {e}")
|
| 553 |
+
raise
|
| 554 |
+
|
| 555 |
+
@register_model("gemini-3.1-pro")
|
| 556 |
+
@register_model("gemini-3.1-pro-preview")
|
| 557 |
+
@register_model("gemini-2.0-flash")
|
| 558 |
+
@register_model("gemini-2.0-flash-lite-preview-02-05")
|
| 559 |
+
@register_model("gemini-3-pro-preview")
|
| 560 |
+
@register_model("gemini-3-flash-preview")
|
| 561 |
+
@register_model("gemini-2.5-pro")
|
| 562 |
+
@register_model("gemini-2.5-flash")
|
| 563 |
+
async def _gemini_basic(model: str, prompt: str, length=0, **kwargs) -> str:
|
| 564 |
+
# Translate user-facing aliases to actual Google SDK model names
|
| 565 |
+
_gemini_sdk_aliases = {"gemini-3.1-pro": "gemini-3.1-pro-preview"}
|
| 566 |
+
sdk_model = _gemini_sdk_aliases.get(model, model)
|
| 567 |
+
|
| 568 |
+
if model == "gemini-2.0-flash":
|
| 569 |
+
gemini_semaphore = gemini_flash_semaphore
|
| 570 |
+
gemini_limiter = gemini_flash_limiter
|
| 571 |
+
else:
|
| 572 |
+
gemini_semaphore = gemini_pro_semaphore
|
| 573 |
+
gemini_limiter = gemini_pro_limiter
|
| 574 |
+
|
| 575 |
+
async with gemini_semaphore:
|
| 576 |
+
async with gemini_limiter:
|
| 577 |
+
# Create a new client instance for this call
|
| 578 |
+
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 579 |
+
aio = client.aio
|
| 580 |
+
|
| 581 |
+
# Try to use caching, but fall back to non-cached if it fails
|
| 582 |
+
cache_name = None
|
| 583 |
+
use_cache = True
|
| 584 |
+
|
| 585 |
+
try:
|
| 586 |
+
display_name = _make_display_name(prompt)
|
| 587 |
+
key = (display_name, model)
|
| 588 |
+
|
| 589 |
+
async with _cache_lock:
|
| 590 |
+
expire_time = None
|
| 591 |
+
if key in _cache_lookup:
|
| 592 |
+
cache_name, expire_time = _cache_lookup[key]
|
| 593 |
+
if to_update(expire_time):
|
| 594 |
+
print('update cache for', display_name + " " + model)
|
| 595 |
+
try:
|
| 596 |
+
await aio.caches.update(
|
| 597 |
+
name=cache_name,
|
| 598 |
+
config=types.UpdateCachedContentConfig(ttl=f"{TTL_SECONDS}s")
|
| 599 |
+
)
|
| 600 |
+
except Exception as e:
|
| 601 |
+
print(f"Failed to update cache for {model}: {e}")
|
| 602 |
+
cache_name = None
|
| 603 |
+
use_cache = False
|
| 604 |
+
|
| 605 |
+
if use_cache and ((not expire_time) or to_create(expire_time)):
|
| 606 |
+
new_expire_time = time.time() + TTL_SECONDS
|
| 607 |
+
print("create cache for", display_name + " " + model)
|
| 608 |
+
try:
|
| 609 |
+
new_cache = await aio.caches.create(
|
| 610 |
+
model=sdk_model,
|
| 611 |
+
config=types.CreateCachedContentConfig(
|
| 612 |
+
display_name=display_name,
|
| 613 |
+
contents=[
|
| 614 |
+
types.Content(
|
| 615 |
+
role="user",
|
| 616 |
+
parts=[types.Part(text=prompt)]
|
| 617 |
+
)
|
| 618 |
+
],
|
| 619 |
+
ttl=f"{TTL_SECONDS}s",
|
| 620 |
+
)
|
| 621 |
+
)
|
| 622 |
+
cache_name = new_cache.name
|
| 623 |
+
_cache_lookup[key] = [cache_name, new_expire_time]
|
| 624 |
+
except Exception as e:
|
| 625 |
+
# Cache requires minimum tokens (4096 for flash, 2048 for pro)
|
| 626 |
+
# This is expected for small prompts - just skip the warning
|
| 627 |
+
if "too small" not in str(e):
|
| 628 |
+
print(f"Cache not supported for {model}, proceeding without cache: {e}")
|
| 629 |
+
cache_name = None
|
| 630 |
+
use_cache = False
|
| 631 |
+
except Exception as e:
|
| 632 |
+
print(f"Cache setup failed for {model}, proceeding without cache: {e}")
|
| 633 |
+
cache_name = None
|
| 634 |
+
use_cache = False
|
| 635 |
+
|
| 636 |
+
# Generate content with or without cache
|
| 637 |
+
try:
|
| 638 |
+
if use_cache and cache_name:
|
| 639 |
+
response = await aio.models.generate_content(
|
| 640 |
+
model=sdk_model,
|
| 641 |
+
contents="Give answer only.",
|
| 642 |
+
config=types.GenerateContentConfig(cached_content=cache_name),
|
| 643 |
+
)
|
| 644 |
+
else:
|
| 645 |
+
# Fall back to non-cached request
|
| 646 |
+
response = await aio.models.generate_content(
|
| 647 |
+
model=sdk_model,
|
| 648 |
+
contents=prompt,
|
| 649 |
+
)
|
| 650 |
+
|
| 651 |
+
# Check if response has text
|
| 652 |
+
if not hasattr(response, 'text') or response.text is None:
|
| 653 |
+
# Try to extract text from candidates
|
| 654 |
+
if hasattr(response, 'candidates') and response.candidates:
|
| 655 |
+
text = response.candidates[0].content.parts[0].text
|
| 656 |
+
else:
|
| 657 |
+
raise ValueError(f"No text in response for model {model}")
|
| 658 |
+
else:
|
| 659 |
+
text = response.text
|
| 660 |
+
|
| 661 |
+
return extract_value(text, length)
|
| 662 |
+
except Exception as e:
|
| 663 |
+
print(f"Error generating content for {model}: {e}")
|
| 664 |
+
raise
|
| 665 |
+
|
| 666 |
+
# ------------------------ Anthropic ------------------------
|
| 667 |
+
@register_model("claude-opus-4-7")
|
| 668 |
+
@register_model("claude-opus-4-6")
|
| 669 |
+
@register_model("claude-sonnet-4-6")
|
| 670 |
+
@register_model("claude-3-7-sonnet-20250219")
|
| 671 |
+
@register_model("claude-opus-4-5")
|
| 672 |
+
@register_model("claude-sonnet-4-5")
|
| 673 |
+
async def _claude_basic(model: str, prompt: str, length=0, **kwargs) -> Any:
|
| 674 |
+
async with claude_semaphore:
|
| 675 |
+
async with claude_limiter:
|
| 676 |
+
headers = {
|
| 677 |
+
"x-api-key": os.getenv("ANTHROPIC_API_KEY"),
|
| 678 |
+
"Content-Type": "application/json",
|
| 679 |
+
"anthropic-version": "2023-06-01"
|
| 680 |
+
}
|
| 681 |
+
|
| 682 |
+
body = {
|
| 683 |
+
"model": model,
|
| 684 |
+
"max_tokens": 64000,
|
| 685 |
+
"messages": [
|
| 686 |
+
{ "role": "user",
|
| 687 |
+
"content": [
|
| 688 |
+
{
|
| 689 |
+
"type": "text",
|
| 690 |
+
"text": prompt,
|
| 691 |
+
"cache_control": {
|
| 692 |
+
"type": "ephemeral"
|
| 693 |
+
}
|
| 694 |
+
}
|
| 695 |
+
]
|
| 696 |
+
}
|
| 697 |
+
]
|
| 698 |
+
}
|
| 699 |
+
|
| 700 |
+
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
|
| 701 |
+
resp = await client.post("https://api.anthropic.com/v1/messages", headers=headers, json=body)
|
| 702 |
+
resp.raise_for_status()
|
| 703 |
+
text = resp.json()["content"][0]["text"]
|
| 704 |
+
return extract_value(text, length)
|
| 705 |
+
|
| 706 |
+
|
| 707 |
+
# ------------------------ Ask wrapper ------------------------
|
| 708 |
+
rate_limited_models = set()
|
| 709 |
+
async def ask_model(model: str, prompt: str, cache, title, return_length=0) -> Any:
|
| 710 |
+
|
| 711 |
+
handler = _REGISTRY.get(model)
|
| 712 |
+
if not handler:
|
| 713 |
+
raise ValueError(f"No handler registered for model {model}")
|
| 714 |
+
|
| 715 |
+
result = None
|
| 716 |
+
for attempt in range(NUM_RETRIES):
|
| 717 |
+
if model in rate_limited_models:
|
| 718 |
+
print(f"Rate limited for model {model}, skipping...")
|
| 719 |
+
return None
|
| 720 |
+
if attempt > 0:
|
| 721 |
+
await asyncio.sleep(60)
|
| 722 |
+
try:
|
| 723 |
+
result = await handler(model, prompt, return_length)
|
| 724 |
+
if check_type(result, return_length):
|
| 725 |
+
break
|
| 726 |
+
raise TypeError(f"Check Type failed, expected return: {return_length}, got {result}")
|
| 727 |
+
except Exception as e:
|
| 728 |
+
if isinstance(e, httpx.HTTPStatusError) and e.response.status_code == 429:
|
| 729 |
+
rate_limited_models.add(model)
|
| 730 |
+
print(f"Error calling model {model} on attempt {attempt+1} with question {title}: {e}")
|
| 731 |
+
|
| 732 |
+
return result
|
| 733 |
+
|
| 734 |
+
|
| 735 |
+
# ------------------------ OpenAI Code Interpreter ------------------------
|
| 736 |
+
|
| 737 |
+
@register_code_execution("gpt-5.4")
|
| 738 |
+
@register_code_execution("gpt-5")
|
| 739 |
+
@register_code_execution("o3-low")
|
| 740 |
+
@register_code_execution("o3-medium")
|
| 741 |
+
@register_code_execution("o3-high")
|
| 742 |
+
@register_code_execution("o4-mini-low")
|
| 743 |
+
@register_code_execution("o4-mini-medium")
|
| 744 |
+
@register_code_execution("o4-mini-high")
|
| 745 |
+
async def _openai_with_code_interpreter(
|
| 746 |
+
model: str,
|
| 747 |
+
prompt: str,
|
| 748 |
+
file_paths: list,
|
| 749 |
+
file_path_to_id: dict,
|
| 750 |
+
title: str,
|
| 751 |
+
return_length: int = 0
|
| 752 |
+
) -> Any:
|
| 753 |
+
"""
|
| 754 |
+
Call OpenAI model with code interpreter enabled and file access.
|
| 755 |
+
Uses responses API with file_ids nested in container.
|
| 756 |
+
Supports reasoning models (e.g., o3-low, o3-medium, o3-high).
|
| 757 |
+
|
| 758 |
+
Args:
|
| 759 |
+
model: OpenAI model name (e.g., "o3-low", "o3-medium", "o3-high", "gpt-4o")
|
| 760 |
+
For o3 models with reasoning effort suffix, the function automatically
|
| 761 |
+
splits "o3-low" into model="o3" with reasoning={"effort": "low"}
|
| 762 |
+
prompt: The question/prompt
|
| 763 |
+
file_paths: List of local file paths needed for this question
|
| 764 |
+
file_path_to_id: Dict mapping file paths to OpenAI file IDs
|
| 765 |
+
title: Question title for logging
|
| 766 |
+
return_length: Expected return length (0 for scalar, >0 for array)
|
| 767 |
+
"""
|
| 768 |
+
async with openai_semaphore:
|
| 769 |
+
async with openai_limiter:
|
| 770 |
+
client = AsyncOpenAI(
|
| 771 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
| 772 |
+
timeout=TIMEOUT # Use the same 4-hour timeout
|
| 773 |
+
)
|
| 774 |
+
|
| 775 |
+
# Parse model name and reasoning effort
|
| 776 |
+
# For o3 models, split "o3-low" -> model="o3", reasoning={"effort": "low"}
|
| 777 |
+
reasoning_effort = None
|
| 778 |
+
model_name = model
|
| 779 |
+
|
| 780 |
+
|
| 781 |
+
effort_level = model.split("-")[-1] # Extract "low", "medium", or "high"
|
| 782 |
+
if effort_level in ["low", "medium", "high"]:
|
| 783 |
+
model_name = model[: -len(effort_level) - 1]
|
| 784 |
+
reasoning_effort = effort_level
|
| 785 |
+
|
| 786 |
+
# Look up file IDs from the map
|
| 787 |
+
# file_path_to_id is now nested: {"openai": {path: id}, "gemini": {path: file_obj}}
|
| 788 |
+
file_ids = []
|
| 789 |
+
if file_paths and file_path_to_id and "openai" in file_path_to_id:
|
| 790 |
+
openai_files = file_path_to_id["openai"]
|
| 791 |
+
for file_path in file_paths:
|
| 792 |
+
if file_path in openai_files:
|
| 793 |
+
file_ids.append(openai_files[file_path])
|
| 794 |
+
else:
|
| 795 |
+
raise ValueError(f"File {file_path} not found in uploaded OpenAI files map")
|
| 796 |
+
|
| 797 |
+
# Build request parameters
|
| 798 |
+
request_params = {
|
| 799 |
+
"model": model_name,
|
| 800 |
+
"tools": [
|
| 801 |
+
{
|
| 802 |
+
"type": "code_interpreter",
|
| 803 |
+
"container": {
|
| 804 |
+
"type": "auto",
|
| 805 |
+
# Critical: file_ids goes INSIDE container
|
| 806 |
+
"file_ids": file_ids,
|
| 807 |
+
},
|
| 808 |
+
}
|
| 809 |
+
],
|
| 810 |
+
"input": [
|
| 811 |
+
{
|
| 812 |
+
"role": "user",
|
| 813 |
+
"content": [
|
| 814 |
+
{
|
| 815 |
+
"type": "input_text",
|
| 816 |
+
"text": prompt,
|
| 817 |
+
}
|
| 818 |
+
],
|
| 819 |
+
}
|
| 820 |
+
],
|
| 821 |
+
}
|
| 822 |
+
|
| 823 |
+
# Add reasoning parameter for o3 models
|
| 824 |
+
if reasoning_effort:
|
| 825 |
+
request_params["reasoning"] = {"effort": reasoning_effort}
|
| 826 |
+
|
| 827 |
+
# Use responses API with file_ids inside container
|
| 828 |
+
try:
|
| 829 |
+
response = await client.responses.create(**request_params)
|
| 830 |
+
|
| 831 |
+
# Extract output text from response
|
| 832 |
+
output_text = ""
|
| 833 |
+
if hasattr(response, 'output_text') and response.output_text:
|
| 834 |
+
output_text = response.output_text
|
| 835 |
+
elif hasattr(response, 'output') and response.output:
|
| 836 |
+
for item in response.output:
|
| 837 |
+
if hasattr(item, 'content'):
|
| 838 |
+
for content_item in item.content:
|
| 839 |
+
if hasattr(content_item, 'text'):
|
| 840 |
+
output_text += content_item.text
|
| 841 |
+
|
| 842 |
+
print(f"Model {model} response for {title}: {output_text[:200]}")
|
| 843 |
+
|
| 844 |
+
# Extract the numerical result with reasoning
|
| 845 |
+
result = extract_value(output_text, return_length)
|
| 846 |
+
|
| 847 |
+
return result
|
| 848 |
+
|
| 849 |
+
except Exception as e:
|
| 850 |
+
print(f"Error in code interpreter call for {model} on {title}: {e}")
|
| 851 |
+
raise
|
| 852 |
+
|
| 853 |
+
# ------------------------ Unified Code Execution Interface ------------------------
|
| 854 |
+
|
| 855 |
+
# ------------------------ Claude Code Execution ------------------------
|
| 856 |
+
|
| 857 |
+
# Cache and reference counting for Claude file uploads
|
| 858 |
+
_claude_file_cache = {} # {file_path: file_id}
|
| 859 |
+
_claude_file_refs = {} # {file_path: ref_count}
|
| 860 |
+
_claude_upload_lock = None
|
| 861 |
+
|
| 862 |
+
def _get_claude_upload_lock():
|
| 863 |
+
"""Lazy init lock to avoid issues with asyncio event loop at import time"""
|
| 864 |
+
global _claude_upload_lock
|
| 865 |
+
if _claude_upload_lock is None:
|
| 866 |
+
_claude_upload_lock = asyncio.Lock()
|
| 867 |
+
return _claude_upload_lock
|
| 868 |
+
|
| 869 |
+
async def clear_claude_cache():
|
| 870 |
+
"""
|
| 871 |
+
Clear Claude file cache and refs (call at start of run).
|
| 872 |
+
Also deletes all files from Claude API to start fresh.
|
| 873 |
+
|
| 874 |
+
Note: Files are deleted successfully (API returns 200), but the list
|
| 875 |
+
may still show files due to eventual consistency or other workspace activity.
|
| 876 |
+
This is OK - new uploads will work correctly with reference counting.
|
| 877 |
+
"""
|
| 878 |
+
global _claude_file_cache, _claude_file_refs
|
| 879 |
+
|
| 880 |
+
headers = {
|
| 881 |
+
"x-api-key": os.getenv("ANTHROPIC_API_KEY"),
|
| 882 |
+
"anthropic-version": "2023-06-01",
|
| 883 |
+
"anthropic-beta": "files-api-2025-04-14"
|
| 884 |
+
}
|
| 885 |
+
|
| 886 |
+
try:
|
| 887 |
+
async with httpx.AsyncClient(timeout=60) as client:
|
| 888 |
+
# List all files
|
| 889 |
+
resp = await client.get("https://api.anthropic.com/v1/files", headers=headers)
|
| 890 |
+
resp.raise_for_status()
|
| 891 |
+
files = resp.json().get('data', [])
|
| 892 |
+
|
| 893 |
+
if files:
|
| 894 |
+
print(f"Clearing {len(files)} files from Claude API...")
|
| 895 |
+
# Delete all files
|
| 896 |
+
for f in files:
|
| 897 |
+
file_id = f.get('id')
|
| 898 |
+
try:
|
| 899 |
+
await client.delete(f"https://api.anthropic.com/v1/files/{file_id}", headers=headers)
|
| 900 |
+
except Exception as e:
|
| 901 |
+
# Non-fatal, continue with other files
|
| 902 |
+
pass
|
| 903 |
+
print(f"✓ Cleared {len(files)} files from Claude API")
|
| 904 |
+
except Exception as e:
|
| 905 |
+
print(f"Warning: Could not clear Claude files: {e}")
|
| 906 |
+
|
| 907 |
+
# Clear local cache
|
| 908 |
+
_claude_file_cache.clear()
|
| 909 |
+
_claude_file_refs.clear()
|
| 910 |
+
|
| 911 |
+
async def _delete_claude_file(file_id: str):
|
| 912 |
+
"""Delete a file from Claude API"""
|
| 913 |
+
headers = {
|
| 914 |
+
"x-api-key": os.getenv("ANTHROPIC_API_KEY"),
|
| 915 |
+
"anthropic-version": "2023-06-01",
|
| 916 |
+
"anthropic-beta": "files-api-2025-04-14"
|
| 917 |
+
}
|
| 918 |
+
try:
|
| 919 |
+
async with httpx.AsyncClient(timeout=30) as client:
|
| 920 |
+
await client.delete(
|
| 921 |
+
f"https://api.anthropic.com/v1/files/{file_id}",
|
| 922 |
+
headers=headers
|
| 923 |
+
)
|
| 924 |
+
except Exception:
|
| 925 |
+
pass # Non-fatal
|
| 926 |
+
|
| 927 |
+
async def _get_or_upload_claude_file(file_path: str) -> str:
|
| 928 |
+
"""
|
| 929 |
+
Get or upload a single file to Claude with reference counting.
|
| 930 |
+
Returns file_id.
|
| 931 |
+
"""
|
| 932 |
+
# Check cache first (no lock needed for read)
|
| 933 |
+
if file_path in _claude_file_cache:
|
| 934 |
+
return _claude_file_cache[file_path]
|
| 935 |
+
|
| 936 |
+
# Need to upload - acquire lock for ls/upload atomic operation
|
| 937 |
+
async with _get_claude_upload_lock():
|
| 938 |
+
# Double-check cache after acquiring lock
|
| 939 |
+
if file_path in _claude_file_cache:
|
| 940 |
+
return _claude_file_cache[file_path]
|
| 941 |
+
|
| 942 |
+
headers = {
|
| 943 |
+
"x-api-key": os.getenv("ANTHROPIC_API_KEY"),
|
| 944 |
+
"anthropic-version": "2023-06-01",
|
| 945 |
+
"anthropic-beta": "files-api-2025-04-14"
|
| 946 |
+
}
|
| 947 |
+
|
| 948 |
+
file_basename = os.path.basename(file_path)
|
| 949 |
+
file_size = os.path.getsize(file_path)
|
| 950 |
+
|
| 951 |
+
try:
|
| 952 |
+
async with httpx.AsyncClient(timeout=60) as client:
|
| 953 |
+
# List existing files
|
| 954 |
+
resp = await client.get("https://api.anthropic.com/v1/files", headers=headers)
|
| 955 |
+
resp.raise_for_status()
|
| 956 |
+
existing_files = resp.json().get('data', [])
|
| 957 |
+
|
| 958 |
+
# Check if file exists
|
| 959 |
+
for f in existing_files:
|
| 960 |
+
if f.get('filename') == file_basename and f.get('size_bytes') == file_size:
|
| 961 |
+
file_id = f['id']
|
| 962 |
+
_claude_file_cache[file_path] = file_id
|
| 963 |
+
return file_id
|
| 964 |
+
|
| 965 |
+
# File not found, upload it
|
| 966 |
+
async with aiofiles.open(file_path, 'rb') as f:
|
| 967 |
+
file_content = await f.read()
|
| 968 |
+
|
| 969 |
+
# Upload with retry
|
| 970 |
+
for attempt in range(3):
|
| 971 |
+
try:
|
| 972 |
+
files = {'file': (file_basename, file_content)}
|
| 973 |
+
resp = await client.post(
|
| 974 |
+
"https://api.anthropic.com/v1/files",
|
| 975 |
+
headers=headers,
|
| 976 |
+
files=files
|
| 977 |
+
)
|
| 978 |
+
resp.raise_for_status()
|
| 979 |
+
file_id = resp.json()['id']
|
| 980 |
+
_claude_file_cache[file_path] = file_id
|
| 981 |
+
return file_id
|
| 982 |
+
except httpx.HTTPStatusError as e:
|
| 983 |
+
if e.response.status_code == 500 and attempt < 2:
|
| 984 |
+
await asyncio.sleep(2 ** attempt)
|
| 985 |
+
else:
|
| 986 |
+
raise
|
| 987 |
+
except Exception as e:
|
| 988 |
+
print(f"Error uploading {file_basename} to Claude: {e}")
|
| 989 |
+
raise
|
| 990 |
+
|
| 991 |
+
@register_code_execution("claude-opus-4-7")
|
| 992 |
+
@register_code_execution("claude-opus-4-6")
|
| 993 |
+
@register_code_execution("claude-sonnet-4-6")
|
| 994 |
+
@register_code_execution("claude-3-7-sonnet-20250219")
|
| 995 |
+
@register_code_execution("claude-opus-4-5")
|
| 996 |
+
@register_code_execution("claude-sonnet-4-5")
|
| 997 |
+
async def _claude_with_code_execution(
|
| 998 |
+
model: str,
|
| 999 |
+
prompt: str,
|
| 1000 |
+
file_paths: list,
|
| 1001 |
+
file_path_to_id: dict,
|
| 1002 |
+
title: str,
|
| 1003 |
+
return_length: int = 0
|
| 1004 |
+
) -> Any:
|
| 1005 |
+
"""
|
| 1006 |
+
Call Claude model with code execution enabled.
|
| 1007 |
+
Uses Anthropic's native code execution tool (code_execution_20250825).
|
| 1008 |
+
Does NOT reuse containers - each request gets a fresh container.
|
| 1009 |
+
Files are uploaded just-in-time with reference counting to combat 20-file FIFO eviction.
|
| 1010 |
+
"""
|
| 1011 |
+
# Increment reference counts for files we need
|
| 1012 |
+
async with _get_claude_upload_lock():
|
| 1013 |
+
for file_path in file_paths:
|
| 1014 |
+
_claude_file_refs[file_path] = _claude_file_refs.get(file_path, 0) + 1
|
| 1015 |
+
|
| 1016 |
+
try:
|
| 1017 |
+
async with claude_semaphore:
|
| 1018 |
+
async with claude_limiter:
|
| 1019 |
+
headers = {
|
| 1020 |
+
"x-api-key": os.getenv("ANTHROPIC_API_KEY"),
|
| 1021 |
+
"Content-Type": "application/json",
|
| 1022 |
+
"anthropic-version": "2023-06-01",
|
| 1023 |
+
"anthropic-beta": "code-execution-2025-08-25,files-api-2025-04-14"
|
| 1024 |
+
}
|
| 1025 |
+
|
| 1026 |
+
# Upload files just-in-time (one by one within semaphore limit)
|
| 1027 |
+
claude_files = {}
|
| 1028 |
+
for file_path in file_paths:
|
| 1029 |
+
file_id = await _get_or_upload_claude_file(file_path)
|
| 1030 |
+
claude_files[file_path] = file_id
|
| 1031 |
+
|
| 1032 |
+
# Build message content with files and prompt
|
| 1033 |
+
content = []
|
| 1034 |
+
|
| 1035 |
+
# Add files
|
| 1036 |
+
for file_path in file_paths:
|
| 1037 |
+
if file_path in claude_files:
|
| 1038 |
+
content.append({
|
| 1039 |
+
"type": "container_upload",
|
| 1040 |
+
"file_id": claude_files[file_path]
|
| 1041 |
+
})
|
| 1042 |
+
|
| 1043 |
+
# Add text prompt with ephemeral cache
|
| 1044 |
+
content.append({
|
| 1045 |
+
"type": "text",
|
| 1046 |
+
"text": prompt,
|
| 1047 |
+
"cache_control": {"type": "ephemeral"}
|
| 1048 |
+
})
|
| 1049 |
+
|
| 1050 |
+
# Build initial messages
|
| 1051 |
+
messages = [{"role": "user", "content": content}]
|
| 1052 |
+
|
| 1053 |
+
# Loop to handle pause_turn (max 5 pause continuations + 1 initial = 6 total)
|
| 1054 |
+
max_attempts = 9
|
| 1055 |
+
data = None
|
| 1056 |
+
container_id = None
|
| 1057 |
+
|
| 1058 |
+
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
|
| 1059 |
+
for attempt in range(max_attempts):
|
| 1060 |
+
body = {
|
| 1061 |
+
"model": model,
|
| 1062 |
+
"max_tokens": 64000,
|
| 1063 |
+
"messages": messages,
|
| 1064 |
+
"tools": [{
|
| 1065 |
+
"type": "code_execution_20250825",
|
| 1066 |
+
"name": "code_execution"
|
| 1067 |
+
}]
|
| 1068 |
+
}
|
| 1069 |
+
# Reuse container from previous turn so sandbox files persist
|
| 1070 |
+
if container_id:
|
| 1071 |
+
body["container"] = container_id
|
| 1072 |
+
|
| 1073 |
+
try:
|
| 1074 |
+
resp = await client.post(
|
| 1075 |
+
"https://api.anthropic.com/v1/messages",
|
| 1076 |
+
headers=headers,
|
| 1077 |
+
json=body
|
| 1078 |
+
)
|
| 1079 |
+
resp.raise_for_status()
|
| 1080 |
+
data = resp.json()
|
| 1081 |
+
|
| 1082 |
+
stop_reason = data.get("stop_reason", "unknown")
|
| 1083 |
+
usage = data.get("usage", {})
|
| 1084 |
+
|
| 1085 |
+
# If pause_turn and not last attempt, continue with same container
|
| 1086 |
+
if stop_reason == "pause_turn" and attempt < max_attempts - 1:
|
| 1087 |
+
container_id = (data.get("container") or {}).get("id") or container_id
|
| 1088 |
+
content_blocks = data.get("content", [])
|
| 1089 |
+
block_types = [b.get("type") for b in content_blocks]
|
| 1090 |
+
text_snippets = [
|
| 1091 |
+
b.get("text", "")[:120]
|
| 1092 |
+
for b in content_blocks if b.get("type") == "text"
|
| 1093 |
+
]
|
| 1094 |
+
print(
|
| 1095 |
+
f"[{title}] Claude paused (attempt {attempt + 1}/{max_attempts}) "
|
| 1096 |
+
f"container={container_id} "
|
| 1097 |
+
f"tokens(in={usage.get('input_tokens')} out={usage.get('output_tokens')}) "
|
| 1098 |
+
f"block_types={block_types}"
|
| 1099 |
+
)
|
| 1100 |
+
for i, snippet in enumerate(text_snippets):
|
| 1101 |
+
print(f" text[{i}]: {snippet!r}")
|
| 1102 |
+
# On continuation turns, omit container_upload blocks —
|
| 1103 |
+
# files are already present in the existing container.
|
| 1104 |
+
continuation_content = [
|
| 1105 |
+
block for block in content
|
| 1106 |
+
if block.get("type") != "container_upload"
|
| 1107 |
+
]
|
| 1108 |
+
messages = [
|
| 1109 |
+
{"role": "user", "content": continuation_content},
|
| 1110 |
+
{"role": "assistant", "content": content_blocks}
|
| 1111 |
+
]
|
| 1112 |
+
continue
|
| 1113 |
+
else:
|
| 1114 |
+
# end_turn, max_tokens, or max attempts reached
|
| 1115 |
+
break
|
| 1116 |
+
|
| 1117 |
+
except httpx.HTTPStatusError as http_err:
|
| 1118 |
+
if attempt == max_attempts - 1:
|
| 1119 |
+
raise
|
| 1120 |
+
print(f"HTTP error on attempt {attempt + 1}, retrying...")
|
| 1121 |
+
await asyncio.sleep(2)
|
| 1122 |
+
continue
|
| 1123 |
+
|
| 1124 |
+
if data is None:
|
| 1125 |
+
raise ValueError("No response from Claude after all attempts")
|
| 1126 |
+
|
| 1127 |
+
# Check stop reason for debugging
|
| 1128 |
+
stop_reason = data.get("stop_reason", "unknown")
|
| 1129 |
+
|
| 1130 |
+
# Extract all content blocks
|
| 1131 |
+
content_blocks = data.get("content", [])
|
| 1132 |
+
|
| 1133 |
+
# Extract text from content blocks (Claude returns multiple blocks with code execution)
|
| 1134 |
+
# Pattern: text → tool_use → tool_result → text → ... → text (final answer)
|
| 1135 |
+
text_blocks = [block.get("text", "") for block in content_blocks if block.get("type") == "text"]
|
| 1136 |
+
if not text_blocks:
|
| 1137 |
+
raise ValueError("No text blocks found in Claude response")
|
| 1138 |
+
|
| 1139 |
+
# Use the last text block which contains the final answer
|
| 1140 |
+
text = text_blocks[-1]
|
| 1141 |
+
|
| 1142 |
+
print(f"Claude {model} code execution response for {title}: {text[:200]}")
|
| 1143 |
+
|
| 1144 |
+
return extract_value(text, return_length)
|
| 1145 |
+
|
| 1146 |
+
except Exception as e:
|
| 1147 |
+
print(f"\n{'!'*80}")
|
| 1148 |
+
print(f"ERROR in Claude code execution for {model} on {title}: {type(e).__name__}: {e}")
|
| 1149 |
+
print(f"{'!'*80}")
|
| 1150 |
+
|
| 1151 |
+
# Print traceback for debugging
|
| 1152 |
+
import traceback
|
| 1153 |
+
print("\nTraceback:")
|
| 1154 |
+
traceback.print_exc()
|
| 1155 |
+
|
| 1156 |
+
# Print stop reason and usage (only if data exists)
|
| 1157 |
+
if 'data' in locals() and data is not None:
|
| 1158 |
+
try:
|
| 1159 |
+
stop_reason = data.get("stop_reason", "unknown")
|
| 1160 |
+
print(f"\nStop reason: {stop_reason}")
|
| 1161 |
+
usage = data.get("usage", {})
|
| 1162 |
+
if usage:
|
| 1163 |
+
print(f"Usage: {usage}")
|
| 1164 |
+
except:
|
| 1165 |
+
print("\nStop reason/usage: (error accessing)")
|
| 1166 |
+
else:
|
| 1167 |
+
print("\nStop reason/usage: (data not available - error before API response)")
|
| 1168 |
+
|
| 1169 |
+
# Print all text blocks in full (only if text_blocks exists)
|
| 1170 |
+
if 'text_blocks' in locals() and text_blocks:
|
| 1171 |
+
try:
|
| 1172 |
+
print(f"\nTotal text blocks: {len(text_blocks)}")
|
| 1173 |
+
for i, tb in enumerate(text_blocks):
|
| 1174 |
+
print(f"\n--- TEXT BLOCK {i+1} (full content) ---")
|
| 1175 |
+
print(tb)
|
| 1176 |
+
print(f"--- END TEXT BLOCK {i+1} ---")
|
| 1177 |
+
except:
|
| 1178 |
+
print("\nText blocks: (error printing)")
|
| 1179 |
+
else:
|
| 1180 |
+
print("\nText blocks: (not available - error before text extraction)")
|
| 1181 |
+
|
| 1182 |
+
print(f"{'!'*80}\n")
|
| 1183 |
+
raise
|
| 1184 |
+
|
| 1185 |
+
finally:
|
| 1186 |
+
# Decrement reference counts and cleanup cache if no longer needed
|
| 1187 |
+
async with _get_claude_upload_lock():
|
| 1188 |
+
for file_path in file_paths:
|
| 1189 |
+
if file_path in _claude_file_refs:
|
| 1190 |
+
_claude_file_refs[file_path] -= 1
|
| 1191 |
+
if _claude_file_refs[file_path] <= 0:
|
| 1192 |
+
# Delete from API and remove from cache
|
| 1193 |
+
file_id = _claude_file_cache.get(file_path)
|
| 1194 |
+
if file_id:
|
| 1195 |
+
await _delete_claude_file(file_id)
|
| 1196 |
+
_claude_file_refs.pop(file_path, None)
|
| 1197 |
+
_claude_file_cache.pop(file_path, None)
|
| 1198 |
+
|
| 1199 |
+
|
| 1200 |
+
# ------------------------ Qwen Agent Code Execution ------------------------
|
| 1201 |
+
|
| 1202 |
+
@register_code_execution("qwen3-235b-thinking")
|
| 1203 |
+
async def _qwen_agent_with_code_execution(
|
| 1204 |
+
model: str,
|
| 1205 |
+
prompt: str,
|
| 1206 |
+
file_paths: list,
|
| 1207 |
+
file_path_to_id: dict,
|
| 1208 |
+
title: str,
|
| 1209 |
+
return_length: int = 0
|
| 1210 |
+
) -> Any:
|
| 1211 |
+
"""
|
| 1212 |
+
Call Qwen model with code execution using Qwen Agent framework.
|
| 1213 |
+
Qwen Agent provides sandboxed Python environment with configurable packages.
|
| 1214 |
+
Uses local file paths directly (no pre-upload needed).
|
| 1215 |
+
"""
|
| 1216 |
+
from qwen_agent.agents import Assistant
|
| 1217 |
+
from qwen_agent.tools.code_interpreter import CodeInterpreter
|
| 1218 |
+
|
| 1219 |
+
async with qwen_semaphore:
|
| 1220 |
+
async with qwen_limiter:
|
| 1221 |
+
def _run_qwen_agent_sync():
|
| 1222 |
+
# Configure code interpreter
|
| 1223 |
+
# Note: CodeInterpreter doesn't accept extra_packages parameter in current version
|
| 1224 |
+
# It uses default Python environment with standard packages
|
| 1225 |
+
tools = [CodeInterpreter()]
|
| 1226 |
+
|
| 1227 |
+
# Map model name to OpenRouter model ID
|
| 1228 |
+
# Use the same mapping as call_openrouter function
|
| 1229 |
+
model_map = {
|
| 1230 |
+
"qwen3-235b-thinking": "qwen/qwen3-235b-a22b-thinking-2507"
|
| 1231 |
+
}
|
| 1232 |
+
agent_model = model_map.get(model, model)
|
| 1233 |
+
|
| 1234 |
+
# Create agent with code execution capability
|
| 1235 |
+
# Configure for OpenRouter (OpenAI-compatible API)
|
| 1236 |
+
agent = Assistant(
|
| 1237 |
+
llm={
|
| 1238 |
+
'model': agent_model,
|
| 1239 |
+
'model_server': 'https://openrouter.ai/api/v1', # OpenRouter endpoint
|
| 1240 |
+
'api_key': os.getenv('OPENROUTER_API_KEY')
|
| 1241 |
+
},
|
| 1242 |
+
function_list=tools
|
| 1243 |
+
)
|
| 1244 |
+
|
| 1245 |
+
# Build message
|
| 1246 |
+
# Include file paths in the prompt text instead of using 'file' type
|
| 1247 |
+
# to avoid triggering RAG features
|
| 1248 |
+
if file_paths:
|
| 1249 |
+
file_paths_text = '\n'.join([f"File: {fp}" for fp in file_paths])
|
| 1250 |
+
full_prompt = f"{file_paths_text}\n\n{prompt}"
|
| 1251 |
+
else:
|
| 1252 |
+
full_prompt = prompt
|
| 1253 |
+
|
| 1254 |
+
messages = [{'role': 'user', 'content': full_prompt}]
|
| 1255 |
+
|
| 1256 |
+
# Run agent and collect response
|
| 1257 |
+
responses = []
|
| 1258 |
+
for response in agent.run(messages):
|
| 1259 |
+
responses.append(response)
|
| 1260 |
+
|
| 1261 |
+
# Extract final text response
|
| 1262 |
+
if responses:
|
| 1263 |
+
final_response = responses[-1]
|
| 1264 |
+
if isinstance(final_response, list):
|
| 1265 |
+
text = final_response[-1].get('content', '')
|
| 1266 |
+
else:
|
| 1267 |
+
text = final_response.get('content', '')
|
| 1268 |
+
else:
|
| 1269 |
+
raise ValueError("No response from Qwen Agent")
|
| 1270 |
+
|
| 1271 |
+
return text
|
| 1272 |
+
|
| 1273 |
+
try:
|
| 1274 |
+
# Run synchronous Qwen Agent in thread pool
|
| 1275 |
+
text = await asyncio.to_thread(_run_qwen_agent_sync)
|
| 1276 |
+
|
| 1277 |
+
print(f"Qwen Agent {model} code execution response for {title}: {text[:200]}")
|
| 1278 |
+
|
| 1279 |
+
return extract_value(text, return_length)
|
| 1280 |
+
|
| 1281 |
+
except Exception as e:
|
| 1282 |
+
print(f"Error in Qwen Agent code execution for {model} on {title}: {e}")
|
| 1283 |
+
raise
|
| 1284 |
+
|
| 1285 |
+
|
| 1286 |
+
async def ask_model_with_code_interpreter(
|
| 1287 |
+
model: str,
|
| 1288 |
+
prompt: str,
|
| 1289 |
+
file_paths: list,
|
| 1290 |
+
file_path_to_id: dict,
|
| 1291 |
+
title: str,
|
| 1292 |
+
return_length: int = 0
|
| 1293 |
+
) -> Any:
|
| 1294 |
+
"""
|
| 1295 |
+
Unified interface for code execution across all models.
|
| 1296 |
+
Routes to appropriate provider-specific implementation using registry.
|
| 1297 |
+
|
| 1298 |
+
Args:
|
| 1299 |
+
model: Model name
|
| 1300 |
+
prompt: The question/prompt
|
| 1301 |
+
file_paths: List of local file paths needed for this question
|
| 1302 |
+
file_path_to_id: Dict mapping file paths to file IDs (provider-specific)
|
| 1303 |
+
title: Question title for logging
|
| 1304 |
+
return_length: Expected return length (0 for scalar, >0 for array)
|
| 1305 |
+
"""
|
| 1306 |
+
handler = _CODE_EXECUTION_REGISTRY.get(model)
|
| 1307 |
+
if not handler:
|
| 1308 |
+
# Default to OpenAI code interpreter for unknown models
|
| 1309 |
+
handler = _openai_with_code_interpreter
|
| 1310 |
+
|
| 1311 |
+
return await handler(model, prompt, file_paths, file_path_to_id, title, return_length)
|
server/serve.py
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from tqdm import tqdm
|
| 3 |
+
from query import ask_model, ask_model_with_code_interpreter # now an async function
|
| 4 |
+
from db import append_result, safe_key
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
import aiofiles
|
| 8 |
+
import httpx
|
| 9 |
+
|
| 10 |
+
async def run_model_prompt_queries(
|
| 11 |
+
models: list,
|
| 12 |
+
prompts: list,
|
| 13 |
+
titles: list,
|
| 14 |
+
num_elems: list,
|
| 15 |
+
num_repeats,
|
| 16 |
+
progress_cb = None,
|
| 17 |
+
error_cb = None,
|
| 18 |
+
max_workers: int = 100000,
|
| 19 |
+
):
|
| 20 |
+
prompt_to_title = dict(zip(prompts, titles))
|
| 21 |
+
cache = {}
|
| 22 |
+
|
| 23 |
+
# semaphore to cap concurrent ask_model calls
|
| 24 |
+
sem = asyncio.Semaphore(max_workers)
|
| 25 |
+
|
| 26 |
+
async def bound_ask(model, prompt, num_elem, title):
|
| 27 |
+
async with sem:
|
| 28 |
+
try:
|
| 29 |
+
out = await ask_model(model, prompt, cache, title, num_elem)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Error for model={model}, {e}, {title}")
|
| 32 |
+
return None, model, prompt
|
| 33 |
+
print(f"Model: {model}, out: {out}, title: {title}")
|
| 34 |
+
return out, model, prompt
|
| 35 |
+
|
| 36 |
+
# prepare all (model, prompt, num_elem) triples
|
| 37 |
+
if isinstance(num_repeats, list):
|
| 38 |
+
all_tasks = [
|
| 39 |
+
(m, p, n, t)
|
| 40 |
+
for j, (p, n, t) in enumerate(zip(prompts, num_elems, titles))
|
| 41 |
+
for i, m in enumerate(models)
|
| 42 |
+
for _ in range(num_repeats[i][j])
|
| 43 |
+
]
|
| 44 |
+
else:
|
| 45 |
+
all_tasks = [
|
| 46 |
+
(m, p, n, t)
|
| 47 |
+
for p, n, t in zip(prompts, num_elems, titles)
|
| 48 |
+
for _ in range(num_repeats)
|
| 49 |
+
for m in models
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
tasks = []
|
| 53 |
+
for idx, (model, prompt, num_elem, title) in enumerate(all_tasks):
|
| 54 |
+
# buffer initial semaphore load
|
| 55 |
+
tasks.append(asyncio.create_task(bound_ask(model, prompt, num_elem, title)))
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
for fut in tqdm(asyncio.as_completed(tasks), total=len(tasks), desc="Processing"):
|
| 59 |
+
output, model, prompt = await fut
|
| 60 |
+
if output:
|
| 61 |
+
title = prompt_to_title[prompt]
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
# Firebase
|
| 65 |
+
append_result(title, model, output)
|
| 66 |
+
|
| 67 |
+
# Local disk
|
| 68 |
+
save_dir = "./outputs"
|
| 69 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 70 |
+
filename = f"{title}-{safe_key(model)}.jsonl"
|
| 71 |
+
filepath = os.path.join(save_dir, filename)
|
| 72 |
+
|
| 73 |
+
async with aiofiles.open(filepath, mode="a") as f:
|
| 74 |
+
await f.write(json.dumps(output) + "\n")
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Error saving output for {title} - {model}: {e}")
|
| 78 |
+
if progress_cb:
|
| 79 |
+
await progress_cb()
|
| 80 |
+
else:
|
| 81 |
+
if error_cb:
|
| 82 |
+
await error_cb()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
return cache
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def resolve_file_path(file_path: str) -> str:
|
| 89 |
+
"""
|
| 90 |
+
Resolve file paths to absolute paths.
|
| 91 |
+
Relative paths are placed under server_dir/Data_csv/Plots/
|
| 92 |
+
|
| 93 |
+
Args:
|
| 94 |
+
file_path: Relative (e.g., "A1/file.csv") or absolute file path
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
Absolute file path
|
| 98 |
+
"""
|
| 99 |
+
# If already absolute, return as-is
|
| 100 |
+
if os.path.isabs(file_path):
|
| 101 |
+
return file_path
|
| 102 |
+
|
| 103 |
+
# Get the directory where serve.py is located (server/)
|
| 104 |
+
server_dir = os.path.dirname(os.path.abspath(__file__))
|
| 105 |
+
|
| 106 |
+
# Prepend Data_csv/Plots/ to relative paths, then make absolute
|
| 107 |
+
return os.path.join(server_dir, "Data_csv", "Plots", file_path)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# Define model sets for checking which APIs need file uploads
|
| 111 |
+
GEMINI_MODELS = {
|
| 112 |
+
"gemini-2.0-flash",
|
| 113 |
+
"gemini-2.0-flash-lite-preview-02-05",
|
| 114 |
+
"gemini-3-pro-preview",
|
| 115 |
+
"gemini-3-flash-preview",
|
| 116 |
+
"gemini-2.5-pro",
|
| 117 |
+
"gemini-2.5-flash",
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
OPENAI_CODE_INTERPRETER_MODELS = {
|
| 121 |
+
"gpt-5",
|
| 122 |
+
"o3-low",
|
| 123 |
+
"o3-medium",
|
| 124 |
+
"o3-high",
|
| 125 |
+
"o4-mini-low",
|
| 126 |
+
"o4-mini-medium",
|
| 127 |
+
"o4-mini-high",
|
| 128 |
+
"gpt-4o",
|
| 129 |
+
"gpt-4o-mini",
|
| 130 |
+
"gpt-4-turbo"
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
QWEN_AGENT_MODELS = {
|
| 134 |
+
"qwen3-235b-thinking",
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
CLAUDE_CODE_EXECUTION_MODELS = {
|
| 138 |
+
"claude-3-7-sonnet-20250219",
|
| 139 |
+
"claude-opus-4-5",
|
| 140 |
+
"claude-sonnet-4-5",
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
async def run_model_prompt_queries_with_code_interpreter(
|
| 145 |
+
models: list,
|
| 146 |
+
prompts: list,
|
| 147 |
+
titles: list,
|
| 148 |
+
num_elems: list,
|
| 149 |
+
file_paths_per_question: list, # Array of arrays - one file list per question
|
| 150 |
+
num_repeats,
|
| 151 |
+
progress_cb = None,
|
| 152 |
+
error_cb = None,
|
| 153 |
+
max_workers: int = 100000,
|
| 154 |
+
):
|
| 155 |
+
"""
|
| 156 |
+
Run queries with code interpreter enabled and file access.
|
| 157 |
+
Files are uploaded once at the beginning and reused across all attempts.
|
| 158 |
+
Each attempt still gets a fresh container (auto mode ensures isolation).
|
| 159 |
+
|
| 160 |
+
Args:
|
| 161 |
+
file_paths_per_question: List where each element is a list of file paths for that question
|
| 162 |
+
Paths can be:
|
| 163 |
+
- Absolute: /full/path/to/file.csv
|
| 164 |
+
- Relative to server directory: Data_csv/Plots/A1/file.csv
|
| 165 |
+
"""
|
| 166 |
+
from openai import AsyncOpenAI
|
| 167 |
+
from google import genai
|
| 168 |
+
|
| 169 |
+
prompt_to_title = dict(zip(prompts, titles))
|
| 170 |
+
prompt_to_files = dict(zip(prompts, file_paths_per_question))
|
| 171 |
+
|
| 172 |
+
# Step 1: Collect all unique file paths across all questions and resolve them
|
| 173 |
+
all_file_paths = set()
|
| 174 |
+
for file_list in file_paths_per_question:
|
| 175 |
+
for file_path in file_list:
|
| 176 |
+
resolved_path = resolve_file_path(file_path)
|
| 177 |
+
all_file_paths.add(resolved_path)
|
| 178 |
+
|
| 179 |
+
# Check if files exist
|
| 180 |
+
for file_path in all_file_paths:
|
| 181 |
+
if not os.path.exists(file_path):
|
| 182 |
+
print(f"✗ File does not exist: {file_path}")
|
| 183 |
+
raise FileNotFoundError(f"File not found: {file_path}")
|
| 184 |
+
|
| 185 |
+
# Step 2: Determine which APIs need file uploads
|
| 186 |
+
has_gemini_models = any(model in GEMINI_MODELS for model in models)
|
| 187 |
+
has_openai_models = any(model in OPENAI_CODE_INTERPRETER_MODELS for model in models)
|
| 188 |
+
has_qwen_models = any(model in QWEN_AGENT_MODELS for model in models)
|
| 189 |
+
has_claude_models = any(model in CLAUDE_CODE_EXECUTION_MODELS for model in models)
|
| 190 |
+
|
| 191 |
+
print(f"Models requiring file uploads: Gemini={has_gemini_models}, OpenAI={has_openai_models}, Qwen={has_qwen_models}, Claude={has_claude_models}")
|
| 192 |
+
|
| 193 |
+
# Create nested dictionary structure
|
| 194 |
+
file_path_to_id = {}
|
| 195 |
+
|
| 196 |
+
# Step 3: Upload files to OpenAI if needed
|
| 197 |
+
if has_openai_models:
|
| 198 |
+
print("\n" + "="*60)
|
| 199 |
+
print("Uploading files to OpenAI...")
|
| 200 |
+
print("="*60)
|
| 201 |
+
|
| 202 |
+
client = AsyncOpenAI(
|
| 203 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
| 204 |
+
timeout=14400 # 4 hours in seconds
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# List existing uploaded files to avoid re-uploading
|
| 208 |
+
try:
|
| 209 |
+
existing_files_response = await client.files.list(purpose="user_data", limit=10000)
|
| 210 |
+
existing_files = existing_files_response.data
|
| 211 |
+
existing_file_map = {
|
| 212 |
+
(f.filename, f.bytes): f.id
|
| 213 |
+
for f in existing_files
|
| 214 |
+
}
|
| 215 |
+
print(f"Found {len(existing_file_map)} existing OpenAI files")
|
| 216 |
+
except Exception as e:
|
| 217 |
+
print(f"Warning: Could not list existing files: {e}")
|
| 218 |
+
existing_file_map = {}
|
| 219 |
+
|
| 220 |
+
openai_file_map = {}
|
| 221 |
+
files_to_upload = []
|
| 222 |
+
|
| 223 |
+
for file_path in all_file_paths:
|
| 224 |
+
file_size = os.path.getsize(file_path)
|
| 225 |
+
file_basename = os.path.basename(file_path)
|
| 226 |
+
file_key = (file_basename, file_size)
|
| 227 |
+
|
| 228 |
+
if file_key in existing_file_map:
|
| 229 |
+
openai_file_map[file_path] = existing_file_map[file_key]
|
| 230 |
+
else:
|
| 231 |
+
files_to_upload.append(file_path)
|
| 232 |
+
|
| 233 |
+
print(f"Found {len(openai_file_map)} existing, need to upload {len(files_to_upload)} new files")
|
| 234 |
+
|
| 235 |
+
if files_to_upload:
|
| 236 |
+
FILE_UPLOAD_TIMEOUT = 600
|
| 237 |
+
MAX_CONCURRENT_UPLOADS = 40
|
| 238 |
+
upload_semaphore = asyncio.Semaphore(MAX_CONCURRENT_UPLOADS)
|
| 239 |
+
|
| 240 |
+
async def upload_openai_file(file_path: str):
|
| 241 |
+
async with upload_semaphore:
|
| 242 |
+
try:
|
| 243 |
+
file_basename = os.path.basename(file_path)
|
| 244 |
+
async with aiofiles.open(file_path, 'rb') as f:
|
| 245 |
+
file_content = await f.read()
|
| 246 |
+
|
| 247 |
+
from io import BytesIO
|
| 248 |
+
file_obj = await asyncio.wait_for(
|
| 249 |
+
client.files.create(
|
| 250 |
+
file=(file_basename, BytesIO(file_content)),
|
| 251 |
+
purpose="user_data"
|
| 252 |
+
),
|
| 253 |
+
timeout=FILE_UPLOAD_TIMEOUT
|
| 254 |
+
)
|
| 255 |
+
return file_path, file_obj.id
|
| 256 |
+
except Exception as e:
|
| 257 |
+
print(f"✗ Error uploading {file_path} to OpenAI: {e}")
|
| 258 |
+
raise
|
| 259 |
+
|
| 260 |
+
upload_tasks = [upload_openai_file(fp) for fp in files_to_upload]
|
| 261 |
+
upload_results = await asyncio.gather(*upload_tasks)
|
| 262 |
+
|
| 263 |
+
for file_path, file_id in upload_results:
|
| 264 |
+
openai_file_map[file_path] = file_id
|
| 265 |
+
|
| 266 |
+
file_path_to_id["openai"] = openai_file_map
|
| 267 |
+
print(f"✓ All {len(openai_file_map)} OpenAI files ready")
|
| 268 |
+
|
| 269 |
+
# Step 4: Upload files to Gemini if needed
|
| 270 |
+
if has_gemini_models:
|
| 271 |
+
print("\n" + "="*60)
|
| 272 |
+
print("Uploading files to Gemini...")
|
| 273 |
+
print("="*60)
|
| 274 |
+
|
| 275 |
+
gemini_client = genai.Client()
|
| 276 |
+
|
| 277 |
+
# List existing Gemini files
|
| 278 |
+
try:
|
| 279 |
+
existing_gemini_files = list(gemini_client.files.list())
|
| 280 |
+
existing_gemini_map = {
|
| 281 |
+
f.display_name: f for f in existing_gemini_files
|
| 282 |
+
}
|
| 283 |
+
print(f"Found {len(existing_gemini_map)} existing Gemini files")
|
| 284 |
+
except Exception as e:
|
| 285 |
+
print(f"Warning: Could not list existing Gemini files: {e}")
|
| 286 |
+
existing_gemini_map = {}
|
| 287 |
+
|
| 288 |
+
gemini_file_map = {}
|
| 289 |
+
|
| 290 |
+
for file_path in all_file_paths:
|
| 291 |
+
file_basename = os.path.basename(file_path)
|
| 292 |
+
|
| 293 |
+
# Check if file already exists in Gemini
|
| 294 |
+
if file_basename in existing_gemini_map:
|
| 295 |
+
gemini_file_map[file_path] = existing_gemini_map[file_basename]
|
| 296 |
+
print(f" ✓ Using existing Gemini file: {file_basename}")
|
| 297 |
+
else:
|
| 298 |
+
try:
|
| 299 |
+
# Upload new file to Gemini
|
| 300 |
+
uploaded_file = gemini_client.files.upload(file=file_path)
|
| 301 |
+
gemini_file_map[file_path] = uploaded_file
|
| 302 |
+
print(f" ✓ Uploaded to Gemini: {file_basename}")
|
| 303 |
+
except Exception as e:
|
| 304 |
+
print(f" ✗ Error uploading {file_basename} to Gemini: {e}")
|
| 305 |
+
raise
|
| 306 |
+
|
| 307 |
+
file_path_to_id["gemini"] = gemini_file_map
|
| 308 |
+
print(f"✓ All {len(gemini_file_map)} Gemini files ready")
|
| 309 |
+
|
| 310 |
+
# Step 5: Upload files to Claude if needed
|
| 311 |
+
if has_claude_models:
|
| 312 |
+
print("\n" + "="*60)
|
| 313 |
+
print("Uploading files to Claude (Anthropic Files API)...")
|
| 314 |
+
print("="*60)
|
| 315 |
+
|
| 316 |
+
headers = {
|
| 317 |
+
"x-api-key": os.getenv("ANTHROPIC_API_KEY"),
|
| 318 |
+
"anthropic-version": "2023-06-01",
|
| 319 |
+
"anthropic-beta": "files-api-2025-04-14"
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
claude_file_map = {}
|
| 323 |
+
|
| 324 |
+
async def upload_claude_file(file_path: str):
|
| 325 |
+
try:
|
| 326 |
+
file_basename = os.path.basename(file_path)
|
| 327 |
+
async with aiofiles.open(file_path, 'rb') as f:
|
| 328 |
+
file_content = await f.read()
|
| 329 |
+
|
| 330 |
+
# Use httpx for file upload
|
| 331 |
+
async with httpx.AsyncClient(timeout=600) as client:
|
| 332 |
+
files = {'file': (file_basename, file_content)}
|
| 333 |
+
resp = await client.post(
|
| 334 |
+
"https://api.anthropic.com/v1/files",
|
| 335 |
+
headers=headers,
|
| 336 |
+
files=files
|
| 337 |
+
)
|
| 338 |
+
resp.raise_for_status()
|
| 339 |
+
file_data = resp.json()
|
| 340 |
+
return file_path, file_data['id']
|
| 341 |
+
except Exception as e:
|
| 342 |
+
print(f"✗ Error uploading {file_basename} to Claude: {e}")
|
| 343 |
+
raise
|
| 344 |
+
|
| 345 |
+
upload_tasks = [upload_claude_file(fp) for fp in all_file_paths]
|
| 346 |
+
upload_results = await asyncio.gather(*upload_tasks)
|
| 347 |
+
|
| 348 |
+
for file_path, file_id in upload_results:
|
| 349 |
+
claude_file_map[file_path] = file_id
|
| 350 |
+
print(f" ✓ Uploaded to Claude: {os.path.basename(file_path)}")
|
| 351 |
+
|
| 352 |
+
file_path_to_id["claude"] = claude_file_map
|
| 353 |
+
print(f"✓ All {len(claude_file_map)} Claude files ready")
|
| 354 |
+
|
| 355 |
+
# Step 6: Handle Qwen Agent (uses local file paths, no upload needed)
|
| 356 |
+
if has_qwen_models:
|
| 357 |
+
print("\n" + "="*60)
|
| 358 |
+
print("Qwen Agent will use local file paths (no upload needed)")
|
| 359 |
+
print("="*60)
|
| 360 |
+
# Qwen Agent uses local file paths directly, so just map paths to themselves
|
| 361 |
+
qwen_file_map = {file_path: file_path for file_path in all_file_paths}
|
| 362 |
+
file_path_to_id["qwen"] = qwen_file_map
|
| 363 |
+
print(f"✓ {len(qwen_file_map)} files ready for Qwen Agent")
|
| 364 |
+
|
| 365 |
+
# Step 7: Run queries with pre-uploaded files
|
| 366 |
+
# semaphore to cap concurrent ask_model calls
|
| 367 |
+
sem = asyncio.Semaphore(max_workers)
|
| 368 |
+
|
| 369 |
+
async def bound_ask(model, prompt, num_elem, title, file_paths):
|
| 370 |
+
async with sem:
|
| 371 |
+
try:
|
| 372 |
+
# Resolve file paths before passing to ask_model_with_code_interpreter
|
| 373 |
+
resolved_file_paths = [resolve_file_path(fp) for fp in file_paths]
|
| 374 |
+
out = await ask_model_with_code_interpreter(
|
| 375 |
+
model, prompt, resolved_file_paths, file_path_to_id, title, num_elem
|
| 376 |
+
)
|
| 377 |
+
except Exception as e:
|
| 378 |
+
print(f"Error for model={model}, {e}, {title}")
|
| 379 |
+
return None, model, prompt
|
| 380 |
+
print(f"Model: {model}, out: {out}, title: {title}")
|
| 381 |
+
return out, model, prompt
|
| 382 |
+
|
| 383 |
+
# prepare all (model, prompt, num_elem, file_paths) tuples
|
| 384 |
+
if isinstance(num_repeats, list):
|
| 385 |
+
all_tasks = [
|
| 386 |
+
(m, p, n, t, prompt_to_files[p])
|
| 387 |
+
for j, (p, n, t) in enumerate(zip(prompts, num_elems, titles))
|
| 388 |
+
for i, m in enumerate(models)
|
| 389 |
+
for _ in range(num_repeats[i][j])
|
| 390 |
+
]
|
| 391 |
+
else:
|
| 392 |
+
all_tasks = [
|
| 393 |
+
(m, p, n, t, prompt_to_files[p])
|
| 394 |
+
for p, n, t in zip(prompts, num_elems, titles)
|
| 395 |
+
for _ in range(num_repeats)
|
| 396 |
+
for m in models
|
| 397 |
+
]
|
| 398 |
+
|
| 399 |
+
tasks = []
|
| 400 |
+
for idx, (model, prompt, num_elem, title, file_paths) in enumerate(all_tasks):
|
| 401 |
+
tasks.append(asyncio.create_task(bound_ask(model, prompt, num_elem, title, file_paths)))
|
| 402 |
+
|
| 403 |
+
for fut in tqdm(asyncio.as_completed(tasks), total=len(tasks), desc="Processing with Code Interpreter"):
|
| 404 |
+
output, model, prompt = await fut
|
| 405 |
+
if output:
|
| 406 |
+
title = prompt_to_title[prompt]
|
| 407 |
+
|
| 408 |
+
try:
|
| 409 |
+
# Firebase
|
| 410 |
+
append_result(title, model, output)
|
| 411 |
+
|
| 412 |
+
# Local disk
|
| 413 |
+
save_dir = "./outputs"
|
| 414 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 415 |
+
filename = f"{title}-{safe_key(model)}.jsonl"
|
| 416 |
+
filepath = os.path.join(save_dir, filename)
|
| 417 |
+
|
| 418 |
+
async with aiofiles.open(filepath, mode="a") as f:
|
| 419 |
+
await f.write(json.dumps(output) + "\n")
|
| 420 |
+
|
| 421 |
+
except Exception as e:
|
| 422 |
+
print(f"Error saving output for {title} - {model}: {e}")
|
| 423 |
+
if progress_cb:
|
| 424 |
+
await progress_cb()
|
| 425 |
+
else:
|
| 426 |
+
if error_cb:
|
| 427 |
+
await error_cb()
|
| 428 |
+
|
| 429 |
+
# Files are kept in OpenAI storage for future reuse
|
| 430 |
+
print(f"✓ Completed all queries. {len(file_path_to_id)} files remain in storage for future use.")
|
server/setup_ngrok.sh
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Set install dir
|
| 4 |
+
INSTALL_DIR="$HOME/bin"
|
| 5 |
+
|
| 6 |
+
# Download ngrok v3
|
| 7 |
+
echo "Downloading ngrok v3..."
|
| 8 |
+
wget -q https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz -O ngrok.tgz
|
| 9 |
+
|
| 10 |
+
# Unpack and move to ~/bin
|
| 11 |
+
echo "Installing to $INSTALL_DIR..."
|
| 12 |
+
mkdir -p "$INSTALL_DIR"
|
| 13 |
+
tar -xzf ngrok.tgz || { echo "❌ Failed to extract ngrok.tgz"; exit 1; }
|
| 14 |
+
|
| 15 |
+
mv ngrok "$INSTALL_DIR/ngrok"
|
| 16 |
+
rm ngrok.tgz
|
| 17 |
+
|
| 18 |
+
# Add to PATH if not already there
|
| 19 |
+
if ! grep -q 'export PATH="$HOME/bin:$PATH"' "$HOME/.bashrc"; then
|
| 20 |
+
echo 'export PATH="$HOME/bin:$PATH"' >> "$HOME/.bashrc"
|
| 21 |
+
echo "Added ngrok to PATH in .bashrc"
|
| 22 |
+
fi
|
| 23 |
+
|
| 24 |
+
# Source the new path for this session
|
| 25 |
+
export PATH="$HOME/bin:$PATH"
|
| 26 |
+
|
| 27 |
+
# Verify install
|
| 28 |
+
echo "Installed ngrok version:"
|
| 29 |
+
ngrok version
|
server/test.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from serve import run_model_prompt_queries
|
| 2 |
+
import random, numpy as np
|
| 3 |
+
from db import get_all_results, reset_db, save_result
|
| 4 |
+
import asyncio
|
| 5 |
+
|
| 6 |
+
def array2string(array):
|
| 7 |
+
return '\n'.join(['\t'.join(map(str, row)) for row in array])
|
| 8 |
+
|
| 9 |
+
E_pnts = 51
|
| 10 |
+
k_pnts = 101
|
| 11 |
+
|
| 12 |
+
E_min_0 = 2; E_max_0 = 3
|
| 13 |
+
k_min_0 = -1; k_max_0 = 1
|
| 14 |
+
tau_0 = 3
|
| 15 |
+
noise_0 = 0.3
|
| 16 |
+
|
| 17 |
+
E_min_1 = 5; E_max_1 = 7
|
| 18 |
+
k_min_1 = -1; k_max_1 = 1
|
| 19 |
+
tau_1 = 1
|
| 20 |
+
noise_1 = 0.2
|
| 21 |
+
|
| 22 |
+
dE_0 = (E_max_0 - E_min_0)/(E_pnts - 1)
|
| 23 |
+
dk_0 = (k_max_0 - k_min_0)/(k_pnts - 1)
|
| 24 |
+
Ex_0 = 2.5
|
| 25 |
+
|
| 26 |
+
dE_1 = (E_max_1 - E_min_1)/(E_pnts - 1)
|
| 27 |
+
dk_1 = (k_max_1 - k_min_1)/(k_pnts - 1)
|
| 28 |
+
Ex_1 = 5.0
|
| 29 |
+
|
| 30 |
+
E_array_0 = np.linspace(E_min_0, E_max_0, E_pnts, endpoint = True)
|
| 31 |
+
k_array_0 = np.linspace(k_min_0, k_max_0, k_pnts, endpoint = True)
|
| 32 |
+
|
| 33 |
+
E_array_1 = np.linspace(E_min_1, E_max_1, E_pnts, endpoint = True)
|
| 34 |
+
k_array_1 = np.linspace(k_min_1, k_max_1, k_pnts, endpoint = True)
|
| 35 |
+
|
| 36 |
+
spectrum_array_0 = np.zeros((E_pnts, k_pnts))
|
| 37 |
+
spectrum_array_1 = np.zeros((E_pnts, k_pnts))
|
| 38 |
+
|
| 39 |
+
def Ek_0(k, Ex):
|
| 40 |
+
E = k + Ex
|
| 41 |
+
return E
|
| 42 |
+
|
| 43 |
+
def Ek_1(k, Ex):
|
| 44 |
+
E = 3*k + Ex
|
| 45 |
+
return E
|
| 46 |
+
|
| 47 |
+
def Ak_0(E, k, tau, Ex):
|
| 48 |
+
A = 1/(1 + ((E - Ek_0(k, Ex))**2)*((2*tau)**2))
|
| 49 |
+
return A
|
| 50 |
+
|
| 51 |
+
def Ak_1(E, k, tau, Ex):
|
| 52 |
+
A = 1/(1 + ((E - Ek_1(k, Ex))**2)*((2*tau)**2))
|
| 53 |
+
return A
|
| 54 |
+
|
| 55 |
+
k_dispersion_array_0 = np.zeros(E_pnts)
|
| 56 |
+
k_dispersion_array_1 = np.zeros(E_pnts)
|
| 57 |
+
k_trace_array_1 = np.zeros(E_pnts)
|
| 58 |
+
|
| 59 |
+
for num in range(E_pnts):
|
| 60 |
+
k_dispersion_array_0[num] = E_array_0[num] - Ex_0
|
| 61 |
+
k_dispersion_array_1[num] = (E_array_1[num] - Ex_1)/3
|
| 62 |
+
|
| 63 |
+
for E_num in range(E_pnts):
|
| 64 |
+
k_trace_array_1[E_num] = (E_array_1[E_num] - Ex_1)/3
|
| 65 |
+
for k_num in range(k_pnts):
|
| 66 |
+
spectrum_array_0[E_num,k_num] = Ak_0(E_array_0[E_num], k_array_0[k_num], tau_0, Ex_0) + random.random()*noise_0
|
| 67 |
+
spectrum_array_1[E_num,k_num] = Ak_1(E_array_1[E_num], k_array_1[k_num], tau_1, Ex_1) + random.random()*noise_1
|
| 68 |
+
|
| 69 |
+
temp_0 = np.array(E_array_0)[:, np.newaxis]
|
| 70 |
+
temp_1 = np.array(E_array_1)[:, np.newaxis]
|
| 71 |
+
|
| 72 |
+
data_0 = array2string(np.concatenate((np.round(temp_0, decimals = 3), np.round(spectrum_array_0, decimals = 3)), axis = 1))
|
| 73 |
+
data_1 = array2string(np.concatenate((np.round(temp_1, decimals = 3), np.round(spectrum_array_1, decimals = 3)), axis = 1))
|
| 74 |
+
|
| 75 |
+
content = "Dataset A:\nEnergy (eV) \t Intensity \n" + "Spectrum:\t" + '\t'.join(map(str, k_array_1)) + "\n" + data_1 + "Dataset B:\nEnergy (eV) \t Intensity \n" + "Spectrum:\t" + '\t'.join(map(str, k_array_0)) + "\n" + data_0
|
| 76 |
+
|
| 77 |
+
prompt = 'You are given two ARPES datasets in the form of tables.' \
|
| 78 |
+
'Their bandstructure is the set of momenta corresponding to the maximum spectral intensity at each energy. Here is an example. '\
|
| 79 |
+
'Read Dataset A. Its bandstructure is given by the array: [' + ','.join(str(x) for x in k_trace_array_1) + f']. '\
|
| 80 |
+
'Now read Dataset B. What are the momenta of the bandstructure for each energy? '\
|
| 81 |
+
'Do not print any files read, or code written. Return only an array of {E_pnts} numbers}, with no text.'
|
| 82 |
+
|
| 83 |
+
message = [f"{content}\n\n{prompt}"]
|
| 84 |
+
num_elems = [E_pnts]
|
| 85 |
+
message = [f"return [2 for _ in range(3)] as array of three numbers.]"]
|
| 86 |
+
num_elems = [3]
|
| 87 |
+
|
| 88 |
+
title = ["test"]
|
| 89 |
+
|
| 90 |
+
models = [
|
| 91 |
+
# "o1-low",
|
| 92 |
+
# "o1-medium",
|
| 93 |
+
# "o1-high",
|
| 94 |
+
# "o3-low",
|
| 95 |
+
# "o3-medium",
|
| 96 |
+
# "o3-high",
|
| 97 |
+
# "o3-mini-low",
|
| 98 |
+
# "o3-mini-medium",
|
| 99 |
+
# "o3-mini-high",
|
| 100 |
+
# "o4-mini-low",
|
| 101 |
+
# "o4-mini-medium",
|
| 102 |
+
# "o4-mini-high",
|
| 103 |
+
# "o1-mini",
|
| 104 |
+
# "deepseek-reasoner",
|
| 105 |
+
# "gemini-2.0-flash",
|
| 106 |
+
# "gemini-1.5-flash",
|
| 107 |
+
# "gemini-2.5-pro-exp-03-25",
|
| 108 |
+
# "gemini-2.0-flash-lite-preview-02-05",
|
| 109 |
+
# "claude-3-7-sonnet-20250219"
|
| 110 |
+
]
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# num_repeats = 1
|
| 114 |
+
|
| 115 |
+
# cache = asyncio.run(
|
| 116 |
+
# run_model_prompt_queries(
|
| 117 |
+
# models=models,
|
| 118 |
+
# prompts=message,
|
| 119 |
+
# titles=title,
|
| 120 |
+
# num_elems=num_elems,
|
| 121 |
+
# num_repeats=[[1]]
|
| 122 |
+
# )
|
| 123 |
+
# )
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
# cache = asyncio.run(
|
| 127 |
+
# run_model_prompt_queries(
|
| 128 |
+
# models=models,
|
| 129 |
+
# prompts=message,
|
| 130 |
+
# titles=titles,
|
| 131 |
+
# num_elems=num_elems,
|
| 132 |
+
# num_repeats=1
|
| 133 |
+
# )
|
| 134 |
+
# )
|
| 135 |
+
# reset_db()
|
| 136 |
+
# now we know it’s done:
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# print(get_all_results("test", models))
|
| 140 |
+
# for model in models:
|
| 141 |
+
# get_results("ARPES Bandstructure", model, save_file="model.jsonl")
|
| 142 |
+
|
| 143 |
+
|