CoolFace
Apppublic

aouladbouchta/sqlite-ai-agent

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
App README

SQLite AI Agent ๐Ÿค–๐Ÿ—„๏ธ

Python FastAPI SQLite LangChain OpenAI

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 7

The AI agent automatically:

  1. 1.Inspects the schema
  2. 2.Chooses the correct tool
  3. 3.Executes the database operation
  4. 4.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-agent

2๏ธโƒฃ Create a virtual environment

python -m venv venv

Activate it:

Windows

venv\Scripts\activate

Mac / Linux

source venv/bin/activate

3๏ธโƒฃ Install dependencies

pip install -r requirements.txt

4๏ธโƒฃ Create .env

Create a .env file:

OPENAI_API_KEY=your_openai_api_key
DATABASE_URL=sqlite:///./shop_database.db
OPENAI_MODEL=gpt-4.1-mini

5๏ธโƒฃ 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 --reload

Open the browser:

http://127.0.0.1:8000

๐ŸŒ API Endpoints

Chat with AI

POST /api/chat

Example request:

json
{
  "message": "Show me the first 10 users"
}

List Tables

GET /api/tables

Tables With Row Counts

GET /api/tables-with-counts

Table 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

VariableDescription
OPENAIAPIKEYYour OpenAI API key
DATABASE_URLSQLite connection string
OPENAI_MODELOpenAI model

๐Ÿ› ๏ธ 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:

  1. 1.Fork the repository
  2. 2.Create a branch
  3. 3.Commit your changes
  4. 4.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/