InsightSQL is a next-generation AI Data Analyst powered by Google's Gemini 2.5 Flash and built upon the LangGraph architecture.
Unlike standard chatbots that guess answers, InsightSQL utilizes a Graph-Based Reasoning Engine. It treats data analysis as a cyclic workflow: validating schema, generating precise SQL, executing queries, and inspecting results. If an error occurs, the graph allows the agent to "loop back" and correct its own SQL syntax before presenting the final answer—providing a level of precision that linear chains cannot match.
Using langgraph.prebuilt.create_react_agent, the system navigates a structured cognitive cycle:
- Plan: Analyzes the user's intent and selects the appropriate SQL tool.
- Execute: Runs SQL queries against the SQLite database.
- Verify: Observes the output. If the query fails, the agent self-corrects and retries.
- Synthesize: Translates raw data into business insights in the requested language.
- View Reasoning Trace: Users can open an expander to watch the AI's internal monologue, tool invocations, and raw database outputs in real-time. Nothing is hidden "under the hood."
- Dual Language Support: Seamlessly switch between English and Indonesian via the sidebar.
- Strict Output Logic: The system prompt enforces a critical logic layer. Even if you ask in Indonesian, if the setting is "English", the Agent translates the final business insight into English automatically.
- Persistent Memory: The AI manages conversation context within the active session (
st.session_state). - Dual Reset Modes:
🧹 Clear Conversation: Clears the UI and resets the Agent's memory to start a fresh topic. The database connection remains active, but the AI forgets the previous context.🔄 Full System Reset: Performs a "Hard Reset"—wiping memory, killing the agent executor, disconnecting the database, and resetting API connections.
- LLM: Google Gemini 2.5 Flash (via
ChatGoogleGenerativeAI). - Framework: Streamlit (Frontend).
- Orchestration: LangGraph (Cyclic ReAct Agent).
- Database Tools: SQLDatabaseToolkit (LangChain Community).
- Database: SQLite (via SQLAlchemy).
- Currently hardcoded for SQLite (
.db) files. - Privacy: The Agent does not upload your entire database to Google. Only the Table Schema and the specific Rows returned by queries are sent to the LLM for analysis.
- The Agent is strictly instructed via System Prompts to avoid DML statements (
INSERT,UPDATE,DELETE) to ensure data integrity.
-
Clone the Repository
git clone https://github.com/viochris/InsightSQL-LangGraph-Engine.git cd InsightSQL-LangGraph-Engine -
Install Dependencies
pip install -r requirements.txt
-
Setup Your Database The system uses SQLAlchemy, making it compatible with SQLite, MySQL, PostgreSQL, and more.
-
Option A: SQLite (Default)
- Place your
.dbfile (e.g.,dresses.db) in the project root folder. - If using a custom filename, update
app.py(Line ~253):db = SQLDatabase.from_uri("sqlite:///my_filename.db")
- Place your
-
Option B: External Databases (MySQL / PostgreSQL)
- Open
app.pyand replace the connection URI with your server credentials:# For MySQL db = SQLDatabase.from_uri("mysql+pymysql://username:password@host:port/database_name") # For PostgreSQL db = SQLDatabase.from_uri("postgresql+psycopg2://username:password@host:port/database_name")
- Note: Ensure you have installed the necessary drivers (e.g.,
pip install pymysqlorpsycopg2) in your environment.
- Open
-
-
Run the Application
streamlit run app.py
- Configuration (Sidebar):
- Enter your Google Gemini API Key (Required).
- Select your Language Preference (English/Indonesian).
- Connect:
- Click "🚀 Connect to Database".
- This initializes the
SQLDatabaseToolkitand builds the LangGraph Agent.
- Query:
- Type your question naturally (e.g., "Show me the top 3 most expensive dresses" or "Berapa rata-rata rating untuk style casual?").
- Pro Tip: You can switch languages in the sidebar mid-conversation, and the Agent will adapt immediately without needing a reset.
- Manage:
- Use "View Reasoning Trace" to verify the SQL query used.
- Use "🧹 Clear Conversation" to declutter the screen and start a new analysis topic (Note: AI forgets previous context, but the Database connection remains active).
- Use "🔄 Full System Reset" if you change API keys, switch databases, or encounter a system error.

The clean, modern landing page offering quick configuration. Users input their API key, select a language, and connect to the database securely.

Visual feedback using a spinner ("Executing reasoning workflow...") assures the user that the Graph Engine is actively planning and querying in the background.

Inside the "View Reasoning Trace" expander. Here, you can see the Agent's internal "Thought" (planning), the "Action" (invoking the SQL tool), and the "Tool Observation" (raw data returned from SQLite).

The final output. The Agent synthesizes the raw SQL results into a clear, natural language answer formatted strictly according to the user's language preference.
Author: Silvio Christian, Joe "Transparent Reasoning. Data-Driven Answers."