Views
No views yet
Meta-Llama-3.1-8B, developed by ruslanmv for text generation tasks. It leverages 4-bit quantization, making it more efficient for inference while maintaining strong performance in natural language generation.unsloth/meta-llama-3.1-8b-bnb-4bitpip install transformers accelerate bitsandbytestransformers library:1#!pip install bitsandbytes
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3import torch
4
5# Define the quantization config
6bnb_config = BitsAndBytesConfig(
7 load_in_4bit=True,
8 bnb_4bit_use_double_quant=True,
9 bnb_4bit_quant_type="nf4",
10 bnb_4bit_compute_dtype=torch.float16,
11)
12
13# Ensure you have the right device setup
14device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
16# Load the model and tokenizer from the Hugging Face Hub with BitsAndBytesConfig
17model_name = "ruslanmv/Meta-Llama-3.1-8B-Text-to-SQL-4bit"
18model = AutoModelForCausalLM.from_pretrained(
19 model_name,
20 device_map="auto",
21 quantization_config=bnb_config)
22tokenizer = AutoTokenizer.from_pretrained(model_name)
23
24# Define EOS token for terminating the sequences
25EOS_TOKEN = tokenizer.eos_token
26
27# Define Alpaca-style prompt template
28alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
29
30### Instruction:
31{}
32
33### Input:
34{}
35
36### Response:
37"""
38
39# Format the prompt without the response part
40prompt = alpaca_prompt.format(
41 "Provide the SQL query",
42 "Seleziona tutte le colonne della tabella table1 dove la colonna anni è uguale a 2020"
43)
44
45# Tokenize the prompt and generate text
46inputs = tokenizer([prompt], return_tensors="pt").to(device)
47outputs = model.generate(**inputs, max_new_tokens=64, use_cache=True)
48
49# Decode the generated text
50generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
51
52# Extract the generated response only (remove the prompt part)
53response_start = generated_text.find("### Response:") + len("### Response:\n")
54response = generated_text[response_start:].strip()
55
56# Print the response (excluding the prompt)
57print(response)
58SELECT * FROM table1 WHERE anni = 2020bitsandbytes library, it optimizes memory and inference performance.unsloth and the meta-llama-3.1-8b-bnb-4bit model.