How vaishnavi-newalkar/query_potter_sql_to_nl works
Purpose and Entry Points
This is a Python natural-language-to-SQL assistant for SQLite databases. It accepts conversational questions, derives safe read-only SQL with an LLM, executes it, and returns interpreted answers with reasoning and recovery details.
• FastAPI service entry: api.py
• Streamlit chat client: ui.py
• Alternate/local application launcher: app.py
• Bundled target database: data/Chinook_Sqlite.sqlite
API, UI, and Session Boundary
The FastAPI backend exposes database upload, query processing, and schema access; the Streamlit UI calls that backend and presents chat history plus pipeline reasoning. Session state preserves multi-turn context and supports clarification follow-ups rather than treating each question independently.
• Service boundary: api.py
• Client boundary: ui.py
• Conversation/session state: session/session_manager.py, session/memory.py
Database Intake and Schema Model
Uploaded or configured SQLite databases are validated before use. Schema extraction and caching provide tables, columns, and relationships to downstream stages. A foreign-key graph represents joinable relationships, while schema refinement narrows the LLM context to relevant tables rather than passing the entire database schema.
• Validation/execution boundary: db/validator.py, db/executor.py
• Schema discovery/cache: db/schema_extractor.py, db/schema_cache.py
• Relationship and relevance model: schema/fk_graph.py, schema/refiner.py
Intent Routing and Query Planning
Natural-language processing first identifies schema-oriented meta-queries and ambiguity. Meta requests can be answered directly from extracted schema. Ambiguous requests produce clarification prompts; clarification intent is merged with prior context. Otherwise, the planner identifies filters, joins, aggregations, exclusions, and query complexity to guide SQL generation.
• Meta and ambiguity routing: nlp/meta_handler.py, nlp/ambiguity_detector.py
• Context and clarification merge: nlp/context_builder.py, nlp/intent_merger.py
• Query plan construction: nlp/planner.py, nlp/classifier.py
LLM SQL Generation and Recovery
The system uses Groq-hosted LLM inference, configured through environment settings, to produce SQL and reasoning from the query plan plus refined schema. Prompt templates encode complexity-aware generation rules. If execution fails, a self-correction stage analyzes the database error and retries generation, with the README specifying up to two retries.
• External LLM integration: llm/client.py
• SQL/reasoning generation: llm/sql_generator.py, llm/prompt_templates.py
• Error-driven regeneration: llm/self_correction.py
• Runtime configuration: config/settings.py
Safety and Execution Boundary
Generated SQL must pass layered validation before reaching SQLite. The validator enforces SELECT-only/read-only behavior and blocks unsafe patterns; execution occurs through the database executor rather than directly from API handlers. This is the critical trust boundary between probabilistic LLM output and local database access.
• SQL policy enforcement: validation/sql_validator.py
• Database-specific validation and execution: db/validator.py, db/executor.py
Result Interpretation
Successful row sets, empty results, errors, generated SQL, and reasoning traces are transformed into user-facing responses. General conversational responses are handled separately from database-result interpretation, allowing schema/meta and non-query interactions to remain conversational.
• Structured result interpretation: response/interpreter.py, response/answer_generator.py
• General chat response path: response/general_chat.py
Main entry points: api.py FastAPI service · ui.py Streamlit chat client · sql_validator.py Read-only SQL policy · self_correction.py Error-driven correction
Components 18
Entry Points
- Streamlit chat client · Presents chat history and pipeline reasoning.
ui.py - FastAPI service · Handles upload, schema, and query requests.
api.py - Local launcher
app.py - Conversation state · Preserves multi-turn context and follow-ups.
Database & Schema
- SQLite database
data/Chinook_Sqlite.sqlite - DB validation & execution · Validates database access and executes approved SQL.
- Schema discovery & cache · Extracts and caches tables, columns, and relationships.
- FK graph & refinement · Finds join paths and narrows schema context.
Intent & Planning
- Meta & ambiguity routing · Answers schema requests directly or requests clarification.
- Context & intent merge · Combines clarification turns with prior intent.
- Query planner · Identifies joins, filters, aggregations, and complexity.
LLM Generation
- Runtime settings · Configures Groq-hosted inference.
config/settings.py - Groq LLM client
llm/client.py - SQL & reasoning generation · Uses complexity-aware prompts and refined schema.
- Read-only SQL policy · Enforces SELECT-only SQL and blocks unsafe patterns.
validation/sql_validator.py - Error-driven correction · Regenerates SQL from execution errors, up to two retries.
llm/self_correction.py
Response
- Result interpretation · Formats rows, empty results, errors, SQL, and reasoning.
- General chat response
response/general_chat.py
Connections 26
- Local launcher → FastAPI service: launches service
- Streamlit chat client → FastAPI service: API requests
- FastAPI service → Conversation state: loads and saves context
- FastAPI service → DB validation & execution: upload and query boundary
- DB validation & execution → SQLite database: validated access
- FastAPI service → Schema discovery & cache: schema requests
- Schema discovery & cache → SQLite database: extracts schema
- Schema discovery & cache → FK graph & refinement: catalog and relationships
- FastAPI service → Meta & ambiguity routing: natural-language request
- Conversation state → Context & intent merge: prior turns
- Meta & ambiguity routing → Context & intent merge: clarification path
- Meta & ambiguity routing → General chat response: meta or conversational request
- Context & intent merge → Query planner: resolved intent
- Query planner → FK graph & refinement: relevance requirements
- Query planner → SQL & reasoning generation: query plan
- FK graph & refinement → SQL & reasoning generation: refined schema
- Runtime settings → Groq LLM client: configuration
- SQL & reasoning generation → Groq LLM client: generation request
- SQL & reasoning generation → Read-only SQL policy: generated SQL
- Read-only SQL policy → DB validation & execution: approved read-only SQL
- DB validation & execution → Error-driven correction: execution error
- Error-driven correction → SQL & reasoning generation: corrected regeneration
- DB validation & execution → Result interpretation: query result or error
- Result interpretation → FastAPI service: structured answer
- General chat response → FastAPI service: conversational answer
- FastAPI service → Streamlit chat client: answer and reasoning