run_infer.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import asyncio
  2. import json
  3. import logging
  4. import os
  5. import pathlib
  6. from functools import partial
  7. import pandas as pd
  8. from datasets import load_dataset
  9. from evaluation.biocoder.biocoder_env_box import BiocoderData, BiocoderSSHBox
  10. from evaluation.utils.shared import (
  11. EvalMetadata,
  12. codeact_user_response,
  13. make_metadata,
  14. prepare_dataset,
  15. run_evaluation,
  16. )
  17. from opendevin.controller.agent import Agent
  18. from opendevin.controller.state.state import State
  19. from opendevin.core.config import config, get_llm_config_arg, parse_arguments
  20. from opendevin.core.logger import get_console_handler
  21. from opendevin.core.logger import opendevin_logger as logger
  22. from opendevin.core.main import run_agent_controller
  23. from opendevin.llm.llm import LLM
  24. AGENT_CLS_TO_FAKE_USER_RESPONSE_FN = {
  25. 'CodeActAgent': partial(
  26. codeact_user_response, encapsulate_solution=True, try_parse=None
  27. ),
  28. }
  29. AGENT_CLS_TO_INST_SUFFIX = {
  30. 'CodeActAgent': 'When you think you have fixed the issue through code changes, please run the following command: <execute_bash> exit </execute_bash>.\n'
  31. }
  32. def get_test_result(instance, sandbox, workspace_dir_name):
  33. test_result = {'result': {}, 'metadata': {}}
  34. try:
  35. code = sandbox.get_changed_code(include_signature=True)
  36. sandbox.copy_changed_code()
  37. test_result['metadata']['1_copy_change_success'] = True
  38. test_result['metadata']['1_copy_change_code'] = code
  39. except Exception:
  40. logger.error('Error fetching changed code for this instance')
  41. test_result['metadata']['1_copy_change_success'] = False
  42. test_result['metadata']['1_copy_change_code'] = None
  43. exit_code, output = sandbox.execute_and_check(
  44. 'cd /testing',
  45. 'Failed to cd /testing',
  46. )
  47. logger.info(f'cd $REPO_PATH: {output}')
  48. exit_code, output = sandbox.execute_and_check(
  49. 'whoami',
  50. 'Failed to run whoami',
  51. )
  52. logger.info(f'whoami: {output}')
  53. exit_code, output = sandbox.execute(
  54. '/home/devin/mambaforge/bin/mamba run -n test python3 /testing/start_test_opendevin.py'
  55. )
  56. logger.info(f'$TEST_CMD:\n{output}')
  57. exit_code, output = sandbox.execute_and_check(
  58. 'cat /testing_files/results_biocoder.json', 'Failed to read the result file'
  59. )
  60. if exit_code == 0:
  61. test_result['metadata']['2_run_test_success'] = True
  62. test_result['metadata']['2_run_test_result'] = str(output)
  63. else:
  64. test_result['metadata']['2_run_test_success'] = False
  65. test_result['metadata']['2_run_test_result'] = str(output)
  66. json_obj = json.loads(output)
  67. test_result['result'] = json_obj['result']
  68. return test_result
  69. def process_instance(
  70. instance: pd.Series,
  71. metadata: EvalMetadata,
  72. reset_logger: bool = True,
  73. ):
  74. # Create the agent
  75. agent = Agent.get_cls(metadata.agent_class)(llm=LLM(config=metadata.llm_config))
  76. instance = BiocoderData(**instance)
  77. print(instance)
  78. workspace_dir_name = (
  79. f'{instance.repository}__{instance.test_case_id[:10]}__{os.getpid()}'.replace(
  80. '/', '__'
  81. )
  82. )
  83. workspace_mount_path = os.path.join(config.workspace_base, workspace_dir_name)
  84. # create process-specific workspace dir
  85. # if `not skip_workspace_mount` - we will create a workspace directory for EACH process
  86. # so that different agent don't interfere with each other.
  87. workspace_mount_path = os.path.join(workspace_mount_path, str(os.getpid()))
  88. pathlib.Path(workspace_mount_path).mkdir(parents=True, exist_ok=True)
  89. # Setup the logger properly, so you can run multi-processing to parallize the evaluation
  90. if reset_logger:
  91. # Set up logger
  92. log_file = os.path.join(
  93. metadata.eval_output_dir, 'logs', f'instance_{instance.test_case_id}.log'
  94. )
  95. # Remove all existing handlers from logger
  96. for handler in logger.handlers[:]:
  97. logger.removeHandler(handler)
  98. # add back the console handler to print ONE line
  99. logger.addHandler(get_console_handler())
  100. logger.info(
  101. f'Starting evaluation for instance {instance.test_case_id}.\nHint: run "tail -f {log_file}" to see live logs in a seperate shell'
  102. )
  103. # Remove all existing handlers from logger
  104. for handler in logger.handlers[:]:
  105. logger.removeHandler(handler)
  106. file_handler = logging.FileHandler(log_file)
  107. file_handler.setFormatter(
  108. logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
  109. )
  110. logger.addHandler(file_handler)
  111. logger.info(f'Process-specific workspace mounted at {workspace_mount_path}')
  112. # NOTE: this is something special we do for SWE-Bench due to the reason described in the previous section
  113. # You can omit this if you don't need to setup specialized sandbox
  114. workspace_dir_name = f'{instance.repository}__{instance.test_case_id[:10]}'.replace(
  115. '/', '__'
  116. )
  117. sandbox = BiocoderSSHBox.get_box_for_instance(
  118. instance,
  119. workspace_dir_name,
  120. skip_workspace_mount=False,
  121. workspace_mount_path=workspace_mount_path,
  122. sandbox_plugins=agent.sandbox_plugins,
  123. )
  124. sandbox.remove_code()
  125. # Prepare instruction
  126. instruction = (
  127. f'Please complete the function "{instance.signature}" in the file /workspace/{instance.repository.split("/")[1]}/{instance.filePath}.\n'
  128. f'The environment has been set up for you to start working. You may assume all necessary tools are installed.\n'
  129. f'To complete the task, you must directly modify the file and fill in the function, keeping in mind that the function signature is on line {instance.lineStart-1}\n\n'
  130. f'The function should do the following:\n'
  131. f'{instance.promptSummaryOnly}\n\n'
  132. )
  133. instruction += (
  134. 'IMPORTANT: You should ONLY interact with the environment provided to you AND NEVER ASK FOR HUMAN HELP.\n'
  135. 'You should NOT modify any other files other than the file intended. This means that you should NOT write any test cases.\n'
  136. 'You may need context from other files in the repository to complete this task.'
  137. 'Do NOT add any import statements or change anything else other than the writing the function body.\n'
  138. 'You do not need to run the code to check if it works. \n'
  139. 'Make sure to include proper formatting in Java and Python, including correct braces and/or indentation.\n'
  140. )
  141. # NOTE: You can actually set slightly different instruction for different agents
  142. instruction += AGENT_CLS_TO_INST_SUFFIX[agent.__class__.__name__]
  143. # use a session id for concurrent evaluation
  144. sid = instance.test_case_id.replace('/', '__')
  145. # Here's how you can run the agent (similar to the `main` function) and get the final task state
  146. state: State | None = asyncio.run(
  147. run_agent_controller(
  148. agent,
  149. instruction,
  150. max_iterations=metadata.max_iterations,
  151. max_budget_per_task=config.max_budget_per_task,
  152. fake_user_response_fn=AGENT_CLS_TO_FAKE_USER_RESPONSE_FN[
  153. agent.__class__.__name__
  154. ],
  155. sandbox=sandbox,
  156. sid=sid,
  157. )
  158. )
  159. test_result = get_test_result(instance, sandbox, workspace_dir_name)
  160. if state is None:
  161. raise ValueError('State should not be None.')
  162. metrics = state.metrics.get() if state.metrics else None
  163. # history is now available as a stream of events, rather than list of pairs of (Action, Observation)
  164. # for compatibility with the existing output format, we can remake the pairs here
  165. # remove when it becomes unnecessary
  166. histories = state.history.compatibility_for_eval_history_pairs()
  167. # Save the output
  168. output = {
  169. 'test_case_id': instance.test_case_id,
  170. 'biocoder_instance': instance.to_dict(),
  171. 'instruction': instruction,
  172. 'generated': test_result['metadata']['1_copy_change_code'],
  173. 'metadata': metadata.model_dump(),
  174. 'history': histories,
  175. 'metrics': metrics,
  176. 'error': state.last_error if state and state.last_error else None,
  177. 'test_result': test_result,
  178. }
  179. # Close the sandbox
  180. sandbox.close()
  181. return output
  182. if __name__ == '__main__':
  183. id_column = 'test_case_id'
  184. args = parse_arguments()
  185. dataset = load_dataset('lilbillbiscuit/biocoder_public')
  186. biocoder_tests = dataset['test'].to_pandas()
  187. llm_config = get_llm_config_arg(args.llm_config) if args.llm_config else config.llm
  188. logger.info(f'Config for evaluation: {config}')
  189. metadata = make_metadata(
  190. llm_config,
  191. args.dataset_name,
  192. args.agent_cls,
  193. args.max_iterations,
  194. args.eval_note,
  195. args.eval_output_dir,
  196. )
  197. output_file = os.path.join(metadata.eval_output_dir, 'output.jsonl')
  198. instances = prepare_dataset(dataset, output_file, args.eval_n_limit, id_column)
  199. run_evaluation(
  200. instances,
  201. metadata,
  202. output_file,
  203. args.eval_num_workers,
  204. process_instance,
  205. id_column,
  206. )