Views
No views yet
1# !pip install accelerate bitsandbytes peft
2from transformers import AutoModelForCausalLM, BitsAndBytesConfig, AutoTokenizer
3import torch
4
5model_name = "mistralai/Mistral-7B-Instruct-v0.3"
6peft_model_id = "date3k2/Mistral-7B-Instruct-vi-alpaca"
7
8bnb_config = BitsAndBytesConfig(load_in_8bit=True)
9model = AutoModelForCausalLM.from_pretrained(
10 model_name,
11 quantization_config=bnb_config,
12 device_map="auto",
13 trust_remote_code=True,
14)
15tokenizer = AutoTokenizer.from_pretrained(model_name)
16
17
18model.load_adapter(peft_model_id)
19device = "cuda"
20
21messages = [
22 {
23 "role": "user",
24 "content": """You are a helpful Vietnamese AI chatbot. Below is an instruction that describes a task. Write a response that appropriately completes the request. Your response should be in Vietnamese.
25 Instruction:
26 Viết công thức để nấu một món ngon từ thịt bò.""",
27 },
28]
29
30encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
31
32model_inputs = encodeds.to(device)
33
34generated_ids = model.generate(model_inputs, max_new_tokens=500, do_sample=True)
35decoded = tokenizer.batch_decode(generated_ids)
36print(decoded[0])
37