Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
library_name: transformers
|
| 6 |
+
pipeline_tag: text2text-generation
|
| 7 |
+
tags:
|
| 8 |
+
- sql
|
| 9 |
+
- t2sql
|
| 10 |
+
- text2sql
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
The **Hrida-T2SQL-3B-V0.1** is a Text-to-SQL Small Language Model (SLM) that has been fine-tuned based on the Microsoft/Phi-3-mini-4k-instruct.
|
| 14 |
+
|
| 15 |
+
For full details of this model please read our [blog post](https://www.hridaai.com/blog/t2sql).
|
| 16 |
+
|
| 17 |
+
## Prompt Template
|
| 18 |
+
|
| 19 |
+
```txt
|
| 20 |
+
### Instruction:
|
| 21 |
+
Provide the system prompt.
|
| 22 |
+
|
| 23 |
+
### Dialect:
|
| 24 |
+
Specify the SQL dialect (e.g., MySQL, PostgreSQL, SQL Server, etc.).
|
| 25 |
+
|
| 26 |
+
### Context:
|
| 27 |
+
Provide the database schema including table names, column names, and data types.
|
| 28 |
+
|
| 29 |
+
### Input:
|
| 30 |
+
User's query.
|
| 31 |
+
|
| 32 |
+
### Response:
|
| 33 |
+
Expected SQL query output based on the input and context.
|
| 34 |
+
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
- **Instruction (System Prompt)**: This guides the model on processing input to generate the SQL query response effectively.
|
| 38 |
+
- **Dialect (Optional)**: Specify the SQL variant the model should use to ensure the generated query conforms to the correct syntax.
|
| 39 |
+
- **Context**: Provide the database schema to the model for generating accurate SQL queries.
|
| 40 |
+
- **Input**: Provide the user query for the model to comprehend and transform into an SQL query.
|
| 41 |
+
- **Response**: Expected output from the model.
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
## Chat Prompt Template
|
| 45 |
+
|
| 46 |
+
```txt
|
| 47 |
+
<s>
|
| 48 |
+
<|system|>
|
| 49 |
+
{ Instruction / System Prompt }
|
| 50 |
+
<|user|>
|
| 51 |
+
{ Context / User Query } <|end|>
|
| 52 |
+
<|assistant|>
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
## Run the Model
|
| 56 |
+
|
| 57 |
+
### Using Transformers
|
| 58 |
+
|
| 59 |
+
```python
|
| 60 |
+
import torch
|
| 61 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 62 |
+
|
| 63 |
+
# Define the model and tokenizer
|
| 64 |
+
model_id = "HridaAI/Hrida-T2SQL-3B-V0.1"
|
| 65 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 66 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, trust_remote_code=True)
|
| 67 |
+
|
| 68 |
+
# Define the context and prompt
|
| 69 |
+
prompt = """
|
| 70 |
+
Answer to the query will be in the form of an SQL query.
|
| 71 |
+
### Context: CREATE TABLE Employees (
|
| 72 |
+
EmployeeID INT PRIMARY KEY,
|
| 73 |
+
FirstName VARCHAR(50),
|
| 74 |
+
LastName VARCHAR(50),
|
| 75 |
+
Age INT,
|
| 76 |
+
DepartmentID INT,
|
| 77 |
+
Salary DECIMAL(10, 2),
|
| 78 |
+
DateHired DATE,
|
| 79 |
+
Active BOOLEAN,
|
| 80 |
+
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
|
| 81 |
+
);
|
| 82 |
+
|
| 83 |
+
CREATE TABLE Departments (
|
| 84 |
+
DepartmentID INT PRIMARY KEY,
|
| 85 |
+
DepartmentName VARCHAR(100),
|
| 86 |
+
Location VARCHAR(100)
|
| 87 |
+
);
|
| 88 |
+
### Input: Write a SQL query to select all the employees who are active.
|
| 89 |
+
### Response:
|
| 90 |
+
"""
|
| 91 |
+
# Prepare the input
|
| 92 |
+
messages = [{"role": "user", "content": prompt}]
|
| 93 |
+
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True)
|
| 94 |
+
|
| 95 |
+
# Generate the output
|
| 96 |
+
outputs = model.generate(inputs, max_length=300)
|
| 97 |
+
print(tokenizer.decode(outputs[0]))
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
### Using MLX
|
| 103 |
+
|
| 104 |
+
```python
|
| 105 |
+
from mlx_lm import generate, load
|
| 106 |
+
|
| 107 |
+
model,tokenizer = load("HridaAI/Hrida-T2SQL-3B-V0.1")
|
| 108 |
+
|
| 109 |
+
prompt = """
|
| 110 |
+
Answer to the quey will be in the form of SQL query.
|
| 111 |
+
### Context: CREATE TABLE Employees (
|
| 112 |
+
EmployeeID INT PRIMARY KEY,
|
| 113 |
+
FirstName VARCHAR(50),
|
| 114 |
+
LastName VARCHAR(50),
|
| 115 |
+
Age INT,
|
| 116 |
+
DepartmentID INT,
|
| 117 |
+
Salary DECIMAL(10, 2),
|
| 118 |
+
DateHired DATE,
|
| 119 |
+
Active BOOLEAN,
|
| 120 |
+
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
|
| 121 |
+
);
|
| 122 |
+
|
| 123 |
+
CREATE TABLE Departments (
|
| 124 |
+
DepartmentID INT PRIMARY KEY,
|
| 125 |
+
DepartmentName VARCHAR(100),
|
| 126 |
+
Location VARCHAR(100)
|
| 127 |
+
); ### Input: Write a SQL query to select all the employees who are active. ### Response:"""
|
| 128 |
+
|
| 129 |
+
response = generate(model=model,tokenizer=tokenizer,prompt=prompt, verbose=True)
|
| 130 |
+
|
| 131 |
+
```
|