-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathevaluation.py
More file actions
1466 lines (1299 loc) · 64.7 KB
/
evaluation.py
File metadata and controls
1466 lines (1299 loc) · 64.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ast # Added for AST parsing
import importlib.util # Added for dynamic module loading
import json
import logging
import os
import sys # Added for path manipulation
import time
import types
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, cast
if TYPE_CHECKING:
# For type checking only
import datasets
import requests
from eval_protocol.auth import (
get_fireworks_account_id,
get_fireworks_api_key,
verify_api_key_and_get_account_id,
)
from eval_protocol.typed_interface import EvaluationMode
from eval_protocol.get_pep440_version import get_pep440_version
logger = logging.getLogger(__name__)
# Flag to track if the preview API was successfully used
used_preview_api = False
def huggingface_dataset_to_jsonl(
dataset_name: str,
split: str = "train",
output_file: Optional[str] = None,
max_samples: int = 100,
message_key_map: Optional[Dict[str, str]] = None,
response_key: str = "response",
prompt_key: str = "prompt",
) -> str:
"""
Converts a HuggingFace dataset to JSONL format suitable for Eval Protocol evaluation.
Args:
dataset_name: The name of the HuggingFace dataset (e.g., "deepseek-ai/DeepSeek-ProverBench")
split: The dataset split to use (default: "train")
output_file: Optional file path to save the JSONL output (if None, generates a temp file)
max_samples: Maximum number of samples to include
message_key_map: Optional mapping of dataset keys to Eval Protocol message keys
response_key: Key in the dataset containing the response text (default: "response")
prompt_key: Key in the dataset containing the prompt text (default: "prompt")
Returns:
Path to the generated JSONL file
"""
try:
from datasets import load_dataset # pyright: ignore[reportAttributeAccessIssue]
except ImportError:
raise ImportError(
"The 'datasets' package is required to use this function. "
"Please install it with 'pip install \"eval-protocol[deepseek]\"'"
)
import tempfile
logger.info(f"Loading dataset {dataset_name} (split: {split})")
dataset = load_dataset(dataset_name, split=split)
if not output_file:
temp_dir = tempfile.gettempdir()
dataset_basename = dataset_name.split("/")[-1]
output_file = os.path.join(temp_dir, f"{dataset_basename}_{split}_{int(time.time())}.jsonl")
os.makedirs(os.path.dirname(os.path.abspath(output_file)), exist_ok=True)
if message_key_map is None:
message_key_map = {}
processed_samples = 0
# Initialize i to handle empty dataset case for logging
i = -1
with open(output_file, "w") as f:
for i, item in enumerate(dataset):
if processed_samples >= max_samples:
break
if prompt_key not in item and "statement" not in item:
logger.debug(f"Skipping sample {i} due to missing prompt/statement key.")
continue
prompt_text = item.get(prompt_key, item.get("statement", ""))
response_text = item.get(
response_key,
item.get("reference_solution", item.get("expected_proof", "")),
)
if not prompt_text or not response_text:
logger.debug(f"Skipping sample {i} due to missing prompt or response text.")
continue
messages = [
{"role": "user", "content": prompt_text},
{"role": "assistant", "content": response_text},
]
entry = {"messages": messages}
for ds_key, rk_key in message_key_map.items():
if ds_key in item:
entry[rk_key] = item[ds_key]
for key, value in item.items():
if key not in [prompt_key, response_key] and key not in message_key_map:
entry[key] = value
f.write(json.dumps(entry) + "\n")
processed_samples += 1
if processed_samples == 0 and i == -1:
logger.info(f"No samples converted to JSONL format: {output_file}")
else:
logger.info(f"Converted {processed_samples} samples to JSONL format: {output_file}")
return output_file
class EvaluatorPreviewResult:
def __init__(self):
self.results = []
self.total_samples = 0
self.total_runtime_ms = 0
def add_result(self, sample_index, success, score, per_metric_evals):
result_obj = types.SimpleNamespace(
index=sample_index,
success=success,
score=score,
per_metric_evals=per_metric_evals,
)
self.results.append(result_obj)
def display(self):
print("Evaluation Preview Results")
print("------------------------")
print(f"Total Samples: {self.total_samples}")
print(f"Total Runtime: {self.total_runtime_ms} ms\n")
print("Individual Results:")
print("------------------")
for i, result_obj in enumerate(self.results):
print(f"Sample {result_obj.index + 1}:")
print(f" Success: {result_obj.success}")
print(f" Score: {result_obj.score}")
if hasattr(result_obj, "per_metric_evals") and isinstance(result_obj.per_metric_evals, dict):
for metric, value in result_obj.per_metric_evals.items():
print(f" {metric}: {value}")
elif hasattr(result_obj, "per_metric_evals"):
print(f" Per-Metric Evals: {result_obj.per_metric_evals}")
if i < len(self.results) - 1:
print()
class Evaluator:
def __init__(
self,
multi_metrics=False, # Relates to output structure (dict of metrics vs single)
remote_url: Optional[str] = None,
ts_mode_config: Optional[Dict[str, Any]] = None,
reward_function_mode: EvaluationMode = "pointwise", # New parameter for input processing mode
account_id: Optional[str] = None,
api_key: Optional[str] = None,
entry_point: Optional[str] = None,
):
self.multi_metrics = multi_metrics
self.remote_url = remote_url
self.ts_mode_config = ts_mode_config
self.reward_function_mode = reward_function_mode
self.code_files = {}
self.metric_folders: Dict[str, Dict[str, Any]] = {} # Changed to store path and requirements
self.account_id = account_id
self.api_key = api_key
self.description = ""
self.display_name = ""
self.api_base = os.environ.get("FIREWORKS_API_BASE", "https://api.fireworks.ai")
# Optional requirements string for multi-metric mode (when loaded differently)
self._loaded_multi_metric_requirements_str: Optional[str] = None
# Optional entry point metadata (module::function or path::function)
self.entry_point: Optional[str] = entry_point
if self.ts_mode_config:
python_code = self.ts_mode_config.get("python_code")
file_name = self.ts_mode_config.get("file_name", "main.py")
if not python_code:
raise ValueError("python_code is required in ts_mode_config")
self.code_files[file_name] = python_code
# ts_mode implies multiMetrics: true for the payload structure
# but it's distinct from folder-based multi_metrics for loading.
# The original self.multi_metrics flag is for folder loading.
# The payload's multiMetrics field will be set to True if ts_mode_config is active.
# The check for (metric_folders or folder) is not applicable in __init__ and was causing an error.
# If ts_mode_config is active, it takes precedence for code definition.
# The multi_metrics flag passed to __init__ is for folder-based loading if ts_mode_config is not used.
def _should_include_file(self, filename: str) -> bool:
"""Check if a file should be included in the evaluator upload."""
return (
filename.endswith(".py")
or filename.endswith(".txt")
or filename.endswith(".toml")
or os.path.basename(filename) == "Dockerfile"
)
def _load_python_files_from_folder(self, folder_path: str) -> Dict[str, str]:
"""
Recursively loads Python, text, and TOML files from a given folder (excluding common ignored dirs).
Args:
folder_path: Absolute path to the folder.
Returns:
A dictionary mapping relative file paths (within folder) to their content.
Raises:
ValueError: If folder_path is invalid or not a directory.
"""
if not os.path.exists(folder_path):
raise ValueError(f"Folder does not exist: {folder_path}")
if not os.path.isdir(folder_path):
raise ValueError(f"Not a directory: {folder_path}")
files: Dict[str, str] = {}
ignored_dirs = {".git", "__pycache__", "node_modules", "venv", ".venv", "dist", "build", "vendor"}
base_path = Path(folder_path)
for dirpath, dirnames, filenames in os.walk(folder_path):
# prune ignored directories
dirnames[:] = [d for d in dirnames if d not in ignored_dirs and not d.startswith(".")]
for name in filenames:
if not self._should_include_file(name):
continue
abs_path = Path(dirpath) / name
rel_path = str(abs_path.relative_to(base_path))
with open(abs_path, "r", encoding="utf-8") as f:
content = f.read()
files[rel_path] = content
if not files:
raise ValueError(f"No Python, text, or TOML files found in {folder_path}")
return files
def load_metric_folder(self, metric_name, folder_path):
"""
Load code files from a metric folder
Args:
metric_name: Name of the metric
folder_path: Path to the folder containing code files
Returns:
Dict mapping filenames to their contents
"""
folder_path = os.path.abspath(folder_path)
files = self._load_python_files_from_folder(folder_path) # Reads all .py files into a dict
metric_requirements_list: Optional[List[str]] = None
main_py_content = files.get("main.py")
if main_py_content:
try:
tree = ast.parse(main_py_content)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and node.name == "evaluate":
for decorator_node in node.decorator_list:
if (
isinstance(decorator_node, ast.Call)
and isinstance(decorator_node.func, ast.Name)
and decorator_node.func.id == "reward_function"
):
for keyword in decorator_node.keywords:
if keyword.arg == "requirements":
if isinstance(keyword.value, ast.List):
reqs: List[str] = []
for elt in keyword.value.elts:
if isinstance(elt, ast.Constant): # Python 3.8+
if isinstance(elt.value, str):
reqs.append(cast(str, elt.value))
elif isinstance(elt, ast.Str): # Python < 3.8
reqs.append(cast(str, elt.s))
if reqs:
metric_requirements_list = cast(List[str], reqs)
elif isinstance(keyword.value, ast.Constant) and isinstance(
keyword.value.value, str
): # Python 3.8+ (single req string)
metric_requirements_list = [cast(str, keyword.value.value)]
elif isinstance(keyword.value, ast.Str): # Python < 3.8 (single req string)
metric_requirements_list = [cast(str, keyword.value.s)]
break
if metric_requirements_list:
break
if metric_requirements_list:
logger.info(
f"Found requirements for metric '{metric_name}' via AST: {metric_requirements_list}"
)
break
except SyntaxError as e:
logger.error(f"Syntax error parsing main.py for metric '{metric_name}' to find requirements: {e}")
except Exception as e:
logger.error(f"Error parsing main.py AST for metric '{metric_name}': {e}")
self.metric_folders[metric_name] = {
"path": folder_path,
"requirements": metric_requirements_list, # This is now a list of strings or None
}
for filename, content in files.items():
self.code_files[f"{metric_name}/{filename}"] = content
logger.info(f"Loaded {len(files)} files for metric '{metric_name}' from {folder_path}")
return files
def load_multi_metrics_folder(self, folder_path):
"""
Load code files from a folder with multiple metrics
Args:
folder_path: Path to the folder containing code files
Returns:
Dict mapping filenames to their contents
"""
folder_path = os.path.abspath(folder_path)
files = self._load_python_files_from_folder(folder_path)
self.code_files = files
logger.info(f"Loaded {len(files)} files from {folder_path} for multi-metrics evaluation")
return files
def load_samples_from_jsonl(self, sample_file, max_samples=5):
if not os.path.exists(sample_file):
raise ValueError(f"Sample file does not exist: {sample_file}")
samples = []
with open(sample_file, "r") as f:
for i, line in enumerate(f):
if i >= max_samples:
break
line = line.strip()
if not line:
continue
try:
sample = json.loads(line)
samples.append(sample)
except json.JSONDecodeError:
logger.warning(f"Invalid JSON on line {i + 1}, skipping")
logger.info(f"Loaded {len(samples)} samples from {sample_file}")
return samples
def preview(self, sample_file, max_samples=5):
if not self.remote_url and not self.ts_mode_config and not self.code_files:
raise ValueError("No code files loaded. Load metric folder(s) or provide ts_mode_config/remote_url first.")
# If not remote and not ts_mode, then main.py check applies to loaded code_files
if not self.remote_url and not self.ts_mode_config:
if "main.py" not in self.code_files and not any(k.endswith("/main.py") for k in self.code_files):
raise ValueError("No main.py found in loaded code files for folder-based evaluation.")
samples = self.load_samples_from_jsonl(sample_file, max_samples)
if not samples:
raise ValueError(f"No valid samples found in {sample_file}")
auth_token = self.api_key or get_fireworks_api_key()
account_id = self.account_id or get_fireworks_account_id()
if not account_id and auth_token:
account_id = verify_api_key_and_get_account_id(api_key=auth_token, api_base=self.api_base)
logger.debug(f"Preview using account_id: {account_id}")
if not account_id or not auth_token:
logger.error("Authentication error: Missing Fireworks Account ID or API Key.")
raise ValueError("Missing Fireworks Account ID or API Key.")
# Keep multiMetrics/rollupSettings for backward compatibility with tests
payload_multi_metrics = True
payload_rollup_settings = {"skipRollup": True}
# For preview, evaluator_id might not be as critical for shim's env var name,
# but pass it for consistency. Use display_name as a proxy if no specific ID.
preview_evaluator_id_for_shim = self.display_name or "preview_evaluator"
evaluator_payload_data = {
"displayName": self.display_name or "Preview Evaluator",
"description": self.description or "Preview Evaluator",
"multiMetrics": payload_multi_metrics,
"criteria": self._construct_criteria(criteria_data={}),
"requirements": self._get_combined_requirements(),
"rollupSettings": payload_rollup_settings,
}
sample_strings = [json.dumps(sample) for sample in samples]
payload = {
"evaluator": evaluator_payload_data,
"sampleData": sample_strings,
"maxSamples": max_samples,
}
api_base = os.environ.get("FIREWORKS_API_BASE", "https://api.fireworks.ai")
if "dev.api.fireworks.ai" in api_base and account_id == "fireworks":
account_id = "pyroworks-dev"
url = f"{api_base}/v1/accounts/{account_id}/evaluators:previewEvaluator"
headers = {
"Authorization": f"Bearer {auth_token}",
"Content-Type": "application/json",
}
logger.info(f"Previewing evaluator using API endpoint: {url} with account: {account_id}")
logger.debug(f"Preview API Request URL: {url}")
logger.debug(f"Preview API Request Headers: {json.dumps(headers, indent=2)}")
logger.debug(f"Preview API Request Payload: {json.dumps(payload, indent=2)}")
global used_preview_api
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
used_preview_api = True
preview_result_obj = EvaluatorPreviewResult()
preview_result_obj.total_samples = result.get("totalSamples", len(samples))
preview_result_obj.total_runtime_ms = int(result.get("totalRuntimeMs", 0))
sample_results = result.get("results", [])
for i, sample_result_item in enumerate(sample_results):
preview_result_obj.add_result(
sample_index=i,
success=sample_result_item.get("success", False),
score=sample_result_item.get("score", 0.0),
per_metric_evals=sample_result_item.get("perMetricEvals", {}),
)
return preview_result_obj
except Exception as e:
logger.error(f"Error previewing evaluator: {str(e)}")
if isinstance(e, requests.exceptions.HTTPError) and hasattr(e, "response"):
logger.error(f"Response: {e.response.text}")
used_preview_api = False
logger.warning("Falling back to simulated preview mode")
return self._simulated_preview(samples)
def _get_combined_requirements(self) -> str:
"""Combines requirements from all loaded metrics."""
all_requirements_set = set()
for metric_data in self.metric_folders.values():
req_list_or_str = metric_data.get("requirements")
if req_list_or_str:
if isinstance(req_list_or_str, list):
for req_item in req_list_or_str:
if isinstance(req_item, str):
all_requirements_set.add(req_item.strip())
elif isinstance(req_list_or_str, str): # Fallback if somehow a string is still passed
items = [r.strip() for r in req_list_or_str.splitlines() if r.strip()]
for item in items:
all_requirements_set.add(item)
# For multi_metrics loaded directly into self.code_files (not via metric_folders)
# This part is more complex as it requires loading the 'main.py' from self.code_files
# if self.multi_metrics and not self.metric_folders and "main.py" in self.code_files:
# We would need a temporary way to load this main.py to get its requirements.
# For now, focusing on metric_folders which is the primary path for --metrics-folders.
# If a multi_metrics folder is loaded via load_multi_metrics_folder, it also needs a similar
# dynamic import logic to fetch requirements from its main 'evaluate' function.
# This part is NOT YET IMPLEMENTED for multi_metrics folders.
if not all_requirements_set and hasattr(self, "_loaded_multi_metric_requirements_str"):
# Fallback for multi_metrics if requirements were loaded differently (hypothetical)
# This attribute doesn't exist yet, placeholder for future enhancement if needed.
if self._loaded_multi_metric_requirements_str: # type: ignore
requirements_list = [
r.strip() for r in self._loaded_multi_metric_requirements_str.splitlines() if r.strip()
] # type: ignore
for req_item in requirements_list:
all_requirements_set.add(req_item)
logger.info(f"Combined unique requirements: {all_requirements_set}")
return "\n".join(sorted(list(all_requirements_set)))
def _simulated_preview(self, samples):
preview_result = EvaluatorPreviewResult()
preview_result.total_samples = len(samples)
start_time = time.time()
for i, sample in enumerate(samples):
try:
if "messages" not in sample:
raise ValueError(f"Sample {i + 1} is missing 'messages' field")
_ = sample.get("messages", [])
_ = sample.get("ground_truth", [])
_ = sample.get("tools", [])
_ = {
k: v
for k, v in sample.items()
if k
not in [
"messages",
"ground_truth",
"tools",
]
}
if self.multi_metrics or self.ts_mode_config: # ts_mode also implies a single set of results
per_metric_evals = {"quality": 0.8, "relevance": 0.7, "safety": 0.9}
else:
per_metric_evals = {metric_name: 0.75 for metric_name in self.metric_folders}
score = sum(per_metric_evals.values()) / len(per_metric_evals) if per_metric_evals else 0.0
preview_result.add_result(
sample_index=i,
success=True,
score=score,
per_metric_evals=per_metric_evals,
)
except Exception as e:
logger.error(f"Error processing sample {i + 1}: {str(e)}")
preview_result.add_result(
sample_index=i,
success=False,
score=0.0,
per_metric_evals={"error": str(e)},
)
end_time = time.time()
preview_result.total_runtime_ms = max(1, int((end_time - start_time) * 1000))
return preview_result
def _build_minimal_criteria(self) -> List[Dict[str, str]]:
"""Build minimal criteria (name, type, description) without code snippets."""
# Remote URL mode
if self.remote_url:
return [
{
"name": "remote_eval_proxy",
"type": "CODE_SNIPPETS",
"description": f"Proxies evaluation to remote URL: {self.remote_url}",
}
]
# TS mode (direct code snippet)
elif self.ts_mode_config:
criterion_name = self.ts_mode_config.get("criterion_name", "default_code_criterion")
description = self.ts_mode_config.get("description", "Python code execution")
return [
{
"name": criterion_name,
"type": "CODE_SNIPPETS",
"description": description,
}
]
# Multi-metrics mode
elif self.multi_metrics:
return [
{
"name": "eval",
"type": "CODE_SNIPPETS",
"description": self.description or "Multi-metric evaluation",
}
]
# Single metric folders
else:
criteria = []
for metric_name in self.metric_folders:
criteria.append(
{
"name": metric_name,
"type": "CODE_SNIPPETS",
"description": self.description or f"Evaluation metric: {metric_name}",
}
)
return criteria
@staticmethod
def _parse_ignore_file(ignore_path: str) -> List[str]:
"""Parse .gitignore or .dockerignore and return patterns."""
patterns = []
if not os.path.exists(ignore_path):
return patterns
try:
with open(ignore_path, "r") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
patterns.append(line)
except Exception:
pass
return patterns
@staticmethod
def _ensure_requirements_present(source_dir: str) -> None:
req_path = os.path.join(source_dir, "requirements.txt")
if not os.path.isfile(req_path):
logger.error("Missing requirements.txt in upload directory: %s", source_dir)
raise ValueError(
"Upload requires requirements.txt in the project root. "
"Please add requirements.txt and re-run ep upload."
)
@staticmethod
def _should_ignore(path: str, ignore_patterns: List[str]) -> bool:
"""Check if path matches any ignore pattern."""
from pathlib import Path
import fnmatch
default_ignores = [
".git",
".github",
"__pycache__",
"*.pyc",
"*.pyo",
"*.pyd",
".venv",
"venv",
".tox",
".pytest_cache",
".mypy_cache",
".ruff_cache",
".ipynb_checkpoints",
".idea",
".vscode",
".cache",
"node_modules",
"vendor",
"dist",
"build",
"*.egg-info",
"*.egg",
"*.whl",
"*.tar.gz",
"*.zip",
"*.log",
"*.tmp",
"*.swp",
".DS_Store",
"coverage",
"htmlcov",
".coverage",
"coverage.xml",
".env",
".env.*",
"*.so",
"*.dylib",
".pytest_cache/",
"env/",
]
all_patterns = default_ignores + ignore_patterns
path_obj = Path(path)
for pattern in all_patterns:
if pattern.endswith("/"):
if path_obj.is_dir() and fnmatch.fnmatch(path_obj.name, pattern.rstrip("/")):
return True
elif fnmatch.fnmatch(path_obj.name, pattern) or fnmatch.fnmatch(str(path_obj), pattern):
return True
return False
@staticmethod
def _create_tar_gz_with_ignores(output_path: str, source_dir: str) -> int:
"""Create tar.gz of source_dir with parent directory included."""
import tarfile
from pathlib import Path
source_path = Path(source_dir)
gitignore_patterns = Evaluator._parse_ignore_file(str(source_path / ".gitignore"))
dockerignore_patterns = Evaluator._parse_ignore_file(str(source_path / ".dockerignore"))
all_ignore_patterns = gitignore_patterns + dockerignore_patterns
logger.info(f"Creating tar.gz with {len(all_ignore_patterns)} ignore patterns")
# Get directory name for the archive root
dir_name = os.path.basename(source_dir)
parent_dir = os.path.dirname(source_dir)
with tarfile.open(output_path, "w:gz") as tar:
for root, dirs, files in os.walk(source_dir):
dirs[:] = [d for d in dirs if not Evaluator._should_ignore(os.path.join(root, d), all_ignore_patterns)]
for file in files:
file_path = os.path.join(root, file)
if Evaluator._should_ignore(file_path, all_ignore_patterns):
continue
# Include parent directory in archive path
rel_path = os.path.relpath(file_path, parent_dir) # Relative to parent
tar.add(file_path, arcname=rel_path) # Keeps "python-sdk/..." structure
size_bytes = os.path.getsize(output_path)
logger.info(f"Created {output_path} ({size_bytes:,} bytes)")
return size_bytes
def create(self, evaluator_id, display_name=None, description=None, force=False):
if not self.remote_url and not self.ts_mode_config and not self.code_files:
raise ValueError("No code files loaded. Load metric folder(s) or provide ts_mode_config/remote_url first.")
auth_token = self.api_key or get_fireworks_api_key()
account_id = self.account_id or get_fireworks_account_id()
if not account_id and auth_token:
# Attempt to verify the API key and derive account id from server headers
account_id = verify_api_key_and_get_account_id(api_key=auth_token, api_base=self.api_base)
if not auth_token or not account_id:
logger.error("Authentication error: API credentials appear to be invalid or incomplete.")
raise ValueError("Invalid or missing API credentials.")
self.display_name = display_name or evaluator_id
self.description = description or f"Evaluator created from {evaluator_id}"
# Keep multiMetrics/rollupSettings for backward compatibility with tests
payload_multi_metrics = True
payload_rollup_settings = {"skipRollup": True}
parent = f"accounts/{account_id}"
try:
version_str = get_pep440_version()
except Exception:
version_str = None
payload_data = {
"parent": parent,
"evaluator": {
"displayName": self.display_name,
"description": self.description,
"multiMetrics": payload_multi_metrics,
"commitHash": version_str,
"criteria": self._build_minimal_criteria(),
"requirements": "",
"rollupSettings": payload_rollup_settings,
},
"evaluatorId": evaluator_id,
}
# Include optional entry point when provided
if self.entry_point:
payload_data["evaluator"]["entryPoint"] = self.entry_point
logger.info(f"Including entryPoint in payload: {self.entry_point}")
# Debug log the create payload structure (without sample data)
try:
logger.info(f"Create API Request Payload: {json.dumps(payload_data, indent=2)}")
except Exception:
# If serialization fails for any reason, skip debug dump
pass
if "dev.api.fireworks.ai" in self.api_base and account_id == "fireworks":
account_id = "pyroworks-dev"
base_url = f"{self.api_base}/v1/{parent}/evaluatorsV2"
headers = {
"Authorization": f"Bearer {auth_token}",
"Content-Type": "application/json",
}
self._ensure_requirements_present(os.getcwd())
logger.info(f"Creating evaluator '{evaluator_id}' for account '{account_id}'...")
try:
if force:
check_url = f"{self.api_base}/v1/{parent}/evaluators/{evaluator_id}"
try:
logger.info(f"Checking if evaluator exists: {check_url}")
check_response = requests.get(check_url, headers=headers)
if check_response.status_code == 200:
logger.info(f"Evaluator '{evaluator_id}' already exists, deleting and recreating...")
delete_url = f"{self.api_base}/v1/{parent}/evaluators/{evaluator_id}"
try:
delete_response = requests.delete(delete_url, headers=headers)
if delete_response.status_code < 400:
logger.info(f"Successfully deleted evaluator '{evaluator_id}'")
else:
logger.warning(
f"Unable to delete evaluator '{evaluator_id}', status: {delete_response.status_code}"
)
except Exception as e_del:
logger.warning(f"Error deleting evaluator: {str(e_del)}")
response = requests.post(base_url, json=payload_data, headers=headers)
else:
response = requests.post(base_url, json=payload_data, headers=headers)
except requests.exceptions.RequestException:
response = requests.post(base_url, json=payload_data, headers=headers)
else:
logger.info(f"Creating evaluator at: {base_url}")
response = requests.post(base_url, json=payload_data, headers=headers)
response.raise_for_status()
result = response.json()
logger.info(f"Successfully created evaluator '{evaluator_id}'")
# Upload code as tar.gz to GCS
evaluator_name = result.get("name") # e.g., "accounts/pyroworks/evaluators/test-123"
if not evaluator_name:
raise ValueError(
"Create evaluator response missing 'name' field. "
f"Cannot proceed with code upload. Response: {result}"
)
try:
# Create tar.gz of current directory
cwd = os.getcwd()
dir_name = os.path.basename(cwd)
tar_filename = f"{dir_name}.tar.gz"
tar_path = os.path.join(cwd, tar_filename)
tar_size = self._create_tar_gz_with_ignores(tar_path, cwd)
# Call GetEvaluatorUploadEndpoint
upload_endpoint_url = f"{self.api_base}/v1/{evaluator_name}:getUploadEndpoint"
upload_payload = {"name": evaluator_name, "filename_to_size": {tar_filename: tar_size}}
logger.info(f"Requesting upload endpoint for {tar_filename}")
upload_response = requests.post(upload_endpoint_url, json=upload_payload, headers=headers)
upload_response.raise_for_status()
# Check for signed URLs
upload_response_data = upload_response.json()
signed_urls = upload_response_data.get("filenameToSignedUrls", {})
if not signed_urls:
raise ValueError(f"GetUploadEndpoint returned no signed URLs. Response: {upload_response_data}")
signed_url = signed_urls.get(tar_filename)
if not signed_url:
raise ValueError(
f"No signed URL received for {tar_filename}. Available files: {list(signed_urls.keys())}"
)
# Upload to GCS
logger.info(f"Uploading {tar_filename} to GCS...")
file_size = os.path.getsize(tar_path)
# Retry configuration
max_retries = 3
retry_delay = 2 # seconds
for attempt in range(max_retries):
try:
with open(tar_path, "rb") as f:
# Create request exactly like Golang
req = requests.Request(
"PUT",
signed_url,
data=f,
headers={
"Content-Type": "application/octet-stream",
"X-Goog-Content-Length-Range": f"{file_size},{file_size}",
},
)
prepared = req.prepare()
# Don't let requests add extra headers
session = requests.Session()
gcs_response = session.send(prepared, timeout=600)
gcs_response.raise_for_status()
logger.info(f"Successfully uploaded {tar_filename}")
break # Success, exit retry loop
except (requests.exceptions.RequestException, IOError) as e:
if attempt < max_retries - 1:
# Check if it's a retryable error
is_retryable = False
if isinstance(e, requests.exceptions.RequestException):
if hasattr(e, "response") and e.response is not None:
# Retry on 5xx errors or 408 (timeout)
is_retryable = e.response.status_code >= 500 or e.response.status_code == 408
else:
# Network errors (no response) are retryable
is_retryable = True
else:
# IOError is retryable
is_retryable = True
if is_retryable:
wait_time = retry_delay * (2**attempt) # Exponential backoff
logger.warning(
f"Upload attempt {attempt + 1}/{max_retries} failed: {e}. "
f"Retrying in {wait_time}s..."
)
time.sleep(wait_time)
else:
# Non-retryable error, raise immediately
raise
else:
# Last attempt failed
logger.error(f"Upload failed after {max_retries} attempts")
raise
# Step 3: Validate upload
validate_url = f"{self.api_base}/v1/{evaluator_name}:validateUpload"
validate_payload = {"name": evaluator_name}
validate_response = requests.post(validate_url, json=validate_payload, headers=headers)
validate_response.raise_for_status()
validate_data = validate_response.json()
logger.info("Upload validated successfully")
# Clean up tar file
if os.path.exists(tar_path):
os.remove(tar_path)
except Exception as upload_error:
logger.warning(f"Code upload failed (evaluator created but code not uploaded): {upload_error}")
# Don't fail - evaluator is created, just code upload failed
return result # Return after attempting upload
except Exception as e:
logger.error(f"Error creating evaluator: {str(e)}")
if isinstance(e, requests.exceptions.HTTPError) and hasattr(e, "response"):
logger.error(f"Response: {e.response.text}")
raise
def _construct_criteria(self, criteria_data: Any) -> Any:
assertions = []
if self.remote_url:
shim_main_py_content = f"""
import json
import os
import requests
REMOTE_EVALUATOR_URL = "{self.remote_url}"
def evaluate(messages, ground_truth: Optional[Union[str, List[Dict[str, Any]]]] = None, tools=None, **kwargs):
payload = {{
"messages": messages,
"ground_truth": ground_truth,
"tools": tools,
"kwargs": kwargs
}}
headers = {{"Content-Type": "application/json"}}
try:
response = requests.post(REMOTE_EVALUATOR_URL, json=payload, headers=headers, timeout=30)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
error_info = {{
"error": f"Failed to call remote evaluator at {{REMOTE_EVALUATOR_URL}}: {{str(e)}}",
"status_code": getattr(e.response, 'status_code', None),
"response_text": getattr(e.response, 'text', None)
}}
return {{
"score": 0.0, "reason": f"Error calling remote evaluator: {{str(e)}}",
"is_score_valid": False, "metrics": {{"remote_call_error": {{"score": 0.0, "is_score_valid": False, "reason": json.dumps(error_info)}}}}
}}
except Exception as e:
return {{
"score": 0.0, "reason": f"Unexpected error in remote evaluator shim: {{str(e)}}",
"is_score_valid": False, "metrics": {{"shim_error": {{"score": 0.0, "is_score_valid": False, "reason": str(e)}}}}
}}
"""
file_contents = {"main.py": shim_main_py_content}
assertions.append(
{
"codeSnippets": {
"language": "python",
"fileContents": file_contents,
},
"name": "remote_eval_proxy",
"type": "CODE_SNIPPETS",
"description": f"Proxies evaluation to remote URL: {self.remote_url}",
}
)
elif self.ts_mode_config:
python_code = self.ts_mode_config.get("python_code")
file_name = self.ts_mode_config.get("file_name", "main.py")
criterion_name = self.ts_mode_config.get("criterion_name", "default_code_criterion")
description = self.ts_mode_config.get("description", "Python code execution")
if not python_code:
raise ValueError("python_code is required in ts_mode_config")
entry_func = "evaluate"
try:
if self.entry_point and "::" in self.entry_point:
entry_func = self.entry_point.split("::", 1)[1]
except Exception:
entry_func = "evaluate"
assertions.append(
{
"type": "CODE_SNIPPETS",
"name": criterion_name,
"description": description,
"codeSnippets": {
"language": "python",
"fileContents": {file_name: python_code},
"entryFile": file_name,
"entryFunc": entry_func,
},
}
)
elif self.multi_metrics:
file_contents = {}
for filename, content in self.code_files.items():
if filename.endswith(".py"):
file_contents[filename] = self._update_evaluate_signature(content)
elif self._should_include_file(filename) and not filename.endswith(".py"):
file_contents[filename] = content
if not file_contents: