run_infer.py 9.0 KB

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