File size: 9,113 Bytes
badcf3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def get_prompt(ask_name, shot_type):
    import pandas as pd
    import random
    if ask_name == 'yes_no':
        # 设置判断数据路径
        data_path = './data/data.xlsx'
        sheet_train = 'Yes or No Train'
        sheet_test = 'Yes or No Test'   
        # 读取数据
        data_train = pd.read_excel(data_path, sheet_name=sheet_train)
        data_test = pd.read_excel(data_path, sheet_name=sheet_test)
        # 设置问题和文本
        question_train = data_train['Question']
        text_train = data_train['Text']
        answer_train = data_train['Answer']
        question_test = data_test['Question']
        text_test = data_test['Text']
        answer_test = data_test['Answer']
        # 设置prompt
        if shot_type == 'zero_shot':
            prompt = '''

请根据给定的文本回答问题,

给定文本:“''' + text_train + '''”'''+'\n'+'问题:“''' + question_train + '''”

请用Yes或No直接回答。

            '''
        elif shot_type == 'one_shot':
            # 随机设置一个例子
            random_index = random.randint(0, len(data_test)-1)
            example = '文本:' + str(text_test[random_index]) +'\n'+ '问题:' + str(question_test[random_index]) +'\n'+ '答案:' + str(answer_test[random_index])
            prompt = '''

请根据给定的文本回答问题,

            ''' + '\n' + '''例子如下:''' + '\n' + example + '''

            ''' + '\n' + '''给定文本:“''' + text_train + '''”'''+'\n'+'''问题:“''' + question_train + '”' + '''

请用Yes或No直接回答。

            ''' 
        elif shot_type == 'two_shot':
            # 随机设置两个例子
            random_index1 = random.randint(0, len(data_test)-1)
            random_index2 = random.randint(0, len(data_test)-1)
            while random_index2 == random_index1:
                random_index2 = random.randint(0, len(data_test)-1)
            example1 = '文本:' + str(text_test[random_index1]) + '\n'+ '问题:' + str(question_test[random_index1]) + '\n'+ '答案:' + str(answer_test[random_index1])
            example2 = '文本:' + str(text_test[random_index2]) + '\n'+ '问题:' + str(question_test[random_index2]) + '\n'+ '答案:' + str(answer_test[random_index2])
            prompt = '''

请根据给定的文本回答问题,

            ''' + '\n' + '''例子一如下:''' + '\n' + example1 + '''

            ''' + '\n' + '''例子二如下:''' + '\n' + example2 + '''

            ''' + '\n' + '''给定文本:“''' + text_train + '''”'''+'\n'+'''问题:“''' + question_train + '”' + '''

请用Yes或No直接回答。

            '''
        elif shot_type == 'three_shot':
            # 随机设置三个例子
            random_index1 = random.randint(0, len(data_test)-1)
            random_index2 = random.randint(0, len(data_test)-1)
            random_index3 = random.randint(0, len(data_test)-1)
            while random_index3 == random_index1 or random_index3 == random_index2:
                random_index3 = random.randint(0, len(data_test)-1)
            example1 = '文本:' + str(text_test[random_index1]) + '\n'+ '问题:' + str(question_test[random_index1]) + '\n'+ '答案:' + str(answer_test[random_index1])
            example2 = '文本:' + str(text_test[random_index2]) + '\n'+ '问题:' + str(question_test[random_index2]) + '\n'+ '答案:' + str(answer_test[random_index2])
            example3 = '文本:' + str(text_test[random_index3]) + '\n'+ '问题:' + str(question_test[random_index3]) + '\n'+ '答案:' + str(answer_test[random_index3])
            prompt = '''

请根据给定的文本回答问题,

            ''' + '\n' + '''例子一如下:''' + '\n' + example1 + '''

            ''' + '\n' + '''例子二如下:''' + '\n' + example2 + '''

            ''' + '\n' + '''例子三如下:''' + '\n' + example3 + '''

            ''' + '\n' + '''给定文本:“''' + text_train + '''”'''+'\n'+'''问题:“''' + question_train + '”' + '''

请用Yes或No直接回答。

            '''
    elif ask_name == 'factoid': 
        # 读取数据
        data_path = './data/data.xlsx'
        sheet_train = 'Factoid Train'
        sheet_test = 'Factoid Test'
        data_train = pd.read_excel(data_path, sheet_name=sheet_train)
        data_test = pd.read_excel(data_path, sheet_name=sheet_test)
        # 设置问题和文本
        question_train = data_train['Question']
        text_train = data_train['Text']
        answer_train = data_train['Answer']
        question_test = data_test['Question']
        text_test = data_test['Text']
        answer_test = data_test['Answer']
        if shot_type == 'zero_shot':
            prompt = '''

请根据给定的文本回答问题,

给定文本:“''' + text_train + '”' +'\n' +'''问题:“''' + question_train + '''”

请直接回答该问题。

            ''' 
        elif shot_type == 'one_shot':
            # 随机设置一个例子
            random_index = random.randint(0, len(data_test)-1)
            example = '文本:' + str(text_test[random_index]) + '\n'+ '问题:' + str(question_test[random_index]) + '\n'+ '答案:' + str(answer_test[random_index])
            prompt = '''

请根据给定的文本回答问题,

            ''' + '\n' + '''例子如下:''' + '\n' + example + '''

            ''' + '\n' + '''给定文本:“''' + text_train + '''”'''+'\n'+'''问题:“''' + question_train + '”' + '''

请直接回答该问题。

            ''' 
        elif shot_type == 'two_shot':
            # 随机设置两个例子
            random_index1 = random.randint(0, len(data_test)-1)
            random_index2 = random.randint(0, len(data_test)-1)
            while random_index2 == random_index1:
                random_index2 = random.randint(0, len(data_test)-1)
            example1 = '文本:' + str(text_test[random_index1]) + '\n'+ '问题:' + str(question_test[random_index1]) + '\n'+ '答案:' + str(answer_test[random_index1])
            example2 = '文本:' + str(text_test[random_index2]) + '\n'+ '问题:' + str(question_test[random_index2]) + '\n'+ '答案:' + str(answer_test[random_index2])
            prompt = '''

请根据给定的文本回答问题,

            ''' + '\n' + '''例子一如下:''' + '\n' + example1 + '''

            ''' + '\n' + '''例子二如下:''' + '\n' + example2 + '''

            ''' + '\n' + '''给定文本:“''' + text_train + '''”'''+'\n'+'''问题:“''' + question_train + '”' + '''

请直接回答该问题。

            '''
        elif shot_type == 'three_shot':
            # 随机设置三个例子
            random_index1 = random.randint(0, len(data_test)-1)
            random_index2 = random.randint(0, len(data_test)-1)
            random_index3 = random.randint(0, len(data_test)-1)
            while random_index3 == random_index1 or random_index3 == random_index2:
                random_index3 = random.randint(0, len(data_test)-1)
            example1 = '文本:' + str(text_test[random_index1]) + '\n'+ '问题:' + str(question_test[random_index1]) + '\n'+ '答案:' + str(answer_test[random_index1])
            example2 = '文本:' + str(text_test[random_index2]) + '\n'+ '问题:' + str(question_test[random_index2]) + '\n'+ '答案:' + str(answer_test[random_index2])
            example3 = '文本:' + str(text_test[random_index3]) + '\n'+ '问题:' + str(question_test[random_index3]) + '\n'+ '答案:' + str(answer_test[random_index3])
            prompt = '''

请根据给定的文本回答问题,

            ''' + '\n' + '''例子一如下:''' + '\n' + example1 + '''

            ''' + '\n' + '''例子二如下:''' + '\n' + example2 + '''

            ''' + '\n' + '''例子三如下:''' + '\n' + example3 + '''

            ''' + '\n' + '''给定文本:“''' + text_train + '''”'''+'\n'+'''问题:“''' + question_train + '”' + '''

请直接回答该问题。

            '''
    return prompt

if __name__ == '__main__':
    prompt = get_prompt('yes_no', 'zero_shot')
    print('--------------------------------')
    print(len(prompt))
    print(prompt[0])
    # prompt = get_prompt('yes_no', 'one_shot')
    # print('--------------------------------')
    # print(len(prompt))
    # print(prompt[0])
    # prompt = get_prompt('yes_no', 'two_shot')
    # print('--------------------------------')
    # print(prompt[0])
    # prompt = get_prompt('factoid', 'zero_shot')
    # print('--------------------------------')
    # print(prompt[0])
    # prompt = get_prompt('factoid', 'one_shot')
    # print('--------------------------------')
    # print(prompt[0])
    # prompt = get_prompt('factoid', 'two_shot')
    # print('--------------------------------')
    # print(prompt[0])
    # prompt = get_prompt('factoid', 'three_shot')
    # print('--------------------------------')
    # print(prompt[0])