Are you Data Analyst or Scientist and hate writing SQL?

Ignacio Rigoni
3 min readJul 18, 2024

--

Links

Github Repository: https://github.com/nachokhan/ask-my-db

Introduction

In today’s data-driven world, data analysts and scientists play a crucial role in transforming raw data into valuable insights. However, querying complex databases can often be a challenging and time-consuming task. This is where “Ask My DB” comes in — a tool designed to facilitate the creation and execution of SQL queries using artificial intelligence, specifically tailored for data analysts and scientists.

What is “Ask My DB”?

“Ask My DB” is an innovative solution that allows users to efficiently and accurately generate SQL queries using OpenAI’s GPT-4 technology. The tool integrates seamlessly with PostgreSQL databases and can interpret the database schema to generate appropriate SQL queries in response to user requests. This functionality is particularly valuable for data analysts and scientists who deal with large and complex datasets.

How to Use “Ask My DB”

  1. Preparation: Clone the repository and navigate to the project directory.
git clone https://github.com/nachokhan/ask-my-db
cd ask-my-db

2. Installing Dependencies: Ensure you have pip and Docker installed. Then, install the project dependencies.

pip install -r requirements.txt  

3. Configuration: Create a .env file in the .conf folder based on .env.template and fill in the necessary environment variables (PostgreSQL credentials and OpenAI API key).

4. Running with Docker: Use Docker to set up the environment.

This is an optional step, you can configure the .env file to point your own database

docker-compose up -d

5. Using the Tool: You can modify and run the main.py script to see examples of how to perform queries.

python src/main.py

Advantages of “Ask My DB” for Data Analysts and Scientists

  1. Ease of Use: “Ask My DB” allows you to generate SQL queries without needing to know the intricate details of the database schema, making the process much more accessible and efficient.
  2. AI Integration: Integration with GPT-4 enables the tool to interpret natural language requests and translate them into precise SQL queries. This reduces the time spent on writing complex queries and minimizes the risk of errors.
  3. Handling Complex Queries: The tool is especially useful when dealing with complex database schemas, where writing queries manually can be error-prone and time-consuming. “Ask My DB” simplifies this process, allowing you to focus on analyzing the results.
  4. CSV Results: Query results are automatically converted to CSV format, facilitating further analysis and easy sharing of data.

Conclusion

“Ask My DB” is a powerful tool that combines artificial intelligence with database management, providing an efficient solution for generating and executing SQL queries. Its well-defined structure and ability to handle complex schemas make it a valuable addition for data analysts and scientists who need to extract insights from large datasets.

If you are looking to improve the efficiency of your SQL queries and leverage the power of artificial intelligence, “Ask My DB” is the tool you need. By simplifying the query generation process, it allows you to focus on what truly matters — analyzing and interpreting your data to drive impactful insights.

Invitation for Collaboration

This project is an initial prototype, and there is still much room for improvement. As an open-source project:

  • Constructive criticism is welcomed, and you are invited to participate and help it grow.
  • Currently, it connects only to PostgreSQL databases, but it can be extended to support others with minimal effort.
  • While it currently uses ChatGPT, it can be adapted to accept other large language models (LLMs) for greater versatility.
  • I am aware that similar projects exist, but this one is my own creation, and I wanted to share it with the community.
  • Always remember that LLMs can generate errors, and a significant part of the user’s responsibility is not only to write good prompts but also to review the solutions.

--

--

Ignacio Rigoni
Ignacio Rigoni

Written by Ignacio Rigoni

Senior Software Developer and Systems Engineer based in Argentina. Enduro MTB rider.