A sophisticated terminal-based database assistant that uses artificial intelligence to understand natural language queries and execute complex database operations. The system has been migrated to use LangGraph for orchestrating AI-powered workflows, intelligently determining user intent, planning multi-step database operations, and providing clear, contextual responses.
- Natural Language Processing: Understands and processes natural language database queries
- Intelligent Intent Recognition: Automatically classifies queries as questions, context updates, or combined requests
- Ambiguity Resolution: Iteratively clarifies ambiguous queries through AI-powered conversation
- Automated Task Planning: Generates detailed, step-by-step execution plans for complex database operations
- Multi-Step Execution: Executes database operations with proper schema exploration and relationship handling
- Context Management: Maintains conversation context across interactions
- LangGraph Orchestration: Uses LangGraph for stateful, graph-based workflow management
- Web Interface: Clean Streamlit web interface
- Comprehensive Logging: Detailed execution logging for debugging and monitoring
The system has been migrated to use LangGraph, a framework for building stateful, multi-actor applications with LLMs. The application is structured as a directed graph where each node represents a processing step, and edges define the flow of execution based on the current state.
intent_determination: Analyzes user queries to classify intent as QUESTION, CONTEXT, or BOTHcontext_update: Updates conversation context when needed (for CONTEXT or BOTH intents)task_planning: Generates detailed execution plans for database operations using AItask_execution: Executes planned tasks step-by-step using database toolsoutput_generation: Creates natural language responses from execution results
START → intent_determination → context_update → [CONDITIONAL ROUTING]
↓
┌─────────┴─────────┐
│ │
CONTEXT │ │ QUESTION/BOTH
│ │
└─────────┬─────────┘
↓
output_generation ← task_execution ← task_planning
↓
END
langgraph_app.py: LangGraph workflow definition and node implementationsmain.py: Entry point with terminal UI loopcontext_manager.py: Manages conversation context and information persistencemodel_inference.py: Handles AI model interactions (Google Gemini API)logger.py: Comprehensive logging systemterminal.py: Terminal UI management and display functions
The system provides three primary database interaction tools:
query_database(query: str): Execute SQL queries and return formatted resultsget_tables(): Retrieve list of all database tablesget_table_schema(table_name: str): Get detailed schema information for specific tables
- Database: SQLite database (
datasets/chinook.db) - sample database with music store data - Context: Persistent context storage in
data/context.txt - Logs: Execution logs stored in
logs/directory
The LangGraph workflow processes user queries through a series of orchestrated nodes:
- Query Input: User enters natural language queries through the terminal interface
- Intent Determination: The
intent_determinationnode analyzes whether the input is a QUESTION, CONTEXT, or BOTH - Context Update: The
context_updatenode updates conversation context for CONTEXT/BOTH intents - Conditional Routing: Based on intent, routes to either output generation (for context-only) or task planning
- Task Planning: The
task_planningnode generates detailed step-by-step execution plans using AI - Task Execution: The
task_executionnode executes each plan step, exploring schemas and running queries - Output Generation: The
output_generationnode creates natural language responses from results - State Persistence: Results update the conversation context for future interactions
- Python 3.13+
- Google AI Studio API key
- SQLite3
- Clone the repository:
git clone <repository-url>
cd project-i- Create a virtual environment:
python -m venv project-1-venv
source project-1-venv/bin/activate # On Windows: project-1-venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtThe key dependencies include:
langgraph: Framework for building stateful, multi-actor applications with LLMslangchain-core: Core LangChain abstractions and interfacesgoogle-generativeai: Google Gemini AI API client
- Create a
.envfile with your API credentials:
API_KEY=your_google_ai_api_key_here
DEFAULT_MODEL=gemini-1.5-flash # or your preferred modelpython app.pyThe application uses Streamlit to provide a web-based interface for natural language database queries.
Simple Query:
What is the capital of France?
Response: The capital of France is Paris.
Context + Query:
My name is John. What albums do I have?
Response: Based on the context that your name is John, here are your albums: ...
Complex Database Query:
Show me all albums by artists from Canada, including the artist name and album title.
*The LangGraph workflow will automatically:
- Classify the query as a QUESTION
- Generate a detailed execution plan exploring database schemas
- Execute multi-step database operations with JOIN queries
- Generate a natural language response*
- Enter natural language queries
- Type
quitto exit the application
The system processes each query through the following LangGraph nodes:
- intent_determination: Classifies query type
- context_update: Updates conversation context
- task_planning: Creates execution plans (for questions)
- task_execution: Runs database operations
- output_generation: Formats responses
The system works with the Chinook database, which includes tables for:
- Artists: Artist information
- Albums: Album details with artist relationships
- Tracks: Individual track information
- Genres: Music genre classifications
- Customers: Customer data
- Invoices: Purchase records
- Employees: Staff information
API_KEY: Google AI API key (required)COHERE_API_KEY: Cohere API key (optional, for Cohere models)BASE_URL: LM Studio base URL (optional, default: http://localhost:1234)DATABASE_PATH: Path to SQLite database (default: datasets/chinook.db)DEFAULT_MODEL: AI model to use (default: gemini-1.5-flash)
The application now supports flexible database selection through the web interface:
-
Enter Path Method: Manually enter the path to your SQLite database
- Supports relative paths:
datasets/chinook.db - Supports absolute paths:
/Users/username/Documents/mydb.db
- Supports relative paths:
-
Upload Database Method: Upload a SQLite database file directly through the UI
- Supported formats:
.db,.sqlite,.sqlite3 - Uploaded files are stored in
data/uploaded_databases/
- Supported formats:
See Database Configuration Documentation for detailed information.
To configure the database:
- Open the Streamlit application
- Click Settings in the sidebar
- Under Database Configuration, choose your method
- Click Save Settings to persist your configuration
Project-I/
├── src/ # Main source code (organized by concern)
│ ├── config/ # ✅ Configuration (single source of truth)
│ │ ├── config.yaml # Main configuration
│ │ ├── model_config.json # Model selection persistence
│ │ ├── settings.py # Configuration loader
│ │ └── defaults.py # Default values
│ ├── core/ # Core business logic & agents
│ ├── services/ # External service integrations
│ ├── tools/ # MCP server & tool registry
│ ├── workflow/ # LangGraph workflow definitions
│ └── shared/ # Shared utilities (DI, paths)
├── data/ # ✅ Runtime data only (gitignored)
│ ├── context.txt # Runtime context storage
│ ├── chroma_db/ # Vector database
│ ├── vectors/ # Vector embeddings
│ └── uploaded_databases/ # User uploads
├── datasets/ # Development datasets (gitignored)
├── tests/ # Comprehensive test suite
├── docs/ # Detailed documentation
├── logs/ # Runtime logs (gitignored)
├── app.py # Streamlit web interface
├── requirements.txt # Python dependencies
├── ARCHITECTURE.md # ✅ Detailed architecture guide
└── README.md # This file
See ARCHITECTURE.md for detailed architecture documentation.
- LangGraph Nodes: Add new nodes to
langgraph_app.pyfor additional processing steps - Database Tools: Extend database functions in
langgraph_app.pywith new operations - State Management: Modify the
GraphStateTypedDict to include new state fields - Conditional Routing: Update routing logic in the workflow for new execution paths
- UI Components: Modify
terminal.pyfor interface enhancements - AI Capabilities: Update prompt engineering in LangGraph node functions
- Context Management: Enhance
context_manager.pyfor better information handling
The system provides comprehensive logging through logger.py:
- Execution Stages: Tracks major operation phases
- Database Operations: Logs all database queries and results
- AI Interactions: Records model inference calls and responses
- Error Handling: Captures and logs exceptions with context
Logs are stored in the logs/ directory with timestamps and detailed metadata.
- API Key Errors: Ensure
API_KEYis set in.envfile - Database Connection: Verify
chinook.dbexists indatasets/directory - Permission Errors: Check write permissions for
data/andlogs/directories - LangGraph Import Errors: Ensure all LangGraph dependencies are installed (
pip install -r requirements.txt) - Graph Compilation Errors: Check for syntax errors in
langgraph_app.pynode definitions - State Flow Issues: Verify that all required state fields are properly initialized and passed between nodes
- Node Execution Failures: Check individual node logs in the
logs/directory for specific error messages - Conditional Routing Problems: Verify that state fields used in routing conditions are correctly set
- Async Operation Errors: Ensure all node functions are properly defined as async and handle exceptions
- State Persistence: Check that state updates are correctly applied and persisted between nodes
Enable debug output by modifying logging levels in logger.py or checking terminal debug prints. For LangGraph-specific debugging, examine the state transitions and node execution logs.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
[Add license information here]
- Built using Google's Gemini AI models
- Uses the Chinook sample database for demonstrations
- Terminal UI powered by Python curses library