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

8

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

    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
27
        return False
Anish Thite's avatar
Anish Thite committed
28
29

    def training_docs(self):
Leo Gao's avatar
Leo Gao committed
30
        return map(json.loads, open('data/triviaqa/unfiltered-web-train.jsonl'))
Anish Thite's avatar
Anish Thite committed
31
32

    def validation_docs(self):
Leo Gao's avatar
Leo Gao committed
33
        return map(json.loads, open('data/triviaqa/unfiltered-web-dev.jsonl'))
Anish Thite's avatar
Anish Thite committed
34
35

    def test_docs(self):
36
        raise NotImplementedError()
Anish Thite's avatar
Anish Thite committed
37
38
    
    def fewshot_description(self):
Leo Gao's avatar
Leo Gao committed
39
40
        # TODO: figure out fewshot description
        return ""
Anish Thite's avatar
Anish Thite committed
41
    
42
    def doc_to_text(self, doc):
43
        return f"Question: {doc['Question']}\nAnswer:"
44
45

    def doc_to_target(self, doc):
Leo Gao's avatar
Leo Gao committed
46
47
48
49
50
51
52
53
54
55
56
57
58
        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
59

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

Leo Gao's avatar
Leo Gao committed
67
    def process_results(self, doc, results):
68
        return {
Leo Gao's avatar
Leo Gao committed
69
            "acc": float(any(results))
70
        }
Leo Gao's avatar
Leo Gao committed
71
72

    def aggregation(self):
73
74
75
        return {
            "acc": mean,
        }
Leo Gao's avatar
Leo Gao committed
76
77

    def higher_is_better(self):
78
79
        return {
            "acc": True
Leo Gao's avatar
Leo Gao committed
80
        }