Views
No views yet
sqlchat Modelnnul/sqlchat model. These adapters represent the fine-tuned "knowledge layer" that specializes the base model for Text-to-SQL tasks.sqlchat model, or use them as a starting point for further fine-tuning. This approach is highly efficient for experimentation and allows for easy conversion to various quantized formats (like GGUF) with minimal quality loss.Qwen/Qwen3-1.7Br): 32lora_alpha): 32nnul/sql-chat-dataset (a combination of b-mc2/sql-create-context and gretelai/synthetic_text_to_sql).1pip install unsloth
2pip install "torch>=2.3.1"1import torch
2from unsloth import FastLanguageModel
3from transformers import TextStreamer
4
5# When loading LoRA adapters, you must specify the base model they were trained on.
6# Unsloth will first load the 4-bit base model, then fuse these adapters into it.
7print("Loading base model and applying sqlchat-lora adapters...")
8model, tokenizer = FastLanguageModel.from_pretrained(
9 model_name="nnul/sqlchat-lora", # YOUR LoRA adapter repository
10 max_seq_length=4096,
11 dtype=None,
12 load_in_4bit=True,
13)
14print("Model and adapters loaded successfully.")
15
16# Optimize the model for the fastest possible inference.
17FastLanguageModel.for_inference(model)
18
19def generate_sql(instruction: str, context: str = ""):
20 """
21 A helper function to generate SQL from a natural language prompt.
22 """
23 prompt = tokenizer.apply_chat_template(
24 [
25 {"role": "system", "content": "You are a helpful assistant that generates SQL queries based on natural language questions and database schemas."},
26 {"role": "user", "content": f"### Instruction:\n{instruction}\n\n### Context:\n{context}"},
27 ],
28 tokenize=False,
29 add_generation_prompt=True,
30 enable_thinking=False, # Ensures direct SQL output
31 )
32
33 inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
34 text_streamer = TextStreamer(tokenizer, skip_prompt=True, clean_up_tokenization_spaces=True)
35
36 print(f"User Instruction: {instruction}")
37 print("\nModel Output:")
38 print("---------------------------------")
39 _ = model.generate(
40 **inputs,
41 streamer=text_streamer,
42 max_new_tokens=256,
43 do_sample=False, # Use greedy decoding for deterministic output
44 use_cache=True,
45 )
46 print("---------------------------------\n")
47
48# --- Example Usage ---
49generate_sql(
50 instruction="Which department has the most number of employees?",
51 context="CREATE TABLE department (name VARCHAR, num_employees INTEGER)"
52)nnul/sqlchat), you can do so easily.1# Load the model and adapters as shown above
2model, tokenizer = FastLanguageModel.from_pretrained(model_name="nnul/sqlchat-lora", ...)
3
4# Merge and save locally
5model.save_pretrained_merged("sqlchat_merged_4bit", tokenizer, save_method="merged_4bit_forced")
6
7# Or, push the merged model directly to a new Hub repository
8# model.push_to_hub_merged("your-username/your-new-merged-repo", tokenizer, save_method="merged_4bit_forced")