File size: 2,325 Bytes
c3e6814
b7772e7
 
 
c3e6814
 
b7772e7
c3e6814
 
0f9b0d6
c3e6814
 
 
b7772e7
c3e6814
f610c4d
c3e6814
dcb57f5
b7772e7
 
c3e6814
b7772e7
 
c3e6814
 
b7772e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
---
license: cc-by-4.0
tags:
- Text-to-sql
library_name: transformers
---
### Llama3-OGSQL-8B


![image/png](https://cdn-uploads.huggingface.co/production/uploads/657ad4e2583493c1d1efb05b/YzOlyYJEeD4HWAIhcdHGB.png)


### Model Description
Llama3-OGSQL-8B  was fine-tuned on the most recent and state of the art models (LLAMA 3) for the task of converting natural language text into SQL queries.

The model has been trained on more than 270 million tokens, ensuring robust performance and high accuracy in SQL generation tasks.

- **Model type**: Auto-regressive language model
- **Language(s) (NLP)**: SQL (target language for generation)
- **Finetuned from model**: Llama3-8B

## Use Case
OGSQL-7B is designed to facilitate the conversion of natural language queries into structured SQL commands, aiding in database querying without the need for manual SQL knowledge.

## How to Get Started with the Model
```python
# Example code to load and use the model
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

model_name = "Llama3-OGSQL-8B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

def generate_sql(query):
    inputs = tokenizer.encode(query, return_tensors="pt")
    outputs = model.generate(inputs)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Example use
query = """
using this context:
-- Create Customers Table
CREATE TABLE Customers (
    customer_id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT,
    join_date DATE
);

-- Create Products Table
CREATE TABLE Products (
    product_id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    price DECIMAL(10, 2)
);

-- Create Orders Table
CREATE TABLE Orders (
    order_id INTEGER PRIMARY KEY,
    customer_id INTEGER,
    product_id INTEGER,
    order_date DATE,
    quantity INTEGER,
    total_price DECIMAL(10, 2),
    FOREIGN KEY (customer_id) REFERENCES Customers(customer_id),
    FOREIGN KEY (product_id) REFERENCES Products(product_id)
);

show me all the orders from last month , sort by date


"""
print(generate_sql(query))

```


## alternatively  you can use this notebook:  
[![Colab notebook](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1pQuIuCdoFMG76AH3BNZzep8PgRaZkkYS?usp=sharing)