triviaqa.py 2.18 KB
Newer Older
1
import os
Anish Thite's avatar
Anish Thite committed
2
import json
Leo Gao's avatar
Leo Gao committed
3
import jsonlines
&'s avatar
& committed
4
5
from lm_eval.base import Task, rf
from ..metrics import mean
Anish Thite's avatar
Anish Thite committed
6
from ..utils import sh
Leo Gao's avatar
Leo Gao committed
7
from best_download import download_file
Anish Thite's avatar
Anish Thite committed
8

9

10
class TriviaQA(Task):
Leo Gao's avatar
Leo Gao committed
11
    VERSION = 0
Anish Thite's avatar
Anish Thite committed
12
    def download(self):
Leo Gao's avatar
Leo Gao committed
13
14
        if not os.path.exists('data/triviaqa/unfiltered-web-train.jsonl'):
            os.makedirs("data/triviaqa/", exist_ok=True)
15
            download_file("http://eaidata.bmk.sh/data/triviaqa-unfiltered.tar.gz", local_file="data/triviaqa/triviaqa-unfiltered.tar.gz", expected_checksum="adc19b42769062d241a8fbe834c56e58598d9322eb6c614e9f33a68a2cf5523e")
16
            sh("""
Leo Gao's avatar
Leo Gao committed
17
18
            cd data/triviaqa/
            tar -xf triviaqa-unfiltered.tar.gz
19
            """)
Anish Thite's avatar
Anish Thite committed
20
21
22
23
24
25
26
27

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
Leo Gao's avatar
Leo Gao committed
28
        return False
Anish Thite's avatar
Anish Thite committed
29
30

    def training_docs(self):
Leo Gao's avatar
Leo Gao committed
31
        return jsonlines.open('data/triviaqa/unfiltered-web-train.jsonl')
Anish Thite's avatar
Anish Thite committed
32
33

    def validation_docs(self):
Leo Gao's avatar
Leo Gao committed
34
        return jsonlines.open('data/triviaqa/unfiltered-web-dev.jsonl')
Anish Thite's avatar
Anish Thite committed
35
36

    def test_docs(self):
37
        raise NotImplementedError()
Anish Thite's avatar
Anish Thite committed
38
    
39
    def doc_to_text(self, doc):
40
        return f"Question: {doc['Question']}\nAnswer:"
41
42

    def doc_to_target(self, doc):
Leo Gao's avatar
Leo Gao committed
43
44
45
46
47
48
49
50
51
52
53
54
        return " " + doc['Answer']['Value']

    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
Anish Thite's avatar
Anish Thite committed
55

Leo Gao's avatar
Leo Gao committed
56
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Leo Gao committed
57
58
59
60
61
        ret = []
        for alias in self._remove_prefixes(doc['Answer']['Aliases']):
            _, is_prediction = rf.loglikelihood(ctx, " " + alias)
            ret.append(is_prediction)
        return ret
62

Leo Gao's avatar
Leo Gao committed
63
    def process_results(self, doc, results):
64
        return {
Leo Gao's avatar
Leo Gao committed
65
            "acc": float(any(results))
66
        }
Leo Gao's avatar
Leo Gao committed
67
68

    def aggregation(self):
69
70
71
        return {
            "acc": mean,
        }
Leo Gao's avatar
Leo Gao committed
72
73

    def higher_is_better(self):
74
75
        return {
            "acc": True
Leo Gao's avatar
Leo Gao committed
76
        }