Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

DataMind Chat: AI-Powered SQL Chatbot

A sophisticated full-stack application that leverages artificial intelligence to facilitate natural language interactions with relational and NoSQL databases. This system translates conversational queries into optimized SQL statements while maintaining session context, comprehensive audit logs, and enterprise-grade security.

Overview

DataMind Chat provides a seamless interface for non-technical users to query databases through natural conversation. The platform comprises a modern React frontend and a robust FastAPI backend, enabling secure, authenticated access to multiple database systems through AI-powered SQL generation.

System Architecture

graph TB
    subgraph Frontend["Frontend Layer"]
        React["React TypeScript Application"]
        UI["UI Components & State Management"]
    end
    
    subgraph Backend["API Layer"]
        FastAPI["FastAPI Server"]
        Auth["JWT Authentication"]
        Chat["Chat Processing"]
    end
    
    subgraph Processing["Processing Layer"]
        OpenAI["OpenAI GPT-4 Integration"]
        SQLGen["SQL Generation"]
        Validation["Query Validation"]
    end
    
    subgraph Data["Data Layer"]
        PostgreSQL["PostgreSQL"]
        MySQL["MySQL"]
        MongoDB["MongoDB"]
        SQLite["SQLite Logs"]
    end
    
    React -->|API Requests| FastAPI
    FastAPI --> Auth
    FastAPI --> Chat
    Chat --> OpenAI
    OpenAI --> SQLGen
    SQLGen --> Validation
    Validation --> PostgreSQL
    Validation --> MySQL
    Validation --> MongoDB
    FastAPI -->|Query Logs| SQLite
Loading

Core Features

Authentication & Security

  • JWT-based token authentication for all API endpoints
  • Secure credential management through environment variables
  • Role-based access control with read-only database operations
  • Session isolation and user context tracking

Database Support

  • PostgreSQL with full schema introspection
  • MySQL with comprehensive table metadata
  • MongoDB with collection structure analysis
  • Extensible architecture for additional database systems

AI Integration

  • OpenAI GPT-4 powered natural language to SQL conversion
  • Context-aware query generation based on database schema
  • Intelligent error handling and query optimization
  • Session memory for maintaining conversation context

Data Management

  • Query execution logging with SQLite backend
  • Complete audit trail of user interactions
  • Schema viewer API for database structure exploration
  • Secure SELECT-only query execution

DevOps Ready

  • Docker containerization for seamless deployment
  • Environment-based configuration management
  • Production-grade security recommendations
  • Health check endpoints for monitoring

Technology Stack

Component Technology
Frontend React, TypeScript, Vite, Tailwind CSS
Backend FastAPI, Python 3.11+
Authentication JWT (JSON Web Tokens)
AI Integration OpenAI API
Databases PostgreSQL, MySQL, MongoDB, SQLite
Containerization Docker
Package Manager Bun (Frontend), pip (Backend)

Prerequisites

System Requirements

  • Python 3.11 or higher
  • Node.js 18+ (for frontend development)
  • Docker (optional, for containerized deployment)
  • Git for version control

API Keys & Credentials

  • OpenAI API key with GPT-4 access
  • Database connection strings for target systems
  • Secure secret key for JWT operations

Installation

Backend Setup

  1. Navigate to backend directory:

    cd backend
  2. Create virtual environment:

    python3 -m venv venv
    source venv/bin/activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Configure environment variables:

    cp .env.example .env

    Update .env with your credentials:

    OPENAI_API_KEY=your_openai_key_here
    POSTGRES_URL=postgresql://user:password@host:5432/database
    MYSQL_URL=mysql://user:password@host:3306/database
    MONGO_URL=mongodb://user:password@host:27017/database
    JWT_SECRET=your_secure_secret_key
    JWT_ALGORITHM=HS256
  5. Run the server:

    uvicorn app.main:app --reload

    API available at http://localhost:8000

Frontend Setup

  1. Install dependencies:

    bun install
  2. Start development server:

    bun run dev
  3. Build for production:

    bun run build

Docker Deployment

Build Container Image

docker build -t datamind-chat-backend .

Run Container

docker run \
  --env-file .env \
  -p 8000:8000 \
  --name datamind-backend \
  datamind-chat-backend

Docker Compose (Optional)

For multi-container orchestration with databases:

docker-compose up -d

API Reference

Authentication

POST /auth/login
POST /auth/logout
POST /auth/refresh

Chat Operations

POST /chat
Request body: { "message": "string", "database": "string" }
Response: { "response": "string", "query": "string", "timestamp": "ISO8601" }

Query Logs

GET /logs?skip=0&limit=10
Response: [{ "id": int, "user_id": int, "query": "string", "timestamp": "ISO8601" }]

Schema Information

GET /schema/{database_type}
Response: { "tables": [...], "schemas": [...] }

Health Check

GET /health
Response: { "status": "healthy" }

Configuration

Environment Variables

Variable Description Example
OPENAI_API_KEY OpenAI API authentication key sk-...
POSTGRES_URL PostgreSQL connection string postgresql://user:pass@localhost:5432/db
MYSQL_URL MySQL connection string mysql://user:pass@localhost:3306/db
MONGO_URL MongoDB connection string mongodb://user:pass@localhost:27017/db
JWT_SECRET Secret key for JWT signing your-secret-key-min-32-chars
JWT_ALGORITHM JWT algorithm HS256
LOG_LEVEL Application logging level INFO, DEBUG, ERROR

Development Workflow

Code Structure

graph LR
    A["Frontend<br/>src/"] -->|API Calls| B["Backend<br/>app/"]
    B --> C["API<br/>api.py"]
    B --> D["Auth<br/>auth.py"]
    B --> E["Database<br/>db.py"]
    B --> F["Models<br/>models.py"]
    B --> G["AI Client<br/>openai_client.py"]
Loading

Debugging

Enable debug logging:

export LOG_LEVEL=DEBUG
uvicorn app.main:app --reload --log-level debug

Production Deployment

Security Checklist

  • Set JWT_SECRET to a cryptographically secure random string (minimum 32 characters)
  • Implement HTTPS with valid SSL/TLS certificates
  • Use environment variable management systems (AWS Secrets Manager, HashiCorp Vault)
  • Configure database users with read-only permissions
  • Enable query result pagination to prevent data exfiltration
  • Implement rate limiting on API endpoints
  • Set up monitoring and alerting for unusual access patterns

Performance Optimization

  • Deploy with production ASGI server (Gunicorn, Uvicorn)
  • Implement Redis for session persistence instead of in-memory storage
  • Configure database connection pooling
  • Enable query caching for frequent requests
  • Use CDN for static frontend assets

Monitoring

  • Implement centralized logging (ELK Stack, Datadog)
  • Monitor API response times and error rates
  • Track database query performance
  • Set up alerts for authentication failures
  • Log all SQL execution for compliance

Troubleshooting

Common Issues

Database Connection Failed

  • Verify connection string format in .env
  • Ensure database service is running
  • Check firewall rules and network connectivity

OpenAI API Errors

  • Validate API key has appropriate permissions
  • Check rate limits and usage quotas
  • Ensure model version (GPT-4) is available

Authentication Issues

  • Verify JWT_SECRET is consistent across sessions
  • Check token expiration time settings
  • Ensure Authorization header format: Bearer <token>

Contributing

Guidelines for contributing to the DataMind Chat project:

  1. Create feature branches from main
  2. Ensure code follows project style guidelines
  3. Write comprehensive tests for new functionality
  4. Submit pull requests with detailed descriptions
  5. Code review required before merge

Project Structure

data-mind-chat/
├── backend/                 # FastAPI backend application
│   ├── app/                # Application code
│   │   ├── main.py        # FastAPI application entry point
│   │   ├── api.py         # API route definitions
│   │   ├── auth.py        # Authentication logic
│   │   ├── db.py          # Database connections
│   │   ├── models.py      # Data models
│   │   └── openai_client.py # AI integration
│   ├── requirements.txt    # Python dependencies
│   └── Dockerfile         # Container configuration
├── src/                    # React frontend application
│   ├── components/        # React components
│   ├── pages/            # Page components
│   ├── hooks/            # Custom React hooks
│   ├── lib/              # Utility libraries
│   └── store/            # State management
└── package.json           # Frontend dependencies

Support & Documentation

For detailed documentation and additional resources:

  • API Documentation: Available at http://localhost:8000/docs (Swagger UI)
  • Backend README: See backend/README.md
  • Issue Tracking: GitHub Issues for bug reports and feature requests

License

MIT License - See LICENSE file for complete terms

Acknowledgments

  • OpenAI for GPT-4 language model
  • FastAPI community for the excellent web framework
  • React community for frontend tooling and libraries
  • Contributors and maintainers

Last Updated: January 2026

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages