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 transformers library:1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Load the model and tokenizer
5model_name = "ruslanmv/Meta-Llama-3.1-8B-Text-to-SQL"
6
7# Ensure you have the right device setup
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
10# Load the model and tokenizer from the Hugging Face Hub
11model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
12tokenizer = AutoTokenizer.from_pretrained(model_name)
13
14# Initialize the tokenizer (adjust the model name as needed)
15# Define EOS token for terminating the sequences
16EOS_TOKEN = tokenizer.eos_token
17
18# Define Alpaca-style prompt template
19alpaca_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.
20
21### Instruction:
22{}
23
24### Input:
25{}
26
27### Response:
28"""
29
30# Format the prompt without the response part
31prompt = alpaca_prompt.format(
32 "Provide the SQL query",
33 "Seleziona tutte le colonne della tabella table1 dove la colonna anni è uguale a 2020"
34)
35# Tokenize the prompt and generate text
36inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
37outputs = model.generate(**inputs, max_new_tokens=64, use_cache=True)
38
39# Decode the generated text
40generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
41
42# Extract the generated response only (remove the prompt part)
43response_start = generated_text.find("### Response:") + len("### Response:\n")
44response = generated_text[response_start:].strip()
45
46# Print the response (excluding the prompt)
47print(response)
48
49
50SELECT * FROM table1 WHERE anni = 2020unsloth and the meta-llama-3.1-8b-bnb-4bit model.