File size: 12,386 Bytes
82bc1c9
 
 
 
 
 
 
 
e148f2b
 
 
82bc1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9b97c6
82bc1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9b97c6
 
82bc1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9b97c6
 
82bc1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e148f2b
 
 
 
 
 
 
 
 
 
 
 
0d15ac6
e9b97c6
82bc1c9
 
e9b97c6
82bc1c9
e148f2b
82bc1c9
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import json
import random
import tqdm
import re
import argparse
import pandas as pd
from collections import OrderedDict

from openai import OpenAIError

from .gpt3_api import make_requests as make_gpt3_requests
from .templates.instance_gen_template import output_first_template_for_clf, input_first_template_for_gen
from .templates.clf_task_template import template_1

random.seed(42)

engine = "davinci"

# def parse_args():
#     parser = argparse.ArgumentParser()
#     parser.add_argument(
#         "--batch_dir",
#         type=str,
#         required=True,
#         help="The directory where the batch is stored.",
#     )
#     parser.add_argument(
#         "--input_file",
#         type=str,
#         default="machine_generated_instructions.jsonl"
#     )
#     parser.add_argument(
#         "--output_file",
#         type=str,
#         default="machine_generated_instances.jsonl",
#     )
#     parser.add_argument(
#         "--num_instructions",
#         type=int,
#         help="if specified, only generate instance input for this many instructions",
#     )
#     parser.add_argument(
#         "--max_instances_to_generate",
#         type=int,
#         default=5,
#         help="The max number of instances to generate for each instruction.",
#     )
#     parser.add_argument(
#         "--generation_tasks_only",
#         action="store_true",
#         help="If specified, only do for generation tasks.",
#     )
#     parser.add_argument(
#         "--classification_tasks_only",
#         action="store_true",
#         help="If specified, only do for classification tasks.",
#     )
#     parser.add_argument(
#         "--engine",
#         type=str,
#         default="davinci",
#         help="The engine to use."
#     )
#     parser.add_argument(
#         "--request_batch_size",
#         type=int,
#         default=5,
#         help="The number of requests to send in a batch."
#     )
#     parser.add_argument(
#         "--api_key",
#         type=str,
#         help="The API key to use. If not specified, the key will be read from the environment variable OPENAI_API_KEY."
#     )
#     parser.add_argument(
#         "--organization",
#         type=str,
#         help="The organization to use. If not specified, the default organization id will be used."
#     )
#     return parser.parse_args()

def if_classify(instructions, api_key):
    prefix = template_1
    prompts = [prefix + " " + instruct.strip() + "\n" + "Is it classification?" for instruct in instructions]
    results = make_gpt3_requests(
        engine=engine,
        prompts=prompts,
        max_tokens=3,
        temperature=0,
        top_p=0,
        frequency_penalty=0,
        presence_penalty=0,
        stop_sequences=["\n", "Task"],
        logprobs=1,
        n=1,
        best_of=1,
        api_key=api_key)
    classify_res = []
    for i in range(len(prompts)):
        if results[i]["response"] is not None:
            if results[i]["response"]["choices"][0]["text"] in ["Yes", "yes", "YES"]:
                classify_res.append(True)
            else:
                classify_res.append(False)
        else:
            print("**分类出错,", results[i])
            classify_res.append("Unknown")
    return classify_res

def filter_duplicate_instances(instances):
    # if the instances have same non-empty input, but different output, we will not use such instances
    same_input_diff_output = False
    for i in range(1, len(instances)):
        for j in range(0, i):
            if instances[i][1] == "":
                continue
            if instances[i][1] == instances[j][1] and instances[i][2] != instances[j][2]:
                same_input_diff_output = True
                break
    if same_input_diff_output:
        return []

    # remove duplicate instances
    instances = list(set(instances))
    return instances

def filter_invalid_instances(instances):
    filtered_instances = []
    for instance in instances:
        # if input and output are the same, we will not use such instances
        if instance[1] == instance[2]:
            continue
        # if output is empty, we will not use such instances
        if instance[2] == "":
            continue
        # if input or output ends with a colon, these are usually imcomplete generation. We will not use such instances
        if instance[1].strip().endswith(":") or instance[2].strip().endswith(":"):
            continue
        filtered_instances.append(instance)
    return filtered_instances

def encode_instance(instruction, input, output, random_template=True):
    encoding_templates_w_input = [
        ("{instruction}\nInput: {input}\nOutput:", " {output}<|endoftext|>"),
        ("{instruction}\n\nInput: {input}\n\nOutput:", " {output}<|endoftext|>"),
        ("Task: {instruction}\nInput: {input}\nOutput:", " {output}<|endoftext|>"),
        ("{instruction}\n\n{input}\n\nOutput:", " {output}<|endoftext|>"),
        ("{instruction}\n\n{input}\n\n", "{output}<|endoftext|>"),
        ("{instruction}\n{input}\n\n", "{output}<|endoftext|>"),
        ("Task: {instruction}\n\n{input}\n\n", "{output}<|endoftext|>"),
    ]
    encoding_templates_wo_input = [
        ("{instruction} Output:", " {output}<|endoftext|>"),
        ("{instruction}\nOutput:", " {output}<|endoftext|>"),
        ("{instruction}\n\nOutput:", " {output}<|endoftext|>"),
        ("{instruction}\n", "{output}<|endoftext|>"),
        ("{instruction}\n\n", "{output}<|endoftext|>"),
        ("Task: {instruction}\n\n", "{output}<|endoftext|>"),
    ]
    if random_template:
        if input.strip() != "":
            prompt_template, completion_template = random.choice(encoding_templates_w_input)
            prompt = prompt_template.format(instruction=instruction.strip(), input=input.strip())
            completion = completion_template.format(output=output.strip())
        else:
            prompt_template, completion_template = random.choice(encoding_templates_wo_input)
            prompt = prompt_template.format(instruction=instruction.strip())
            completion = completion_template.format(output=output.strip())
    else:
        prompt = instruction.strip() + "\n\n" + input.strip() + "\n\n"
        completion = output.strip() + "<|endoftext|>"

    data = {
        "prompt": prompt,
        "completion": completion,
        "instruction": instruction.strip(),
        "input": input.strip(),
        "output": output.strip(),
    }
    return data

def parse_input_output(response_text):
    if re.findall(r"Output\s*\d*\s*:", response_text):
        inst_input = re.split(r"Output\s*\d*\s*:", response_text)[0].strip()
        inst_output = re.split(r"Output\s*\d*\s*:", response_text)[1].strip()
    else:
        inst_input = ""
        inst_output = response_text.strip()
    # to avoid the case multiple input/output pairs are generated
    if re.findall(r"Input\s*\d*\s*:", inst_output):
        inst_output = re.split(r"Input\s*\d*\s*:", inst_output)[0].strip()
    # remove the prefix "Input:" from the string
    inst_input = re.sub(r"^Input\s*\d*\s*:", "", inst_input).strip()
    return inst_input, inst_output

def parse_instances_for_generation_task(raw_text, instruction, response_metadata):
    instances = []
    raw_text = raw_text.strip()
    if re.findall("Example\s?\d*\.?", raw_text):
        instance_texts = re.split(r"Example\s?\d*\.?", raw_text)
        instance_texts = [it.strip() for it in instance_texts if it.strip() != ""]
        for instance_text in instance_texts:
            inst_input, inst_output = parse_input_output(instance_text)
            instances.append((instruction.strip(), inst_input.strip(), inst_output.strip()))
    elif re.findall(r"Output\s*\d*\s*:", raw_text):
        # we assume only one input/output pair in this case
        inst_input, inst_output = parse_input_output(raw_text)
        instances.append((instruction.strip(), inst_input.strip(), inst_output.strip()))
    else:
        return []
    # if the generation stops because of length, we remove the last instance
    if response_metadata["response"]["choices"][0]["finish_reason"] == "length":
        instances = instances[:-1]

    instances = filter_invalid_instances(instances)
    instances = filter_duplicate_instances(instances)
    return instances

def parse_instances_for_classification_task(raw_text, instruction, response_metadata):
    instances = []
    if not "Class label:" in raw_text:
        return []
    instance_texts = raw_text.split("Class label:")[1:]
    for instance_text in instance_texts:
        instance_text = instance_text.strip()
        fields = instance_text.split("\n", 1)
        if len(fields) == 2:
            # the first field split by \n is the class label
            class_label = fields[0].strip()
            # the rest is the input
            input_text = fields[1].strip()
        elif len(fields) == 1:
            # the first field split by \n is the input
            class_label = fields[0].strip()
            input_text = ""
        else:
            raise ValueError("Invalid instance text: {}".format(instance_text))
        instances.append((instruction.strip(), input_text.strip(), class_label.strip()))

    # if the generation stops because of length, we remove the last instance
    if response_metadata["response"]["choices"][0]["finish_reason"] == "length":
        instances = instances[:-1]
    instances = filter_invalid_instances(instances)
    instances = filter_duplicate_instances(instances)
    return instances

def generate_instance(inputs, api_key):
    classify_res = if_classify(inputs, api_key)
    prompts = []
    for i in range(len(inputs)):
        if classify_res[i] in ["Yes", "yes", "YES"]:
            prompt = output_first_template_for_clf + " " + inputs[i].strip() + "\n"
            prompts.append(prompt)
        else:
            prompt = input_first_template_for_gen + " " + inputs[i].strip() + "\n"
            prompts.append(prompt)
    # print("prompts", prompts)
    results = make_gpt3_requests(
        engine=engine,
        prompts=prompts,
        # because the clf template is longer, we need to decrease the max_tokens
        max_tokens=350,
        temperature=0,
        top_p=0,
        frequency_penalty=0,
        presence_penalty=1.5,
        stop_sequences=["Task:"],
        logprobs=1,
        n=1,
        best_of=1,
        api_key=api_key)
    return results, classify_res

def prepare_finetune(inputs, api_key):
    instance_outputs, classify_res = generate_instance(inputs, api_key)
    training_instances = []
    results1, results2 = [], []
    for i in range(len(inputs)):
        if classify_res[i]:
            task_instances = parse_instances_for_classification_task(instance_outputs[i]["response"]["choices"][0]["text"],
                                                                     inputs[i].strip(), instance_outputs[i])
        else:
            task_instances = parse_instances_for_generation_task(instance_outputs[i]["response"]["choices"][0]["text"],
                                                                 inputs[i].strip(),  instance_outputs[i])
        # we only allow max 5 instances per task
        task_instances = random.sample(task_instances, min(len(task_instances), 5))

        if not task_instances:
            continue

        training_instances += task_instances

        for instance in training_instances:
            results1.append({
                "instruction": instance[0],
                "input": instance[1],
                "output": instance[2],
            })
            results2.append(json.dumps({
                "instruction": instance[0],
                "input": instance[1],
                "output": instance[2],
            }, ensure_ascii=False))
    return results1, classify_res, instance_outputs, results2

def instance_main(inputs, key):
    try:
        import openai
        openai.api_key = key
        MODEL = "gpt-3.5-turbo"
        openai.ChatCompletion.create(
            model=MODEL,
            messages=[
                {"role": "user", "content": "Hi"}
            ],
            temperature=1
        )
    except OpenAIError:
        return {"Wrong": "Key!"}, " ", " ", " "
    api_key = key
    inputs = inputs.split('\n')
    print("***", inputs)
    return prepare_finetune(inputs, api_key)


# instance_main()