lorashen commited on
Commit
e083390
·
verified ·
1 Parent(s): 065841a

upload eval files

Browse files
Files changed (3) hide show
  1. eval/eval.py +239 -0
  2. eval/eval.sh +2 -0
  3. eval/stat.py +34 -0
eval/eval.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import re
3
+ import requests
4
+ from datetime import date,timedelta
5
+ from openai import OpenAI
6
+ import csv
7
+
8
+ def resolve(slot):
9
+ pos=slot.find("place_name")
10
+ if pos!=-1:
11
+ bgpos=slot[pos:].find(":")
12
+ citypos=slot[pos+bgpos:].find("'")
13
+ oldcity=slot[pos+bgpos+1:pos+bgpos+citypos]
14
+ oldcity=oldcity.strip()
15
+ if oldcity=="my city":
16
+ newcity="new york"
17
+ slot=slot[:pos+bgpos+1]+str(newcity)+slot[pos+bgpos+citypos:]
18
+ pos=slot.find("date")
19
+ if pos!=-1:
20
+ bgpos=slot[pos:].find(":")
21
+ datepos=slot[pos+bgpos:].find("'")
22
+ olddate=slot[pos+bgpos+1:pos+bgpos+datepos]
23
+ olddate=olddate.strip()
24
+ today=date.today()
25
+ weekday=date.isoweekday(today)% 7
26
+ #print(today)
27
+ #print(weekday)
28
+ newdate2=None
29
+ if olddate=="today":
30
+ newdate=str(today)
31
+ elif olddate=="tomorrow":
32
+ newdate=today+timedelta(days=1)
33
+ elif olddate=="this week":
34
+ weekday=date.isoweekday(today)% 7
35
+ newdate2=today+ timedelta(days=7-weekday)
36
+ newdate=today-timedelta(days=weekday-1)
37
+ elif olddate=="last week":
38
+ weekday=date.isoweekday(today)% 7
39
+ newdate2=today-timedelta(days=weekday)
40
+ newdate=today-timedelta(days=weekday+6)
41
+
42
+ elif olddate=="this weekend":
43
+ weekday=date.isoweekday(today)% 7
44
+ newdate=today+timedelta(days=6-weekday)
45
+ newdate2=today+timedelta(days=7-weekday)
46
+ elif olddate=="sunday":
47
+ weekday=date.isoweekday(today)% 7
48
+ newdate=today+timedelta(7-weekday)
49
+ elif olddate=="august fifteenth":
50
+ if str(today).find("-")!=-1:
51
+ ypos=str(today).find("-")
52
+ newdate=str(today)[:ypos+1]+"08-15"
53
+ else:
54
+ newdate="08-15"
55
+ else:
56
+ newdate=olddate
57
+ #print(slot)
58
+ slot=slot[:pos+bgpos+1]+str(newdate)+slot[pos+bgpos+datepos:]
59
+ if newdate2:
60
+ slot=slot+",'date2:"+str(newdate2)+"'"
61
+ #print(slot)
62
+ pos=slot.find("timeofday")
63
+ oldtimeofday=""
64
+ if pos!=-1:
65
+ bgpos=slot[pos:].find(":")
66
+ datepos=slot[pos+bgpos:].find("'")
67
+ oldtimeofday=slot[pos+bgpos+1:pos+bgpos+datepos]
68
+ oldtimeofday=oldtimeofday.strip()
69
+ cpos=slot[pos:].rfind(",")
70
+ if cpos!=-1:
71
+ slot=slot[:cpos]+slot[pos+bgpos+datepos+1:]
72
+ else:
73
+ cpos=slot[pos:].find(",")
74
+ if cpos==-1:
75
+ cbpos=slot[:pos].rfind(",")
76
+ if cbpos==-1:
77
+ slot=slot[:pos]+slot[pos+bgpos+datepos+1:]
78
+ else:
79
+ slot=slot[:cbpos]+slot[pos+bgpos+datepos+1:]
80
+
81
+ else:
82
+ slot=slot[:pos]+slot[cpos+1:]
83
+ pos=slot.find("time:")
84
+ if pos==-1:
85
+ pos=slot.find("time :")
86
+ if pos!=-1:
87
+ bgpos=slot[pos:].find(":")
88
+ timepos=slot[pos+bgpos:].find("'")
89
+ oldtime=slot[pos+bgpos+1:pos+bgpos+timepos]
90
+ oldtime=oldtime.strip()
91
+ newtime=oldtime.replace("five","05:00").replace("six","06:00").replace("nine","09:00").replace("ten","10:00").replace("three","3:00").replace("one","1:00")
92
+ if not(newtime.endswith("am") or newtime.endswith("pm")):
93
+ if oldtimeofday=="morning":
94
+ newtime=newtime+" am"
95
+ elif oldtimeofday=="evening":
96
+ newtime=newtime+" pm"
97
+ elif oldtimeofday.find("afternoon")!=-1:
98
+ newtime=newtime+" pm"
99
+ #print(slot)
100
+ slot=slot[:pos]+"time:"+str(newtime)+slot[pos+bgpos+timepos:]
101
+ #print(slot)
102
+ else:
103
+ if oldtimeofday.find("afternoon")!=-1:
104
+ newtime="12:00"
105
+ newtime2="17:00"
106
+ #print(slot)
107
+ slot=slot+",'from_time:"+newtime+"','to_time:"+newtime2+"'"
108
+ #print(slot)
109
+ pos=slot.find("time2:")
110
+ if pos==-1:
111
+ pos=slot.find("time2 :")
112
+ if pos!=-1:
113
+ bgpos=slot[pos:].find(":")
114
+ timepos=slot[pos+bgpos:].find("'")
115
+ oldtime=slot[pos+bgpos+1:pos+bgpos+timepos]
116
+ oldtime=oldtime.strip()
117
+ newtime=oldtime.replace("five","05:00").replace("six","06:00").replace("nine","09:00").replace("ten","10:00").replace("three","3:00").replace("one","1:00")
118
+ if not(newtime.endswith("am") or newtime.endswith("pm")):
119
+ if oldtimeofday=="morning":
120
+ newtime=newtime+" am"
121
+ elif oldtimeofday=="evening":
122
+ newtime=newtime+" pm"
123
+ elif oldtimeofday.find("afternoon")!=-1:
124
+ newtime=newtime+" pm"
125
+ #print(slot)
126
+ slot=slot[:pos]+"to_time:"+str(newtime)+slot[pos+bgpos+timepos:]
127
+ slot=slot.replace("'time:","'from_time:")
128
+
129
+
130
+ return slot
131
+ def read_data(file_path):
132
+ queries=[]
133
+ with open(file_path, newline='') as csvfile:
134
+ spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
135
+ count=0
136
+ for row in spamreader:
137
+ if count==0:
138
+ count+=1
139
+ continue
140
+ #print(row[2])
141
+ #print(len(row))
142
+ iid=row[0]
143
+ slot=row[3]
144
+ slot=slot.strip('[]')
145
+ #print(iid,slot)
146
+ slot=resolve(slot)
147
+ #print(slot)
148
+ query={"iid":iid,"query":row[1],"intent":row[2],"slot":slot}#,"slots":slot,"domain":row[4]}
149
+ queries.append(query)
150
+ return queries
151
+ if __name__=="__main__":
152
+ client = OpenAI()
153
+ tasks=read_data("~/data/test.csv")
154
+ intp=re.compile(r"http[s]*://.+intent=[a-z_&=0-9\-]+")
155
+ resp=re.compile(r'{"code":"SUCCESS","data"(.)+')#[":, \}\{a-zA-Z0-9\_]+,"msg":".."}')
156
+ for i in range(0,100):
157
+ print("-----*****------")
158
+ print(i)
159
+ data=[]
160
+ with open(str(i)+".log") as f:
161
+ lines=f.readlines()
162
+ for line in lines:
163
+ #if line.find("whole_manager")!=-1 and line.find("receives the following")!=-1:
164
+ # continue
165
+ #else:
166
+ data.append(line)
167
+ content="\n".join(data)
168
+ intentf=intp.search(content)
169
+ goldintent=tasks[i]["intent"]
170
+ if intentf:
171
+ intent=intentf.group(0)
172
+ if intent.find("weather")!=-1 or intent.find("news_query")!=-1 or intent.find("qa")!=-1 or intent.find("stock")!=-1 or intent.find("general")!=-1 or intent.find("currency")!=-1:
173
+ if goldintent.find("weather")!=-1 or goldintent.find("news_query")!=-1 or goldintent.find("qa")!=-1 or goldintent.find("stock")!=-1 or goldintent.find("general")!=-1 or goldintent.find("currency")!=-1:
174
+ print("ChatCompletionMessage(content='')")
175
+ else:
176
+ print("ChatCompletionMessage(content='the result is wrong. intent error.')")
177
+ continue
178
+ #pass
179
+ else:
180
+ resf= resp.search(content)
181
+ if resf:
182
+ sizer=len(resf.groups())
183
+ res=resf.group(sizer-1)
184
+ msg=[{"role": "system", "content":"please judge the following result is right or wrong. if slot is {} and result also {}, it is right. if slot are same, it is right. If intent is datetime_query, then the slot date:"+str(date.today())+" can be neglected. The slot change_to and change_amount are not the same. if time is same, no matter if has am/pm, it is right. if slot event_name or list_name has difference, then can neglect it. if slot is descriptor:all, then can neglect it, if slot is descriptor:default, then can not neglect it, other descriptor can not neglect. the slot person is same with slot from_person and to_person. if slot is query:song name, can be neglected. the place_name will be resolved, so if it is added by state name and country name, it is right."},{"role":"user", "content":"The golden slot is :{"+tasks[i]["slot"]+"}. The result is "+res}],
185
+ print(msg)
186
+ completion = client.chat.completions.create(
187
+ model="gpt-4",
188
+ temperature=0.0,
189
+ messages=[{"role": "system", "content":"please judge the following result is right or wrong. if slot is {} and result also {}, it is right. if slot are same, it is right. If intent is datetime_query, then the slot date:"+str(date.today())+" can be neglected. The slot change_to and change_amount are not the same. if time is same, no matter if has am/pm, it is right. if slot event_name or list_name has difference, then can neglect it. if slot is descriptor:all, then can neglect it, if slot is descriptor:default, then can not neglect it, other descriptor can not neglect. the slot person is same with slot from_person and to_person. if slot is query:song name, can be neglected. the place_name will be resolved, so if it is added by state name and country name, it is right."},{"role":"user", "content":"The golden slot is :{"+tasks[i]["slot"]+"}. The result is "+res}],
190
+ )
191
+ print(completion.choices[0].message)
192
+ else:
193
+ print("ChatCompletionMessage(content='the result is wrong, no server response')")
194
+ continue
195
+ else:
196
+ if content.find("\"results\"")==-1 and content.find("\"data\"")==-1 and content.find("\"success\"")==-1 and content.find("\"amount\"")==-1:
197
+ if content.find("general_quirky")==-1:
198
+ print("ChatCompletionMessage(content='the result is wrong. no server response.')")
199
+ continue
200
+ if goldintent.find("weather")!=-1 or goldintent.find("news_query")!=-1 or goldintent.find("qa")!=-1 or goldintent.find("stock")!=-1 or goldintent.find("general")!=-1 or goldintent.find("currency")!=-1:
201
+ msg=[{"role": "system", "content":"please judge the following content finish the task is right or wrong. Must has the server response ,if not, it's wrong. if intent is general_quirky, if it's a chit-chat like 'are you happy', and there is no chit-chat reply to user, it is wrong. If intent is weather_query, need to provide the weather condition, and if no weather condition, it is wrong. if intent is qa, the response need to provide the answer to the question, and if only provide url, it is wrong. if ask for currency, the result need provide the conversion rate, the currency name may be Proper Noun, it is right. also need to check if the annotaion are the same with server response"},{"role": "user", "content": "the task is "+tasks[i]["query"]+". The annotation is "+tasks[i]["slot"]+"\n"+"\n".join(data)+"\nyour answer is :"}],
202
+ print(msg)
203
+ try:
204
+ completion = client.chat.completions.create(
205
+ model="gpt-4",
206
+ temperature=0.0,
207
+ messages=[{"role": "system", "content":"please judge the following content finish the task is right or wrong. Must has the server response ,if not, it's wrong. if intent is general_quirky, if it's a chit-chat like 'are you happy', and there is no chit-chat reply to user, it is wrong. If intent is weather_query, need to provide the weather condition, and if no weather condition, it is wrong. if intent is qa, the response need to provide the answer to the question, and if only provide url, it is wrong. if ask for currency, the result need provide the conversion rate, the currency name may be Proper Noun, it is right. also need to check if the annotaion are the same with server response"},{"role": "user", "content": "the task is "+tasks[i]["query"]+". The annotation is "+tasks[i]["slot"]+"\n"+"\n".join(data)+"\nyour answer is :"}],
208
+ )
209
+
210
+ print(completion.choices[0].message)
211
+ except:
212
+ completion = client.chat.completions.create(
213
+ model="gpt-4",
214
+ temperature=0.0,
215
+ messages=[{"role": "system", "content":"please judge the following content finish the task is right or wrong. Must has the server response ,if not, it's wrong. if intent is general_quirky, if it's a chit-chat like 'are you happy', and there is no chit-chat reply to user, it is wrong. If intent is weather_query, need to provide the weather condition, and if no weather condition, it is wrong. if intent is qa, the response need to provide the answer to the question, and if only provide url, it is wrong. if ask for currency, the result need provide the conversion rate, the currency name may be Proper Noun, it is right. also need to check if the annotaion are the same with server response"},{"role": "user", "content": "the task is "+tasks[i]["query"]+". The annotation is "+tasks[i]["slot"]+"\n"+"\n".join(data)[-16000:]+"\nyour answer is :"}],
216
+ )
217
+ print(completion.choices[0].message)
218
+
219
+
220
+ else:
221
+ msg=[{"role": "system", "content":"please judge the following content finish the task is right or wrong. Must has the server response {\"code\":\"SUCCESS\"},if not, it's wrong. if response is not supported intent, it is wrong."},{"role": "user", "content": "the task is "+tasks[i]["query"]+". The annotation is "+tasks[i]["slot"]+"\n"+"\n".join(data)+"\nyour answer is :"}],
222
+ print(msg)
223
+ try:
224
+ completion = client.chat.completions.create(
225
+ model="gpt-4",
226
+ temperature=0.0,
227
+ messages=[{"role": "system", "content":"please judge the following content finish the task is right or wrong. Must has the server response {\"code\":\"SUCCESS\"},if not, it's wrong. if response is not supported intent, it is wrong. If the server response need further more information, it is right."},{"role": "user", "content": "the task is "+tasks[i]["query"]+". The annotation is "+tasks[i]["slot"]+"\n"+"\n".join(data)+"\nyour answer is :"}],
228
+ )
229
+
230
+ print(completion.choices[0].message)
231
+ except:
232
+ completion = client.chat.completions.create(
233
+ model="gpt-4",
234
+ temperature=0.0,
235
+ messages=[{"role": "system", "content":"please judge the following content finish the task is right or wrong. Must has the server response {\"code\":\"SUCCESS\"},if not, it's wrong. if response is not supported intent, it is wrong. If the server response need further more information, it is right."},{"role": "user", "content": "the task is "+tasks[i]["query"]+". The annotation is "+tasks[i]["slot"]+"\n"+"\n".join(data)[-16000:]+"\nyour answer is :"}],
236
+ )
237
+
238
+ print(completion.choices[0].message)
239
+
eval/eval.sh CHANGED
@@ -0,0 +1,2 @@
 
 
 
1
+ python3 eval.py > eval.log
2
+ python3 stat.py
eval/stat.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ wrong=0
3
+ right=0
4
+ wrongcount=0
5
+ rightcount=0
6
+ judgesent=re.compile(r"ChatCompletionMessage")
7
+ with open("eval.log") as f:
8
+ lines=f.readlines()
9
+ for line in lines:
10
+ lowline=line.lower()
11
+ if line.startswith("-----**"):
12
+ if wrong>0:
13
+ wrongcount+=1
14
+ elif right>0:
15
+ rightcount+=1
16
+ else:
17
+ print("error")
18
+ wrong=0
19
+ right=0
20
+ elif judgesent.match(line):
21
+ if lowline.find("wrong")!=-1 or lowline.find("incorrect")!=-1 or lowline.find("not completed")!=-1:
22
+ wrong+=1
23
+ elif lowline.find("right")!=-1 or lowline.find("correct")!=-1:
24
+ right+=1
25
+ elif lowline.find("content=''")!=-1:
26
+ pass
27
+ else:
28
+ print("error")
29
+ if wrong>0:
30
+ wrongcount+=1
31
+ else:
32
+ rightcount+=1
33
+ acc=float(rightcount)/100
34
+ print(acc,"right",rightcount,"wrong",wrongcount)