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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import json
  2. import os
  3. import re
  4. import sys
  5. from huggingface_hub import snapshot_download
  6. SCRIPT_PATH = os.path.realpath(__file__)
  7. SCRIPT_NAME = os.path.basename(SCRIPT_PATH)
  8. SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)
  9. SCENARIO_DIR = os.path.realpath(os.path.join(SCRIPT_DIR, os.path.pardir))
  10. TEMPLATES_DIR = os.path.join(SCENARIO_DIR, "Templates")
  11. TASKS_DIR = os.path.join(SCENARIO_DIR, "Tasks")
  12. DOWNLOADS_DIR = os.path.join(SCENARIO_DIR, "Downloads")
  13. REPO_DIR = os.path.join(DOWNLOADS_DIR, "AssistantBench")
  14. def download_assistantbench():
  15. """Download the AssistantBench benchmark from Hugging Face."""
  16. if not os.path.isdir(DOWNLOADS_DIR):
  17. os.mkdir(DOWNLOADS_DIR)
  18. """Download the AssistantBench dataset from Hugging Face Hub"""
  19. snapshot_download(
  20. repo_id="AssistantBench/AssistantBench",
  21. repo_type="dataset",
  22. local_dir=REPO_DIR,
  23. local_dir_use_symlinks=True,
  24. )
  25. def create_jsonl(data_file_path, file_name, template):
  26. """Creates a JSONL scenario file with a given name, and template path."""
  27. tasks = []
  28. with open(data_file_path) as fh:
  29. for line in fh:
  30. data = json.loads(line)
  31. tasks.append(data)
  32. file_name = os.path.basename(file_name)
  33. if not os.path.isdir(TASKS_DIR):
  34. os.mkdir(TASKS_DIR)
  35. with open(os.path.join(TASKS_DIR, file_name), "wt") as fh:
  36. for task in tasks:
  37. if "answer" not in task or task["answer"] is None:
  38. task["answer"] = ""
  39. print(f"Converting: [{file_name}] {task['id']}")
  40. template_cp_list = [template]
  41. record = {
  42. "id": task["id"],
  43. "template": template_cp_list,
  44. "substitutions": {
  45. "scenario.py": {
  46. "__FILE_NAME__": "",
  47. },
  48. "expected_answer.txt": {"__EXPECTED_ANSWER__": task["answer"]},
  49. "prompt.txt": {"__PROMPT__": task["task"]},
  50. },
  51. "difficulty": task["difficulty"],
  52. "explanation": task["explanation"],
  53. "metadata": task["metadata"],
  54. "gold_url": task["gold_url"],
  55. "set": task["set"],
  56. }
  57. fh.write(json.dumps(record).strip() + "\n")
  58. ###############################################################################
  59. def main():
  60. ab_validation_files = os.path.join(REPO_DIR, "assistant_bench_v1.0_dev.jsonl")
  61. ab_test_files = os.path.join(REPO_DIR, "assistant_bench_v1.0_test.jsonl")
  62. if not os.path.isfile(ab_validation_files) or not os.path.isfile(ab_test_files):
  63. download_assistantbench()
  64. if not os.path.isfile(ab_validation_files) or not os.path.isfile(ab_test_files):
  65. sys.exit(f"Error: '{REPO_DIR}' does not appear to be a copy of the AssistantBench repository.")
  66. templates = {}
  67. for entry in os.scandir(TEMPLATES_DIR):
  68. if entry.is_dir():
  69. templates[re.sub(r"\s", "", entry.name)] = entry.path
  70. print(templates)
  71. # make a copy of the data in the Tasks directory
  72. for t in templates.items():
  73. create_jsonl(ab_validation_files, f"assistant_bench_v1.0_dev__{t[0]}.jsonl", t[1])
  74. create_jsonl(ab_test_files, f"assistant_bench_v1.0_test__{t[0]}.jsonl", t[1])
  75. if __name__ == "__main__" and __package__ is None:
  76. main()