-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhi3_5.py
52 lines (44 loc) · 1.46 KB
/
Phi3_5.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
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from LLM import LLM
import torch
class Phi3_5(LLM):
def load_model(self):
self.id = 11
self.tokenizer = AutoTokenizer.from_pretrained(
"microsoft/Phi-3.5-mini-instruct"
)
self.model = AutoModelForCausalLM.from_pretrained(
"microsoft/Phi-3.5-mini-instruct",
device_map="auto",
torch_dtype="auto",
trust_remote_code=True,
)
self.model.eval()
print("Phi3 model loaded")
def generate(self, prompt: str) -> str:
# inputs = self.tokenizer(prompt, return_tensors="pt")
# outputs = self.model.generate(**inputs)
# return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
messages = [
{
"role": "system",
"content": "You are an AI assistant that answers Place related MCQ questions.",
},
{
"role": "user",
"content": prompt,
},
]
pipe = pipeline(
"text-generation",
model=self.model,
tokenizer=self.tokenizer,
)
generation_args = {
"max_new_tokens": 512,
"return_full_text": False,
"temperature": 0.0,
"do_sample": False,
}
output = pipe(messages, **generation_args)
return output[0]["generated_text"]