Natural Language Model Database - Query databases using natural language
Project description
NLMDB: Natural Language & MCP-powered Database
Query your databases using natural language through the Model Context Protocol (MCP) approach. NLMDB provides a simple API for interacting with databases using either OpenAI or Hugging Face models.
✨ Features
- 💬 Query databases using natural language
- 🔄 Support for both OpenAI and Hugging Face models
- 🔒 Enhanced privacy options with local Hugging Face models
- 📊 Automatic schema extraction
- 📝 Clean, professional responses
- 🧩 Simple, intuitive API
🚀 Installation
pip install nlmdb
🏁 Quick Start
Using OpenAI
from nlmdb import dbagent
# Initialize the agent with your API key and database path
response = dbagent(
api_key="your-openai-api-key",
db_path="path/to/your/database.db",
query="What tables are in the database and what columns do they have?"
)
print(response["output"])
Using Hugging Face
from nlmdb import dbagent_private
# Initialize the agent with your Hugging Face token and model name
response = dbagent_private(
hf_config=("your-huggingface-token", "model-repo-name"),
db_path="path/to/your/database.db",
query="What tables are in the database and what columns do they have?"
)
print(response["output"])
🔒 Privacy and Data Security
NLMDB offers enhanced privacy options through its support for Hugging Face models:
Enhanced Privacy with Hugging Face Models
When using dbagent_private with use_local=True, all processing happens locally on your machine, ensuring your database schema and query data never leave your environment:
response = dbagent_private(
hf_config=("your-huggingface-token", "model-repo-name"),
db_path="path/to/your/database.db",
query="What tables are in the database?",
use_local=True # Ensures all processing happens locally
)
Data Security Considerations
-
OpenAI Integration: When using
dbagentwith OpenAI models, database schema and queries are sent to OpenAI's API. While only schema information and not actual data is shared, consider privacy implications. -
Hugging Face Cloud API: Using
dbagent_privatewithoutuse_local=Truesends queries to Hugging Face's Inference API. -
Local Processing: For maximum privacy, use
dbagent_privatewithuse_local=Trueto keep all processing on your machine. -
No Data Storage: NLMDB does not store or log your database contents, queries, or responses.
🔄 Model Comparison
| Feature | OpenAI Models (dbagent) |
Hugging Face Models (dbagent_private) |
|---|---|---|
| SQL Generation Quality | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| Privacy | ⭐⭐ | ⭐⭐⭐⭐⭐ (with use_local=True) |
| Cost | 💰💰💰 | 💰 (self-hosted) / 💰💰 (HF API) |
| Offline Usage | ❌ | ✅ (with use_local=True) |
| Setup Complexity | Simple | Moderate |
| Resource Requirements | Minimal (Cloud-based) | High (for local models) |
| Speed | Fast | Varies (depends on hardware) |
| Customizability | Limited | Extensive |
🧩 Advanced Usage
Running with Verbose Output
You can enable verbose output to see the SQL queries being generated and executed:
response = dbagent(
api_key="your-openai-api-key",
db_path="path/to/your/database.db",
query="How many customers do we have?",
verbose=True
)
Using Local Hugging Face Models
For improved performance, privacy, or when working offline, you can run Hugging Face models locally:
response = dbagent_private(
hf_config=("your-huggingface-token", "model-repo-name"),
db_path="path/to/your/database.db",
query="What tables are in the database?",
use_local=True # This will download and run the model locally
)
Customizing Model Parameters
You can customize the behavior of the language model by passing additional parameters:
model_kwargs = {
"temperature": 0.2,
"max_new_tokens": 1024,
"repetition_penalty": 1.1
}
response = dbagent_private(
hf_config=("your-huggingface-token", "mistralai/Mixtral-8x7B-Instruct-v0.1"),
db_path="path/to/your/database.db",
query="Summarize the sales data for the last quarter",
model_kwargs=model_kwargs
)
🔍 Choosing the Right Model
Recommended Hugging Face Models
| Model | Performance | Resource Usage | Best For |
|---|---|---|---|
| mistralai/Mixtral-8x7B-Instruct-v0.1 | ⭐⭐⭐⭐⭐ | 🖥️🖥️🖥️🖥️ | Best overall SQL generation |
| meta-llama/Llama-2-7b-chat-hf | ⭐⭐⭐⭐ | 🖥️🖥️🖥️ | Balance of performance and resources |
| Qwen/Qwen2-7B-Instruct | ⭐⭐⭐ | 🖥️🖥️ | Efficient for simpler queries |
📊 Supported Databases
Currently, NLMDB supports:
- SQLite ✅
Future releases will add support for:
- PostgreSQL 🔜
- MySQL 🔜
- Microsoft SQL Server 🔜
⚙️ Requirements
- Python 3.8+
- openai>=1.0.0
- langchain>=0.1.0
- langchain-core>=0.1.0
- langchain-community>=0.0.0
- langchain-huggingface>=0.0.1 (for Hugging Face integration)
📜 License
MIT
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
🙏 Acknowledgements
This library is built on top of:
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 nlmdb-1.3.3.tar.gz.
File metadata
- Download URL: nlmdb-1.3.3.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c163b22c747f4edf1d5b0717d2c9d05bcb74f8b729257813003a236788274fc
|
|
| MD5 |
09ca57a85e226df6b985c5fc82e64974
|
|
| BLAKE2b-256 |
6ff1d55b327e5fd7fdc8b848524322b7478b1613b7c7404e3c7250f72a7fb367
|
File details
Details for the file nlmdb-1.3.3-py3-none-any.whl.
File metadata
- Download URL: nlmdb-1.3.3-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
625faae14219c087ad228055054c42984e1e2c6c98fb4c91a2e8681e5606a49b
|
|
| MD5 |
fba07f37c67ac4885cc888f4b6e5b490
|
|
| BLAKE2b-256 |
c435c5dcbabf0552854fc6c0de179049f6e540327e9e15f9fcf46fe086016f6a
|