How novelxv/langchain-sql-qa works
This is a command-line natural-language SQL Q&A application over a PostgreSQL database. A user submits a question; the LangGraph workflow generates SQL, executes it, and uses the result to produce an answer for the CLI. The repository also includes a Chinook database reset/import utility and supports local Ollama with OpenAI fallback. Edges reflect calls and setup behavior visible in the excerpts; database provisioning is shown separately from the runtime query workflow.
Main entry points: chain.py SQL generation · llm_config.py LLM selection · main.py Q&A CLI · clean_database.py Chinook setup
Components 13
User interaction
- Q&A CLI
src/main.py
Query workflow
- LangGraph workflow
src/chain.py - SQL generation
src/chain.py - SQL execution
src/chain.py - Answer generation
src/chain.py
Runtime integrations
- LLM selection
src/llm_config.py - Ollama
- OpenAI
- DB connection
src/database.py - PostgreSQL
Database setup
- Chinook setup
scripts/clean_database.py - Chinook SQL data
data/Chinook_PostgreSql.sql
Other components
- User
Connections 18
- User → Q&A CLI: enters question
src/main.py - Q&A CLI → LangGraph workflow: streams question
src/main.py - LangGraph workflow → SQL generation: starts sequence
src/chain.py - SQL generation → SQL execution: passes query
src/chain.py - SQL execution → Answer generation: passes result
src/chain.py - Answer generation → Q&A CLI: returns answer
src/main.py - SQL generation → LLM selection: uses prompts
src/chain.py - SQL generation → Ollama: invokes model
src/chain.py - Answer generation → Ollama: invokes model
src/chain.py - Answer generation → OpenAI: invokes model
src/chain.py - LLM selection → Ollama: probes models
src/llm_config.py - LLM selection → OpenAI: fallback probe
src/llm_config.py - LangGraph workflow → DB connection: imports database
src/chain.py - DB connection → PostgreSQL: connects to
src/database.py - SQL generation → PostgreSQL: reads table info
src/chain.py - SQL execution → PostgreSQL: runs SQL
src/chain.py - Chinook setup → PostgreSQL: recreates and loads
scripts/clean_database.py - Chinook setup → Chinook SQL data: imports when present
scripts/clean_database.py