Prem-1B-SQL is the one of the very first series of fully local Text-to-SQL models developed by Prem AI. Being a 1B parameter model
it easily fits on low GPU devices (and CPU devices when quantized). We believe that AI assisted data analysis should be a Local first
approach. Because exposing Databases to third party closed source models can lead to data security breaches. We will be publishing some
of the public benchmarks results of this model very soon. We will also be iterating on this model for more better results.
Since it is a model built upon transformers, so it can be directly used with transformers. However running Text-to-SQL is not as simple
as running normal LLMs. The reason lies in model input prompt formations which is tightly coupled with databases. So we have developed PremSQL,
a fully open source library which is:
Local-First: Avoid third-party closed-source providers and keep your data secure.
Customizable Datasets: Create, fine-tune, and evaluate models with built-in or custom datasets.
Robust Executors and Evaluators: Easily connect to databases and assess model performance.
Advanced Generators: Convert natural language prompts into executable SQL queries.
Error Handling and Self-Correction: Automatically correct SQL queries during inference.
Fine-Tuning Support: Fine-tune models with LoRA, QLoRA, or full fine-tuning strategies.
End-to-End Pipelines: Seamlessly integrate all components for autonomous data analysis.
To install PremSQL just create a new environment and type:
The easiest way to use this model is through PremSQL pipelines. All you need to do is provide the database path (in case of SQLite databases)
or provide the DB connection URI. After this, all you need to do is, connect it with the model. Here is how you do that:
python
1from premsql.pipelines import SimpleText2SQLAgent
2from premsql.generators import Text2SQLGeneratorHF
3from premsql.executors import SQLiteExecutor
45# Provide a SQLite file here or see documentation for more customization6dsn_or_db_path ="./data/db/california_schools.sqlite"78agent = SimpleText2SQLAgent(9 dsn_or_db_path=dsn_or_db_path,10 generator=Text2SQLGeneratorHF(11 model_or_name_or_path="premai-io/prem-1B-SQL",12 experiment_name="simple_pipeline",13 device="cuda:0",14type="test"15),16)1718question ="please list the phone numbers of the direct charter-funded schools that are opened after 2000/1/1"1920response = agent.query(question)21response["table"]
Under the hood, it automatically connects with your Database and do all the heavy lifting like prompt creation, execution etc for you.
Running Prem-1B-SQL using PremSQL Generators
You can also run the model using PremSQL Generators. This is helpful when you want to do generations in
bulk on some dataset. Here is an example:
python
1from premsql.generators import Text2SQLGeneratorHF
2from premsql.datasets import Text2SQLDataset
34# Define a dataset5dataset = bird_dataset = Text2SQLDataset(6 dataset_name='bird', split="validation", force_download=False,7 dataset_folder="/path/to/dataset"8).setup_dataset(num_rows=10, num_fewshot=3)910# Define a generator 11generator = Text2SQLGeneratorHF(12 model_or_name_or_path="premai-io/prem-1B-SQL",13 experiment_name="test_generators",14 device="cuda:0",15type="test"16)1718# Generate on the full dataset19responses = generator.generate_and_save_results(20 dataset=bird_dataset,21 temperature=0.1,22 max_new_tokens=25623)2425print(responses)
Using Execution guided Decoding
This strategy executes the generated SQL against the DB and, if it fails, uses the error message for correction, repeating until it gets a valid result or the retries run out.
image/png
python
1from premsql.executors import SQLiteExecutor
23executor = SQLiteExecutor()4response = generator.generate_and_save_results(5 dataset=bird_dataset,6 temperature=0.1,7 max_new_tokens=256,8 force=True,9 executor=executor,10 max_retries=5# this is optional (default is already set to 5)11)
You can also fine-tune Prem-1B-SQL using HuggingFace Transformers and with PremSQL Tuners as well.
Please check out our documentation to know about more about PremSQL and all the features
we provide.
Datasets used to train the model
Prem-1B-SQL is trained using the following datasets: