cfahlgren1 HF staff commited on
Commit
4844cec
1 Parent(s): ee8bda4

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +158 -0
README.md ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: deepseek-ai/deepseek-coder-6.7b-instruct
3
+ tags:
4
+ - instruct
5
+ - finetune
6
+ library_name: transformers
7
+ license: cc-by-sa-4.0
8
+ pipeline_tag: text-generation
9
+ ---
10
+
11
+ # **Natural-SQL-7B by ChatDB**
12
+ ## Natural-SQL-7B is a model with very strong performance in Text-to-SQL instructions, has an excellent understanding of complex questions, and outperforms models of the same size in its space.
13
+
14
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/648a374f00f7a3374ee64b99/hafdsfrFCqrVbATIzV_EN.png" width="600">
15
+
16
+ [ChatDB.ai](https://chatdb.ai) | [Notebook](https://github.com/cfahlgren1/natural-sql/blob/main/natural-sql-7b.ipynb) | [Twitter](https://twitter.com/calebfahlgren)
17
+
18
+ # **Benchmarks**
19
+ ### *Results on Novel Datasets not trained on via SQL-Eval*
20
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/648a374f00f7a3374ee64b99/5ynfoKPzI3_-WasQQt7qR.png" width="800">
21
+
22
+ <em>Big thanks to the [defog](https://huggingface.co/defog) team for open sourcing [sql-eval](https://github.com/defog-ai/sql-eval)</em>👏
23
+
24
+ Natural-SQL also can handle complex, compound questions that other models typically struggle with. There is a more detailed writeup Here is a write up, small test done [here](https://chatdb.ai/post/naturalsql-vs-sqlcoder-for-text-to-sql).
25
+ # Usage
26
+
27
+ Make sure you have the correct version of the transformers library installed:
28
+
29
+ ```sh
30
+ pip install transformers==4.35.2
31
+ ```
32
+
33
+ ### Loading the Model
34
+
35
+ Use the following Python code to load the model:
36
+
37
+ ```python
38
+ import torch
39
+ from transformers import AutoModelForCausalLM, AutoTokenizer
40
+ tokenizer = AutoTokenizer.from_pretrained("chatdb/natural-sql-7b")
41
+ model = AutoModelForCausalLM.from_pretrained(
42
+ "chatdb/natural-sql-7b",
43
+ device_map="auto",
44
+ torch_dtype=torch.float16,
45
+ )
46
+ ```
47
+
48
+ ### **License**
49
+
50
+ The model weights are licensed under `CC BY-SA 4.0`, with extra guidelines for responsible use.
51
+ Essentially, you're free to use and adapt the model, even commercially.
52
+ If you alter the weights, such as through fine-tuning, you must publicly share your changes under the same `CC BY-SA 4.0` license.
53
+
54
+
55
+ ### Generating SQL
56
+
57
+ ```python
58
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
59
+ generated_ids = model.generate(
60
+ **inputs,
61
+ num_return_sequences=1,
62
+ eos_token_id=100001,
63
+ pad_token_id=100001,
64
+ max_new_tokens=400,
65
+ do_sample=False,
66
+ num_beams=1,
67
+ )
68
+
69
+ outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
70
+ print(outputs[0].split("```sql")[-1])
71
+ ```
72
+ # Prompt Template
73
+
74
+ ```
75
+ # Task
76
+ Generate a SQL query to answer the following question: `{natural language question}`
77
+
78
+ ### PostgreSQL Database Schema
79
+ The query will run on a database with the following schema:
80
+
81
+ <SQL Table DDL Statements>
82
+
83
+ # SQL
84
+ Here is the SQL query that answers the question: `{natural language question}`
85
+ '''sql
86
+ ```
87
+
88
+
89
+ # Example SQL Output
90
+
91
+ ### Example Schemas
92
+
93
+ ```sql
94
+ CREATE TABLE users (
95
+ user_id SERIAL PRIMARY KEY,
96
+ username VARCHAR(50) NOT NULL,
97
+ email VARCHAR(100) NOT NULL,
98
+ password_hash TEXT NOT NULL,
99
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
100
+ );
101
+ CREATE TABLE projects (
102
+ project_id SERIAL PRIMARY KEY,
103
+ project_name VARCHAR(100) NOT NULL,
104
+ description TEXT,
105
+ start_date DATE,
106
+ end_date DATE,
107
+ owner_id INTEGER REFERENCES users(user_id)
108
+ );
109
+ CREATE TABLE tasks (
110
+ task_id SERIAL PRIMARY KEY,
111
+ task_name VARCHAR(100) NOT NULL,
112
+ description TEXT,
113
+ due_date DATE,
114
+ status VARCHAR(50),
115
+ project_id INTEGER REFERENCES projects(project_id)
116
+ );
117
+ CREATE TABLE taskassignments (
118
+ assignment_id SERIAL PRIMARY KEY,
119
+ task_id INTEGER REFERENCES tasks(task_id),
120
+ user_id INTEGER REFERENCES users(user_id),
121
+ assigned_date DATE NOT NULL DEFAULT CURRENT_TIMESTAMP
122
+ );
123
+ CREATE TABLE comments (
124
+ comment_id SERIAL PRIMARY KEY,
125
+ content TEXT NOT NULL,
126
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
127
+ task_id INTEGER REFERENCES tasks(task_id),
128
+ user_id INTEGER REFERENCES users(user_id)
129
+ );
130
+ ```
131
+ ### Example SQL Outputs
132
+
133
+ **Question**: **Show me the day with the most users joining**
134
+ ```sql
135
+ SELECT created_at::DATE AS day, COUNT(*) AS user_count
136
+ FROM users
137
+ GROUP BY day
138
+ ORDER BY user_count DESC
139
+ LIMIT 1;
140
+ ```
141
+ **Question**: **Show me the project that has a task with the most comments**
142
+ ```sql
143
+ SELECT p.project_name, t.task_name, COUNT(c.comment_id) AS comment_count
144
+ FROM projects p
145
+ JOIN tasks t ON p.project_id = t.project_id
146
+ JOIN comments c ON t.task_id = c.task_id
147
+ GROUP BY p.project_name, t.task_name
148
+ ORDER BY comment_count DESC
149
+ LIMIT 1;
150
+ ```
151
+
152
+ **Question**: **What is the ratio of users with gmail addresses vs without?**
153
+ ```sql
154
+ SELECT
155
+ SUM(CASE WHEN email ILIKE '%@gmail.com%' THEN 1 ELSE 0 END)::FLOAT / NULLIF(SUM(CASE WHEN email NOT ILIKE '%@gmail.com%' THEN 1 ELSE 0 END), 0) AS gmail_ratio
156
+ FROM
157
+ users;
158
+ ```