2023-11-28 14:48:41 +08:00
from transformers import LlamaTokenizer , AutoModelForCausalLM , AutoConfig , GenerationConfig
import torch
DEFAULT_SYSTEM_PROMPT = """ You are a helpful assistant. 你是一个乐于助人的助手。请你提供专业、有逻辑、内容真实、有价值的详细回复。 """
TEMPLATE = (
" <s> A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user ' s questions. USER: [INST] <<SYS>> \n "
" {system_prompt} \n "
" <</SYS>> \n \n "
" {instruction} [/INST] ASSISTANT: "
)
def generate_prompt ( instruction ) :
return TEMPLATE . format_map ( { ' instruction ' : instruction , ' system_prompt ' : DEFAULT_SYSTEM_PROMPT } )
ckpt_path = ' ./LLM/TechGPT2-Alpaca-hf/ '
load_type = torch . float16
device = torch . device ( 1 )
tokenizer = LlamaTokenizer . from_pretrained ( ckpt_path )
tokenizer . pad_token_id = 0
tokenizer . bos_token_id = 1
tokenizer . eos_token_id = 2
tokenizer . padding_side = " left "
model_config = AutoConfig . from_pretrained ( ckpt_path )
model = AutoModelForCausalLM . from_pretrained ( ckpt_path , torch_dtype = load_type , config = model_config )
model . to ( device )
model . eval ( )
generation_config = GenerationConfig (
temperature = 0.1 ,
top_p = 0.75 ,
top_k = 40 ,
num_beams = 1 ,
bos_token_id = 1 ,
eos_token_id = 2 ,
pad_token_id = 0 ,
max_new_tokens = 128 ,
min_new_tokens = 10 ,
do_sample = True ,
)
2023-11-29 16:18:46 +08:00
example = ' 请把下列标题扩写成摘要, 不少于100字: 基于视觉语言多模态的实体关系联合抽取的研究。 '
instruction = generate_prompt ( instruction = example )
instruction = tokenizer ( instruction , return_tensors = " pt " )
input_ids = instruction [ " input_ids " ] . to ( device )
with torch . no_grad ( ) :
generation_output = model . generate (
input_ids = input_ids ,
generation_config = generation_config ,
return_dict_in_generate = True ,
output_scores = True ,
repetition_penalty = 1.2 ,
)
output = generation_output . sequences [ 0 ]
output = tokenizer . decode ( output , skip_special_tokens = True )
print ( output )