DEV Community

Cover image for Revolutionizing Database Interaction with NLMDB: Where Natural Language Meets Data
Rakshith Dharmappa
Rakshith Dharmappa

Posted on

Revolutionizing Database Interaction with NLMDB: Where Natural Language Meets Data

Introduction to NLMDB

Ever found yourself staring at a complex database, struggling to craft the perfect SQL query? What if you could simply ask your database questions in plain English? That's exactly what I wanted when I built NLMDB, a Python library that lets you query databases using natural language through what I call the Model Context Protocol (MCP) approach.

In this post, I'll share why I created NLMDB, how it works, and how you can use it to transform your database workflow.

The Problem: SQL Is a Barrier

As a developer, I've often been the go-to person for database queries. While I'm comfortable with SQL, I noticed a persistent problem: team members without SQL expertise were constantly asking me to write queries for them. This created:

  1. Bottlenecks in our data workflow
  2. Dependency on technical team members
  3. Delayed insights and decision-making

There had to be a better way.

The Solution: Model Context Protocol

The core innovation in NLMDB is what I call the Model Context Protocol (MCP). MCP provides AI language models with structured context about database schemas, enabling them to generate accurate SQL queries from natural language questions.

Here's how it works:

  1. MCP extracts your database schema (tables, columns, relationships)
  2. It formats this schema in a way optimized for language model comprehension
  3. When a user asks a question, this context is included with the query
  4. The model generates appropriate SQL that works with your specific database
  5. The SQL is executed, and results are returned in your preferred format

Getting Started with NLMDB

Let's jump straight into how you can use NLMDB in your projects. First, install it:

pip install nlmdb
Enter fullscreen mode Exit fullscreen mode

Basic Usage: Getting Explanations

Let's start with the most straightforward use case - asking your database a question and getting a detailed explanation:

from nlmdb import dbagent

response = dbagent(
    api_key="your-openai-api-key",
    db_path="your_database.db",
    query="What tables are in the database and how are they related?"
)

print(response["output"])
Enter fullscreen mode Exit fullscreen mode

This produces a comprehensive natural language explanation of your database schema, including tables, columns, and their relationships.

Direct Data Access: SQL Agent Mode

Need to integrate with a data pipeline? Skip the explanations and get the data directly:

from nlmdb import sql_agent
import pandas as pd

# Get results as a pandas DataFrame
df = sql_agent(
    api_key="your-openai-api-key",
    db_path="your_database.db",
    query="Find customers who spent over $1000 in the last quarter",
    return_type="dataframe"  # Options: "dataframe", "dict", or "json"
)

# Now you can directly use pandas for analysis
high_value_customers = df[df['total_spending'] > 5000]
print(f"Number of VIP customers: {len(high_value_customers)}")
Enter fullscreen mode Exit fullscreen mode

Instant Visualizations: The New Viz Agent

The latest addition to NLMDB is the visualization agent, which generates interactive Plotly charts directly from your natural language queries:

from nlmdb import viz_agent

# Create a visualization
fig = viz_agent(
    api_key="your-openai-api-key",
    db_path="your_database.db",
    query="Show me a bar chart of monthly sales by product category"
)

# Display the interactive plot
fig.show()

# Save for sharing
fig.write_html("monthly_sales.html")
Enter fullscreen mode Exit fullscreen mode

This creates an interactive visualization without you having to write a single line of plotting code!

Real-World Use Cases

1. Data Democratization in Organizations

One of my clients, a mid-sized e-commerce company, used NLMDB to give their marketing team direct access to customer data. Before NLMDB, the marketing team would submit data requests to the tech team, waiting days for responses. Now, they simply ask questions like "Which customers purchased multiple times in the last 30 days?" and get immediate answers.

2. Accelerating Data Analysis Workflows

A data science team I work with integrated NLMDB with their Jupyter notebooks. Analysts can now query their data warehouse using natural language, get results as pandas DataFrames, and immediately continue their analysis workflow without context-switching to SQL.

3. Interactive Business Dashboards

Another interesting use case came from a finance team who built a Streamlit dashboard with NLMDB's visualization agent. Executives can now type questions like "Show me a breakdown of expenses by department" and get instant visualizations.

4. Database Education

A university professor told me they're using NLMDB to teach database concepts. Students learn how their natural language queries translate to SQL, accelerating their understanding of database operations without getting bogged down in syntax.

Privacy Considerations

Not everyone is comfortable sending their database schema to OpenAI. That's why NLMDB includes dbagent_private, sql_agent_private, and viz_agent_private, which can use local Hugging Face models instead:

from nlmdb import dbagent_private

response = dbagent_private(
    hf_config=("your-huggingface-token", "mistralai/Mixtral-8x7B-Instruct-v0.1"),
    db_path="your_database.db",
    query="What were our top-selling products last month?",
    use_local=True  # Process everything locally
)
Enter fullscreen mode Exit fullscreen mode

When use_local=True, all processing happens on your machine, ensuring your database schema and queries never leave your environment.

Future Directions

NLMDB is still evolving, and there are several exciting enhancements on the roadmap:

  1. Support for more database types (PostgreSQL, MySQL, MS SQL Server)
  2. Integration with database connection strings rather than just file paths
  3. Custom visualization templates and themes
  4. Memory for conversational context across queries
  5. Fine-tuned models specifically trained on database schemas

Contributing

NLMDB started as a solution to a problem I faced, but it's grown into something much bigger. The open-source community has been incredibly supportive, and contributions are always welcome.

Watch Demo

Conclusion

The Model Context Protocol approach in NLMDB represents a significant step forward in making databases more accessible to everyone. Whether you're a data analyst who wants to skip writing SQL, a team lead trying to democratize data access, or a developer building the next generation of data tools, NLMDB offers a new paradigm for database interaction.

Have you tried using natural language to interact with databases? What challenges have you faced? I'd love to hear your thoughts and experiences in the comments below!


P.S. If you're interested in how I built NLMDB and the Model Context Protocol, stay tuned for my upcoming post on the technical architecture and lessons learned from developing an AI-powered library.

Top comments (0)