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
Metadata
Release files for private-db-llm 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| private_db_llm-0.1.1.tar.gz | 8.9 kB | Without provenance | Details for private_db_llm-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| private_db_llm-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for private_db_llm-0.1.1-py3-none-any.whl |
Total release size: 19.0 kB
Release files / private_db_llm-0.1.1.tar.gz
| Download URL | private_db_llm-0.1.1.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cbe495e9a1c513fde64ac797c9d271d4b89ffcaadf80e42767d555b35a4d8cce
|
|
BLAKE2b-256 checksum How to use checksums |
55baa281424ff190350b2ea3ff55003dc28328a35d0469d6cc26e9d29c56567b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / private_db_llm-0.1.1-py3-none-any.whl
| Download URL | private_db_llm-0.1.1-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e8a1b7da48843ee1ffcdba761224d24b296f8620307e6d552e10e8c04bee27f
|
|
BLAKE2b-256 checksum How to use checksums |
df4d3ebcf32e6f3a14f5b1fa30d238a6b7310c31c10f580dd336682e5f2f813d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Security for private-db-llm 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.