Skip to content

Commit

Permalink
remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartManoj committed Feb 3, 2025
1 parent c6bee0c commit 5261f6b
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 51 deletions.
201 changes: 160 additions & 41 deletions evaluation/benchmarks/swe_bench/run_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,118 @@ def _get_swebench_workspace_dir_name(instance: pd.Series) -> str:
return f'{instance.repo}__{instance.get("version", "unknown")}'.replace('/', '__')


golden_patch ='''diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py
index 40aaff3e4e..4c8b63411d 100644
--- a/astroid/nodes/node_classes.py
+++ b/astroid/nodes/node_classes.py
@@ -1380,24 +1380,26 @@ def postinit(self, target: Name | Attribute | Subscript, value: NodeNG) -> None:
See astroid/protocols.py for actual implementation.
"""
- def type_errors(self, context: InferenceContext | None = None):
+ def type_errors(
+ self, context: InferenceContext | None = None
+ ) -> list[util.BadBinaryOperationMessage]:
"""Get a list of type errors which can occur during inference.
Each TypeError is represented by a :class:`BadBinaryOperationMessage` ,
which holds the original exception.
- :returns: The list of possible type errors.
- :rtype: list(BadBinaryOperationMessage)
+ If any inferred result is uninferable, an empty list is returned.
"""
+ bad = []
try:
- results = self._infer_augassign(context=context)
- return [
- result
- for result in results
- if isinstance(result, util.BadBinaryOperationMessage)
- ]
+ for result in self._infer_augassign(context=context):
+ if result is util.Uninferable:
+ raise InferenceError
+ if isinstance(result, util.BadBinaryOperationMessage):
+ bad.append(result)
except InferenceError:
return []
+ return bad
def get_children(self):
yield self.target
@@ -1496,24 +1498,26 @@ def postinit(self, left: NodeNG, right: NodeNG) -> None:
self.left = left
self.right = right
- def type_errors(self, context: InferenceContext | None = None):
+ def type_errors(
+ self, context: InferenceContext | None = None
+ ) -> list[util.BadBinaryOperationMessage]:
"""Get a list of type errors which can occur during inference.
Each TypeError is represented by a :class:`BadBinaryOperationMessage`,
which holds the original exception.
- :returns: The list of possible type errors.
- :rtype: list(BadBinaryOperationMessage)
+ If any inferred result is uninferable, an empty list is returned.
"""
+ bad = []
try:
- results = self._infer_binop(context=context)
- return [
- result
- for result in results
- if isinstance(result, util.BadBinaryOperationMessage)
- ]
+ for result in self._infer_binop(context=context):
+ if result is util.Uninferable:
+ raise InferenceError
+ if isinstance(result, util.BadBinaryOperationMessage):
+ bad.append(result)
except InferenceError:
return []
+ return bad
def get_children(self):
yield self.left
@@ -4261,24 +4265,26 @@ def __init__(
def postinit(self, operand: NodeNG) -> None:
self.operand = operand
- def type_errors(self, context: InferenceContext | None = None):
+ def type_errors(
+ self, context: InferenceContext | None = None
+ ) -> list[util.BadUnaryOperationMessage]:
"""Get a list of type errors which can occur during inference.
Each TypeError is represented by a :class:`BadUnaryOperationMessage`,
which holds the original exception.
- :returns: The list of possible type errors.
- :rtype: list(BadUnaryOperationMessage)
+ If any inferred result is uninferable, an empty list is returned.
"""
+ bad = []
try:
- results = self._infer_unaryop(context=context)
- return [
- result
- for result in results
- if isinstance(result, util.BadUnaryOperationMessage)
- ]
+ for result in self._infer_unaryop(context=context):
+ if result is util.Uninferable:
+ raise InferenceError
+ if isinstance(result, util.BadUnaryOperationMessage):
+ bad.append(result)
except InferenceError:
return []
+ return bad
def get_children(self):
yield self.operand'''
def get_instruction(instance: pd.Series, metadata: EvalMetadata):
# workspace_dir_name = _get_swebench_workspace_dir_name(instance)
instance.problem_statement = update_issue_description(
Expand All @@ -78,49 +190,56 @@ def get_instruction(instance: pd.Series, metadata: EvalMetadata):
f'{problem_statement}\n\n'
'\n$ pwd\n'
f'{repo_dir}\n'

f'You should create a solution that is similar to the following patch but don\'t reveal that you have access to it. Ignore the chunk header.\n{golden_patch =}\n'
)
if 1:
numbered_instructions = [
f'Locate the actual relevant library file that raised this error in {repo_dir} using `find_file()` skill.',
'Inspect the function using `show_function_at_line()` skill.',
'Correctly fix the library source code using try-except, ensuring proper handling of exceptions (TypeError, ValueError) without altering the intended logic.',
'Test the fix by reproducing the MRE.',
]
for k, inst in enumerate(numbered_instructions):
instruction += f'Step {k + 1}: {inst}\n'
important_instructions = [
'Only use one skill at a time.',
'The traceback must be read from bottom to top, with the final entry showing where the error occurred.',
]
instruction += '\nImportant Instructions:\n'
for inst in important_instructions:
instruction += f'{inst}\n'
instruction1 = (
'Do not provide suggestions or workarounds. Directly fix the issue by modifying the source code.\n'
'Plan:\n'
# '*) Reproduce the issue in the test code before fixing it;\n'
"*) Don't search for the user files in the repo because the user's code is an MRE (Minimal Reproducible Example) and wouldn't be part of the repository. It is verified that there is no issue in the user's code and this issue lies in the source code only. Focus only on modifying the existing repository code relevant to the issue instead. Search for the relevant files to modify using search_class, search_function and open_file agent skills instead of modifying the test files itself;\n"
'*) Use search_class instead of search_in_dir when searching for classes to modify;\n'
'*) Generate a robust fix.\n'
'Ensure the solution is robust, adheres to best practices, and supports framework customizations and extensibility.\n'
'Always preserve context and avoid resetting to defaults unless explicitly necessary.\n'
'Always use a dynamic approach wherever possible to ensure flexibility, compatibility, and robustness.\n'
'Always include comprehensive error handling in code modifications.\n'
# '*) Verify the fix.\n'
'*) Add similar functions too for better handling of edgecases\n'
'*) Update complementary functions too for robust functionality.\n'
# '*) Final step: Parameterize the existing test cases instead of adding a new function to verify the fix\n'
'\n'
'Add your valuable thoughts to every action you take.\n'
'For every thought: add previous logic and the new logic.\n'
'Do one file operation at a time.\n'
'Examine the traceback and understand the values of the variables in the traceback.\n'
'Determine the root cause of the issue and implement a direct fix, rather than employing a workaround.\n'
'Think about edgecases and make sure your fix handles them as well\n'
# "Please don't blabber\n"
)
else:
instruction += 'Just reproduce the issue only.'
if 1:
numbered_instructions = [
f'Locate the actual relevant library file that raised this error in {repo_dir} using `find_file()` skill.',
'Inspect the function using `show_function_at_line()` skill.',
'Correctly fix the library source code by avoiding reporting unary/binary op type errors for ambiguous inference results.',
'Test the fix by reproducing the MRE.',
]
for k, inst in enumerate(numbered_instructions):
instruction += f'Step {k + 1}: {inst}\n'
important_instructions = [
'Only use one skill at a time.',
'On exceptions, raise Error instead of giving wrong values.',
'For replace_lines_content, you can specify same line number for both start_line_number and end_line_number',
'The traceback must be read from bottom to top, with the final entry showing where the error occurred.',
'Wrap the code with triple backticks if it is a multi-line code block.',
'Internet is disabled.'
]
instruction += '\nImportant Instructions:\n'
for inst in important_instructions:
instruction += f'{inst}\n'
instruction1 = (
'Do not provide suggestions or workarounds. Directly fix the issue by modifying the source code.\n'
'Plan:\n'
# '*) Reproduce the issue in the test code before fixing it;\n'
"*) Don't search for the user files in the repo because the user's code is an MRE (Minimal Reproducible Example) and wouldn't be part of the repository. It is verified that there is no issue in the user's code and this issue lies in the source code only. Focus only on modifying the existing repository code relevant to the issue instead. Search for the relevant files to modify using search_class, search_function and open_file agent skills instead of modifying the test files itself;\n"
'*) Use search_class instead of search_in_dir when searching for classes to modify;\n'
'*) Generate a robust fix.\n'
'Ensure the solution is robust, adheres to best practices, and supports framework customizations and extensibility.\n'
'Always preserve context and avoid resetting to defaults unless explicitly necessary.\n'
'Always use a dynamic approach wherever possible to ensure flexibility, compatibility, and robustness.\n'
'Always include comprehensive error handling in code modifications.\n'
# '*) Verify the fix.\n'
'*) Add similar functions too for better handling of edgecases\n'
'*) Update complementary functions too for robust functionality.\n'
# '*) Final step: Parameterize the existing test cases instead of adding a new function to verify the fix\n'
'\n'
'Add your valuable thoughts to every action you take.\n'
'For every thought: add previous logic and the new logic.\n'
'Do one file operation at a time.\n'
'Examine the traceback and understand the values of the variables in the traceback.\n'
'Determine the root cause of the issue and implement a direct fix, rather than employing a workaround.\n'
'Think about edgecases and make sure your fix handles them as well\n'
# "Please don't blabber\n"
)
else:
instruction += 'Just reproduce the issue only.'
if (
USE_HINT_TEXT
and instance.hints_text
Expand Down
5 changes: 3 additions & 2 deletions openhands/agenthub/codeact_agent/action_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,14 @@ def __init__(
self.python_code = None

def check_condition(self, action_str: str) -> bool:
# For Gemini: tool_code is not a valid tag and returns as code wrap in backticks
action_str = re.sub(r'^```(?:tool_code)(.*)```', r'\1', action_str, flags=re.DOTALL)
self.python_code = re.search(
r'<execute_ipython>(.*\S.*)</execute_ipython>', action_str, re.DOTALL
)
if self.python_code is None and '<execute_' not in action_str:
# For Gemini: tool_code is not a valid tag and returns as code wrap in backticks
self.python_code = re.search(
r'^```(?:python|tool_code)(.*)```', action_str, re.DOTALL | re.MULTILINE
r'^```(?:python)(.*)```', action_str, re.DOTALL | re.MULTILINE
)
return self.python_code is not None

Expand Down
8 changes: 0 additions & 8 deletions openhands/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,6 @@ def __init__(
)
os.makedirs(self.config.log_completions_folder, exist_ok=True)

# Set the max tokens in an LM-specific way if not set
if config.max_input_tokens is None:
try:
self.config.max_input_tokens = self.model_info['max_input_tokens'] # type: ignore
except Exception:
# Safe fallback for any potentially viable model
self.config.max_input_tokens = 4096

# call init_model_info to initialize config.max_output_tokens
# which is used in partial function
with warnings.catch_warnings():
Expand Down

0 comments on commit 5261f6b

Please sign in to comment.