Skip to main content

A Python package for querying databases using natural language.

Project description

askmydb

A Python package for querying databases using natural language.

Overview

askmydb allows you to interact with your databases by asking questions in natural language. It leverages large language models (LLMs) to convert your queries into SQL and execute them on the specified database.

Features

  • Query databases using natural language prompts.
  • Supports multiple LLM providers (e.g., dummy, OpenAI, Ollama).
  • Works with SQLite and other databases supported by SQLAlchemy.
  • Easy to set up and use.

Installation

Install the package and its dependencies using pip:

pip install askmydb

The package requires the following dependencies:

  • openai
  • ollama
  • sqlalchemy

You can install these dependencies individually if needed:

pip install openai ollama sqlalchemy

Usage

Below are examples of how to use askmydb with different LLM providers.

Using OllamaProvider

from askmydb import AskMyDB
from askmydb.llm.ollama_provider import OllamaProvider

if __name__ == "__main__":
    llm = OllamaProvider(base_url="http://localhost:32768", model="qwen2.5:1.5b")
    askDb = AskMyDB(db_url="sqlite:///IMDB.db", llm=llm)
    query, result = askDb.ask("get the movies on action genre with rating more than 5 sort it high to low")
    print(result)

Using OpenAIProvider (via OpenRouter)

from askmydb import AskMyDB
from askmydb.llm.openai_provider import OpenAIProvider

if __name__ == "__main__":
    llm = OpenAIProvider(api_key="your_api_key_here", base_url="https://openrouter.ai/api/v1", model="meta-llama/llama-4-maverick:free")
    askDb = AskMyDB(db_url="sqlite:///IMDB.db", llm=llm)
    query, result = askDb.ask("get the movies on action genre with rating more than 5 sort it high to low")
    print(query, result)

License

This project is licensed under the MIT License.

Author and Repository

Author: Shanthosh
Email: shanthubolt@gmail.com
Repository: https://github.com/Msalways/Ask-My-DB

Custom LLM Provider Example

You can create your own custom LLM provider by subclassing LLMProvider. Below is an example implementation of a CustomProvider:

from askmydb.llm.base import LLMProvider
from askmydb.llm.sql_prompt import build_sql_prompt, build_system_prompt

class CustomProvider(LLMProvider):
    def __init__(self, base_url, model, temperature):
        self.base_url = base_url
        self.model = model
        self.temperature = temperature

    def generate_sql(self, prompt: str, schema: str) -> str:
        system_prompt = build_system_prompt()
        full_prompt = build_sql_prompt(prompt, schema)
        # Implement your custom logic here to generate the SQL query using your LLM
        query = None
        return query

Using CustomProvider

from askmydb import AskMyDB
from my_custom_provider import CustomProvider

if __name__ == "__main__":
    llm = CustomProvider(base_url="your_base_url", model="your_model", temperature=0.7)
    askDb = AskMyDB(db_url="sqlite:///IMDB.db", llm=llm)
    query, result = askMyDB.ask("get the movies on action genre with rating more than 5 sort it high to low")
    print(query, result)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

askmydb-0.1.5.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

askmydb-0.1.5-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file askmydb-0.1.5.tar.gz.

File metadata

  • Download URL: askmydb-0.1.5.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for askmydb-0.1.5.tar.gz
Algorithm Hash digest
SHA256 e51811b4dc9833a0378785c9560867fc7663c15c1b283f0ade13b7c077c13c65
MD5 5fa49e9b6cca4310c0d0bd0653837cb9
BLAKE2b-256 37f7ec35d3da559f6472f4da93b4aff8e6123060424d92d51804458b24714c1e

See more details on using hashes here.

File details

Details for the file askmydb-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: askmydb-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for askmydb-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c1d13928aa4350f11731cefde590e9690df7d7eb5cbdea52b77e3f2999d885e8
MD5 9eff66414034725ab068fa9bb3aba9eb
BLAKE2b-256 3aaf838e0bd6c116c27aa9a906a88ddbdf50e0698bc48d6b65424ca9707b30c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page