forked from dmg-illc/JUDGE-BENCH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
144 lines (129 loc) · 5 KB
/
models.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
from transformers import (
AutoTokenizer,
pipeline,
)
import torch
from tqdm import tqdm
import time
from openai import OpenAI
from anthropic import Anthropic
import google.generativeai as genai
from google.generativeai.types import HarmCategory, HarmBlockThreshold
class HFModel:
def __init__(self, name, new_tokens) -> None:
self.model = pipeline(
"text-generation",
model=name,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True if "OLMo" in name else False,
)
if not self.model.tokenizer.pad_token_id:
self.model.tokenizer.pad_token_id = (
self.model.model.config.eos_token_id
)
self.n_tokens = new_tokens
def process(self, example, system_prompt=None):
user_message = {"role": "user", "content": example}
if system_prompt is None:
messages = [user_message]
else:
system_message = {"role": "system", "content": system_prompt}
messages = [system_message, user_message]
return self.model.tokenizer.apply_chat_template(
messages, tokenize=False
)
def generate_responses(self, dataset, batch_size, system_prompt=None):
dataset = list(map(lambda x: self.process(x, system_prompt), dataset))
responses = []
for response in tqdm(
self.model(
dataset,
batch_size=batch_size,
max_new_tokens=self.n_tokens,
do_sample=False,
num_beams=1,
return_full_text=False,
),
total=len(dataset),
):
responses.append(response[0]["generated_text"])
return responses
class APIModel:
name: str
new_tokens: int
family: str
def __init__(self, name: str, new_tokens: int) -> None:
self.name = name
self.new_tokens = new_tokens
if "gpt" in name:
self.family = "OpenAI"
self.client = OpenAI()
elif "claude" in name:
self.family = "Anthropic"
self.client = Anthropic()
elif "gemini" in name:
self.family = "Google"
self.client = genai.GenerativeModel(name)
else:
raise ValueError(
f'Model "{name} is not a valid ClosedModel (gpt, claude, gemini)'
)
def generate_responses(self, dataset, batch_size, system_prompt=None):
responses = []
for query in tqdm(dataset):
user_message = {"role": "user", "content": query}
if system_prompt is None:
messages = [user_message]
else:
system_message = {"role": "system", "content": system_prompt}
messages = [system_message, user_message]
if self.family == "OpenAI":
response = (
self.client.chat.completions.create(
model=self.name,
messages=messages,
max_tokens=self.new_tokens,
temperature=0,
)
.choices[0]
.message.content
)
responses.append(response)
elif self.family == "Anthropic":
response = (
self.client.messages.create(
model=self.name,
messages=messages,
max_tokens=self.new_tokens,
temperature=0,
)
.content[0]
.text
)
responses.append(response)
# time.sleep(3) # in case of rate limiting resort to 15 RPM
elif self.family == "Google":
response = self.client.generate_content(
query,
generation_config={
"max_output_tokens": self.new_tokens,
"temperature": 0,
},
safety_settings={ # https://ai.google.dev/gemini-api/docs/safety-settings (e.g., usecase: QAGS dataset)
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
},
)
try:
responses.append(response.text)
except ValueError:
print(f"FAILED REQUEST: {messages}\nRESPONSE: {response}")
responses.append("") # to keep all valid responses
else:
raise ValueError(
f'Family "{self.family} is not a valid family'
)
return responses