2024-03-20 16:31:30 +08:00
|
|
|
import os
|
2023-05-28 18:09:04 +08:00
|
|
|
import json
|
|
|
|
import datasets
|
2023-11-09 15:53:23 +08:00
|
|
|
from typing import List
|
2023-05-28 18:09:04 +08:00
|
|
|
|
2024-03-20 16:31:30 +08:00
|
|
|
_HF_ENDPOINT = os.getenv("_HF_ENDPOINT", "https://huggingface.co")
|
2023-05-28 18:09:04 +08:00
|
|
|
|
|
|
|
_DESCRIPTION = "UltraChat: Large-scale, Informative, and Diverse Multi-round Dialogue Data."
|
|
|
|
|
|
|
|
_CITATION = """\
|
|
|
|
@misc{UltraChat,
|
|
|
|
author = {Ding, Ning and Chen, Yulin and Xu, Bokai and Hu, Shengding and Qin, Yujia and Liu, Zhiyuan and Sun, Maosong and Zhou, Bowen},
|
|
|
|
title = {UltraChat: A Large-scale Auto-generated Multi-round Dialogue Data},
|
|
|
|
year = {2023},
|
|
|
|
publisher = {GitHub},
|
|
|
|
journal = {GitHub repository},
|
|
|
|
howpublished = {\\url{https://github.com/thunlp/ultrachat}},
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2024-03-20 16:31:30 +08:00
|
|
|
_HOMEPAGE = f"{_HF_ENDPOINT}/datasets/stingning/ultrachat"
|
2023-05-28 18:09:04 +08:00
|
|
|
_LICENSE = "cc-by-nc-4.0"
|
2024-03-20 16:31:30 +08:00
|
|
|
_BASE_DATA_URL = "{_HF_ENDPOINT}/datasets/stingning/ultrachat/resolve/main/train_{idx}.jsonl"
|
2023-05-28 18:09:04 +08:00
|
|
|
|
|
|
|
|
2023-11-09 15:53:23 +08:00
|
|
|
class UltraChat(datasets.GeneratorBasedBuilder):
|
2023-05-28 18:09:04 +08:00
|
|
|
|
|
|
|
VERSION = datasets.Version("0.0.0")
|
|
|
|
|
2023-11-09 15:53:23 +08:00
|
|
|
def _info(self):
|
2023-05-28 18:09:04 +08:00
|
|
|
features = datasets.Features({
|
2023-11-09 15:53:23 +08:00
|
|
|
"conversations": [{"from": datasets.Value("string"), "value": datasets.Value("string")}]
|
2023-05-28 18:09:04 +08:00
|
|
|
})
|
|
|
|
return datasets.DatasetInfo(
|
|
|
|
description=_DESCRIPTION,
|
|
|
|
features=features,
|
|
|
|
homepage=_HOMEPAGE,
|
|
|
|
license=_LICENSE,
|
|
|
|
citation=_CITATION
|
|
|
|
)
|
|
|
|
|
2023-11-09 15:53:23 +08:00
|
|
|
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
2024-03-20 16:31:30 +08:00
|
|
|
file_paths = [dl_manager.download(_BASE_DATA_URL.format(_HF_ENDPOINT=_HF_ENDPOINT,idx=idx)) for idx in range(10)] # multiple shards
|
2023-05-28 18:09:04 +08:00
|
|
|
return [
|
|
|
|
datasets.SplitGenerator(
|
|
|
|
name=datasets.Split.TRAIN,
|
|
|
|
gen_kwargs={
|
|
|
|
"filepaths": file_paths
|
|
|
|
}
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2023-11-09 15:53:23 +08:00
|
|
|
def _generate_examples(self, filepaths: List[str]):
|
2023-05-28 18:09:04 +08:00
|
|
|
for filepath in filepaths:
|
|
|
|
with open(filepath, "r", encoding="utf-8") as f:
|
|
|
|
for row in f:
|
|
|
|
try:
|
|
|
|
data = json.loads(row)
|
|
|
|
except:
|
|
|
|
continue
|
2023-11-09 15:53:23 +08:00
|
|
|
key: int = data["id"]
|
|
|
|
content: List[str] = data["data"]
|
2023-05-28 18:09:04 +08:00
|
|
|
if len(content) % 2 == 1:
|
|
|
|
content.pop(-1)
|
|
|
|
if len(content) < 2:
|
|
|
|
continue
|
2023-11-09 15:53:23 +08:00
|
|
|
conversations = [{
|
|
|
|
"from": "human" if i % 2 == 0 else "gpt",
|
|
|
|
"value": content[i]
|
|
|
|
} for i in range(len(content))]
|
2023-11-16 02:08:04 +08:00
|
|
|
yield key, {"conversations": conversations}
|