๐Ÿ“Š Natural Language to SQL

SLM Text-to-SQL

Translates natural language questions into valid SQL queries against custom database schemas offline.

๐Ÿš€ Overview & Capabilities

Translates natural language questions into valid SQL queries against custom database schemas offline.

Key Features

  • Offline schema parsing and SQL translation
  • Dialect support: PostgreSQL, SQLite, MySQL
  • Guarantees valid syntax without leaking data to external servers

๐Ÿ’ป Installation

Install the local package using pip:

Terminal
$pip install slm-text-to-sql

๐Ÿ™ Checkout from GitHub

Clone only this agent's folder from the monorepo using Git sparse-checkout โ€” no need to download the full repository:

Option 1 โ€” Sparse Checkout (Recommended)

Terminal โ€” Git Sparse Checkout
# 1. Create and enter a new directory
$ mkdir slm_text_to_sql && cd slm_text_to_sql

# 2. Initialise empty git repo and add remote
$ git init
$ git remote add origin https://github.com/t00114218-stack/SLMAgents.git

# 3. Enable sparse-checkout and set target folder
$ git sparse-checkout init --cone
$ git sparse-checkout set slm_text_to_sql

# 4. Pull only that agent's source
$ git pull origin main

Option 2 โ€” Full Repository Clone

Terminal โ€” Full Clone
$ git clone https://github.com/t00114218-stack/SLMAgents.git
$ cd SLMAgents/slm_text_to_sql

๐Ÿ’ก Tip: After checkout, install the package locally with pip install -e ./slm_text_to_sql to run in editable mode without publishing to PyPI.

โš™๏ธ Configuration API

Constructor Parameters

Instantiate SLMTextToSQL with performance and runtime options:

ParameterType / DefaultDescription
model_pathstr | NonePath to local ONNX model. Default: None.
temperaturefloat | 0.0Sampling temperature. Default: 0.0.
top_pfloat | 0.9Nucleus sampling threshold. Default: 0.9.
max_tokensint | 256Max token output limit. Default: 256.
n_threadsint | 4CPU thread count. Default: 4.

Methods

Method SignatureReturn TypeDescription
generate_sql(schema, query, ...)strGenerates valid SQL query string from natural language prompt.

Execution Parameters

Complete list of execution parameters accepted by the primary agent method:

ParameterType / DefaultDescription
schemastrDatabase DDL creation schema string.
querystrNatural language user question.
system_promptstr | NoneSQL dialect prompt override. Default: None.
user_inputstr | NoneAdditional SQL constraints. Default: None.
temperaturefloat | 0.0Sampling temperature. Default: 0.0.
top_pfloat | 0.9Nucleus sampling probability. Default: 0.9.
max_tokensint | 256Maximum token limit. Default: 256.

Quick Start

Python Example
from slm_text_to_sql import SLMTextToSQL

sql_agent = SLMTextToSQL(temperature=0.0, top_p=0.9, max_tokens=256)
query = sql_agent.generate_sql(
    schema="CREATE TABLE sales (id INT, amount DECIMAL, region TEXT);",
    query="What is total sales in West?",
    system_prompt="Dialect: PostgreSQL",
    user_input="Add WHERE amount > 0 condition",
    temperature=0.0,
    top_p=0.9,
    max_tokens=256
)
print(query)

๐Ÿ” Verified Output Logs

Diagnostic execution console output running locally on CPU:

Output Console
โ†’ INPUT:
Query: What is total sales in West?

โ† OUTPUT:
"SELECT SUM(amount) FROM sales WHERE region = 'West' AND amount > 0;"