"test/srt/openai_server/basic/test_serving_completions.py" did not exist on "02bf31ef29a31e6dc05cc16f2c8411da6fdf1b8c"
webqs.py 2.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
"""
Semantic Parsing on Freebase from Question-Answer Pairs
https://cs.stanford.edu/~pliang/papers/freebase-emnlp2013.pdf

WebQuestions is a benchmark for question answering. The dataset consists of 6,642
question/answer pairs. The questions are supposed to be answerable by Freebase, a
large knowledge graph. The questions are mostly centered around a single named entity.
The questions are popular ones asked on the web (at least in 2013).

Homepage: https://worksheets.codalab.org/worksheets/0xba659fe363cb46e7a505c5b6a774dc8a
11
"""
Jonathan Tow's avatar
Jonathan Tow committed
12
13
from lm_eval.base import rf, Task
from lm_eval.metrics import mean
14

15
16

_CITATION = """
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@inproceedings{berant-etal-2013-semantic,
    title = "Semantic Parsing on {F}reebase from Question-Answer Pairs",
    author = "Berant, Jonathan  and
      Chou, Andrew  and
      Frostig, Roy  and
      Liang, Percy",
    booktitle = "Proceedings of the 2013 Conference on Empirical Methods in Natural Language Processing",
    month = oct,
    year = "2013",
    address = "Seattle, Washington, USA",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/D13-1160",
    pages = "1533--1544",
}
"""
&'s avatar
& committed
32

Leo Gao's avatar
Leo Gao committed
33

Jonathan Tow's avatar
Jonathan Tow committed
34
class WebQs(Task):
Leo Gao's avatar
Leo Gao committed
35
    VERSION = 0
Leo Gao's avatar
Leo Gao committed
36
37
    DATASET_PATH = "web_questions"
    DATASET_NAME = None
Leo Gao's avatar
Leo Gao committed
38
39
40
41
42
43
44
45
46
47

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return False

    def has_test_docs(self):
        return True

Jonathan Tow's avatar
Jonathan Tow committed
48
49
50
51
52
53
54
55
    def training_docs(self):
        if self._training_docs is None:
            self._training_docs = list(self.dataset["train"])
        return self._training_docs

    def test_docs(self):
        return self.dataset["test"]

56
    def doc_to_text(self, doc):
bzantium's avatar
bzantium committed
57
58
59
60
61
62
63
        return "Question: " + doc["question"] + "\nAnswer:"

    def should_decontaminate(self):
        return True

    def doc_to_decontamination_query(self, doc):
        return doc["question"]
Leo Gao's avatar
Leo Gao committed
64

65
    def doc_to_target(self, doc):
bzantium's avatar
bzantium committed
66
        # this picks one answer to be the "correct" one, despite sometimes
Leo Gao's avatar
Leo Gao committed
67
68
        # multiple correct answers being possible.
        # TODO: make sure we're actually handling multi-answer correctly
bzantium's avatar
bzantium committed
69
70
        return " " + doc["answers"][0]

71
72
73
74
75
76
77
78
79
80
    def _remove_prefixes(self, aliases):
        # Optimization: Remove any alias that has a strict prefix elsewhere in the list
        # we can do this because if the prefix is acceptable by isgreedy, we can stop looking
        aliases.sort()
        ret = [aliases[0]]
        for alias in aliases[1:]:
            if not alias.startswith(ret[-1]):
                ret.append(alias)

        return ret
Leo Gao's avatar
Leo Gao committed
81

Leo Gao's avatar
Leo Gao committed
82
    def construct_requests(self, doc, ctx):
83
        ret = []
bzantium's avatar
bzantium committed
84
        for alias in self._remove_prefixes(doc["answers"]):
85
86
87
            _, is_prediction = rf.loglikelihood(ctx, " " + alias)
            ret.append(is_prediction)
        return ret
88

Leo Gao's avatar
Leo Gao committed
89
    def process_results(self, doc, results):
bzantium's avatar
bzantium committed
90
        return {"acc": float(any(results))}
Leo Gao's avatar
Leo Gao committed
91
92

    def aggregation(self):
93
94
95
        return {
            "acc": mean,
        }
Leo Gao's avatar
Leo Gao committed
96
97

    def higher_is_better(self):
bzantium's avatar
bzantium committed
98
        return {"acc": True}