refactor: Use file dictionaries for diff generation in feedback module (#574)

This commit is contained in:
you-n-g
2025-02-10 09:03:13 +08:00
committed by GitHub
parent 61e9089519
commit e2803e096b
2 changed files with 8 additions and 19 deletions
+5 -13
View File
@@ -14,7 +14,7 @@ from rdagent.scenarios.data_science.experiment.experiment import DSExperiment
from rdagent.scenarios.data_science.proposal.exp_gen import DSTrace
from rdagent.utils import convert2bool, remove_path_info_from_str
from rdagent.utils.agent.tpl import T
from rdagent.utils.repo.diff import generate_diff
from rdagent.utils.repo.diff import generate_diff_from_dict
class DSExperiment2Feedback(Experiment2Feedback):
@@ -40,22 +40,14 @@ class DSExperiment2Feedback(Experiment2Feedback):
# Retrieve the last experiment from the history
last_exp = trace.hist[-1][0] if trace.hist else None
if last_exp:
last_workspace_path = last_exp.experiment_workspace.workspace_path
current_workspace_path = exp.experiment_workspace.workspace_path
if last_exp and last_exp.experiment_workspace and exp.experiment_workspace:
# Generate a diff between the two workspaces
diff_edition = generate_diff(last_workspace_path, current_workspace_path)
last_exp_files = last_exp.experiment_workspace.file_dict
current_exp_files = exp.experiment_workspace.file_dict
diff_edition = generate_diff_from_dict(last_exp_files, current_exp_files)
else:
diff_edition = []
diff_edition = [
remove_path_info_from_str(
exp.experiment_workspace.workspace_path,
remove_path_info_from_str(last_exp.experiment_workspace.workspace_path, line),
)
for line in diff_edition
]
# assumption:
# The feedback should focus on experiment **improving**.
# Assume that all the the sota exp is based on the previous sota experiment
+3 -6
View File
@@ -3,9 +3,6 @@ import fnmatch
from pathlib import Path
# support two interfaces. add another one like def generate_diff(file_dict1: dict, file_dict2: dict) -> List[str]:
# the file_dict1 and file_dict2 are like {file_path: file_content}.
# the two shuld share code.
def generate_diff(dir1: str, dir2: str, file_pattern: str = "*.py") -> list[str]:
"""
Generate a diff between two directories (from dir1 to dir2) using files that match the specified file pattern.
@@ -20,8 +17,8 @@ def generate_diff(dir1: str, dir2: str, file_pattern: str = "*.py") -> list[str]
list[str]: A list of diffs for files that differ between the two directories.
"""
dir1_files = {f.relative_to(dir1) for f in Path(dir1).rglob("*") if f.is_file()}
dir2_files = {f.relative_to(dir2) for f in Path(dir2).rglob("*") if f.is_file()}
dir1_files = {f.relative_to(dir1) for f in Path(dir1).rglob(file_pattern) if f.is_file()}
dir2_files = {f.relative_to(dir2) for f in Path(dir2).rglob(file_pattern) if f.is_file()}
all_files = dir1_files.union(dir2_files)
file_dict1 = {}
@@ -39,7 +36,7 @@ def generate_diff(dir1: str, dir2: str, file_pattern: str = "*.py") -> list[str]
file_dict2[str(file)] = f2.read()
else:
file_dict2[str(file)] = ""
return generate_diff_from_dict(file_dict1, file_dict2, file_pattern=file_pattern)
return generate_diff_from_dict(file_dict1, file_dict2, file_pattern="*")
def generate_diff_from_dict(file_dict1: dict, file_dict2: dict, file_pattern: str = "*.py") -> list[str]: