An intelligent chat interface for querying the Chinook music database using natural language, powered by LangChain v1.0, DuckDB, and NiceGUI.
NOTE: Pydantic AI version here
- Natural Language Queries: Ask questions about music, artists, albums, tracks, and genres in plain English
- Intelligent SQL Generation: Automatically converts your questions into optimized SQL queries
- DuckDB Integration: Lightning-fast analytical queries on the Chinook music database
- Real-time Chat Interface: Beautiful, responsive chat UI powered by NiceGUI
- LangChain v1.0 Compatible: Built with the latest LangChain architecture and patterns
- Python 3.13+
- OpenAI API Key (set in
.envfile)
- Clone the repository:
git clone <your-repo>
cd nl-to-sql-agent- Install dependencies:
uv sync- Set up your environment variables:
cp .env.example .env
# Edit .env and add your API keys:
# - OPENAI_API_KEY=your_openai_api_key_here
# - GOOGLE_API_KEY=your_google_api_key_here (optional, alternative to OpenAI)
# - ANTHROPIC_API_KEY=your_anthropic_api_key_here (optional, alternative to OpenAI)- The database will be automatically set up on first run
uv run python chat_app.pyThen open your browser to: http://localhost:8080
Try asking these questions in the chat interface:
- "Which genre has the longest average tracks?"
- "Who are the top 5 artists by album count?"
- "Show me tracks longer than 5 minutes"
- "What are the most popular genres by track count?"
- "Which albums have the most tracks?"
- "Find customers from Brazil"
- "Show me the highest grossing invoices"
- DuckDB: High-performance analytical database
- Chinook Dataset: Music store database with artists, albums, tracks, customers, and sales data
- Auto-migration: Automatically converts from SQLite to DuckDB on first run
- LangChain v1.0: Modern agent framework with StateGraph
- OpenAI GPT-4: Natural language understanding and SQL generation
- LangGraph: Sophisticated multi-step reasoning workflow
- NiceGUI: Modern Python web framework
- Real-time Chat: Instant responses with typing indicators
- Responsive Design: Works on desktop and mobile
├── chat_app.py # NiceGUI chat interface (main application)
├── pyproject.toml # uv project configuration
├── .env # Environment variables
├── Chinook.db # SQLite source database (auto-downloaded)
├── Chinook.duckdb # DuckDB database (auto-created)
└── README.md # This file
- Updated from
langchain.agents.ToolNodetolanggraph.prebuilt.ToolNode - Fixed conversation flow for OpenAI API compatibility
- Proper message type handling with
AIMessageandHumanMessage - StateGraph-based agent architecture
- Automatic SQLite to DuckDB migration
- Optimized for analytical queries
- Connection pooling and proper resource management
- Support for complex aggregations and joins
- Typing indicators and loading states
- Message timestamps and avatars
- Scrollable chat history
- Mobile-responsive design
- Error handling and recovery
The agent architecture is modular and extensible:
- Database Tools: Add new tools in the
SQLDatabaseToolkit - Agent Nodes: Create new nodes in the StateGraph
- UI Components: Extend the NiceGUI interface
# Test the chat interface
uv run python chat_app.py
# Then open http://localhost:8080 and try some queriesThe Chinook database contains:
- Artists: Music artists and bands
- Albums: Music albums with release information
- Tracks: Individual songs with duration, genre, etc.
- Genres: Music genres and categories
- Customers: Customer information and demographics
- Invoices: Sales transactions and line items
- Employees: Store employee records
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- LangChain for the agent framework
- DuckDB for the analytical database
- NiceGUI for the web interface
- Chinook Database for the sample data