Views
No views yet
pip install peft transformers jinja2==3.1.01import torch
2from peft import PeftModel
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5# Load the base model and tokenizer
6model = AutoModelForCausalLM.from_pretrained(
7 "meta-llama/Llama-3.1-8B-Instruct", torch_dtype=torch.bfloat16
8)
9tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
10
11# Load the fine-tuned model using LORA
12model = PeftModel.from_pretrained(
13 model,
14 "koyeb/Meta-Llama-3.1-8B-Instruct-Apple-MLX",
15).to("cuda")
16
17# Define input using a chat template with a system prompt and user query
18ids = tokenizer.apply_chat_template(
19 [
20 {
21 "role": "system",
22 "content": "You are a helpful AI coding assistant with expert knowledge of Apple's latest machine learning framework: MLX. You can help answer questions about MLX, provide code snippets, and help debug code.",
23 },
24 {
25 "role": "user",
26 "content": "How do you transpose a matrix in MLX?",
27 },
28 ],
29 tokenize=True,
30 add_generation_prompt=True,
31 return_tensors="pt",
32).to("cuda")
33
34# Generate and print the response
35print(
36 tokenizer.decode(
37 model.generate(input_ids=ids, max_new_tokens=256, temperature=0.5).tolist()[0][
38 len(ids) :
39 ]
40 )
41)