File size: 2,301 Bytes
266a0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
prefix = """Answer the following questions as best you can. You have access to the following tools:

Search: useful for when you need to answer questions about current events
Calculator: useful for when you need to answer questions about math
Chitchat: useful for when you do not need tool to answer questions

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Search, Calculator, Chitchat]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
"""
ids = []
def process(example):
    example = example.strip()
    if not example.startswith("Question"):
        return None
    lines = example.split("\n")
    prelines = ""
    instances = []
    for i,line in enumerate(lines):
        if line.startswith("Thought:"):
            output = ""
            input = ""
            if "previous steps:" not in line:
                output = line[8:].lstrip()
            output+="\n"+"\n".join(lines[i+1:])
            if "previous steps:" in line:
                input = prelines+line
            else:
                input = prelines+"Thought:"
            input,output = input.strip(),output.strip()
            instances.append([input,output])
        prelines += line+'\n'
    return instances
rawdata = open("examples").read().split("###")
for example in rawdata:
    ret = process(example)
    if ret:
#        for i in range(32):
        for i in range(1):
            ids.extend(ret)
import random
random.shuffle(ids)
##format data
outlist = []
for i,instance in enumerate(ids):
    id_dict = {}
    id_dict["id"] = "identity_"+str(i)
    id_dict["conversations"] = []
    id_dict["conversations"].append({
        "from":"human",
        "value":prefix+"\n\n"+instance[0]
    })
    id_dict["conversations"].append({
        "from":"gpt",
        "value":instance[1]
    })
    outlist.append(id_dict)
import json
#json_str = json.dumps(outlist, indent =2, ensure_ascii= False)
json_str = json.dumps(outlist, indent =2)
with open("my.json","w") as outfile:
    outfile.write(json_str)