Views
No views yet

MaziyarPanahi/Llama-3-11B-Instruct-v0.1 model.MaziyarPanahi/Llama-3-16B-Instruct-v0.1 as the model name in Hugging Face's
transformers library.1from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
2from transformers import pipeline
3import torch
4
5model_id = "MaziyarPanahi/Llama-3-16B-Instruct-v0.1"
6
7model = AutoModelForCausalLM.from_pretrained(
8 model_id,
9 torch_dtype=torch.float16,
10 device_map="auto",
11 trust_remote_code=True,
12 # attn_implementation="flash_attention_2"
13)
14
15tokenizer = AutoTokenizer.from_pretrained(
16 model_id,
17 trust_remote_code=True
18)
19
20streamer = TextStreamer(tokenizer)
21
22pipeline = pipeline(
23 "text-generation",
24 model=model,
25 tokenizer=tokenizer,
26 model_kwargs={"torch_dtype": torch.bfloat16},
27 streamer=streamer
28)
29
30# Then you can use the pipeline to generate text.
31
32messages = [
33 {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
34 {"role": "user", "content": "Who are you?"},
35]
36
37prompt = tokenizer.apply_chat_template(
38 messages,
39 tokenize=False,
40 add_generation_prompt=True
41)
42
43terminators = [
44 tokenizer.eos_token_id,
45 tokenizer.convert_tokens_to_ids("<|eot_id|>")
46]
47
48outputs = pipeline(
49 prompt,
50 max_new_tokens=256,
51 eos_token_id=terminators,
52 do_sample=True,
53 temperature=0.6,
54 top_p=0.95,
55)
56print(outputs[0]["generated_text"][len(prompt):])1<|begin_of_text|><|start_header_id|>system<|end_header_id|>
2
3You are a helpful assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>
4
5what's 25-4*2+3<|eot_id|><|start_header_id|>assistant<|end_header_id|>
6
7To evaluate this expression, we need to follow the order of operations (PEMDAS):
8
91. First, multiply 4 and 2: 4*2 = 8
102. Then, subtract 8 from 25: 25 - 8 = 17
113. Finally, add 3: 17 + 3 = 20
12
13So, 25-4*2+3 = 20!<|eot_id|>
14To evaluate this expression, we need to follow the order of operations (PEMDAS):
15
161. First, multiply 4 and 2: 4*2 = 8
172. Then, subtract 8 from 25: 25 - 8 = 17
183. Finally, add 3: 17 + 3 = 20
19
20So, 25-4*2+3 = 20!