File size: 10,495 Bytes
22fec82 |
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 |
import sys
import re
import csv
from datetime import date,timedelta
from openai import OpenAI
def resolve(slot):
pos=slot.find("place_name")
if pos!=-1:
bgpos=slot[pos:].find(":")
citypos=slot[pos+bgpos:].find("'")
oldcity=slot[pos+bgpos+1:pos+bgpos+citypos]
oldcity=oldcity.strip()
if oldcity=="my city":
newcity="new york"
slot=slot[:pos+bgpos+1]+str(newcity)+slot[pos+bgpos+citypos:]
pos=slot.find("date")
if pos!=-1:
bgpos=slot[pos:].find(":")
datepos=slot[pos+bgpos:].find("'")
olddate=slot[pos+bgpos+1:pos+bgpos+datepos]
olddate=olddate.strip()
today=date.today()
weekday=date.isoweekday(today)% 7
#print(today)
#print(weekday)
newdate2=None
if olddate=="today":
newdate=str(today)
elif olddate=="tomorrow":
newdate=today+timedelta(days=1)
elif olddate=="this week":
weekday=date.isoweekday(today)% 7
newdate2=today+ timedelta(days=7-weekday)
newdate=today-timedelta(days=weekday-1)
elif olddate=="last week":
weekday=date.isoweekday(today)% 7
newdate2=today-timedelta(days=weekday)
newdate=today-timedelta(days=weekday+6)
elif olddate=="this weekend":
weekday=date.isoweekday(today)% 7
newdate=today+timedelta(days=6-weekday)
newdate2=today+timedelta(days=7-weekday)
elif olddate=="sunday":
weekday=date.isoweekday(today)% 7
newdate=today+timedelta(7-weekday)
elif olddate=="august fifteenth":
if str(today).find("-")!=-1:
ypos=str(today).find("-")
newdate=str(today)[:ypos+1]+"08-15"
else:
newdate="08-15"
else:
newdate=olddate
#print(slot)
slot=slot[:pos+bgpos+1]+str(newdate)+slot[pos+bgpos+datepos:]
if newdate2:
slot=slot+",'date2:"+str(newdate2)+"'"
#print(slot)
pos=slot.find("timeofday")
oldtimeofday=""
if pos!=-1:
bgpos=slot[pos:].find(":")
datepos=slot[pos+bgpos:].find("'")
oldtimeofday=slot[pos+bgpos+1:pos+bgpos+datepos]
oldtimeofday=oldtimeofday.strip()
cpos=slot[pos:].rfind(",")
if cpos!=-1:
slot=slot[:cpos]+slot[pos+bgpos+datepos+1:]
else:
cpos=slot[pos:].find(",")
if cpos==-1:
slot=slot[:pos]+slot[pos+bgpos+datepos+1:]
else:
slot=slot[:pos]+slot[cpos+1:]
pos=slot.find("time:")
if pos==-1:
pos=slot.find("time :")
if pos!=-1:
bgpos=slot[pos:].find(":")
timepos=slot[pos+bgpos:].find("'")
oldtime=slot[pos+bgpos+1:pos+bgpos+timepos]
oldtime=oldtime.strip()
newtime=oldtime.replace("five","05:00").replace("six","06:00").replace("nine","09:00").replace("ten","10:00")
if not(newtime.endswith("am") or newtime.endswith("pm")):
if oldtimeofday=="morning":
newtime=newtime+" am"
elif oldtimeofday=="evening":
newtime=newtime+" pm"
elif oldtimeofday.find("afternoon")!=-1:
newtime=newtime+" pm"
#print(slot)
slot=slot[:pos+bgpos+1]+str(newtime)+slot[pos+bgpos+timepos:]
#print(slot)
else:
if oldtimeofday.find("afternoon")!=-1:
newtime="15:00"
#print(slot)
slot=slot+",'time:'"+newtime
#print(slot)
return slot
def read_data(file_path):
queries=[]
with open(file_path, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
count=0
for row in spamreader:
if count==0:
count+=1
continue
#print(row[2])
#print(len(row))
iid=row[0]
slot=row[3]
slot=slot.strip('[]')
slot=resolve(slot)
query={"iid":iid,"query":row[1],"intent":row[2],"slot":slot}#,"slots":slot,"domain":row[4]}
queries.append(query)
return queries
if __name__=="__main__":
datep=re.compile(r"(======)*202[45]-\d\d-\d\d")
client = OpenAI()
tasks=read_data("~/data/test.csv")
intp=re.compile(r"intent:[a-z_]+(,|\x1b)")
resp=re.compile(r'{"code":"SUCCESS","data"(.)+')#[":, \}\{a-zA-Z0-9\_]+,"msg":".."}')
for i in range(19,20):
print("-----*****------")
print(i)
data=[]
with open(str(i)+".log") as f:
lines=f.readlines()
for line in lines:
if datep.match(line):
continue
else:
data.append(line)
content="\n".join(data)
intentf=intp.search(content)
if intentf:
intent=intentf.group(0)
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:
print("ChatCompletionMessage(content='')")
#pass
else:
resf= resp.search(content)
if resf:
res=resf.group(0)
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 time is same, no matter if has am/pm, it is right. if slot is descriptor:all, then can neglect it, other descriptor can not neglect. the slot person is same with slot from_person and to_person. 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}],
print(msg)
completion = client.chat.completions.create(
model="gpt-4",
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 time is same, no matter if has am/pm, it is right. if slot is descriptor:all, then can 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}],
)
print(completion.choices[0].message)
else:
print("ChatCompletionMessage(content='the result is wrong')")
continue
else:
if content.find("\"results\"")==-1 and content.find("\"data\"")==-1 and content.find("\"success\"")==-1:
print("ChatCompletionMessage(content='the result is wrong. no server response.')")
continue
goldintent=tasks[i]["intent"]
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:
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 is weather_query, need to provide the weather condition. if intent is qa, the response need to provide the answer to the question, and if only provide url, it is wrong. 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 :"}],
print(msg)
try:
completion = client.chat.completions.create(
model="gpt-4",
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 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. 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 :"}],
)
print(completion.choices[0].message)
except:
completion = client.chat.completions.create(
model="gpt-4",
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 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. 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 :"}],
)
print(completion.choices[0].message)
else:
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 :"}],
print(msg)
completion = client.chat.completions.create(
model="gpt-4",
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."},{"role": "user", "content": "the task is "+tasks[i]["query"]+". The annotation is "+tasks[i]["slot"]+"\n"+"\n".join(data)+"\nyour answer is :"}],
)
print(completion.choices[0].message)
|