forked from SALT-NLP/CoAnnotating
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLM_Inferencing.py
222 lines (203 loc) · 5.58 KB
/
LLM_Inferencing.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import json
import openai
import pandas as pd
import time
import math
from collections import Counter
# This is set to `azure`
openai.api_type = "azure"
openai.api_key = ""
openai.api_base = ''
openai.api_version = '2023-03-15-preview'
# test
def prompt(text):
response = openai.ChatCompletion.create(
engine='chat',
model="gpt-3.5-turbo",
n=1,
messages=[
{
"role": "user",
"content": text
},
]
)
return response['choices'][0]['message']['content'].lower()
#df=df.read_csv('mrpc_train.csv')
#df=df.sample(n=1500,random_state=0)
df=pd.read_csv('mrpc_sample.csv')
#create prompts
df['prompt1']='Please label if the following two sentences are paraphrases of each other. Please give your answer as “paraphrase” or “not paraphrase”\n'+df['text']
df['prompt2']=df['text']+'\nPlease label if the two sentences above are paraphrases of each other. Please give your answer as “paraphrase” or “not paraphrase”'
df['prompt3']='Given the following two sentences, please classify the relationship of the following two sentences as “paraphrase” or “not paraphrase”\n'+df['text']
df['prompt4']='Is it true that the following two sentences are paraphrases of each other? Give your answer as “true” or “false”\n'+df['text']
df['prompt5']='Is it true that the following two sentences are not paraphrases of each other? Give your answer as “true” or “false”\n'+df['text']
df['prompt6']='What relationship do the following two sentences have? Is it “paraphrase” or “not paraphrase”?\n'+df['text']
df['prompt7']='Please choose one option that best describes the relationship between the following two sentences\n'+df['text']+'\n(A)Paraphrase\n(B)Not paraphrase'
df['prompt8']='I think the following two sentences are paraphrases of each other. Do you agree? Please give your answer in “yes” or “no”\n'+df['text']
df['prompt9']='I think the following two sentences are not paraphrases of each other. Do you agree? Please give your answer in “yes” or “no”\n'+df['text']
#df.to_csv('mrpc_train2.csv', index=False)
#inference
prompts=df['prompt1'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except:
print('ERROR!')
res.append('error')
df['label1']=res
prompts=df['prompt2'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except:
print('ERROR!')
res.append('error')
df['label2']=res
prompts=df['prompt3'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except:
print('ERROR!')
res.append('error')
df['label3']=res
prompts=df['prompt4'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except:
print('ERROR!')
res.append('error')
df['label4']=res
prompts=df['prompt5'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except BaseException as e:
print('ERROR!')
res.append('error')
df['label5']=res
prompts=df['prompt6'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except:
print('ERROR!')
res.append('error')
df['label6']=res
prompts=df['prompt7'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except:
print('ERROR!')
res.append('error')
df['label7']=res
prompts=df['prompt8'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except:
print('ERROR!')
res.append('error')
df['label8']=res
prompts=df['prompt9'].tolist()
res=[]
for p in prompts:
try:
resp = prompt(p)
print(resp)
res.append(resp)
time.sleep(0.3)
except:
try:
time.sleep(2)
resp = prompt(p)
print(resp)
res.append(resp)
except:
print('ERROR!')
res.append('error')
df['label9']=res
df.to_csv('result.csv', index=False)