Datasets documentation

加载智能体评估任务

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

加载智能体评估任务

Harbor 数据集是一组用于评估 AI 智能体的任务:每个任务都会为智能体提供一条指令、一个用于执行工作的沙箱环境,以及一个用于对其工作成果进行评分的验证器。

与表格数据集不同,Harbor 数据集并非每个示例对应一行的文件:每个任务都是一个目录。 harbor 构建器会读取此类仓库,并为每个任务生成一行,因此基准测试可以像其他数据集一样被探索、过滤和共享:

>>> from datasets import load_dataset
>>> dataset = load_dataset("harborframework/terminal-bench")
>>> dataset
DatasetDict({
    test: Dataset({
        features: ['name', 'instruction', 'description', 'keywords', 'schema_version', 'config', 'metadata', 'files', 'location'],
        num_rows: 66
    })
})

加载 Harbor 数据集仅会读取任务:它不会运行智能体、构建镜像或执行验证器,并且无需安装 Harbor。请使用Harbor库来实际运行这些任务。

Harbor 任务配置是 TOML 文件。Python 3.11 及更高版本使用标准库(tomllib)读取它们;在 Python 3.10 中,你需要将 TOML 解析器作为额外依赖项安装。请参阅安装指南了解如何安装。

仓库结构

任务目录由两个文件 task.toml 和 instruction.md 标识,通常还包含环境定义、验证器和参考解决方案:

my-harbor-dataset/
├── README.md
└── tasks/
    ├── task-a/
    │   ├── task.toml          # configuration: metadata, resources, timeouts...
    │   ├── instruction.md     # what the agent is asked to do
    │   ├── README.md          # optional human-readable description
    │   ├── environment/       # Dockerfile / docker-compose.yaml + files of the sandbox
    │   ├── tests/             # verifier
    │   └── solution/          # oracle solution
    └── task-b/
        └── ...

只要目录中任意层级的子目录是任务目录(即同时包含 task.toml 文件和 instruction.md 文件),该目录就会被识别为 Harbor 数据集:任务会被自动发现,无需列出任何文件。

加载任务

由于 Harbor 数据集由目录而非数据文件组成,因此通过 path 加载——无需 data_files 参数:

>>> from datasets import load_dataset

>>> # a dataset repository on the Hub
>>> dataset = load_dataset("harborframework/terminal-bench")
>>> # a local directory of tasks
>>> dataset = load_dataset("/path/to/my-harbor-dataset")
>>> # the tasks of a repository that are in a sub-directory
>>> dataset = load_dataset("harborframework/terminal-bench", data_dir="tasks")

也可以显式传入 data_files,只要该模式包含 task.toml 和 instruction.md 文件即可——每匹配到一对文件就生成一行:

>>> dataset = load_dataset(
...     "/path/to/my-harbor-dataset",
...     data_files={"test": ["my-tasks-dir/*/task.toml", "my-tasks-dir/*/instruction.md"]},
... )

列

每一行描述一个任务:

列类型描述
namestringtask.toml 中的 [task].name(通常为 org/name),或任务目录名称。
descriptionstringtask.toml 中的 [task].description。
instructionstringinstruction.md 的内容:智能体需要执行的任务。
keywordslist 类型的 stringtask.toml 中的 [task].keywords。
schema_versionstringtask.toml 中声明的 schema_version。
configdict完整的 task.toml,作为一个 JSON 对象: [task]、[metadata]、[verifier]、[agent]、[environment]、[steps]……
metadatadicttask.toml 中自由格式的 [metadata] 部分。
fileslist 类型的 string该任务的所有文件,路径相对于任务目录。
locationstring数据集中任务目录的位置。
>>> dataset = load_dataset("harborframework/terminal-bench", split="test")
>>> dataset[0]["name"]
'terminal-bench/atrx-vep-crispr'
>>> dataset[0]["instruction"][:90]
'You are provided with a region of genomic DNA representing the human ATRX locus plus sequence'
>>> dataset[0]["config"]["task"]["authors"]
[{'name': 'ScaleAI', 'email': 'tbench@scale.com'}]
>>> dataset[0]["metadata"]["category"]
'Science'
>>> dataset[0]["config"]["environment"]
{'build_timeout_sec': 1800.0, 'cpus': 2, 'memory_mb': 4096, 'storage_mb': 10240, 'gpus': 0}
>>> dataset[0]["files"][:4]
['LICENSE.md', 'README.md', 'environment/Dockerfile', 'environment/data/CDS-information.txt']
>>> dataset[0]["location"]
'tasks/atrx-vep-crispr'

完整的 task.toml 可在 config 中获取,因此当任务使用了未提升为独立列的字段时,不会丢失任何信息:

>>> dataset = dataset.map(lambda task: {"timeout_min": task["config"]["verifier"]["timeout_sec"] / 60})
>>> sorted(set(dataset["timeout_min"]))[:6]
[1.0, 2.0, 3.0, 4.0, 5.0, 7.0]

筛选基准测试

由于任务元数据以字典形式加载,因此可以使用常规的 filter() 和 map() 方法来探索基准测试:

>>> security_tasks = dataset.filter(lambda task: task["metadata"].get("category") == "Security")
>>> [task["name"] for task in security_tasks]
['terminal-bench/formal-crypto', 'terminal-bench/html-js-filter', 'terminal-bench/interleaved-vigenere', 'terminal-bench/shadow-relay', 'terminal-bench/uefi-bootkit']

files 列出了任务附带的所有内容,方便查看任务的验证方式:

>>> pytest_verifiers = dataset.filter(lambda task: any(f.startswith("tests/") and f.endswith(".py") for f in task["files"]))
>>> pytest_verifiers.num_rows
63

读取任务的其它文件

仅将任务的指令和配置作为列加载:其环境、验证器和解决方案文件在 files 列中列出,路径相对于任务目录。将 task_id 与 files 组合,即可使用 fsspec 打开其中任意一个:

>>> from huggingface_hub import hffs
>>> dataset_path = "hf://datasets/harborframework/terminal-bench"
>>> with hffs.open(f"{dataset_path}/tasks/cumulative-layout-shift/environment/Dockerfile", "r") as f:
...     print(f.read()[:20])
'# harbor-canary GUID

配置

HarborConfig 除了 load_dataset 的常规参数外,还接受以下参数:

  • strip_canary(默认值为 True):从指令中移除开头的“基准数据金丝雀”注释行,处理方式与 Harbor 将其发送给智能体时相同。将其设置为 False 可保留原始文件内容。
  • include_files(默认值为 True):在 files 列中列出每个任务的所有文件。将其设置为 False 可跳过列出任务目录,当数据集位于远程文件系统上或每个任务包含数千个文件时,这样速度更快。

任务也可以以流式方式处理,从而避免预先下载整个基准测试:

>>> from datasets import load_dataset
>>> dataset = load_dataset("harborframework/terminal-bench", streaming=True)
>>> for task in dataset["test"]:
...     print(task["name"])

另请参阅

Update on GitHub