Views
No views yet
pip install "transformers @ git+https://github.com/huggingface/transformers.git@cb0f604"1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "HighCWu/Embformer-MiniMind-0.1B"
4
5# load the tokenizer and the model
6tokenizer = AutoTokenizer.from_pretrained(
7 model_name,
8 trust_remote_code=True,
9 cache_dir=".cache"
10)
11model = AutoModelForCausalLM.from_pretrained(
12 model_name,
13 torch_dtype="auto",
14 device_map="auto",
15 trust_remote_code=True,
16 cache_dir=".cache"
17)
18
19# prepare the model input
20prompt = "请为我讲解“大语言模型”这个概念。"
21messages = [
22 {"role": "user", "content": prompt}
23]
24text = tokenizer.apply_chat_template(
25 messages,
26 tokenize=False,
27 add_generation_prompt=True
28)
29model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
30
31# conduct text completion
32generated_ids = model.generate(
33 input_ids=model_inputs['input_ids'],
34 attention_mask=model_inputs['attention_mask'],
35 max_new_tokens=8192
36)
37output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
38
39print(tokenizer.decode(output_ids, skip_special_tokens=True))