ChatWithDB

Inspiration

Databases contain a huge amount of useful information, but accessing that information often requires users to know SQL. This creates a barrier for people who understand the data they need but don't necessarily know how to write database queries.

The inspiration behind ChatWithDB was to make databases easier and more intuitive to interact with. Instead of asking users to write SQL queries manually, we wanted them to simply ask questions in natural language, just like they would ask another person.

For example, instead of writing:

SELECT * FROM STUDENT WHERE MARKS > 80;

a user can simply ask:

"Show me all students who scored more than 80 marks."

The application uses an LLM-powered SQL agent to understand the question, interact with the database, execute the appropriate SQL query, and return the result.


What it does

ChatWithDB allows users to communicate with their databases using natural language.

The application currently supports two database options:

  • SQLite – a local student.db database included with the project.
  • MySQL – users can provide their own MySQL host, username, password, database, and port.

Users also provide a Groq API key, which is used to power the Llama 3.3 70B language model.

The application then:

  1. Takes a natural-language question from the user.
  2. Provides the database schema and tools to the LLM-powered agent.
  3. Generates and executes the appropriate SQL query.
  4. Retrieves the relevant information from the database.
  5. Presents the answer through an interactive Streamlit chat interface.

The project also maintains chat history during the session, allowing users to have a conversational experience while querying their data.


How we built it

We built ChatWithDB using a combination of LLMs, LangChain, SQL databases, and Streamlit.

1. Streamlit

Streamlit is used to create the user interface. It provides the chat interface, database selection options, API-key input, MySQL configuration fields, and conversation history.

2. LangChain

LangChain is responsible for connecting the language model with the database.

We use:

  • SQLDatabase
  • SQLDatabaseToolkit
  • create_sql_agent

The SQL toolkit gives the agent the ability to understand the database structure and interact with it.

3. Groq + Llama 3.3 70B

The language model powering the application is Llama 3.3 70B Versatile, accessed through Groq.

The model interprets the user's natural-language question and determines how the database should be queried.

4. SQLite

For demonstration purposes, the project includes a local student.db database containing student information such as:

  • Name
  • Class
  • Section
  • Marks

The sample database contains records for students from Data Science and DevOps classes.

5. MySQL

To make the project more flexible, we also added support for connecting to an external MySQL database. Users can enter their database credentials directly through the Streamlit sidebar.

Overall architecture

User → Streamlit → LangChain SQL Agent → Groq/Llama → SQL Database → Result → User

This architecture allows the LLM to act as the bridge between human language and structured database queries.


Challenges we ran into

One of the biggest challenges was making an LLM interact reliably with structured database information.

Understanding natural language

Users can ask the same question in many different ways. The system needs to understand the intent behind the question and translate it into an appropriate database operation.

Connecting different databases

Supporting both SQLite and MySQL required handling different connection methods and configurations. We implemented separate database configuration paths so that users can either work with the included local database or connect to their own MySQL database.

SQL generation

Another challenge was getting the language model to generate useful SQL based on the actual database schema rather than making assumptions about the data.

Using LangChain's SQL database toolkit and SQL agent helped bridge the gap between the LLM and the database.

User experience

We also wanted the application to feel like a real conversation rather than a traditional database tool. Streamlit's chat components and session-state functionality allowed us to maintain the conversation history and provide an interactive experience.


Accomplishments that we're proud of

We're proud that we built a working bridge between natural-language conversations and SQL databases.

Some of the key accomplishments are:

  • Built a functional conversational database interface.
  • Enabled users to query databases without manually writing SQL.
  • Integrated Llama 3.3 70B through Groq.
  • Used LangChain's SQL agent and database toolkit to connect the LLM with structured data.
  • Added support for both SQLite and MySQL.
  • Created an interactive Streamlit interface.
  • Implemented conversational message history.
  • Added MySQL connection configuration directly through the UI.
  • Created a project architecture that can be extended to other databases and use cases.

The most important accomplishment is turning a traditionally technical workflow--writing SQL queries--into something much more accessible through natural language.

Built With

Share this project:

Updates

Submission history