Views
No views yet
1import torch
2
3major_version, minor_version = torch.cuda.get_device_capability()
4
5# Must install separately since Colab has torch 2.2.1, which breaks packages
6!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
7
8if major_version >= 8:
9 # Use this for new GPUs like Ampere, Hopper GPUs (RTX 30xx, RTX 40xx, A100, H100, L40)
10 !pip install --no-deps packaging ninja einops flash-attn xformers trl peft accelerate bitsandbytes
11else:
12 # Use this for older GPUs (V100, Tesla T4, RTX 20xx)
13 !pip install --no-deps xformers trl peft accelerate bitsandbytes1from transformers import pipeline
2from unsloth import FastLanguageModel
3
4model, tokenizer = FastLanguageModel.from_pretrained(
5 model_name = "lxyuan/llama-3-8b-Instruct-lora-merged",
6 dtype = None, # auto detect
7 load_in_4bit = True, # default is True
8)
9
10FastLanguageModel.for_inference(model) # Enable native 2x faster inference
11
12pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
13
14messages = [
15 {"role": "system", "content": "You are helpful AI bot that follows instruction to complete task."},
16 {"role": "user", "content": "Write me 10 sentences that end with 'apple"},
17]
18
19prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
20
21terminators = [
22 tokenizer.eos_token_id,
23 tokenizer.convert_tokens_to_ids("<|eot_id|>")
24]
25
26outputs = pipeline(
27 prompt,
28 max_new_tokens=512,
29 eos_token_id=terminators,
30 do_sample=True,
31 temperature=0.6,
32 top_p=0.9,
33)
34
35print(outputs[0]["generated_text"])1<|begin_of_text|><|start_header_id|>system<|end_header_id|>
2
3You are helpful AI bot that follows instruction to complete task.<|eot_id|><|start_header_id|>user<|end_header_id|>
4
5Write me 10 sentences that end with 'apple<|eot_id|><|start_header_id|>assistant<|end_header_id|>
6
7Here are 10 sentences that end with the word "apple":
8
91. The farmer grew a juicy red apple.
102. She ate a crunchy green apple.
113. The tree bore a ripe yellow apple.
124. He bit into a sweet Granny Smith apple.
135. The basket was filled with fresh apples.
146. The juice was squeezed from a ripe red apple.
157. She picked a perfect autumn apple.
168. The pie was filled with tender Granny Smith apple.
179. The farmer's market sold a variety of apples.
1810. The snack was a crisp, juicy apple.