agentomics 0.1.0rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentomics/__init__.py +1 -0
- agentomics/cli/__init__.py +0 -0
- agentomics/cli/check_dataset.py +88 -0
- agentomics/cli/docker_utils.py +57 -0
- agentomics/cli/download_dataset.py +50 -0
- agentomics/cli/inference.py +89 -0
- agentomics/cli/run.py +395 -0
- agentomics/cli/run_arguments.py +204 -0
- agentomics/cli/train.py +74 -0
- agentomics/datasets/__init__.py +0 -0
- agentomics/datasets/create_datasets.py +333 -0
- agentomics/datasets/csv_converter.py +265 -0
- agentomics/datasets/data_contract.py +194 -0
- agentomics/datasets/dataset_preparation.py +377 -0
- agentomics/datasets/datasets_interactive.py +182 -0
- agentomics/datasets/label_processing.py +61 -0
- agentomics/run_agent.py +100 -0
- agentomics/run_agent_interactive.py +198 -0
- agentomics/runtime/__init__.py +1 -0
- agentomics/runtime/best_iteration_snapshot.py +74 -0
- agentomics/runtime/conda_utils.py +303 -0
- agentomics/runtime/evaluate.py +72 -0
- agentomics/runtime/evaluate_result.py +103 -0
- agentomics/runtime/filesystem.py +110 -0
- agentomics/runtime/generate_final_reports.py +897 -0
- agentomics/runtime/git_checkpoints.py +108 -0
- agentomics/runtime/inference_runner.py +66 -0
- agentomics/runtime/inference_workflow.py +170 -0
- agentomics/runtime/iteration_reports.py +215 -0
- agentomics/runtime/read_write_utils.py +239 -0
- agentomics/runtime/report_workflow.py +84 -0
- agentomics/runtime/run_lifecycle.py +93 -0
- agentomics/runtime/run_workflow.py +209 -0
- agentomics/runtime/setup_fork.py +114 -0
- agentomics/runtime/step_outputs.py +60 -0
- agentomics/runtime/system_resources.py +50 -0
- agentomics/runtime/training_workflow.py +126 -0
- agentomics/utils/__init__.py +1 -0
- agentomics/utils/agent_id.py +15 -0
- agentomics/utils/api_keys.py +83 -0
- agentomics/utils/config.py +179 -0
- agentomics/utils/exceptions.py +10 -0
- agentomics/utils/metrics.py +167 -0
- agentomics/utils/metrics_interactive.py +63 -0
- agentomics/utils/printing_utils.py +276 -0
- agentomics/utils/task_types.py +6 -0
- agentomics/utils/text_processing_utils.py +26 -0
- agentomics/utils/user_input.py +29 -0
- agentomics/utils/versioning.py +20 -0
- agentomics-0.1.0rc1.dist-info/METADATA +158 -0
- agentomics-0.1.0rc1.dist-info/RECORD +55 -0
- agentomics-0.1.0rc1.dist-info/WHEEL +5 -0
- agentomics-0.1.0rc1.dist-info/entry_points.txt +6 -0
- agentomics-0.1.0rc1.dist-info/licenses/LICENSE +21 -0
- agentomics-0.1.0rc1.dist-info/top_level.txt +1 -0
agentomics/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0rc1"
|
|
File without changes
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from rich.console import Console
|
|
9
|
+
|
|
10
|
+
from agentomics.datasets.dataset_preparation import check_dataset
|
|
11
|
+
|
|
12
|
+
console = Console()
|
|
13
|
+
|
|
14
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
15
|
+
parser = argparse.ArgumentParser(
|
|
16
|
+
description=(
|
|
17
|
+
"Validate that a dataset matches the Agentomics run contract, "
|
|
18
|
+
"without launching a run."
|
|
19
|
+
),
|
|
20
|
+
allow_abbrev=False,
|
|
21
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
22
|
+
)
|
|
23
|
+
parser.add_argument(
|
|
24
|
+
"--dataset-dir",
|
|
25
|
+
type=Path,
|
|
26
|
+
required=True,
|
|
27
|
+
help="Dataset directory to validate",
|
|
28
|
+
)
|
|
29
|
+
return parser
|
|
30
|
+
|
|
31
|
+
def _is_tty_available() -> bool:
|
|
32
|
+
return sys.stdin.isatty() and sys.stdout.isatty()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _suggested_metadata(summary: dict) -> dict:
|
|
36
|
+
applicable_keys = (
|
|
37
|
+
"task_type",
|
|
38
|
+
"label_column",
|
|
39
|
+
"id_column",
|
|
40
|
+
"label_to_scalar",
|
|
41
|
+
)
|
|
42
|
+
return {
|
|
43
|
+
key: summary[key]
|
|
44
|
+
for key in applicable_keys
|
|
45
|
+
if key in summary
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _print_summary(summary: dict, interactive: bool) -> None:
|
|
50
|
+
console.print("✓ Dataset is valid", style="green")
|
|
51
|
+
console.print(f"Task type: {summary.get('task_type')}")
|
|
52
|
+
|
|
53
|
+
splits = summary.get("splits", {})
|
|
54
|
+
console.print(f"Train rows: {splits.get('train_rows')}")
|
|
55
|
+
validation_rows = splits.get("validation_rows")
|
|
56
|
+
if validation_rows is not None:
|
|
57
|
+
console.print(f"Validation rows: {validation_rows}")
|
|
58
|
+
else:
|
|
59
|
+
console.print("Validation split: not provided (the agent will create one)")
|
|
60
|
+
|
|
61
|
+
test_rows = summary.get("test_rows")
|
|
62
|
+
if test_rows is not None:
|
|
63
|
+
console.print(f"Test rows: {test_rows}")
|
|
64
|
+
|
|
65
|
+
if "label_to_scalar" in summary:
|
|
66
|
+
console.print(f"Label mapping: {summary['label_to_scalar']}")
|
|
67
|
+
console.print(f"Input structure: {summary.get('input_structure')}")
|
|
68
|
+
|
|
69
|
+
if interactive:
|
|
70
|
+
console.print("Suggested metadata.json:", style="cyan")
|
|
71
|
+
console.print(json.dumps(_suggested_metadata(summary), indent=4))
|
|
72
|
+
|
|
73
|
+
def main() -> int:
|
|
74
|
+
arguments = build_parser().parse_args()
|
|
75
|
+
interactive = _is_tty_available()
|
|
76
|
+
try:
|
|
77
|
+
summary = check_dataset(
|
|
78
|
+
source_dir=arguments.dataset_dir,
|
|
79
|
+
interactive=interactive,
|
|
80
|
+
)
|
|
81
|
+
except (ValueError, OSError) as error: # ValueError = data-contract violation; OSError = missing/unreadable paths.
|
|
82
|
+
console.print(f"✗ Dataset is not valid: {error}", style="red")
|
|
83
|
+
return 1
|
|
84
|
+
_print_summary(summary, interactive)
|
|
85
|
+
return 0
|
|
86
|
+
|
|
87
|
+
if __name__ == "__main__":
|
|
88
|
+
sys.exit(main())
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import os
|
|
5
|
+
import shutil
|
|
6
|
+
import subprocess
|
|
7
|
+
|
|
8
|
+
from agentomics.utils.versioning import get_version
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
DEFAULT_IMAGE = f"biogemt/agentomics:{get_version()}"
|
|
12
|
+
CONTAINER_PYTHON = "/opt/conda/envs/agentomics-env/bin/python"
|
|
13
|
+
|
|
14
|
+
def create_parser(description: str) -> argparse.ArgumentParser:
|
|
15
|
+
parser = argparse.ArgumentParser(
|
|
16
|
+
description=description,
|
|
17
|
+
allow_abbrev=False,
|
|
18
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
19
|
+
)
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"--image",
|
|
22
|
+
default=DEFAULT_IMAGE,
|
|
23
|
+
help="Docker image used for the worker",
|
|
24
|
+
)
|
|
25
|
+
return parser
|
|
26
|
+
|
|
27
|
+
def run_python_in_docker(
|
|
28
|
+
image: str,
|
|
29
|
+
cpu_only: bool,
|
|
30
|
+
mounts: list[str],
|
|
31
|
+
python_arguments: list[str],
|
|
32
|
+
docker_arguments: list[str] | None = None,
|
|
33
|
+
check: bool = True,
|
|
34
|
+
) -> int:
|
|
35
|
+
if shutil.which("docker") is None:
|
|
36
|
+
raise RuntimeError("Missing required command: docker")
|
|
37
|
+
|
|
38
|
+
command = ["docker", "run", "--rm"]
|
|
39
|
+
if docker_arguments:
|
|
40
|
+
command.extend(docker_arguments)
|
|
41
|
+
if not cpu_only:
|
|
42
|
+
command.extend(["--gpus", "all"])
|
|
43
|
+
if hasattr(os, "getuid") and hasattr(os, "getgid"):
|
|
44
|
+
command.extend(
|
|
45
|
+
[
|
|
46
|
+
"-e",
|
|
47
|
+
f"HOST_UID={os.getuid()}",
|
|
48
|
+
"-e",
|
|
49
|
+
f"HOST_GID={os.getgid()}",
|
|
50
|
+
]
|
|
51
|
+
)
|
|
52
|
+
command.extend(["-e", "PYTHONPATH=/repository/src"])
|
|
53
|
+
for mount in mounts:
|
|
54
|
+
command.extend(["--mount", mount])
|
|
55
|
+
command.extend(["--entrypoint", CONTAINER_PYTHON, image])
|
|
56
|
+
command.extend(python_arguments)
|
|
57
|
+
return subprocess.run(command, check=check).returncode
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from agentomics.datasets.create_datasets import (
|
|
7
|
+
DATASET_GENERATORS,
|
|
8
|
+
generate_dataset_files,
|
|
9
|
+
list_dataset_names,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
DEFAULT_DATASET = "AGO2_CLASH_Hejret2023"
|
|
14
|
+
|
|
15
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
16
|
+
parser = argparse.ArgumentParser(
|
|
17
|
+
description="Download an example dataset for Agentomics.",
|
|
18
|
+
allow_abbrev=False,
|
|
19
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
20
|
+
)
|
|
21
|
+
selection = parser.add_mutually_exclusive_group()
|
|
22
|
+
selection.add_argument(
|
|
23
|
+
"--dataset",
|
|
24
|
+
choices=sorted(DATASET_GENERATORS),
|
|
25
|
+
default=DEFAULT_DATASET,
|
|
26
|
+
help="Example dataset to download",
|
|
27
|
+
)
|
|
28
|
+
selection.add_argument(
|
|
29
|
+
"--all",
|
|
30
|
+
action="store_true",
|
|
31
|
+
help="Download all available example datasets",
|
|
32
|
+
)
|
|
33
|
+
selection.add_argument(
|
|
34
|
+
"--list",
|
|
35
|
+
action="store_true",
|
|
36
|
+
help="List available example datasets and exit",
|
|
37
|
+
)
|
|
38
|
+
return parser
|
|
39
|
+
|
|
40
|
+
def main() -> int:
|
|
41
|
+
arguments = build_parser().parse_args()
|
|
42
|
+
if arguments.list:
|
|
43
|
+
list_dataset_names()
|
|
44
|
+
return 0
|
|
45
|
+
|
|
46
|
+
generate_dataset_files(arguments.dataset, arguments.all)
|
|
47
|
+
return 0
|
|
48
|
+
|
|
49
|
+
if __name__ == "__main__":
|
|
50
|
+
sys.exit(main())
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from agentomics.cli.docker_utils import (
|
|
8
|
+
create_parser,
|
|
9
|
+
run_python_in_docker,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
from agentomics.runtime.inference_workflow import resolve_inference_paths
|
|
13
|
+
from agentomics.utils.config import Config
|
|
14
|
+
|
|
15
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
16
|
+
parser = create_parser(
|
|
17
|
+
"Run a trained Agentomics model on any compatible input data."
|
|
18
|
+
)
|
|
19
|
+
parser.add_argument(
|
|
20
|
+
"--agent-dir", type=Path, required=True,
|
|
21
|
+
help="Path to the agent output directory",
|
|
22
|
+
)
|
|
23
|
+
parser.add_argument(
|
|
24
|
+
"--input", dest="input_path", type=Path, required=True,
|
|
25
|
+
help="Input split directory or CSV file",
|
|
26
|
+
)
|
|
27
|
+
parser.add_argument(
|
|
28
|
+
"--output", type=Path, required=True, help="Prediction output CSV"
|
|
29
|
+
)
|
|
30
|
+
parser.add_argument("--label-col", help="Label column when the input is a CSV file")
|
|
31
|
+
parser.add_argument(
|
|
32
|
+
"--iteration-dir",
|
|
33
|
+
type=Path,
|
|
34
|
+
default=Path(Config.BEST_ITERATION_SNAPSHOT_DIRNAME),
|
|
35
|
+
help="Iteration directory relative to --agent-dir",
|
|
36
|
+
)
|
|
37
|
+
parser.add_argument(
|
|
38
|
+
"--all-iterations", action="store_true",
|
|
39
|
+
help="Run every archived iteration",
|
|
40
|
+
)
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
"--remove-conda-env", action="store_true",
|
|
43
|
+
help="Remove the model environment afterward",
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument("--cpu-only", action="store_true", help="Disable GPU access")
|
|
46
|
+
return parser
|
|
47
|
+
|
|
48
|
+
def run_inference_in_docker(arguments: argparse.Namespace) -> None:
|
|
49
|
+
resolve_inference_paths(arguments)
|
|
50
|
+
container_input = "/inference-input"
|
|
51
|
+
python_arguments = [
|
|
52
|
+
"-m",
|
|
53
|
+
"agentomics.runtime.inference_workflow",
|
|
54
|
+
"--agent-dir",
|
|
55
|
+
"/agent",
|
|
56
|
+
"--input",
|
|
57
|
+
container_input,
|
|
58
|
+
"--output",
|
|
59
|
+
f"/inference-output/{arguments.output.name}",
|
|
60
|
+
"--iteration-dir",
|
|
61
|
+
str(arguments.iteration_dir),
|
|
62
|
+
]
|
|
63
|
+
if arguments.label_col:
|
|
64
|
+
python_arguments.extend(["--label-col", arguments.label_col])
|
|
65
|
+
if arguments.all_iterations:
|
|
66
|
+
python_arguments.append("--all-iterations")
|
|
67
|
+
if arguments.remove_conda_env:
|
|
68
|
+
python_arguments.append("--remove-conda-env")
|
|
69
|
+
if arguments.cpu_only:
|
|
70
|
+
python_arguments.append("--cpu-only")
|
|
71
|
+
|
|
72
|
+
run_python_in_docker(
|
|
73
|
+
image=arguments.image,
|
|
74
|
+
cpu_only=arguments.cpu_only,
|
|
75
|
+
mounts=[
|
|
76
|
+
f"type=bind,src={arguments.agent_dir},dst=/agent",
|
|
77
|
+
f"type=bind,src={arguments.input_path},dst={container_input},readonly",
|
|
78
|
+
f"type=bind,src={arguments.output.parent},dst=/inference-output",
|
|
79
|
+
],
|
|
80
|
+
python_arguments=python_arguments,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def main() -> int:
|
|
84
|
+
arguments = build_parser().parse_args()
|
|
85
|
+
run_inference_in_docker(arguments)
|
|
86
|
+
return 0
|
|
87
|
+
|
|
88
|
+
if __name__ == "__main__":
|
|
89
|
+
sys.exit(main())
|
agentomics/cli/run.py
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
import tempfile
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from agentomics.cli.docker_utils import (
|
|
11
|
+
create_parser,
|
|
12
|
+
run_python_in_docker,
|
|
13
|
+
)
|
|
14
|
+
from agentomics.cli.inference import run_inference_in_docker
|
|
15
|
+
from agentomics.cli.run_arguments import add_run_arguments
|
|
16
|
+
from agentomics.datasets.dataset_preparation import prepare_test_dataset
|
|
17
|
+
from agentomics.datasets.datasets_interactive import (
|
|
18
|
+
get_all_datasets_info,
|
|
19
|
+
interactive_dataset_selection,
|
|
20
|
+
print_datasets_table,
|
|
21
|
+
)
|
|
22
|
+
from agentomics.runtime.read_write_utils import load_config_from_run_dir
|
|
23
|
+
from agentomics.utils.agent_id import create_agent_id
|
|
24
|
+
from agentomics.utils.config import Config
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
DEFAULT_DATASETS_DIRECTORY = Path("datasets")
|
|
28
|
+
DEFAULT_OUTPUTS_DIRECTORY = Path("outputs")
|
|
29
|
+
CONTAINER_CODEX_DIRECTORY = Path("/mnt/codex-host")
|
|
30
|
+
CONTAINER_DATASETS_DIRECTORY = Path("/datasets")
|
|
31
|
+
CONTAINER_FORK_SOURCE_DIRECTORY = Path("/fork-source")
|
|
32
|
+
CONTAINER_WORKSPACE_DIRECTORY = Path("/workspace")
|
|
33
|
+
PUBLIC_DATASET_ENTRY_NAMES = (
|
|
34
|
+
"train",
|
|
35
|
+
"train.csv",
|
|
36
|
+
"validation",
|
|
37
|
+
"validation.csv",
|
|
38
|
+
"supplementary",
|
|
39
|
+
"metadata.json",
|
|
40
|
+
"dataset_description.md",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
45
|
+
parser = create_parser("Run the Agentomics model-development workflow.")
|
|
46
|
+
add_run_arguments(parser)
|
|
47
|
+
return parser
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _resolve_workspace(
|
|
51
|
+
requested_workspace: Path | None,
|
|
52
|
+
agent_id: str,
|
|
53
|
+
) -> Path:
|
|
54
|
+
# Default outputs belong to the directory from which the user launched the CLI.
|
|
55
|
+
workspace_directory = (
|
|
56
|
+
requested_workspace or Path.cwd() / DEFAULT_OUTPUTS_DIRECTORY / agent_id
|
|
57
|
+
).expanduser().resolve()
|
|
58
|
+
workspace_directory.mkdir(parents=True, exist_ok=True)
|
|
59
|
+
return workspace_directory
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _build_container_arguments(arguments: argparse.Namespace) -> list[str]:
|
|
63
|
+
workflow_arguments = [
|
|
64
|
+
"-m",
|
|
65
|
+
"agentomics.runtime.run_workflow",
|
|
66
|
+
]
|
|
67
|
+
for option, value in (
|
|
68
|
+
("--model", arguments.model),
|
|
69
|
+
("--iteration-plan-model", arguments.iteration_plan_model),
|
|
70
|
+
("--provider", arguments.provider),
|
|
71
|
+
("--dataset", arguments.dataset),
|
|
72
|
+
("--task-type", arguments.task_type),
|
|
73
|
+
("--val-metric", arguments.val_metric),
|
|
74
|
+
("--iterations", arguments.iterations),
|
|
75
|
+
("--timeout", arguments.timeout),
|
|
76
|
+
("--split-timeout", arguments.split_timeout),
|
|
77
|
+
("--run-python-timeout", arguments.run_python_timeout),
|
|
78
|
+
("--split-allowed-iterations", arguments.split_allowed_iterations),
|
|
79
|
+
("--exploration-iterations", arguments.exploration_iterations),
|
|
80
|
+
("--user-prompt", arguments.user_prompt),
|
|
81
|
+
("--fork-from-step", arguments.fork_from_step),
|
|
82
|
+
("--fork-from-iteration", arguments.fork_from_iteration),
|
|
83
|
+
("--spend-limit", arguments.spend_limit),
|
|
84
|
+
("--conda-export-mode", arguments.conda_export_mode),
|
|
85
|
+
("--verbosity", arguments.verbosity),
|
|
86
|
+
):
|
|
87
|
+
if value is not None:
|
|
88
|
+
workflow_arguments.extend([option, str(value)])
|
|
89
|
+
if arguments.tags is not None:
|
|
90
|
+
workflow_arguments.append("--tags")
|
|
91
|
+
workflow_arguments.extend(arguments.tags)
|
|
92
|
+
for option, enabled in (
|
|
93
|
+
("--use-provisioning-key", arguments.use_provisioning_key),
|
|
94
|
+
("--test", arguments.test),
|
|
95
|
+
("--cpu-only", arguments.cpu_only),
|
|
96
|
+
("--disable-training-reporting", arguments.disable_training_reporting),
|
|
97
|
+
("--list-models", arguments.list_models),
|
|
98
|
+
("--list-datasets", arguments.list_datasets),
|
|
99
|
+
("--list-metrics", arguments.list_metrics),
|
|
100
|
+
):
|
|
101
|
+
if enabled:
|
|
102
|
+
workflow_arguments.append(option)
|
|
103
|
+
return workflow_arguments
|
|
104
|
+
|
|
105
|
+
def _docker_environment_arguments(agent_id: str) -> list[str]:
|
|
106
|
+
docker_arguments: list[str] = []
|
|
107
|
+
env_file = Path.cwd() / ".env"
|
|
108
|
+
if env_file.is_file():
|
|
109
|
+
docker_arguments.extend(["--env-file", str(env_file.resolve())])
|
|
110
|
+
for variable_name in (
|
|
111
|
+
"OPENAI_API_KEY", "ANTHROPIC_API_KEY", "OPENROUTER_API_KEY",
|
|
112
|
+
"PROVISIONING_OPENROUTER_API_KEY", "OLLAMA_BASE_URL",
|
|
113
|
+
"WANDB_API_KEY", "WANDB_PROJECT_NAME", "WANDB_ENTITY",
|
|
114
|
+
"HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY",
|
|
115
|
+
"http_proxy", "https_proxy", "all_proxy", "CUDA_VISIBLE_DEVICES",
|
|
116
|
+
):
|
|
117
|
+
if variable_name in os.environ:
|
|
118
|
+
docker_arguments.extend(["-e", variable_name])
|
|
119
|
+
docker_arguments.extend(["-e", f"AGENT_ID={agent_id}"])
|
|
120
|
+
if sys.stdin.isatty() and sys.stdout.isatty():
|
|
121
|
+
docker_arguments.append("-it")
|
|
122
|
+
return docker_arguments
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _resolve_dataset(
|
|
126
|
+
arguments: argparse.Namespace,
|
|
127
|
+
datasets_directory: Path,
|
|
128
|
+
) -> Path:
|
|
129
|
+
if arguments.fork_from_run is not None:
|
|
130
|
+
fork_config = load_config_from_run_dir(
|
|
131
|
+
arguments.fork_from_run.expanduser().resolve() / Config.RUN_DIRNAME
|
|
132
|
+
)
|
|
133
|
+
if (
|
|
134
|
+
arguments.dataset is not None
|
|
135
|
+
and arguments.dataset != fork_config.dataset
|
|
136
|
+
):
|
|
137
|
+
print(
|
|
138
|
+
f"Warning: --dataset is ignored for forked runs. "
|
|
139
|
+
f"Using '{fork_config.dataset}' from the source run config.",
|
|
140
|
+
file=sys.stderr,
|
|
141
|
+
)
|
|
142
|
+
arguments.dataset = fork_config.dataset
|
|
143
|
+
|
|
144
|
+
if arguments.dataset is None:
|
|
145
|
+
if not (sys.stdin.isatty() and sys.stdout.isatty()):
|
|
146
|
+
raise RuntimeError(
|
|
147
|
+
"Non-interactive runs require --dataset or --fork-from-run"
|
|
148
|
+
)
|
|
149
|
+
arguments.dataset = interactive_dataset_selection(
|
|
150
|
+
get_all_datasets_info(datasets_directory)
|
|
151
|
+
)
|
|
152
|
+
if arguments.dataset is None:
|
|
153
|
+
raise RuntimeError("No dataset selected")
|
|
154
|
+
|
|
155
|
+
dataset_directory = (datasets_directory / arguments.dataset).resolve()
|
|
156
|
+
if dataset_directory.parent != datasets_directory:
|
|
157
|
+
raise ValueError(
|
|
158
|
+
f"Dataset must be a direct child of {datasets_directory}: "
|
|
159
|
+
f"{dataset_directory}"
|
|
160
|
+
)
|
|
161
|
+
if not dataset_directory.is_dir():
|
|
162
|
+
raise NotADirectoryError(f"Dataset not found: {dataset_directory}")
|
|
163
|
+
arguments.dataset = dataset_directory.name
|
|
164
|
+
return dataset_directory
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _build_dataset_mounts(
|
|
168
|
+
dataset_directory: Path | None,
|
|
169
|
+
) -> list[str]:
|
|
170
|
+
mounts = [f"type=tmpfs,dst={CONTAINER_DATASETS_DIRECTORY}"]
|
|
171
|
+
if dataset_directory is None:
|
|
172
|
+
return mounts
|
|
173
|
+
|
|
174
|
+
for entry_name in PUBLIC_DATASET_ENTRY_NAMES:
|
|
175
|
+
source_entry = dataset_directory / entry_name
|
|
176
|
+
if not source_entry.exists():
|
|
177
|
+
continue
|
|
178
|
+
if source_entry.is_symlink():
|
|
179
|
+
raise ValueError(
|
|
180
|
+
f"Dataset entries may not be symlinks: {source_entry}"
|
|
181
|
+
)
|
|
182
|
+
mounts.append(
|
|
183
|
+
f"type=bind,src={source_entry.resolve()},"
|
|
184
|
+
f"dst={CONTAINER_DATASETS_DIRECTORY / dataset_directory.name / entry_name},"
|
|
185
|
+
"readonly"
|
|
186
|
+
)
|
|
187
|
+
return mounts
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _run_agent_in_docker(
|
|
191
|
+
arguments: argparse.Namespace,
|
|
192
|
+
workspace_directory: Path,
|
|
193
|
+
agent_id: str,
|
|
194
|
+
dataset_mounts: list[str],
|
|
195
|
+
) -> int:
|
|
196
|
+
mounts = list(dataset_mounts)
|
|
197
|
+
container_arguments = _build_container_arguments(arguments)
|
|
198
|
+
container_arguments.extend(
|
|
199
|
+
["--datasets-dir", str(CONTAINER_DATASETS_DIRECTORY)]
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
mounts.append(
|
|
203
|
+
f"type=bind,src={workspace_directory},dst={CONTAINER_WORKSPACE_DIRECTORY}"
|
|
204
|
+
)
|
|
205
|
+
container_arguments.extend(
|
|
206
|
+
["--workspace-dir", str(CONTAINER_WORKSPACE_DIRECTORY)]
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
if arguments.fork_from_run is not None:
|
|
210
|
+
fork_directory = arguments.fork_from_run.expanduser().resolve()
|
|
211
|
+
if not fork_directory.is_dir():
|
|
212
|
+
raise NotADirectoryError(f"Fork source directory not found: {fork_directory}")
|
|
213
|
+
mounts.append(
|
|
214
|
+
f"type=bind,src={fork_directory},"
|
|
215
|
+
f"dst={CONTAINER_FORK_SOURCE_DIRECTORY},readonly"
|
|
216
|
+
)
|
|
217
|
+
container_arguments.extend(
|
|
218
|
+
["--fork-from-run", str(CONTAINER_FORK_SOURCE_DIRECTORY)]
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
codex_directory = Path.home() / ".codex"
|
|
222
|
+
if codex_directory.is_dir():
|
|
223
|
+
mounts.append(
|
|
224
|
+
f"type=bind,src={codex_directory.resolve()},"
|
|
225
|
+
f"dst={CONTAINER_CODEX_DIRECTORY},readonly"
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
docker_arguments = _docker_environment_arguments(agent_id)
|
|
229
|
+
if arguments.provider == "ollama":
|
|
230
|
+
docker_arguments.extend(["--network", "host"])
|
|
231
|
+
# A non-zero exit is an expected outcome (for example, no best-iteration
|
|
232
|
+
# snapshot); the workflow already reports it, so return the code instead of
|
|
233
|
+
# raising and let the caller skip post-run steps cleanly.
|
|
234
|
+
return run_python_in_docker(
|
|
235
|
+
image=arguments.image,
|
|
236
|
+
cpu_only=arguments.cpu_only,
|
|
237
|
+
mounts=mounts,
|
|
238
|
+
python_arguments=container_arguments,
|
|
239
|
+
docker_arguments=docker_arguments,
|
|
240
|
+
check=False,
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _run_inference_on_test_input(
|
|
245
|
+
image: str,
|
|
246
|
+
cpu_only: bool,
|
|
247
|
+
workspace_directory: Path,
|
|
248
|
+
test_input: Path,
|
|
249
|
+
) -> None:
|
|
250
|
+
print(f"Evaluating the best iteration on {test_input}")
|
|
251
|
+
run_inference_in_docker(
|
|
252
|
+
argparse.Namespace(
|
|
253
|
+
image=image,
|
|
254
|
+
agent_dir=workspace_directory,
|
|
255
|
+
input_path=test_input,
|
|
256
|
+
output=(
|
|
257
|
+
workspace_directory
|
|
258
|
+
/ Config.BEST_ITERATION_SNAPSHOT_DIRNAME
|
|
259
|
+
/ "eval_predictions_test.csv"
|
|
260
|
+
),
|
|
261
|
+
label_col=None,
|
|
262
|
+
iteration_dir=Path(Config.BEST_ITERATION_SNAPSHOT_DIRNAME),
|
|
263
|
+
all_iterations=False,
|
|
264
|
+
remove_conda_env=False,
|
|
265
|
+
cpu_only=cpu_only,
|
|
266
|
+
)
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _run_test_evaluation_in_docker(
|
|
271
|
+
image: str,
|
|
272
|
+
cpu_only: bool,
|
|
273
|
+
workspace_directory: Path,
|
|
274
|
+
dataset_directory: Path,
|
|
275
|
+
) -> None:
|
|
276
|
+
test_directory = dataset_directory / "test"
|
|
277
|
+
if test_directory.is_dir():
|
|
278
|
+
_run_inference_on_test_input(
|
|
279
|
+
image, cpu_only, workspace_directory, test_directory
|
|
280
|
+
)
|
|
281
|
+
return
|
|
282
|
+
|
|
283
|
+
test_csv = dataset_directory / "test.csv"
|
|
284
|
+
if not test_csv.is_file():
|
|
285
|
+
print(
|
|
286
|
+
f"No test split found at {test_directory} or {test_csv}; "
|
|
287
|
+
"skipping test evaluation."
|
|
288
|
+
)
|
|
289
|
+
return
|
|
290
|
+
|
|
291
|
+
metadata_path = (
|
|
292
|
+
workspace_directory
|
|
293
|
+
/ "prepared_datasets"
|
|
294
|
+
/ dataset_directory.name
|
|
295
|
+
/ "metadata.json"
|
|
296
|
+
)
|
|
297
|
+
metadata = json.loads(metadata_path.read_text(encoding="utf-8"))
|
|
298
|
+
with tempfile.TemporaryDirectory() as temporary_directory:
|
|
299
|
+
prepared_directory = Path(temporary_directory) / dataset_directory.name
|
|
300
|
+
prepare_test_dataset(
|
|
301
|
+
source_dir=dataset_directory,
|
|
302
|
+
destination_dir=prepared_directory,
|
|
303
|
+
task_type=metadata["task_type"],
|
|
304
|
+
input_structure=metadata["input_structure"],
|
|
305
|
+
label_to_scalar=metadata.get("label_to_scalar"),
|
|
306
|
+
label_column=metadata.get("label_column"),
|
|
307
|
+
id_column=metadata.get("id_column"),
|
|
308
|
+
)
|
|
309
|
+
_run_inference_on_test_input(
|
|
310
|
+
image,
|
|
311
|
+
cpu_only,
|
|
312
|
+
workspace_directory,
|
|
313
|
+
prepared_directory / "test",
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def _run_reporting_in_docker(
|
|
318
|
+
image: str,
|
|
319
|
+
workspace_directory: Path,
|
|
320
|
+
agent_id: str,
|
|
321
|
+
) -> None:
|
|
322
|
+
run_python_in_docker(
|
|
323
|
+
image=image,
|
|
324
|
+
cpu_only=True,
|
|
325
|
+
mounts=[
|
|
326
|
+
f"type=bind,src={workspace_directory},"
|
|
327
|
+
f"dst={CONTAINER_WORKSPACE_DIRECTORY}"
|
|
328
|
+
],
|
|
329
|
+
python_arguments=[
|
|
330
|
+
"-m",
|
|
331
|
+
"agentomics.runtime.report_workflow",
|
|
332
|
+
"--workspace-dir",
|
|
333
|
+
str(CONTAINER_WORKSPACE_DIRECTORY),
|
|
334
|
+
"--agent-id",
|
|
335
|
+
agent_id,
|
|
336
|
+
],
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def _is_agent_run(arguments: argparse.Namespace) -> bool:
|
|
341
|
+
# TODO: Replace this implicit check with explicit command modes.
|
|
342
|
+
return not (
|
|
343
|
+
arguments.test
|
|
344
|
+
or arguments.list_models
|
|
345
|
+
or arguments.list_datasets
|
|
346
|
+
or arguments.list_metrics
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
def main() -> int:
|
|
350
|
+
arguments = build_parser().parse_args()
|
|
351
|
+
datasets_directory = (
|
|
352
|
+
arguments.datasets_dir or Path.cwd() / DEFAULT_DATASETS_DIRECTORY
|
|
353
|
+
).expanduser().resolve()
|
|
354
|
+
if not datasets_directory.is_dir():
|
|
355
|
+
raise NotADirectoryError(
|
|
356
|
+
f"Datasets directory not found: {datasets_directory}"
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
if arguments.list_datasets:
|
|
360
|
+
print_datasets_table(get_all_datasets_info(datasets_directory))
|
|
361
|
+
return 0
|
|
362
|
+
|
|
363
|
+
dataset_directory = (
|
|
364
|
+
_resolve_dataset(arguments, datasets_directory)
|
|
365
|
+
if _is_agent_run(arguments)
|
|
366
|
+
else None
|
|
367
|
+
)
|
|
368
|
+
agent_id = create_agent_id()
|
|
369
|
+
workspace_directory = _resolve_workspace(arguments.workspace_dir, agent_id)
|
|
370
|
+
dataset_mounts = _build_dataset_mounts(dataset_directory)
|
|
371
|
+
exit_code = _run_agent_in_docker(
|
|
372
|
+
arguments,
|
|
373
|
+
workspace_directory,
|
|
374
|
+
agent_id,
|
|
375
|
+
dataset_mounts,
|
|
376
|
+
)
|
|
377
|
+
if _is_agent_run(arguments) and exit_code == 0:
|
|
378
|
+
try:
|
|
379
|
+
_run_test_evaluation_in_docker(
|
|
380
|
+
image=arguments.image,
|
|
381
|
+
cpu_only=arguments.cpu_only,
|
|
382
|
+
workspace_directory=workspace_directory,
|
|
383
|
+
dataset_directory=dataset_directory,
|
|
384
|
+
)
|
|
385
|
+
finally:
|
|
386
|
+
_run_reporting_in_docker(
|
|
387
|
+
arguments.image,
|
|
388
|
+
workspace_directory,
|
|
389
|
+
agent_id,
|
|
390
|
+
)
|
|
391
|
+
return exit_code
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
if __name__ == "__main__":
|
|
395
|
+
sys.exit(main())
|