Chat with SQLite Database
A lightweight Python agent built with LangChain and OpenAI function calling that translates natural-language questions into SQLite queries and generates HTML reports.
- Published
- Updated
- Reading
- 1 min
- Author
- Sudhanva Narayana
Introduction
Agents is a small Python project using LangChain and OpenAI tool calling to query a local SQLite database. You ask a question in plain English, and the agent inspects the schema in db.sqlite, runs SELECT queries, and optionally writes the results to an HTML report.
main.py sets up the OpenAI chat model, system prompt, and LangChain agent executor. handlers/ defines ChatModelStartHandler (which uses pyboxen to print terminal boxes around each model turn and tool call), and tools/ has the SQLite query runner (sql.py) and HTML report writer (reports.py).
Usage Instructions
- Install
langchain,pyboxen, andpydantic:
pip install langchain pyboxen pydantic
- Run
main.py:
python main.py
-
Type a question about your data. The agent checks the table definitions in
db.sqlitebefore running itsSELECTqueries. -
If you ask for a report, the agent calls
write_reportinreports.pyand saves the HTML file to disk.
Limitations
This is a small prototype for single-table lookups and basic aggregations. It struggles on multi-table joins and window functions, and it only runs read-only queries (no CREATE TABLE, ALTER TABLE, or DROP TABLE).
Related work
Production ML context
See how this topic connects to production ML systems, infrastructure, and inference.