Generate and execute SQL queries against MySQL/PostgreSQL using an LLM
Project description
private-db-llm
Generate and execute SQL queries against MySQL or PostgreSQL using a Large Language Model (LLM)
Features
- Schema Extraction
Automatically discovers tables, columns, and foreign-key relations. - LLM-powered Query Generation
Sends your prompt plus the database schema to an LLM and returns a syntactically valid SQL query. - Multi-Provider Support
Works with OpenAI (v1+), OpenRouter, and Ollama HTTP APIs. - Multi-Database Support
Out of the box, supports MySQL and PostgreSQL (viamysql-connector-python&psycopg2-binary). - SOLID Architecture
Clean separation of concerns, easy to extend to other databases or LLM providers.
Installation
pip install private-db-llm
Optional extras:
- If you need PostgreSQL support, also install
pip install psycopg2-binary
- To load credentials from a .env file, you can add
pip install python-dotenv
Quick Start
1. Configure Your LLM
from private_db_llm.config import LLMConfig
llm_conf = LLMConfig(
provider="openai", # "openai", "openrouter", or "ollama"
api_key="sk-…",
base_url=None, # for OpenRouter or Ollama HTTP endpoint
model="gpt-4o-mini"
)
2. Create the Service
from private_db_llm.service import create_service
service = create_service(llm_conf)
3. Connect to Your Database
MySQL
import mysql.connector
mysql_conn = mysql.connector.connect(
host="localhost",
port=3306,
user="root",
password="your_mysql_password",
database="your_mysql_db"
)
PostgreSQL
import psycopg2
postgres_conn = psycopg2.connect(
host="localhost",
port=5432,
user="postgres_user",
password="your_pg_password",
dbname="your_pg_db"
)
4. Run a Prompt & Get JSON Results
# For MySQL
result_mysql = service.run(
mysql_conn,
"List all users who signed up in the last 7 days"
)
print(result_mysql)
# For PostgreSQL
result_pg = service.run(
postgres_conn,
"Show total sales per region for Q1"
)
print(result_pg)
Advanced
-
Customizing the instruction template:
- Pass your own
instruction_templatetoDBLLMServiceif you want to tweak how the LLM is prompted.
- Pass your own
-
Adding a new database:
- Create
<YourDB>SchemaExtractor&<YourDB>QueryExecutorclasses. - Hook them into
service.run()based on the connection type.
- Supporting another LLM provider:
- Subclass the
LLMClientABC. - Add your implementation in
llm_client.py. - Register it in
create_service().
- Create
Version Compatibility
- Python:
>=3.12 - OpenAI Python SDK:
>=1.0.0 - MySQL driver:
mysql-connector-python>=8.0.0 - HTTP client:
httpx>=0.23.0 - PostgreSQL driver:
psycopg2-binary>=2.9.0
Contributing
- Fork the repo
- Create a feature branch
- Write tests & update docs
- Open a PR — all contributions welcome!
License
MIT © Matin Khosravi
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
private_db_llm-0.1.1.tar.gz
(8.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file private_db_llm-0.1.1.tar.gz.
File metadata
- Download URL: private_db_llm-0.1.1.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbe495e9a1c513fde64ac797c9d271d4b89ffcaadf80e42767d555b35a4d8cce
|
|
| MD5 |
0198d691b01f0b3721f452a701f3f665
|
|
| BLAKE2b-256 |
55baa281424ff190350b2ea3ff55003dc28328a35d0469d6cc26e9d29c56567b
|
File details
Details for the file private_db_llm-0.1.1-py3-none-any.whl.
File metadata
- Download URL: private_db_llm-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e8a1b7da48843ee1ffcdba761224d24b296f8620307e6d552e10e8c04bee27f
|
|
| MD5 |
80aae3aabcfc29d45cb5e00dc79a48e3
|
|
| BLAKE2b-256 |
df4d3ebcf32e6f3a14f5b1fa30d238a6b7310c31c10f580dd336682e5f2f813d
|