-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm_class.py
76 lines (65 loc) · 2.43 KB
/
llm_class.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
# llm_class.py
import prompts
import torch
from constants import PHI_MODEL_NAME
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
class LanguageModel:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(LanguageModel, cls).__new__(cls)
return cls._instance
def __init__(self):
if not hasattr(self, 'initialized'): # Ensure `__init__` runs only once
self.model = AutoModelForCausalLM.from_pretrained(
PHI_MODEL_NAME,
device_map="cuda",
torch_dtype="auto",
trust_remote_code=True,
low_cpu_mem_usage=True
)
self.tokenizer = AutoTokenizer.from_pretrained(PHI_MODEL_NAME)
self.pipe = pipeline(
"text-generation",
model=self.model,
tokenizer=self.tokenizer,
)
self.initialized = True
def prompt(self, msg: str, temp: float = 0.0, new_tokens: int = 500) -> str:
"""
Generate a response from the language model given an input message.
Args:
msg (str): The input message.
temp (float): Temperature for sampling. Default is 0.0.
new_tokens (int): Number of new tokens to generate. Default is 500.
Returns:
str: The generated text.
"""
generation_args = {
"max_new_tokens": new_tokens,
"return_full_text": False,
"do_sample": temp != 0,
}
if temp != 0:
generation_args["temperature"] = temp
output = self.pipe(msg, **generation_args)
return output[0]['generated_text']
def get_keyphrases(self, text: str) -> str:
"""
Extract keyphrases from the given text.
Args:
text (str): The input text.
Returns:
str: The extracted keyphrases.
"""
#print(f"TEST PROMPT {prompts.make_multi_extraction('aaa')}")
return self.prompt(prompts.make_multi_extraction(text), temp=0, new_tokens=500)
def get_searchphrases(self, text: str) -> str:
"""
Extract searchphrases from the given text.
Args:
text (str): The input text.
Returns:
str: The extracted seachphrases.
"""
return self.prompt(prompts.make_search_prompt(text), temp=0, new_tokens=500)