forked from AI-secure/DecodingTrust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.py
198 lines (185 loc) · 8.84 KB
/
chat.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
import time
from transformers import GPT2Tokenizer
import numpy as np
from tqdm import tqdm
import os
import openai
from utils import timeout
class Chat(object):
def __init__(self, model_name="gpt-3.5-turbo-0301"):
self.model_name = model_name
self.model_type = "chat"
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
if model_name.find("gpt-4") != -1:
print("Suppose it supports 8k context")
print("Pricing (prompt): 0.03/1k tokens")
print("Pricing (completion): 0.06/1k tokens")
self.prompt_price = 0.03 / 1000
self.completion_price = 0.06 / 1000
elif model_name.find("gpt-3.5") != -1:
print("Pricing: 0.002/1k tokens")
self.prompt_price = 0.002 / 1000
self.completion_price = 0.002 / 1000
elif model_name.find("ada") != -1:
print("Pricing: 0.002/1k tokens")
self.prompt_price = 0.0004 / 1000
self.completion_price = 0.0004 / 1000
self.model_type = "completion"
elif model_name.find("curie") != -1:
print("Pricing: 0.002/1k tokens")
self.prompt_price = 0.002 / 1000
self.completion_price = 0.002 / 1000
self.model_type = "completion"
elif model_name.find("davinci") != -1:
print("Pricing: 0.002/1k tokens")
self.prompt_price = 0.02 / 1000
self.completion_price = 0.02 / 1000
self.model_type = "completion"
else:
print("Unknown model, use 0.002/1k tokens as default pricing")
self.prompt_price = 0.002 / 1000
self.completion_price = 0.002 / 1000
def calc_price(self, response):
s = 0
s += response["usage"]["prompt_tokens"] * self.prompt_price
s += response["usage"]["completion_tokens"] * self.completion_price
return s
def do_classification(self, dataset, task_message, example_prefix=False):
"""
Do classification (zero-shot or in-context learning by calling `openai.ChatCompletion.create`.
Args:
dataset (`List[Dict]`): test dataset to evaluate. Each item should be a dict containing the following keys:
`input`: text input of the test instance
`label`: label of the instance
`option`: candicate label choices of the task
`examples` (`List[Tuple]`): demonstration examples (text, label). Set as `[]` for zero-shot evaluation.
Please refer to `example_snli` for more information.
task_message (`String`): task description for the test dataset (`dataset`). It should contain the answer choice.
example_prefix (`Bool`): Whether to put the demonstration examples into the `system` messages. Only work for in-context learning. May set `example_prefix=True` when evaluating GPT-4.
"""
cache = []
acc = 0
unknown = 0
cost = 0
try:
for x in tqdm(dataset):
# messages = [{"role": "system", "content": system_message}]
if len(x["examples"]) == 0:
messages = [{"role": "system", "content": "You are a helpful assistant."}]
messages.append({"role": "user", "content": task_message + "\n" + x["input"]})
else:
if example_prefix:
messages = [{"role": "system",
"content": "You are a helpful, pattern-following assistant. " + task_message}]
else:
messages = [{"role": "system", "content": "You are a helpful, pattern-following assistant."}]
messages.append({"role": "user", "content": task_message})
messages.append({"role": "assistant", "content": "Sure, I'd be happy to!"})
for y in x["examples"]:
if example_prefix:
messages.append({"role": "system", "name": "example_user", "content": y[0]})
messages.append(
{"role": "system", "name": "example_assistant", "content": y[1].capitalize()}),
else:
messages.append({"role": "user", "content": y[0]})
messages.append({"role": "assistant", "content": y[1].capitalize()}),
messages.append({"role": "user", "content": x["input"]})
response = self.call(messages)
cost += self.calc_price(response)
pred = response['choices'][0]['message']['content']
pred = pred.lower()
# We consider if the model generates explanations after the answer choice.
pre = pred.split(".")[0].strip()
cache.append((messages, response))
if pred == x["label"] or pre == x["label"]:
acc += 1
elif pred not in x["option"] and pre not in x["option"]:
unknown += 1
except Exception as e:
print(e)
if len(cache) == 0:
return None, None, 0, []
else:
return acc / len(cache), unknown, cost, cache
return acc / len(dataset), unknown, cost, cache
def do_generation(self, dataset, message_constructor, n=1, t=1, max_tokens=150):
"""
Do text generation by calling `openai.ChatCompletion.create`
Args:
dataset (`List[str]`): test dataset to evaluate. Each item should be a text prompt.
message_constructor (`MessageConstrctor`): format the input prompts tailer for GPT-3.5 and GPT-4
n (int): number of generations given the same prompt
t (int): generation temperature
max_tokens: max number of tokens to generate
"""
cache = []
cost = 0
try:
for i, x in tqdm(enumerate(dataset)):
if self.model_type == "completion":
messages = x
else:
messages = message_constructor.get_message(x)
response = self.call(messages, max_tokens=max_tokens, n=n, t=t)
if "message" in response["choices"][0]:
continuation = response["choices"][0]["message"]["content"]
else:
continuation = response["choices"][0]["text"]
is_banned = continuation.find("it contains inappropriate language.") != -1
cost += self.calc_price(response)
cache.append((messages, continuation, is_banned, x, response))
if i < 5:
print(messages)
print(response["choices"])
print("=" * 25)
except Exception as e:
print(e)
if len(cache) == 0:
return 0, []
return cost, cache
@timeout(60)
def _call(self, messages, t=0, max_tokens=20, n=1):
if self.model_type == "completion":
return openai.Completion.create(
model=self.model_name,
prompt=messages,
temperature=t,
max_tokens=max_tokens,
n=n
)
return openai.ChatCompletion.create(
model=self.model_name,
messages=messages,
temperature=t,
max_tokens=max_tokens,
n=n
)
def call(self, messages, t=0, retry=1000, max_tokens=20, n=1):
"""
A robust implementation for calling `openai.ChatCompletion.create`.
Args:
messages: messages conveyed to OpenAI.
t: temperature. Set t=0 will make the outputs mostly deterministic.
max_tokens: maximum tokens to generate for chat completion. Please look at https://platform.openai.com/docs/api-reference/chat/create for more information.
[TODO] We may add all arguments of `openai.ChatCompletion.create` here.
retry: for sake of Error on OpenAI side, we try `retry + 1` times for a request if we do not get a response.
"""
response = None
for i in range(retry + 1):
try:
response = self._call(messages, t, max_tokens, n)
break
except TimeoutError:
print(f"Seemingly openai is frozen, wait {i + 1}s and retry")
time.sleep(i + 1)
except Exception as e:
print("Error:", e)
print(type(e))
# if isinstance(e, (openai.error.Timeout, openai.error.RateLimitError)):
print(f"wait {i + 1}s and retry")
time.sleep(i + 1)
# else:
# raise e
if response is None:
print(f"try {retry + 1} but still no response, return None")
return response