Blog #portfolio #ai #openai

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

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

  1. Install langchain, pyboxen, and pydantic:
pip install langchain pyboxen pydantic
  1. Run main.py:
python main.py
  1. Type a question about your data. The agent checks the table definitions in db.sqlite before running its SELECT queries.

  2. If you ask for a report, the agent calls write_report in reports.py and 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.