How to Use the Prem-1B-SQL Library for Text-to-SQL

Oct 28, 2024 | Educational

The Prem-1B-SQL is a cutting-edge open-source library developed by Prem AI, designed to facilitate the conversion of natural language queries into SQL. With this library, users can perform tasks without exposing sensitive database data to third-party services. In this blog post, we will guide you through the setup and usage of the Prem-1B-SQL library while keeping it straightforward.

What is Prem-1B-SQL?

Prem-1B-SQL is a fully local Text-to-SQL model featuring 1 billion parameters, making it efficient enough to run on low GPU or even CPU devices when quantized. Its focus on local-first processing ensures that your data remains secure from potential breaches associated with external providers.

How to Install the PremSQL Library

To get started with PremSQL, follow these simple steps:

  • Create a new environment.
  • Run the following command in your terminal:
  • pip install -U premsql

Running Prem-1B-SQL Using PremSQL Pipelines

Once you’ve installed PremSQL, the easiest way to utilize the model is through the provided pipelines. Here’s how you can do it:

from premsql.pipelines import SimpleText2SQLAgent
from premsql.generators import Text2SQLGeneratorHF
from premsql.executors import SQLiteExecutor

# Provide a SQLite file or URI here
dsn_or_db_path = "your_database_path_here.sqlite"
agent = SimpleText2SQLAgent(
    dsn_or_db_path=dsn_or_db_path,
    generator=Text2SQLGeneratorHF(
        model_or_name_or_path="premai-ioprem-1B-SQL",
        experiment_name="simple_pipeline",
        device="cuda:0",
        type="test"
    ),
)

question = "please list the phone numbers of the direct charter-funded schools that opened after 2000"
response = agent.query(question)
print(response["table"])

Understanding the Code

Let’s explain the previous code through an analogy: imagine you are the conductor of an orchestra, and each musician represents different components of the PremSQL library. The conductor (you) coordinates the musicians (code components) to create a harmonious output (an SQL query execution). You specify a piece of music (your SQLite database path) and instruct the musicians (agents) on how to play the notes (natural language question). With the conductor directing them, they will create a beautiful melody by transforming your instructions into executable SQL.

Using PremSQL Generators for Bulk Operations

For batch operations, you can use the following method:

from premsql.generators import Text2SQLGeneratorHF
from premsql.datasets import Text2SQLDataset

# Define a dataset
bird_dataset = Text2SQLDataset(
    dataset_name="bird",
    split="validation",
    force_download=False,
    dataset_folder="path_to_dataset"
).setup_dataset(num_rows=10, num_fewshot=3)

# Define a generator
generator = Text2SQLGeneratorHF(
    model_or_name_or_path="premai-ioprem-1B-SQL",
    experiment_name="test_generators",
    device="cuda:0",
    type="test"
)

# Generate on the full dataset
responses = generator.generate_and_save_results(
    dataset=bird_dataset,
    temperature=0.1,
    max_new_tokens=256
)

print(responses)

Troubleshooting Tips

If you encounter issues while using the PremSQL library, consider the following troubleshooting steps:

  • Ensure your database URI is correct and accessible.
  • Check if the necessary packages are installed and up-to-date.
  • Look at error messages carefully, as they often guide you toward the source of the problem.
  • If further help is needed, refer to the documentation for more detailed instructions.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Conclusion

With the Prem-1B-SQL library, transforming natural language into SQL becomes a seamless experience, promoting a local-first approach to data security. As you explore the functionalities of this powerful tool, remember to keep your system and library updated to leverage all the features effectively.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox