-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodeling.py
More file actions
199 lines (158 loc) · 5.78 KB
/
Copy pathmodeling.py
File metadata and controls
199 lines (158 loc) · 5.78 KB
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
import json
import time
import torch
import google.generativeai as genai
from openai import OpenAI
from pydantic import BaseModel
from typing import Optional, List
from transformers import AutoTokenizer, PreTrainedTokenizer
from fire import Fire
class DummyImport:
LLM = None
SamplingParams = None
try:
import vllm
from vllm.lora.request import LoRARequest
except ImportError:
print("vLLM not installed")
vllm = DummyImport()
LoRARequest = lambda *args: args
class EvalModel(BaseModel, arbitrary_types_allowed=True):
path_model: str
temperature: float = 0.0
def run(self, prompt: str) -> str:
raise NotImplementedError
class OpenAIModel(EvalModel):
path_model: str = "openai_key.json"
engine: str = "o1-preview"
timeout: int = 1200
client: Optional[OpenAI] = None
def load(self):
with open(self.path_model) as f:
info = json.load(f)
self.client = OpenAI(api_key=info["api_key"], timeout=self.timeout)
def make_messages(self, prompt: str) -> List[dict]:
return [{"role": "user", "content": prompt}]
def run(self, prompt: str) -> str:
self.load()
while True:
try:
response = self.client.chat.completions.create(
model=self.engine,
messages=self.make_messages(prompt),
)
output = response.choices[0].message.content
break
except Exception as e:
print(e)
time.sleep(5)
continue
return output
class OpenAIGPT4Model(OpenAIModel):
engine: str = "gpt-4o"
class OpenAIGPT3Model(OpenAIModel):
engine: str = "gpt-3.5-turbo"
class GeminiFlashThinkingModel(EvalModel):
path_model: str = "gemini_key.json"
engine: str = "gemini-2.0-flash-thinking-exp-01-21"
timeout: int = 600
model: Optional[genai.GenerativeModel] = None
def load(self):
with open(self.path_model) as f:
info = json.load(f)
api_key = info["api_key"]
genai.configure(api_key=api_key)
self.model = genai.GenerativeModel(self.engine)
def run(self, prompt: str) -> str:
self.load()
while True:
try:
response = self.model.generate_content(prompt)
break
except Exception as e:
print(e)
time.sleep(5)
continue
try:
output = response.text
except Exception as e:
output = ""
return output
class GeminiFlashModel(GeminiFlashThinkingModel):
engine: str = "gemini-2.0-flash-exp"
class VLLMModel(EvalModel):
path_lora: str = ""
model: vllm.LLM = None
quantization: Optional[str] = None
tokenizer: Optional[PreTrainedTokenizer] = None
tensor_parallel_size: Optional[int] = None
max_output_length: int = 512
stopping_words: Optional[List[str]] = None
def load(self):
if self.model is None:
available_gpus = torch.cuda.device_count()
if available_gpus == 0:
raise EnvironmentError("No GPUs detected.")
if self.tensor_parallel_size is None:
self.tensor_parallel_size = available_gpus
print(f"tensor_parallel_size not set. Using all available GPUs: {self.tensor_parallel_size}")
else:
if self.tensor_parallel_size > available_gpus:
raise ValueError(
f"tensor_parallel_size ({self.tensor_parallel_size}) exceeds the number of available GPUs ({available_gpus})."
)
print(f"Using tensor_parallel_size: {self.tensor_parallel_size} out of {available_gpus} available GPUs.")
self.model = vllm.LLM(
model=self.path_model,
trust_remote_code=True,
quantization=self.quantization,
enable_lora=self.path_lora != "",
tensor_parallel_size=self.tensor_parallel_size,
)
if self.tokenizer is None:
self.tokenizer = AutoTokenizer.from_pretrained(self.path_model)
def format_prompt(self, prompt: str) -> str:
self.load()
prompt = prompt.rstrip(" ")
return prompt
def make_kwargs(self, do_sample: bool, **kwargs) -> dict:
if self.stopping_words:
kwargs.update(stop=self.stopping_words)
params = vllm.SamplingParams(
temperature=0.5 if do_sample else 0.0,
max_tokens=self.max_output_length,
**kwargs
)
outputs = dict(sampling_params=params, use_tqdm=False)
if self.path_lora:
outputs.update(lora_request=LoRARequest("lora", 1, self.path_lora))
return outputs
def run(self, prompt: str) -> str:
prompt = self.format_prompt(prompt)
outputs = self.model.generate([prompt], **self.make_kwargs(do_sample=False))
pred = outputs[0].outputs[0].text
pred = pred.split("<|endoftext|>")[0]
return pred
def select_model(model_name: str, **kwargs) -> EvalModel:
model_map = dict(
o1=OpenAIModel,
gpt_4o=OpenAIGPT4Model,
gpt_35_turbo=OpenAIGPT3Model,
gemini_flash=GeminiFlashModel,
gemini_flash_thinking=GeminiFlashThinkingModel,
qwen=VLLMModel,
)
model_class = model_map.get(model_name)
if model_class is None:
raise ValueError(f"{model_name}. Choose from {list(model_map.keys())}")
return model_class(**kwargs)
def test_model(
prompt: str = "What are the rules for Sudoku?",
model_name: str = "gemini_flash_thinking",
**kwargs,
):
model = select_model(model_name, **kwargs)
print(locals())
print(model.run(prompt))
if __name__ == "__main__":
Fire()