A Python package for generating SQL queries using LLMs
Project description
sql-dhost
sql-dhost is a Python utility that integrates SQL database querying with powerful LLMs (like OpenAI, Claude, and Perplexity) to enable natural language to SQL query conversion. It's particularly useful for users who want to interact with databases using plain English.
🚀 Features
- 🔌 Easy connection to PostgreSQL databases
- 🤖 Convert natural language questions into SQL using OpenAI, Claude, or Perplexity
- 🧠 Schema-aware LLM prompts
- 🔍 Execute queries and retrieve results programmatically
- ⚙️ Modular design for easy extension
📦 Installation
Clone the repository and install dependencies:
git clone https://github.com/sujanMidatani7/sql-dhost.git
cd sql-dhost
pip install uv
uv init
uv venv
uv add -r requirements.txt
🛠️ Usage
1. Basic Setup
from dhost import PSQLUtil
# PostgreSQL connection string
conn_str = "db_connection_string"
# Initialize PSQLUtil with API keys
psql = PSQLUtil(
db_connection=conn_str,
openai_api_key="your-openai-key",
claude_api_key="your-claude-key",
perplexity_api_key="your-perplexity-key"
)
2. Set Schema
Before generating queries, provide your database schema:
schema = """
Table: users
Columns: id, name, email
Table: orders
Columns: id, user_id, amount, date
"""
psql.set_schema(schema)
3. Execute Raw SQL Queries
query = "SELECT * FROM users WHERE id = %s"
results = psql.execute_query(query, params=(1,))
print(results)
4. Generate SQL from Natural Language
response = psql.generate_sql_query(
question="List all users who placed an order above $1000",
provider="openai",
llm="gpt-4o",
max_tokens=500
)
print(response['sql'])
5. Generate SQL and Execute It
for output in psql.generate_sql_query_and_execute(
question="Get all users who made orders in January",
provider="claude",
llm="claude-3-opus"
):
print(output)
🧪 Testing
To run test scripts:
python test.py
Ensure your .env or connection config is properly set up before testing.
🔧 API Reference
PSQLUtil.__init__(...)
Initializes the utility with DB and optional LLM API keys.
set_schema(schema: str)
Sets the database schema and updates system prompt for the LLM.
execute_query(query: str, params: tuple = None)
Executes a SQL query and returns results.
update_system_prompt(new_prompt: str)
Manually update the LLM system prompt.
get_system_prompt() -> str
Retrieves the current system prompt.
generate_sql_query(question: str, provider="openai", llm=None, max_tokens=1000)
Returns SQL and explanation generated by selected LLM.
generate_sql_query_and_execute(...)
Generates SQL using LLM and streams execution results.
🤝 Contributing
Pull requests are welcome. For major changes, open an issue first to discuss.
📄 License
MIT License. See LICENSE for details.
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
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 sql_dhost-0.1.1.tar.gz.
File metadata
- Download URL: sql_dhost-0.1.1.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.26
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75785fddacf532b65226f029c0413342fa3e38919c446f13f00c710d3e65bcc7
|
|
| MD5 |
7407d87d7b38445f7da28ce401d26b5b
|
|
| BLAKE2b-256 |
2dc80a610d7a524bfa95342521bf27525573f0d69bb17f6f76d33d6874a550d0
|
File details
Details for the file sql_dhost-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sql_dhost-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.26
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06aecc17ca13881c1f50ef240d5cb147afbe3b08d1c89f67aac7305433ae740e
|
|
| MD5 |
9d1c66e6fb9edc35225661819cb2f739
|
|
| BLAKE2b-256 |
13e8a2770aa97c7133e8a95fb09dc1260124282651ff3c46df7839f270f5ffa5
|