-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprompt_fewshot.py
executable file
·129 lines (99 loc) · 4.81 KB
/
prompt_fewshot.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
### ED
import os
import openai
import numpy as np
import time
import random
import config
random.seed(13)
### initial prompt
##### zero-shot
context = "Speaker: I’ve been hearing some strange noises around the house at night.\n\
Listener: oh no! That’s scary! What do you think it is?\n\
Speaker: I don’t know, that’s what’s making me anxious.\n\
Listener:"
prompt_EP_0 = "This is an empathetic dialogue task: The first worker (Speaker) is given an emotion label and writes his own description of a situation when he has felt that way. Then, Speaker tells his story in a conversation with a second worker (Listener). The emotion label and situation of Speaker are invisible to Listener. Listener should recognize and acknowledge others’ feelings in a conversation as much as possible.\
Now you play the role of Listener, please give the corresponding response according to the existing context. You only need to provide the next round of response of Listener.\n\n" + "The following is the existing dialogue context:\n\n" +context
##### few-shot
prompt_EP_1 = "This is an empathetic dialogue task: The first worker (Speaker) is given an emotion label and writes his own description of a situation when he has felt that way. Then, Speaker tells his story in a conversation with a second worker (Listener). The emotion label and situation of Speaker are invisible to Listener. Listener should recognize and acknowledge others’ feelings in a conversation as much as possible.\
Now you play the role of Listener, please give the corresponding response according to the existing context. You only need to provide the next round of response of Listener.\n\n" + "The following is an instance:\n\n"
prompt_EP_5 = "This is an empathetic dialogue task: The first worker (Speaker) is given an emotion label and writes his own description of a situation when he has felt that way. Then, Speaker tells his story in a conversation with a second worker (Listener). The emotion label and situation of Speaker are invisible to Listener. Listener should recognize and acknowledge others’ feelings in a conversation as much as possible.\
Now you play the role of Listener, please give the corresponding response according to the existing context. You only need to provide the next round of response of Listener.\n\n" + "The following is some instances:\n\n"
data_train_dialog = np.load("data/ED/sys_dialog_texts.train.npy", allow_pickle=True)
data_train_target = np.load("data/ED/sys_target_texts.train.npy", allow_pickle=True)
len_train = len(data_train_dialog)
shot1 = random.randint(0, len_train-1)
shot5 = random.sample(range(0,len_train), 5)
tmp = data_train_dialog[shot1]
tmp.append(data_train_target[shot1].strip())
### for 1-shot
Instance = "Instance:\n"
for i, j in enumerate(tmp):
if i % 2:
Instance += "user: " + j + "\n"
else:
Instance += "assistant: " + j + "\n"
prompt_EP_1 = prompt_EP_1 + Instance
### for 5-shot
Instances = ""
for id, k in enumerate(shot5):
tmp = data_train_dialog[k]
tmp.append(data_train_target[k].strip())
Instances += "Instance " + str(id+1) + ":\n"
for i, j in enumerate(tmp):
if i % 2:
Instances += "user: " + j + "\n"
else:
Instances += "assistant: " + j + "\n"
prompt_EP_5 = prompt_EP_5 + Instances
### your openai.api_key
openai.api_key = config.apikey
def get_res_chatgpt(m_name, contents):
response = openai.ChatCompletion.create(
model=m_name,
messages=contents,
temperature=config.temperature,
)['choices'][0]['message']['content']
return response
def get_conv(m_name, context, few_shot=1):
if few_shot == 1:
prompts = prompt_EP_1
elif few_shot == 5:
prompts = prompt_EP_5
context_list = []
context_list.append({
'role': 'system',
'content': prompts
})
for i, text in enumerate(context):
if len(text) == 0:
continue
if i % 2 == 0:
role_str = 'user'
else:
role_str = 'assistant'
context_list.append({
'role': role_str,
'content': text
})
response = get_res_chatgpt(m_name, context_list)
return response
def main():
fw = open(config.save_path, mode="a")
data_test = np.load("data/ED/sys_dialog_texts.test.npy", allow_pickle=True)
len_test = len(data_test)
continueid = -1 # continue after interruption, default to -1
id = continueid + 1
while id < len_test:
# try:
# res = get_conv(config.model, data_test[id], config.few_shot)
# except:
# continue
res = get_conv(config.model, data_test[id], config.few_shot)
fw.write(str(id) + " #*#*# " + res + "\n")
print(id)
id = id + 1
# time.sleep(20)
fw.close()
if __name__ == '__main__':
main()