Skip to content

manaspros/NeuroDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

36 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  Cognitive Database Agent

AI-Powered Conversational Database System with Unforgeable Security

Query, analyze, and manage databases using natural language โ€” without compromising security.


๐Ÿš€ Overview

Cognitive Database Agent is a full-stack AI application that enables users to interact with a PostgreSQL database using plain English, while enforcing enterprise-grade security through Row-Level Security (RLS).

Unlike traditional AI database tools that rely on application-level permission checks, this system enforces access control directly at the database kernel level, making privilege escalation cryptographically and logically impossible.

The result is a secure, autonomous, explainable AI agent capable of querying, modifying, archiving, and visualizing data โ€” all through conversation.


โœจ Key Features

๐Ÿ—ฃ๏ธ Natural Language Database Interaction

  • Ask questions in plain English โ€” no SQL required
  • Supports complex, multi-step operations
  • Transparent reasoning using the ReAct agent pattern

๐Ÿ” Unforgeable Security (PostgreSQL RLS)

  • Database-level Row-Level Security (not app-level)
  • Prevents privilege escalation by design
  • Role-aware execution: Admin, Manager, Viewer
  • Zero trust in the AI agent โ€” security is enforced by PostgreSQL itself

๐Ÿ“Š Intelligent Data Visualization

  • Automatic chart detection using 11 heuristic rules
  • Supports Bar, Line, Area, Pie, and Table views
  • AI explains why a chart was chosen (with confidence score)
  • Manual override available for users

๐Ÿค– Autonomous Cognitive Agent

  • Built with LangChain + Ollama (qwen2.5:7b)
  • Uses 6 custom tools for safe DB interaction
  • Multi-step planning: select โ†’ insert โ†’ delete โ†’ aggregate
  • Role-aware error handling and messaging

๐Ÿ“š Retrieval-Augmented Generation (RAG)

  • Learns database schema dynamically using embeddings
  • No hardcoded schema or prompt engineering
  • Scales to large and evolving databases

๐Ÿ—๏ธ System Architecture

Frontend (React + Vite)
โ”‚
โ”‚  Chat UI + Role Selector + Auto Visualizations
โ”‚
โ–ผ
Backend (FastAPI)
โ”‚
โ”‚  Cognitive Agent (LangChain ReAct)
โ”‚  โ”œโ”€โ”€ DB Tools (role-aware)
โ”‚  โ”œโ”€โ”€ RAG Retriever (pgvector)
โ”‚  โ””โ”€โ”€ Query Planner
โ”‚
โ–ผ
PostgreSQL (RLS Enforced)
โ”‚
โ”‚  sales_data, sales_archive, knowledge_docs
โ”‚  Roles: admin | manager | viewer
โ”‚  Security: Row-Level Security (Unbypassable)

๐Ÿงฉ Tech Stack

Frontend

  • React 18 + Vite
  • TypeScript
  • Recharts
  • Axios

Backend

  • FastAPI
  • LangChain (ReAct Agent)
  • Ollama (qwen2.5:7b)
  • Pydantic

Database

  • PostgreSQL 14+
  • Row-Level Security (RLS)
  • pgvector

AI / ML

  • Sentence Transformers (all-MiniLM-L6-v2)
  • Retrieval-Augmented Generation (RAG)

๐Ÿ” Role-Based Access Control

Role Permissions
Admin Full access, all regions, all operations
Manager Read/write access to own region only
Viewer Read-only access, no mutations allowed

Security is enforced at the database level โ€” not in the application code.

CREATE POLICY manager_select_own_region
ON sales_data
FOR SELECT
TO db_manager
USING (region = current_setting('app.current_region'));

๐Ÿ“Š Example Queries

"Show total sales by region"
"Archive all 2021 sales from my region"
"Which quarter had the highest sales?"
"Show quarterly trends for 2023"

The agent:

  1. Understands intent
  2. Retrieves schema context (RAG)
  3. Plans steps (ReAct)
  4. Executes queries safely (RLS enforced)
  5. Returns results + visualizations + explanation

๐Ÿงช Tested & Validated

  • RLS privilege escalation attempts blocked
  • SQL injection prevention
  • Multi-step agent reasoning
  • Visualization auto-detection
  • Concurrent user sessions
  • Large dataset handling (table fallback)

๐Ÿš€ Getting Started

Prerequisites

  • Node.js 18+
  • Python 3.11+
  • PostgreSQL 14+
  • Ollama

Run Locally

# Backend
cd backend
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\\Scripts\\activate
pip install -r requirements.txt
uvicorn backend.main:app --reload
# Frontend
cd frontend
npm install
npm run dev

๐Ÿ“ Project Structure

backend/
 โ”œโ”€โ”€ agent/        # Cognitive agent + tools
 โ”œโ”€โ”€ api/          # FastAPI routes
 โ”œโ”€โ”€ db/           # PostgreSQL connection
 โ””โ”€โ”€ core/         # Config

frontend/
 โ”œโ”€โ”€ components/   # Visualization + UI
 โ”œโ”€โ”€ utils/        # Auto chart logic
 โ””โ”€โ”€ api/          # Backend client

database/
 โ”œโ”€โ”€ schema.sql    # Tables + RLS policies
 โ””โ”€โ”€ seed_data.sql

๐ŸŽฏ Why This Project Matters

  • Demonstrates real-world AI system design
  • Combines LLMs with security-critical databases
  • Uses production-grade access control
  • Focuses on explainability, safety, and UX
  • Directly applicable to enterprise analytics platforms

๐Ÿ”ฎ Future Enhancements

  • Scatter plots, heatmaps
  • Query caching (Redis)
  • Export to CSV / PDF
  • Multi-database support
  • Predictive analytics
  • Voice interface

๐Ÿ“œ License

MIT License


๐Ÿ™Œ Author

Developed as a University Full-Stack AI Course Project Focused on AI agents, database security, and system design

About

An AI-powered conversational database agent that lets users query and manage PostgreSQL using natural language with unforgeable Row-Level Security, autonomous reasoning (ReAct), RAG-based schema understanding, and intelligent data visualizations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors