File size: 1,097 Bytes
256a159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# flake8: noqa
import json


def read_jsonl(path):
    with open(path, encoding='utf8') as fh:
        results = []
        for line in fh:
            if line is None:
                continue
            try:
                results.append(json.loads(line) if line != 'null' else line)
            except Exception as e:
                print(e)
                print(path)
                print(line)
                raise e
    return results


def save_jsonl(lines, directory):
    with open(directory, 'w', encoding='utf8') as f:
        for line in lines:
            f.write(json.dumps(line, ensure_ascii=False) + '\n')


def extract_answer(js):
    try:
        if js is None or js == 'null':
            return ''
        answer = ''
        if isinstance(js, str):
            answer = js
        elif 'text' in js['choices'][0]:
            answer = js['choices'][0]['text']
        else:
            answer = js['choices'][0]['message']['content']
            # answer = js['']
        return answer
    except Exception as e:
        # print(e)
        # print(js)
        return ''