Files
ModelHub XC 234cef7727 初始化项目,由ModelHub XC社区提供模型
Model: Arogyasami/Llama-2-7b-text2sql-finetune
Source: Original Platform
2026-07-14 04:13:10 +08:00

4.4 KiB

license
license
mit
# Llama-2 7B Text-to-SQL (Fine-Tuned with LoRA)

## 📌 Model Overview
This repository contains a fine-tuned version of Meta's **Llama-3 (8B)**, optimized specifically for the **Natural Language to SQL (Text-to-SQL)** generation task. By converting conversational English into executable SQL queries, this model is designed to bridge the gap between non-technical stakeholders and complex relational databases.

- **Developer:** DanielMartin Arogyasami
- **Base Model:** Meta-Llama-3-8B
- **Task:** Text-to-SQL (Code Generation)
- **Fine-Tuning Methodology:** Low-Rank Adaptation (LoRA) / PEFT
- **Language:** English, SQL
- **License:** Meta Llama 3 Community License / MIT (for fine-tuned weights)

## 🎯 Intended Use Cases
This model is highly specialized for deployment in regulated enterprise environments (e.g., healthcare, finance), where data sovereignty is paramount.
- **Enterprise Data Retrieval:** Empowering business users to query databases using natural language, significantly reducing reliance on specialized SQL programmers.
- **Agentic AI Workflows:** Serving as the SQL-generation agent within larger Retrieval-Augmented Generation (RAG) and enterprise AI architectures.
- **Privacy-Preserving Analytics:** Allowing companies to run text-to-SQL conversions entirely on-premises or within air-gapped Virtual Private Clouds (VPCs). This ensures compliance with HIPAA and FDA CFR Part 11, as no sensitive data is transmitted to external proprietary APIs.

## ⚙️ Technical Details & Training
This model was trained using **Parameter-Efficient Fine-Tuning (PEFT)**. Specifically, **LoRA (Low-Rank Adaptation)** was applied to the foundational Llama-3 model. This approach adapts the foundational reasoning capabilities of Llama-3 to the strict syntax requirements of SQL generation, while significantly reducing computational overhead.

* **Adapter:** LoRA
* **Target Modules:** Attention weights (`q_proj`, `v_proj`)
* **Precision:** FP16 / 4-bit Quantization (QLoRA) supported for edge-deployment.
* **Architecture:** Auto-Regressive Transformer.

## 🚀 How to Use (Inference)
You can load this model and run inference using the `transformers` and `peft` libraries from Hugging Face.

```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

# Load base model and tokenizer
base_model_id = "meta-llama/Meta-Llama-3-8B"
adapter_id = "Arogyasami/Llama-3-8b-text2sql-finetune"

tokenizer = AutoTokenizer.from_pretrained(base_model_id)
base_model = AutoModelForCausalLM.from_pretrained(base_model_id, torch_dtype=torch.float16, device_map="auto")

# Load LoRA adapter
model = PeftModel.from_pretrained(base_model, adapter_id)

# Define your schema and question
schema = "CREATE TABLE Employees (ID int, Name varchar(255), Department varchar(255), Salary int);"
question = "What is the average salary of employees in the Sales department?"

prompt = f"Given the following database schema:\n{schema}\n\nWrite a SQL query to answer this question: {question}\n\nSQL:"

inputs = tokenizer(prompt, return_tensors="pt").to("cuda")

# Generate SQL
outputs = model.generate(**inputs, max_new_tokens=100)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

⚠️ Limitations & Bias

  • Deterministic Requirement: Generative AI models can "hallucinate." While fine-tuned to minimize this, generated queries must be validated before execution, especially in clinical contexts where erroneous data retrieval could impact regulatory submissions.
  • Schema Complexity: While the model performs exceptionally well on standard schemas, highly complex schemas with dozens of nested joins may reduce accuracy.
  • Security: Always execute generated SQL in a safe, read-only environment. The model does not inherently enforce database permissions.

🌍 Environmental Impact

By utilizing LoRA instead of full-parameter fine-tuning, the computational cost and carbon footprint required to train this model were drastically reduced. Its 8B parameter size allows it to be served efficiently on consumer-grade hardware (e.g., NVIDIA RTX 4090 or A10G), democratizing advanced AI access.

🤝 Acknowledgements & Citation

  • Meta for the foundational Llama-3 architecture.
  • If this model assists in your research or enterprise architecture, please cite: Arogyasami, DanielMartin. (2024). Llama-3 8B Text-to-SQL (Fine-Tuned with LoRA). Hugging Face.