-
Notifications
You must be signed in to change notification settings - Fork 2
/
02_cot_answer.py
133 lines (105 loc) · 3.8 KB
/
02_cot_answer.py
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
import pandas as pd
import openai
import json
from tqdm import tqdm
import argparse
from prompts import prompt_templates
import os
import torch
import time
from transformers import T5Tokenizer, T5ForConditionalGeneration
from multiprocessing.managers import BaseManager
parser = argparse.ArgumentParser()
parser.add_argument('--data-path', help='input-data')
parser.add_argument('--output-path', help='input-data')
parser.add_argument('--cot-answer-path', help='input-data')
parser.add_argument('--prompt-path', help='input-data')
parser.add_argument('--prompt-strategy', help='input-data')
parser.add_argument('--word', help='input-data')
parser.add_argument('--model', default="text-davinci-002")
parser.add_argument('--limit', help='input-data', type=int)
args = parser.parse_args()
limit = args.limit
data = args.data_path
outputs = args.output_path
cot_answer_path = args.cot_answer_path
prompt_strategy = args.prompt_strategy
prompt_template = prompt_templates[prompt_strategy]
word = args.word
prompt_path = args.prompt_path
model = args.model
flan_model = None
tokenizer = None
def get_completion(
templated_prompt,
temp=0.7,
max_tokens=256,
n=1,
model = "text-davinci-002"
):
while True:
try:
response = openai.Completion.create(
model=model,
prompt=templated_prompt,
temperature=temp,
max_tokens=max_tokens,
n=n,
)
return [choice["text"] for choice in response["choices"]]
except:
print("sad")
time.sleep(15)
continue
with open(outputs) as f:
out_map = json.load(f)
open_mode = 'r' if os.path.exists(cot_answer_path) else 'w+'
with open(cot_answer_path, open_mode) as f:
try:
cot_answer = json.load(f)
except:
cot_answer = {}
open_mode = 'r' if os.path.exists(prompt_path) else 'w+'
with open(prompt_path, open_mode) as f:
try:
prompt_map = json.load(f)
except: prompt_map = {}
df = pd.read_csv(data)
if limit:
df = df[:limit]
for i, row in tqdm(df.iterrows(), total=len(df)):
all_choices = [
row["a"],
row["b"],
row["c"]
]
if "context" in row and (not pd.isna(row["context"])):
for ix, choice in enumerate(all_choices):
if row["sent_more"] in choice or row["sent_less"] in choice:
all_choices[ix] = row["context"] + " " + all_choices[ix]
question = None
if "ctx" in row and (not pd.isna(row["ctx"])) and "q_text" in row and (not pd.isna(row["q_text"])):
question = row["ctx"] + " " + row["q_text"]
prompt = prompt_template["template"](all_choices, word, question=question)
prompt += prompt_template["cot_initial"]
prompt_map[str(i)] = []
for idx, completion in enumerate(out_map[str(i)]):
x = prompt + completion + prompt_template["cot_final"]
prompt_map[str(i)].append(x)
if i % 100 == 0:
with open(prompt_path, 'w', encoding='utf-8') as f:
json.dump(prompt_map, f, ensure_ascii=False, indent=4)
if str(i) in cot_answer and len(cot_answer[str(i)]) == 5:
continue
if str(i) not in cot_answer:
cot_answer[str(i)] = {}
if str(idx) in cot_answer[str(i)]:
continue
cot_answer[str(i)][str(idx)] = get_completion(x, model=model)
if i % 100 == 0:
with open(cot_answer_path, 'w', encoding='utf-8') as f:
json.dump(cot_answer, f, ensure_ascii=False, indent=4)
with open(cot_answer_path, 'w', encoding='utf-8') as f:
json.dump(cot_answer, f, ensure_ascii=False, indent=4)
with open(prompt_path, 'w', encoding='utf-8') as f:
json.dump(prompt_map, f, ensure_ascii=False, indent=4)