run_infer.py 8.9 KB

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