aouladbouchta/sqlite-ai-agent
SQLite AI Agent ๐ค๐๏ธ
An AI-powered database assistant that lets you interact with a local SQLite database using natural language.
This project combines FastAPI, LangChain, and OpenAI models to create a smart interface where users can:
- Browse database tables
- View schemas
- Preview rows
- Query data
- Insert records
- Update records
- Delete records
All directly from a web interface or chat commands.
โจ Features
AI Database Assistant
- Natural language database queries
- Supports SELECT / INSERT / UPDATE / DELETE
Smart Web Interface
- AI chat interface
- HTML table rendering
- Chat history stored locally
- Clear chat button
Database Explorer
- Sidebar with all tables
- Search tables instantly
- Row count badges
- View table schema
- Preview first 20 rows
- Auto-select table after AI operations
๐ง Example Commands
Users can type instructions like:
Show me the first 10 users
Show the schema of the orders table
Insert a new user with name John Doe
Update the status to shipped where id is 10
Delete the user where id is 7The AI agent automatically:
- Inspects the schema
- Chooses the correct tool
- Executes the database operation
- Returns formatted results
๐๏ธ Architecture
User Interface
โ
โผ
Frontend (HTML + JS)
โ
โผ
FastAPI Backend
โ
โผ
LangChain AI Agent
โ
โผ
Database Tools Layer
โ
โผ
SQLite Database๐๏ธ Project Structure
sqlite-ai-agent/
โ
โโโ app/
โ โโโ main.py
โ โโโ db.py
โ โโโ tools.py
โ โโโ agent.py
โ โโโ schemas.py
โ
โ โโโ static/
โ โ โโโ style.css
โ โ โโโ app.js
โ
โ โโโ templates/
โ โโโ index.html
โ
โโโ .env
โโโ requirements.txt
โโโ shop_database.dbโ๏ธ Installation
1๏ธโฃ Clone the repository
git clone https://github.com/javamohammed/sqlite-ai-agent.git
cd sqlite-ai-agent2๏ธโฃ Create a virtual environment
python -m venv venvActivate it:
Windows
venv\Scripts\activateMac / Linux
source venv/bin/activate3๏ธโฃ Install dependencies
pip install -r requirements.txt4๏ธโฃ Create .env
Create a .env file:
OPENAI_API_KEY=your_openai_api_key
DATABASE_URL=sqlite:///./shop_database.db
OPENAI_MODEL=gpt-4.1-mini5๏ธโฃ Add your SQLite database
Place your database file in the project root.
Example:
shop_database.db๐ Run the Application
Start the server:
uvicorn app.main:app --reloadOpen the browser:
http://127.0.0.1:8000๐ API Endpoints
Chat with AI
POST /api/chatExample request:
{
"message": "Show me the first 10 users"
}List Tables
GET /api/tablesTables With Row Counts
GET /api/tables-with-countsTable Schema
GET /api/schema/{table_name}Table Preview
GET /api/table-preview/{table_name}Returns the first 20 rows.
๐ Safety Notes
This application allows direct database modification.
Operations supported:
- INSERT
- UPDATE
- DELETE
To reduce risk:
- UPDATE requires
WHERE - DELETE requires
WHERE
โ ๏ธ Do not use this on sensitive production databases without additional protections.
Recommended improvements:
- Authentication
- Authorization
- Audit logs
- Query preview
- Table permissions
- Backups
โ ๏ธ Limitations
- Designed for SQLite
- No built-in authentication
- Large tables may slow down row counts
- Natural language ambiguity may occur
๐งช Example Prompts
Show me all tables
Show the schema of users
Show me the first 20 rows from orders
Insert a new product with name Laptop and price 900
Update the status to delivered where id is 5
Delete the order where id is 2๐ง Environment Variables
๐ ๏ธ Future Improvements
Possible next features:
- Pagination
- Column sorting
- Row filtering
- SQL preview
- CSV export
- Authentication
- Multi-database support
- Dark mode
- Query history
๐ค Contributing
Contributions are welcome.
Steps:
- Fork the repository
- Create a branch
- Commit your changes
- Open a pull request
๐ License
You can use the MIT License for this project.
Example:
MIT License
Copyright (c) 2026๐ Acknowledgements
FastAPI https://fastapi.tiangolo.com/
LangChain https://www.langchain.com/
OpenAI https://platform.openai.com/
SQLAlchemy https://www.sqlalchemy.org/
