|
import os |
|
import json |
|
import datasets |
|
|
|
|
|
_DESCRIPTION = """In open-domain dialogue intelligent agents should exhibit the use of knowledge, however there are few convincing demonstrations of this to date. The most popular sequence to sequence models typically "generate and hope" generic utterances that can be memorized in the weights of the model when mapping from input utterance(s) to output, rather than employing recalled knowledge as context. Use of knowledge has so far proved difficult, in part because of the lack of a supervised learning benchmark task which exhibits knowledgeable open dialogue with clear grounding. To that end we collect and release a large dataset with conversations directly grounded with knowledge retrieved from Wikipedia. We then design architectures capable of retrieving knowledge, reading and conditioning on it, and finally generating natural responses. Our best performing dialogue models are able to conduct knowledgeable discussions on open-domain topics as evaluated by automatic metrics and human evaluations, while our new benchmark allows for measuring further improvements in this important research direction.""" |
|
_HOMEPAGE = "https://arxiv.org/abs/1811.01241" |
|
_CITATION = """@article{dinan2018wizard, |
|
title={Wizard of wikipedia: Knowledge-powered conversational agents}, |
|
author={Dinan, Emily and Roller, Stephen and Shuster, Kurt and Fan, Angela and Auli, Michael and Weston, Jason}, |
|
journal={arXiv preprint arXiv:1811.01241}, |
|
year={2018} |
|
}""" |
|
|
|
|
|
class WOWConfig(datasets.BuilderConfig): |
|
def __init__(self, *args, split="random", **kwargs): |
|
assert split in ["random", "topic"] |
|
super().__init__( |
|
*args, |
|
name=f"{split}", |
|
**kwargs, |
|
) |
|
self.split = split |
|
|
|
|
|
class WOW(datasets.GeneratorBasedBuilder): |
|
|
|
BUILDER_CONFIGS = [WOWConfig(split="random"), WOWConfig(split="topic")] |
|
BUILDER_CONFIG_CLASS = WOWConfig |
|
|
|
def _info(self): |
|
features = { |
|
"chosen_topic": datasets.Value("string"), |
|
"persona": datasets.Value("string"), |
|
"wizard_eval": datasets.Value("int32"), |
|
"dialog": [{ |
|
"speaker": datasets.Value("string"), |
|
"text": datasets.Value("string"), |
|
"checked_sentence_value": datasets.Value("string"), |
|
"checked_sentence_key": datasets.Value("string"), |
|
"checked_passage_value": datasets.Value("string"), |
|
"checked_passage_key": datasets.Value("string"), |
|
"retrieved_passages": [{ |
|
"key": datasets.Value("string"), |
|
"values": [datasets.Value("string")] |
|
}], |
|
"retrieved_topics": [datasets.Value("string")] |
|
}] |
|
} |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features(features), |
|
supervised_keys=None, |
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
train_filename = dl_manager.download_and_extract("data/train.json") |
|
valid_filename = dl_manager.download_and_extract(f"data/valid_{self.config.split}_split.json") |
|
test_filename = dl_manager.download_and_extract(f"data/test_{self.config.split}_split.json") |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={"filename": train_filename}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={"filename": valid_filename}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={"filename": test_filename}, |
|
) |
|
] |
|
|
|
def _generate_examples(self, filename): |
|
with open(filename) as f: |
|
for i, line in enumerate(f): |
|
line = json.loads(line) |
|
history = [] |
|
for j, turn in enumerate(line["dialog"]): |
|
retrieved_passages = [] |
|
for passage in turn["retrieved_passages"]: |
|
key = list(passage.keys())[0] |
|
values = list(passage.values())[0] |
|
retrieved_passages.append({ |
|
"key": key, |
|
"values": values |
|
}) |
|
if turn["speaker"] == "0_Wizard": |
|
checked_sentence = list(turn.get("checked_sentence", {}).items()) |
|
checked_passage = list(turn.get("checked_passage", {}).items()) |
|
history.append({ |
|
"speaker": turn["speaker"], |
|
"text": turn["text"], |
|
"checked_sentence_key": "" if len(checked_sentence) == 0 else checked_sentence[0][0], |
|
"checked_sentence_value": "" if len(checked_sentence) == 0 else checked_sentence[0][1], |
|
"checked_passage_key": "" if len(checked_passage) == 0 else checked_passage[0][0], |
|
"checked_passage_value": "" if len(checked_passage) == 0 else checked_passage[0][1], |
|
"retrieved_passages": retrieved_passages, |
|
"retrieved_topics": line["retrieved_topics"] if "retrieved_topics" in line else [] |
|
}) |
|
else: |
|
history.append({ |
|
"speaker": turn["speaker"], |
|
"text": turn["text"], |
|
"checked_sentence_key": "", |
|
"checked_sentence_value": "", |
|
"checked_passage_key": "", |
|
"checked_passage_value": "", |
|
"retrieved_passages": [], |
|
"retrieved_topics": [] |
|
}) |
|
yield f"{i}_{j}", { |
|
"chosen_topic": line["chosen_topic"], |
|
"persona": line["persona"], |
|
"wizard_eval": line["wizard_eval"], |
|
"dialog": history |
|
} |
|
|
|
|