AI-powered database chat using Llama 3.1, RAG, and local LLMs. Ask questions in plain English, get SQL queries and results instantly.
Ask questions about your database in plain English. No SQL knowledge required.
A self-hosted AI-powered chat application that translates natural language into SQL queries using local LLMs. Built with privacy-first architectureβall processing happens on your machine, no API keys needed.
Turn this:
"Which researcher has led the most patrols this year?"
Into this:
SELECT r.name, COUNT(e.id) as patrol_count
FROM researchers r
JOIN event_reports e ON r.id = e.lead_researcher_id
WHERE EXTRACT(YEAR FROM e.start_time) = 2025
GROUP BY r.name
ORDER BY patrol_count DESC
LIMIT 1And get instant resultsβno SQL knowledge required.
- Uses Llama 3.1 (8B) for accurate text-to-SQL conversion
- Retrieval Augmented Generation (RAG) for context-aware queries
- Learns from your database schema and example queries
- 100% local processing - no data sent to external APIs
- Read-only database access - prevents accidental data modifications
- SQL injection protection - blocks dangerous operations (DELETE, DROP, UPDATE)
- No API costs - runs entirely on your infrastructure
- Streamlit Web App - Clean, user-friendly interface for end users
- Jupyter Notebooks - Interactive development environment for data exploration
- Automatic data visualizations for numeric results
- Multiple output formats (table, JSON, markdown)
- Context-aware follow-up questions
- Query explanation before execution
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β User Interface Layer β
β Streamlit Web App | Jupyter Notebooks β
βββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββ
β Vanna AI Framework β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Query β β RAG β β Safety β β
β β Generator β β Engine β β Guard β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββ¬βββββββββββββββββββββ¬βββββββββββββββββββββ¬ββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ
β Ollama β β Weaviate β β PostgreSQL β
β (Llama 3.1) β β (Vector Store) β β Database β
β β β β β β
β β’ LLM β β β’ DDL Storage β β β’ Marine Data β
β β’ Embeddings β β β’ Documentation β β β’ Read-only β
ββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ
Before you begin, ensure you have:
- Docker Desktop (or Docker Engine + Docker Compose)
- 16GB RAM minimum (32GB recommended)
- 20GB free disk space
- (Optional) NVIDIA GPU with CUDA for faster inference
1. Clone the repository
git clone https://github.com/yourusername/marine-research-chat.git
cd marine-research-chat2. Start all services
docker-compose up -dThis will start:
- PostgreSQL database (with sample marine research data)
- Ollama LLM server
- Weaviate vector database
- Jupyter Lab
- Streamlit web app
3. Wait for services to initialize (~3-5 minutes)
# Watch the logs
docker-compose logs -f
# Check service health
docker-compose ps4. Verify everything is running
# All services should show "healthy"
docker-compose ps
# Quick health check
curl http://localhost:8501 # Streamlit should respond
curl http://localhost:8888 # Jupyter should respond
curl http://localhost:11434/api/tags # Ollama should respond5. Train the AI on your database (one-time setup)
Open Jupyter Lab: http://localhost:8888
Run the training notebook: notebooks/01_setup_and_training.ipynb
This will:
- Connect to your database
- Extract the schema
- Add business context documentation
- Train the AI with example queries
That's it! π
Access: http://localhost:8501
Perfect for non-technical users who want to query the database conversationally.
Example Questions:
- "How many researchers do we have?"
- "Show me all patrols from the last month"
- "Which species have been observed most frequently?"
- "What's the average water temperature by location?"
Features:
- β Clean, intuitive interface
- β Automatic SQL generation and display
- β Visual charts for numeric data
- β One-click example questions
Access: http://localhost:8888
Perfect for exploratory data analysis and advanced queries.
Available Notebooks:
01_setup_and_training.ipynb- Initial setup and training02_interactive_chat.ipynb- Interactive query interface03_examples_and_demos.ipynb- Comprehensive examples
The system includes a sample marine research database with:
- researchers - Marine biologists and scientists
- event_reports - Research patrols and expeditions
- species_observed - Marine species sightings
- environmental_conditions - Water quality measurements
- equipment_used - Scientific equipment deployments
"How many patrols were conducted last month?"
"Show me all researchers specializing in coral ecology"
"What species were observed at Coral Garden Zone A?"
"Compare water temperature trends across locations"
"Which researcher has the highest biodiversity observations?"
| Service | Port | Purpose |
|---|---|---|
| Streamlit | 8501 | Web UI |
| Jupyter | 8888 | Notebooks |
| PostgreSQL | 5432 | Database |
| Weaviate | 8080 | Vector store |
| Ollama | 11434 | LLM API |
Key settings in .env:
# Database
POSTGRES_DB=chatdb
POSTGRES_USER=chatuser
POSTGRES_PASSWORD=chatpass
# LLM Model
OLLAMA_MODEL=llama3.1:8b
# Safety Settings
MAX_QUERY_RESULTS=1000
QUERY_TIMEOUT=30- Update database connection in
config/vanna_config.yaml:
database:
host: your-db-host
port: 5432
database: your-database
user: your-user
password: your-password- Re-run the training notebook to learn your schema
- Add business context specific to your domain
- User asks a question in natural language
- Question is embedded using Ollama's embedding model
- Similar examples are retrieved from Weaviate vector store
- Context is constructed with:
- Database schema (DDL)
- Business documentation
- Similar question-SQL pairs
- Llama 3.1 generates SQL using the context
- Safety checks validate the query (no DELETE/DROP/UPDATE)
- Query executes on PostgreSQL
- Results are formatted and displayed with visualizations
- RAG provides context - The LLM knows your specific schema and terminology
- Examples improve accuracy - Learning from past queries produces better SQL
- Local processing - No data leaves your infrastructure
- Safety first - Multiple validation layers prevent dangerous operations
| Component | Technology | Purpose |
|---|---|---|
| LLM | Llama 3.1 (8B) via Ollama | SQL generation |
| Vector DB | Weaviate | Semantic search, RAG |
| Database | PostgreSQL 16 | Data storage |
| Backend | Vanna AI | Text-to-SQL orchestration |
| Frontend | Streamlit | Web interface |
| Dev Environment | Jupyter Lab | Interactive notebooks |
| Orchestration | Docker Compose | Service management |
- Vanna AI - Text-to-SQL framework
- Meta - Llama 3.1 model
- Ollama - Local LLM serving
- Weaviate - Vector database
This is not just another chatbot. It demonstrates:
β
Modern AI Architecture - RAG, embeddings, vector search
β
Production Patterns - Docker, health checks, safety guards
β
Privacy-First Design - No external APIs, local processing
β
Full-Stack Skills - Database, backend, frontend, ML
β
Real-World Application - Solves actual data access problems