(Query your PostgreSQL database using natural language)
Managing a growing database can get messy — remembering table names, joins, and Sequelize-generated column structures isn’t fun.
So I built an AI-powered SQL Chatbot that turns natural language questions into safe, optimized SQL queries, and streams results instantly.
This project uses:
- LangChain + LangGraph
- Mistral AI (mistral-small-latest)
- PostgreSQL + Sequelize ORM
- Streamlit UI
What This Chatbot Does
Simply type something like:
“Show me the last 5 orders with customer names.”
The chatbot will:
Parse the question
- Understand the relevant tables
- Auto-generate a safe, dialect-correct SQL query
- Avoid soft-deleted rows
- Perform the right JOINs using Sequelize relationships
- Stream the result back to you in seconds
- No manual querying. No database digging.
What This AI Chatbot Can Do
✔ Understand natural language questions
✔ Generate accurate and safe SQL
✔ Join across multiple related tables
✔ Auto-fix incorrect SQL
✔ Enforce business rules (soft-delete filters, safe SELECT-only mode)
✔ Display results interactively in Streamlit
Architecture at a Glance
- User asks a question – “What prepaid services does customer John have?”
- LangChain SQL Toolkit analyzes table schemas
- Mistral LLM writes an optimized SQL query
- PostgreSQL executes it
- Streamlit shows the answer cleanly
Simple. Powerful. Zero SQL typing
Tech Stack
| Component | Purpose |
|---|---|
Mistral AI (mistral-small-latest) |
Generates SQL using natural language |
| LangChain SQLToolkit | Provides DB inspection & SQL execution |
| LangGraph ReAct Agent | Tool-based reasoning loop |
| PostgreSQL | Database |
| Streamlit | Lightweight chat UI |
Core Code Snippet
Here’s the essential logic behind the agent:
llm = ChatMistralAI(model="mistral-small-latest", api_key=api_key)
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
tools = toolkit.get_tools()
agent = create_react_agent(llm, tools, prompt=system_prompt)
for step in agent.stream({"messages": [{"role": "user", "content": question}]}, stream_mode="values"):
last_msg = step["messages"][-1]
if isinstance(last_msg, AIMessage):
st.markdown(last_msg.content)
The SQL agent:
- inspects schemas
- constructs joins
- limits output
- blocks dangerous commands
- retries if SQL errors Pretty smart for 20 lines of code
Why This Is Useful
This chatbot is perfect for:
- internal dashboards
- support teams
- BI analysts
- non-technical managers
- quick debugging
- ad-hoc data exploration
It turns your database into a chat interface, which team members can use instantly — no SQL skills required.
Try It in Your Project
If you’re building tools that query relational data, this setup is incredibly powerful.
Plug in your DB → set your rules → get a natural-language assistant instantly.
Top comments (0)