The result is a powerful and responsive coding assistant, designed to follow instructions and generate accurate, high-quality Python code.
This model is designed to be used with the Unsloth library for maximum performance, but it can also be used with the standard Hugging Face transformers library. For the best results, always use the Llama 3 chat template.
1from unsloth import FastLanguageModel
2import torch
3
4model, tokenizer = FastLanguageModel.from_pretrained(
5 model_name = "YOUR_USERNAME/YOUR_MODEL_NAME", # REMEMBER TO REPLACE THIS
6 max_seq_length = 4096,
7 dtype = None,
8 load_in_4bit = True,
9)
10
11# Prepare the model for faster inference
12FastLanguageModel.for_inference(model)
13
14messages = [
15 {
16 "role": "system",
17 "content": "You are a helpful Python coding assistant. Please provide a clear, concise, and correct Python code response to the user's request."
18 },
19 {
20 "role": "user",
21 "content": "Create a Python function that finds the nth Fibonacci number using recursion."
22 },
23]
24
25input_ids = tokenizer.apply_chat_template(
26 messages,
27 add_generation_prompt=True,
28 return_tensors="pt"
29).to(model.device)
30
31outputs = model.generate(
32 input_ids,
33 max_new_tokens=200,
34 do_sample=True,
35 temperature=0.6,
36 top_p=0.9,
37 eos_token_id=tokenizer.eos_token_id
38)
39
40response = outputs[0][input_ids.shape[-1]:]
41print(tokenizer.decode(response, skip_special_tokens=True))