File size: 9,095 Bytes
906e061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
from typing import List, Tuple

import json
import pandas as pd
import numpy as np
import os

from atomizer import Atomizer, text_to_sentences
from gpt import GPTClient
from scorer import Scorer

def get_prompts(
    dataset : str,
    data_path : str = None
) -> List:
    if dataset.lower() == "factscore":
        with open('data/factscore_names.txt', 'r') as fp:
            names = fp.readlines()
        names = [name.strip() for name in names]
        prompts = [
            f"Please write one biographical paragraph about {name.strip()}."
            for name in names
        ]
        return names, prompts
    if dataset.lower() == "factscore_v2":
        with open('data/factscore_v2_names.txt', 'r') as fp:
            names = fp.readlines()
        names = [name.strip() for name in names]
        prompts = [
            f"Please write one biographical paragraph about {name.strip()}."
            for name in names
        ]
        return names, prompts
    if dataset.lower() == "factscore_v3":
        with open('data/factscore_v3_names.txt', 'r') as fp:
            names = fp.readlines()
        names = [name.strip() for name in names]
        prompts = [
            f"Please write one biographical paragraph about {name.strip()}."
            for name in names
        ]
        return names, prompts
    
    if dataset.lower() == "factscore_final":
        df = pd.read_csv(data_path, index_col=0)
        names = set([n.strip() for n in df['Name']])
        prompts = [
            f"Please write one biographical paragraph about {name.strip()}."
            for name in names
        ]
        return names, prompts

    if dataset.lower() == "medlfqa":
        datasets = {}

        suffix = "_test_MedLFQA.jsonl"

        dataset_dir = "/Users/cherian/Projects/OLAPH/MedLFQA"
        for path in os.listdir(dataset_dir):
            if "MedLFQA" not in path:
                continue
            dataset_name = path[:-len(suffix)]
            with open(os.path.join(dataset_dir, path), 'r') as fp:
                datasets[dataset_name] = [json.loads(line) for line in fp.readlines()]

        prompts = []
        for _, dataset in datasets.items():
            prompts += [pt['Question'] for pt in dataset]
        prompts = list(set(prompts))
        return prompts, prompts
    
    if dataset.lower() == "medlfqav2":
        datasets = {}

        suffix = ".jsonl"

        for filename in os.listdir(data_path):
            dataset_name = filename[:-len(suffix)]
            with open(os.path.join(data_path, filename), 'r') as fp:
                datasets[dataset_name] = [json.loads(line) for line in fp.readlines()]

        prompts = []
        for _, dataset in datasets.items():
            prompts += [pt['Question'] for pt in dataset]

        return prompts, prompts

    else:
        raise ValueError("Unsupported data set.")
    
def find_unique_element(lst, condition, approx_index):
    # Check the approximate index first
    if condition(lst[approx_index]):
        return approx_index
    
    # Initialize left and right pointers
    left = approx_index - 1
    right = approx_index + 1
    
    # Expand outwards from the approximate index
    while left >= 0 or right < len(lst):
        if left >= 0 and condition(lst[left]):
            return left
        if right < len(lst) and condition(lst[right]):
            return right
        left -= 1
        right += 1
    
    # If no element satisfies the condition, return None or raise an exception
    return None

def load_dataset(
    config : dict
) -> List:
    
    print("Loading responder.")
    responder = GPTClient(config.model.responder.cache_path)
        
    topics, prompts = get_prompts(config.dataset.name, config.dataset.path)

    with ThreadPoolExecutor(max_workers=25) as executor:
        responses = list(
            tqdm(
                executor.map(
                    lambda x : responder.query(x),
                    prompts
                ),
                total=len(prompts)
            )
        )
    
    # TODO: Uncomment me if I want to run fresh dataset...

    responder.cache_outputs(
        prompts,
        np.zeros((len(responses),), dtype=int),
        responses
    )

    responder.save_cache()

    responses = [r[0] for r in responses]


    outputs = [{'prompt': p, 'response': o['message']} 
                    for p, o in zip(prompts, responses)] # first output is the response we will filter

    import IPython; IPython.embed()
    print("Loading atomizer.")
    atomizer_client = GPTClient(config.model.parser.cache_path, model=config.model.parser.name)

    atomizer = Atomizer(atomizer_client, demo_dir='data/demos')
    
    CACHE_EXISTS = True

    if CACHE_EXISTS: # TODO: dumb hard-coded variable to side step the slow retrieval
        ordered_messages = [r['message'] for r in responses]

        responder_cache = responder.cache_dict
        messages = []
        for val in responder_cache.values():
            messages.append(val[0]['message'])

        atomizer_cache = atomizer_client.cache_dict
        idx_guess = 0
        atomic_facts = [[] for _ in range(len(messages))]
        atomic_facts_ph = [[] for _ in range(len(messages))]

        sentences = defaultdict(int)
        for k in tqdm(atomizer_cache.keys()):
            atomized_msg = atomizer_cache[k][0]['message']
            atomized_facts = text_to_sentences(atomized_msg)
            sentence = k.split('\n')[-1].split('facts:')[-1].strip()[:-2]
            cur_idx = -1
            sentences[sentence] += 1
            # if the sentence has appeared more than once we need to find the appropriate match...
            for i in range(sentences[sentence]):
                cur_idx = find_unique_element(messages[cur_idx + 1:], lambda x: sentence in x, approx_index=idx_guess)
            if cur_idx is None: # TODO: TERRIBLE SPECIAL CASING that I looked at by hand...
                raise ValueError()
                if idx_guess in (4148, 4149, 4150):
                    cur_idx = 4149
                elif cur_idx == 993:
                    cur_idx = 993
                else:
                    continue
            idx_guess = cur_idx
            atomic_facts[cur_idx].extend(atomized_facts)

        for af, msg in zip(atomic_facts, messages):
            if len(af) == 0:
                continue
            new_idx = ordered_messages.index(msg)
            atomic_facts_ph[new_idx] = af
        atomic_facts = atomic_facts_ph
        
    else:
        with ThreadPoolExecutor(max_workers=10) as executor:
            atoms = list(
                tqdm(
                    executor.map(
                        lambda x : atomizer.run(*x),
                        [(o['response'],) for o in outputs]
                    ),
                    total=len(outputs)
                )
            )

        atomizer.save_cache()
        atomic_facts = [[fact for _, facts in atom[0] for fact in facts] for atom in atoms]
    
    
    dataset = []

    for p, r, af in zip(prompts, responses, atomic_facts):
        atoms = [{'atom': fact} for fact in af]
        data_pt = {'prompt': p, 'response': r, 'atomic_facts': atoms}
        dataset.append(data_pt)

    # time to annotate responses using factscore code
    print("Loading annotator.")
    scorer_client = GPTClient(config.model.annotator.cache_path, model=config.model.annotator.name)
    scorer = Scorer(scorer_client, config, model_name="retrieval")

    scorer_inputs = [(topic, output['response'], fact) for topic, output, fact in zip(topics, outputs, atomic_facts)]
    with ThreadPoolExecutor(max_workers=4) as executor:
        scores = list(
            tqdm(
                executor.map(
                    lambda x : scorer.get_score(*x, knowledge_source='medlfqa'),
                    scorer_inputs
                ),
                total=len(scorer_inputs)
            )
        )
    # scorer.save_cache()

    dataset = []

    for p, r, s in zip(prompts, responses, scores):
        data_pt = {
            'prompt': p,
            'response': r,
            'atomic_facts': s['decisions'][0]
        }
        dataset.append(data_pt)

    import IPython; IPython.embed()

    return dataset

def split_dataset(
    dataset : List,
    train_perc : float = 0.33,
    valid_perc : float = 0.33,
    rng : np.random.Generator = None
) -> Tuple[List, List, List]:
    """
    Splits dataset into three parts. Split into training and validation is specified here.
    """
    total_length = len(dataset)
    
    # Calculate lengths of each part based on percentages
    len1 = int(total_length * train_perc)
    len2 = int(total_length * valid_perc)
    
    # if rng passed in, shuffle the dataset
    if rng is not None:
        rng.shuffle(dataset)

    # Split the list using slicing
    train_data = dataset[:len1]
    valid_data = dataset[len1:len1+len2]
    test_data = dataset[len1+len2:]
    
    return train_data, valid_data, test_data