Dixing (Dex) Xu
commited on
:rotating_light: Add black formatter (#25)
Browse files* :rotating_light: Add black formatter
* Add black to linter
* Fix the formatting
* :rotating_light: Update black linter to see the suggestion change
- .github/workflows/linter.yml +8 -4
- .gitignore +4 -1
- aide/__init__.py +10 -5
- aide/backend/backend_anthropic.py +1 -0
- aide/backend/backend_openai.py +2 -0
- aide/backend/utils.py +1 -1
- aide/interpreter.py +5 -1
- aide/journal2report.py +1 -0
- aide/run.py +9 -4
- aide/utils/config.py +16 -7
- aide/utils/tree_export.py +1 -1
.github/workflows/linter.yml
CHANGED
|
@@ -6,7 +6,7 @@ on:
|
|
| 6 |
|
| 7 |
jobs:
|
| 8 |
lint-python:
|
| 9 |
-
name:
|
| 10 |
runs-on: ubuntu-latest
|
| 11 |
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
| 12 |
steps:
|
|
@@ -15,7 +15,11 @@ jobs:
|
|
| 15 |
- uses: actions/setup-python@v5
|
| 16 |
with:
|
| 17 |
python-version: 3.11
|
| 18 |
-
- name: Install
|
| 19 |
-
run:
|
|
|
|
|
|
|
| 20 |
- name: Run Ruff
|
| 21 |
-
run: ruff check --output-format=github aide/
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
jobs:
|
| 8 |
lint-python:
|
| 9 |
+
name: Python Linting
|
| 10 |
runs-on: ubuntu-latest
|
| 11 |
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
| 12 |
steps:
|
|
|
|
| 15 |
- uses: actions/setup-python@v5
|
| 16 |
with:
|
| 17 |
python-version: 3.11
|
| 18 |
+
- name: Install Dependencies
|
| 19 |
+
run: |
|
| 20 |
+
pip install ruff==0.7.1
|
| 21 |
+
pip install black==24.3.0
|
| 22 |
- name: Run Ruff
|
| 23 |
+
run: ruff check --output-format=github aide/
|
| 24 |
+
- name: Run Black
|
| 25 |
+
run: black --check --diff aide/
|
.gitignore
CHANGED
|
@@ -164,4 +164,7 @@ workspaces
|
|
| 164 |
logs
|
| 165 |
|
| 166 |
.DS_STORE
|
| 167 |
-
.trunk
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
logs
|
| 165 |
|
| 166 |
.DS_STORE
|
| 167 |
+
.trunk
|
| 168 |
+
|
| 169 |
+
.gradio/
|
| 170 |
+
.ruff_cache/
|
aide/__init__.py
CHANGED
|
@@ -5,13 +5,21 @@ from .interpreter import Interpreter
|
|
| 5 |
from .journal import Journal
|
| 6 |
from omegaconf import OmegaConf
|
| 7 |
from rich.status import Status
|
| 8 |
-
from .utils.config import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
@dataclass
|
| 11 |
class Solution:
|
| 12 |
code: str
|
| 13 |
valid_metric: float
|
| 14 |
|
|
|
|
| 15 |
class Experiment:
|
| 16 |
|
| 17 |
def __init__(self, data_dir: str, goal: str, eval: str | None = None):
|
|
@@ -22,7 +30,7 @@ class Experiment:
|
|
| 22 |
goal (str): Description of the goal of the task.
|
| 23 |
eval (str | None, optional): Optional description of the preferred way for the agent to evaluate its solutions.
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
_cfg = _load_cfg(use_cli_args=False)
|
| 27 |
_cfg.data_dir = data_dir
|
| 28 |
_cfg.goal = goal
|
|
@@ -52,6 +60,3 @@ class Experiment:
|
|
| 52 |
|
| 53 |
best_node = self.journal.get_best_node(only_good=False)
|
| 54 |
return Solution(code=best_node.code, valid_metric=best_node.metric.value)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 5 |
from .journal import Journal
|
| 6 |
from omegaconf import OmegaConf
|
| 7 |
from rich.status import Status
|
| 8 |
+
from .utils.config import (
|
| 9 |
+
load_task_desc,
|
| 10 |
+
prep_agent_workspace,
|
| 11 |
+
save_run,
|
| 12 |
+
_load_cfg,
|
| 13 |
+
prep_cfg,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
|
| 17 |
@dataclass
|
| 18 |
class Solution:
|
| 19 |
code: str
|
| 20 |
valid_metric: float
|
| 21 |
|
| 22 |
+
|
| 23 |
class Experiment:
|
| 24 |
|
| 25 |
def __init__(self, data_dir: str, goal: str, eval: str | None = None):
|
|
|
|
| 30 |
goal (str): Description of the goal of the task.
|
| 31 |
eval (str | None, optional): Optional description of the preferred way for the agent to evaluate its solutions.
|
| 32 |
"""
|
| 33 |
+
|
| 34 |
_cfg = _load_cfg(use_cli_args=False)
|
| 35 |
_cfg.data_dir = data_dir
|
| 36 |
_cfg.goal = goal
|
|
|
|
| 60 |
|
| 61 |
best_node = self.journal.get_best_node(only_good=False)
|
| 62 |
return Solution(code=best_node.code, valid_metric=best_node.metric.value)
|
|
|
|
|
|
|
|
|
aide/backend/backend_anthropic.py
CHANGED
|
@@ -21,6 +21,7 @@ def _setup_anthropic_client():
|
|
| 21 |
global _client
|
| 22 |
_client = anthropic.Anthropic(max_retries=0)
|
| 23 |
|
|
|
|
| 24 |
def query(
|
| 25 |
system_message: str | None,
|
| 26 |
user_message: str | None,
|
|
|
|
| 21 |
global _client
|
| 22 |
_client = anthropic.Anthropic(max_retries=0)
|
| 23 |
|
| 24 |
+
|
| 25 |
def query(
|
| 26 |
system_message: str | None,
|
| 27 |
user_message: str | None,
|
aide/backend/backend_openai.py
CHANGED
|
@@ -19,11 +19,13 @@ OPENAI_TIMEOUT_EXCEPTIONS = (
|
|
| 19 |
openai.InternalServerError,
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
@once
|
| 23 |
def _setup_openai_client():
|
| 24 |
global _client
|
| 25 |
_client = openai.OpenAI(max_retries=0)
|
| 26 |
|
|
|
|
| 27 |
def query(
|
| 28 |
system_message: str | None,
|
| 29 |
user_message: str | None,
|
|
|
|
| 19 |
openai.InternalServerError,
|
| 20 |
)
|
| 21 |
|
| 22 |
+
|
| 23 |
@once
|
| 24 |
def _setup_openai_client():
|
| 25 |
global _client
|
| 26 |
_client = openai.OpenAI(max_retries=0)
|
| 27 |
|
| 28 |
+
|
| 29 |
def query(
|
| 30 |
system_message: str | None,
|
| 31 |
user_message: str | None,
|
aide/backend/utils.py
CHANGED
|
@@ -11,7 +11,6 @@ FunctionCallType = dict
|
|
| 11 |
OutputType = str | FunctionCallType
|
| 12 |
|
| 13 |
|
| 14 |
-
|
| 15 |
logger = logging.getLogger("aide")
|
| 16 |
|
| 17 |
|
|
@@ -29,6 +28,7 @@ def backoff_create(
|
|
| 29 |
logger.info(f"Backoff exception: {e}")
|
| 30 |
return False
|
| 31 |
|
|
|
|
| 32 |
def opt_messages_to_list(
|
| 33 |
system_message: str | None, user_message: str | None
|
| 34 |
) -> list[dict[str, str]]:
|
|
|
|
| 11 |
OutputType = str | FunctionCallType
|
| 12 |
|
| 13 |
|
|
|
|
| 14 |
logger = logging.getLogger("aide")
|
| 15 |
|
| 16 |
|
|
|
|
| 28 |
logger.info(f"Backoff exception: {e}")
|
| 29 |
return False
|
| 30 |
|
| 31 |
+
|
| 32 |
def opt_messages_to_list(
|
| 33 |
system_message: str | None, user_message: str | None
|
| 34 |
) -> list[dict[str, str]]:
|
aide/interpreter.py
CHANGED
|
@@ -49,7 +49,11 @@ def exception_summary(e, working_dir, exec_file_name, format_tb_ipython):
|
|
| 49 |
tb_lines = traceback.format_exception(e)
|
| 50 |
# skip parts of stack trace in weflow code
|
| 51 |
tb_str = "".join(
|
| 52 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
# tb_str = "".join([l for l in tb_lines])
|
| 55 |
|
|
|
|
| 49 |
tb_lines = traceback.format_exception(e)
|
| 50 |
# skip parts of stack trace in weflow code
|
| 51 |
tb_str = "".join(
|
| 52 |
+
[
|
| 53 |
+
line
|
| 54 |
+
for line in tb_lines
|
| 55 |
+
if "aide/" not in line and "importlib" not in line
|
| 56 |
+
]
|
| 57 |
)
|
| 58 |
# tb_str = "".join([l for l in tb_lines])
|
| 59 |
|
aide/journal2report.py
CHANGED
|
@@ -2,6 +2,7 @@ from .backend import query
|
|
| 2 |
from .journal import Journal
|
| 3 |
from .utils.config import StageConfig
|
| 4 |
|
|
|
|
| 5 |
def journal2report(journal: Journal, task_desc: dict, rcfg: StageConfig):
|
| 6 |
"""
|
| 7 |
Generate a report from a journal, the report will be in markdown format.
|
|
|
|
| 2 |
from .journal import Journal
|
| 3 |
from .utils.config import StageConfig
|
| 4 |
|
| 5 |
+
|
| 6 |
def journal2report(journal: Journal, task_desc: dict, rcfg: StageConfig):
|
| 7 |
"""
|
| 8 |
Generate a report from a journal, the report will be in markdown format.
|
aide/run.py
CHANGED
|
@@ -28,6 +28,7 @@ from .utils.config import load_task_desc, prep_agent_workspace, save_run, load_c
|
|
| 28 |
|
| 29 |
logger = logging.getLogger("aide")
|
| 30 |
|
|
|
|
| 31 |
def journal_to_rich_tree(journal: Journal):
|
| 32 |
best_node = journal.get_best_node()
|
| 33 |
|
|
@@ -36,7 +37,7 @@ def journal_to_rich_tree(journal: Journal):
|
|
| 36 |
s = "[red]◍ bug"
|
| 37 |
else:
|
| 38 |
style = "bold " if node is best_node else ""
|
| 39 |
-
|
| 40 |
if node is best_node:
|
| 41 |
s = f"[{style}green]● {node.metric.value:.3f} (best)"
|
| 42 |
else:
|
|
@@ -51,6 +52,7 @@ def journal_to_rich_tree(journal: Journal):
|
|
| 51 |
append_rec(n, tree)
|
| 52 |
return tree
|
| 53 |
|
|
|
|
| 54 |
def run():
|
| 55 |
cfg = load_cfg()
|
| 56 |
logger.info(f'Starting run "{cfg.exp_name}"')
|
|
@@ -64,6 +66,7 @@ def run():
|
|
| 64 |
def cleanup():
|
| 65 |
if global_step == 0:
|
| 66 |
shutil.rmtree(cfg.workspace_dir)
|
|
|
|
| 67 |
atexit.register(cleanup)
|
| 68 |
|
| 69 |
journal = Journal()
|
|
@@ -101,7 +104,9 @@ def run():
|
|
| 101 |
f"Agent workspace directory:\n[yellow]▶ {str(cfg.workspace_dir)}",
|
| 102 |
f"Experiment log directory:\n[yellow]▶ {str(cfg.log_dir)}",
|
| 103 |
]
|
| 104 |
-
left = Group(
|
|
|
|
|
|
|
| 105 |
right = tree
|
| 106 |
wide = Group(*file_paths)
|
| 107 |
|
|
@@ -133,10 +138,10 @@ def run():
|
|
| 133 |
print("Generating final report from journal...")
|
| 134 |
report = journal2report(journal, task_desc, cfg.report)
|
| 135 |
print(report)
|
| 136 |
-
report_file_path = cfg.log_dir /
|
| 137 |
with open(report_file_path, "w") as f:
|
| 138 |
f.write(report)
|
| 139 |
-
print(
|
| 140 |
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
|
|
|
| 28 |
|
| 29 |
logger = logging.getLogger("aide")
|
| 30 |
|
| 31 |
+
|
| 32 |
def journal_to_rich_tree(journal: Journal):
|
| 33 |
best_node = journal.get_best_node()
|
| 34 |
|
|
|
|
| 37 |
s = "[red]◍ bug"
|
| 38 |
else:
|
| 39 |
style = "bold " if node is best_node else ""
|
| 40 |
+
|
| 41 |
if node is best_node:
|
| 42 |
s = f"[{style}green]● {node.metric.value:.3f} (best)"
|
| 43 |
else:
|
|
|
|
| 52 |
append_rec(n, tree)
|
| 53 |
return tree
|
| 54 |
|
| 55 |
+
|
| 56 |
def run():
|
| 57 |
cfg = load_cfg()
|
| 58 |
logger.info(f'Starting run "{cfg.exp_name}"')
|
|
|
|
| 66 |
def cleanup():
|
| 67 |
if global_step == 0:
|
| 68 |
shutil.rmtree(cfg.workspace_dir)
|
| 69 |
+
|
| 70 |
atexit.register(cleanup)
|
| 71 |
|
| 72 |
journal = Journal()
|
|
|
|
| 104 |
f"Agent workspace directory:\n[yellow]▶ {str(cfg.workspace_dir)}",
|
| 105 |
f"Experiment log directory:\n[yellow]▶ {str(cfg.log_dir)}",
|
| 106 |
]
|
| 107 |
+
left = Group(
|
| 108 |
+
Panel(Text(task_desc_str.strip()), title="Task description"), prog, status
|
| 109 |
+
)
|
| 110 |
right = tree
|
| 111 |
wide = Group(*file_paths)
|
| 112 |
|
|
|
|
| 138 |
print("Generating final report from journal...")
|
| 139 |
report = journal2report(journal, task_desc, cfg.report)
|
| 140 |
print(report)
|
| 141 |
+
report_file_path = cfg.log_dir / "report.md"
|
| 142 |
with open(report_file_path, "w") as f:
|
| 143 |
f.write(report)
|
| 144 |
+
print("Report written to file:", report_file_path)
|
| 145 |
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
aide/utils/config.py
CHANGED
|
@@ -58,6 +58,7 @@ class ExecConfig:
|
|
| 58 |
agent_file_name: str
|
| 59 |
format_tb_ipython: bool
|
| 60 |
|
|
|
|
| 61 |
@dataclass
|
| 62 |
class Config(Hashable):
|
| 63 |
data_dir: Path
|
|
@@ -91,7 +92,10 @@ def _get_next_logindex(dir: Path) -> int:
|
|
| 91 |
pass
|
| 92 |
return max_index + 1
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
cfg = OmegaConf.load(path)
|
| 96 |
if use_cli_args:
|
| 97 |
cfg = OmegaConf.merge(cfg, OmegaConf.from_cli())
|
|
@@ -106,9 +110,11 @@ def load_cfg(path: Path = Path(__file__).parent / "config.yaml") -> Config:
|
|
| 106 |
def prep_cfg(cfg: Config):
|
| 107 |
if cfg.data_dir is None:
|
| 108 |
raise ValueError("`data_dir` must be provided.")
|
| 109 |
-
|
| 110 |
if cfg.desc_file is None and cfg.goal is None:
|
| 111 |
-
raise ValueError(
|
|
|
|
|
|
|
| 112 |
|
| 113 |
if cfg.data_dir.startswith("example_tasks/"):
|
| 114 |
cfg.data_dir = Path(__file__).parent.parent / cfg.data_dir
|
|
@@ -151,13 +157,15 @@ def load_task_desc(cfg: Config):
|
|
| 151 |
logger.warning(
|
| 152 |
"Ignoring goal and eval args because task description file is provided."
|
| 153 |
)
|
| 154 |
-
|
| 155 |
with open(cfg.desc_file) as f:
|
| 156 |
return f.read()
|
| 157 |
-
|
| 158 |
# or generate it from the goal and eval args
|
| 159 |
if cfg.goal is None:
|
| 160 |
-
raise ValueError(
|
|
|
|
|
|
|
| 161 |
|
| 162 |
task_desc = {"Task goal": cfg.goal}
|
| 163 |
if cfg.eval is not None:
|
|
@@ -165,6 +173,7 @@ def load_task_desc(cfg: Config):
|
|
| 165 |
|
| 166 |
return task_desc
|
| 167 |
|
|
|
|
| 168 |
def prep_agent_workspace(cfg: Config):
|
| 169 |
"""Setup the agent's workspace and preprocess data if necessary."""
|
| 170 |
(cfg.workspace_dir / "input").mkdir(parents=True, exist_ok=True)
|
|
@@ -177,7 +186,7 @@ def prep_agent_workspace(cfg: Config):
|
|
| 177 |
|
| 178 |
def save_run(cfg: Config, journal):
|
| 179 |
cfg.log_dir.mkdir(parents=True, exist_ok=True)
|
| 180 |
-
|
| 181 |
# save journal
|
| 182 |
serialize.dump_json(journal, cfg.log_dir / "journal.json")
|
| 183 |
# save config
|
|
|
|
| 58 |
agent_file_name: str
|
| 59 |
format_tb_ipython: bool
|
| 60 |
|
| 61 |
+
|
| 62 |
@dataclass
|
| 63 |
class Config(Hashable):
|
| 64 |
data_dir: Path
|
|
|
|
| 92 |
pass
|
| 93 |
return max_index + 1
|
| 94 |
|
| 95 |
+
|
| 96 |
+
def _load_cfg(
|
| 97 |
+
path: Path = Path(__file__).parent / "config.yaml", use_cli_args=True
|
| 98 |
+
) -> Config:
|
| 99 |
cfg = OmegaConf.load(path)
|
| 100 |
if use_cli_args:
|
| 101 |
cfg = OmegaConf.merge(cfg, OmegaConf.from_cli())
|
|
|
|
| 110 |
def prep_cfg(cfg: Config):
|
| 111 |
if cfg.data_dir is None:
|
| 112 |
raise ValueError("`data_dir` must be provided.")
|
| 113 |
+
|
| 114 |
if cfg.desc_file is None and cfg.goal is None:
|
| 115 |
+
raise ValueError(
|
| 116 |
+
"You must provide either a description of the task goal (`goal=...`) or a path to a plaintext file containing the description (`desc_file=...`)."
|
| 117 |
+
)
|
| 118 |
|
| 119 |
if cfg.data_dir.startswith("example_tasks/"):
|
| 120 |
cfg.data_dir = Path(__file__).parent.parent / cfg.data_dir
|
|
|
|
| 157 |
logger.warning(
|
| 158 |
"Ignoring goal and eval args because task description file is provided."
|
| 159 |
)
|
| 160 |
+
|
| 161 |
with open(cfg.desc_file) as f:
|
| 162 |
return f.read()
|
| 163 |
+
|
| 164 |
# or generate it from the goal and eval args
|
| 165 |
if cfg.goal is None:
|
| 166 |
+
raise ValueError(
|
| 167 |
+
"`goal` (and optionally `eval`) must be provided if a task description file is not provided."
|
| 168 |
+
)
|
| 169 |
|
| 170 |
task_desc = {"Task goal": cfg.goal}
|
| 171 |
if cfg.eval is not None:
|
|
|
|
| 173 |
|
| 174 |
return task_desc
|
| 175 |
|
| 176 |
+
|
| 177 |
def prep_agent_workspace(cfg: Config):
|
| 178 |
"""Setup the agent's workspace and preprocess data if necessary."""
|
| 179 |
(cfg.workspace_dir / "input").mkdir(parents=True, exist_ok=True)
|
|
|
|
| 186 |
|
| 187 |
def save_run(cfg: Config, journal):
|
| 188 |
cfg.log_dir.mkdir(parents=True, exist_ok=True)
|
| 189 |
+
|
| 190 |
# save journal
|
| 191 |
serialize.dump_json(journal, cfg.log_dir / "journal.json")
|
| 192 |
# save config
|
aide/utils/tree_export.py
CHANGED
|
@@ -62,7 +62,7 @@ def cfg_to_tree_struct(cfg, jou: Journal):
|
|
| 62 |
|
| 63 |
def generate_html(tree_graph_str: str):
|
| 64 |
template_dir = Path(__file__).parent / "viz_templates"
|
| 65 |
-
|
| 66 |
with open(template_dir / "template.js") as f:
|
| 67 |
js = f.read()
|
| 68 |
js = js.replace("<placeholder>", tree_graph_str)
|
|
|
|
| 62 |
|
| 63 |
def generate_html(tree_graph_str: str):
|
| 64 |
template_dir = Path(__file__).parent / "viz_templates"
|
| 65 |
+
|
| 66 |
with open(template_dir / "template.js") as f:
|
| 67 |
js = f.read()
|
| 68 |
js = js.replace("<placeholder>", tree_graph_str)
|