You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

init_tasks.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #
  2. # Run this file to download the human_eval dataset, and create a corresponding testbed scenario:
  3. # (default: ../scenarios/human_eval_two_agents_gpt4.jsonl and ./scenarios/human_eval_two_agents_gpt35.jsonl)
  4. #
  5. import base64
  6. import gzip
  7. import io
  8. import json
  9. import os
  10. import re
  11. import requests
  12. URL = "https://github.com/openai/human-eval/raw/master/data/HumanEval.jsonl.gz"
  13. SCRIPT_PATH = os.path.realpath(__file__)
  14. SCRIPT_NAME = os.path.basename(SCRIPT_PATH)
  15. SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)
  16. SCENARIO_DIR = os.path.realpath(os.path.join(SCRIPT_DIR, os.path.pardir))
  17. TEMPLATES_DIR = os.path.join(SCENARIO_DIR, "Templates")
  18. TASKS_DIR = os.path.join(SCENARIO_DIR, "Tasks")
  19. # A selected subset of HumanEval problems to work with during development
  20. # Deprecated 2/5/2024 -- Use subsample instead
  21. REDUCED_SET = [
  22. "HumanEval/2",
  23. "HumanEval/26",
  24. "HumanEval/32",
  25. "HumanEval/33",
  26. "HumanEval/36",
  27. "HumanEval/38",
  28. "HumanEval/41",
  29. "HumanEval/50",
  30. "HumanEval/56",
  31. "HumanEval/65",
  32. "HumanEval/67",
  33. "HumanEval/84",
  34. "HumanEval/85",
  35. "HumanEval/86",
  36. "HumanEval/89",
  37. "HumanEval/99",
  38. "HumanEval/104",
  39. "HumanEval/113",
  40. "HumanEval/115",
  41. "HumanEval/120",
  42. "HumanEval/124",
  43. "HumanEval/126",
  44. "HumanEval/132",
  45. "HumanEval/135",
  46. "HumanEval/140",
  47. "HumanEval/146",
  48. ]
  49. def download_human_eval():
  50. """Download the HumanEval dataset, un-gzips it, and returns a list of its parsed JSON objects."""
  51. # Send a HTTP request to the URL of the file
  52. response = requests.get(URL)
  53. # Ensure we raise an error if the download failed
  54. response.raise_for_status()
  55. # Create a BytesIO object from the response content
  56. buffer = io.BytesIO(response.content)
  57. # Read the file, line by line, populating a list of parsed JSON objects
  58. results = []
  59. with gzip.GzipFile(fileobj=buffer) as f_in:
  60. for line in f_in:
  61. # Parse each line as JSON
  62. results.append(json.loads(line))
  63. return results
  64. def create_jsonl(name, tasks, template):
  65. """Creates a JSONL scenario file with a given name, list of HumanEval tasks, and template path."""
  66. # Create a task directory if it doesn't exist
  67. if not os.path.isdir(TASKS_DIR):
  68. os.mkdir(TASKS_DIR)
  69. # Create the jsonl file
  70. with open(os.path.join(TASKS_DIR, name + ".jsonl"), "wt") as fh:
  71. for task in tasks:
  72. print(f"Converting: [{name}] {task['task_id']}")
  73. record = {
  74. "id": task["task_id"].replace("/", "_"),
  75. "template": template,
  76. "substitutions": {
  77. "prompt.txt": {"__PROMPT__": task["prompt"]},
  78. "test.txt": {"__TEST__": task["test"]},
  79. "custom_code_executor.py": {"__ENTRY_POINT__": task["entry_point"]},
  80. },
  81. }
  82. fh.write(json.dumps(record).strip() + "\n")
  83. ###############################################################################
  84. def main():
  85. human_eval = download_human_eval()
  86. # Deprecated: reduced_human_eval = [t for t in human_eval if t["task_id"] in REDUCED_SET]
  87. # list all directories in the Templates directory
  88. # and populate a dictionary with the name and path
  89. templates = {}
  90. for entry in os.scandir(TEMPLATES_DIR):
  91. if entry.is_dir():
  92. templates[re.sub(r"\s", "", entry.name)] = entry.path
  93. # Create the various combinations of [models] x [templates]
  94. for t in templates.items():
  95. create_jsonl(f"human_eval_{t[0]}", human_eval, t[1])
  96. # Deprecated: create_jsonl(f"r_human_eval_{t[0]}", reduced_human_eval, t[1])
  97. if __name__ == "__main__" and __package__ is None:
  98. main()