A production-ready QueryCraft web application powered by LangChain, ChatGroq, Streamlit, and SQLite (ecommerce.db). Ask questions about your data in natural language and get accurate SQL queries and results, with the ability to upload custom CSV/Excel files for on-the-fly analysis.
- Streamlit Web UI: Interactive interface for natural language querying
- Dynamic File Upload: Upload CSV or Excel files via Pandas to ingest custom data directly as new tables into
ecommerce.db - Modular Architecture: Clean separation of concerns in the
main_code/package - Robust Error Handling: Comprehensive try-except blocks with user-friendly error messages
- Production Logging: Detailed audit trail in
agent.logwith timestamps and log levels - LangSmith Tracing: Optional integration for debugging and performance monitoring
- Custom Agent Prompt: Formal, professional tone with SQL query transparency
main_code/
├── __init__.py # Package initializer
├── app.py # Streamlit Web UI frontend
├── agent.py # LangChain & ChatGroq agent logic
└── database.py # SQLite connection & file ingestion handler
- Python 3.11 or higher
- GROQ API key (get one here)
- (Optional) LangSmith API key for tracing (sign up here)
-
Clone the repository
git clone https://github.com/yourusername/text-to-sql-agent.git cd text-to-sql-agent -
Set up a virtual environment
# Using uv (recommended) uv venv --python 3.11 source .venv/bin/activate # On Windows: .venv\Scripts\activate # Or using standard venv python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies
uv pip install -e . # Or using pip pip install -e .
-
Configure environment variables
cp .env.example .env # Edit .env and add your API keysRequired in
.env:GROQ_API_KEY=your_groq_api_key_hereOptional for LangSmith tracing:
LANGCHAIN_TRACING_V2=true LANGSMITH_ENDPOINT=https://api.smith.langchain.com LANGCHAIN_API_KEY=your_langsmith_api_key_here LANGCHAIN_PROJECT=text2sql-agent
Start the Streamlit web application:
streamlit run main_code/app.pyThen open your browser to http://localhost:8501.
You can also use the CLI directly:
# Basic query
uv run python main_code/agent.py "How many customers are there?"
# With file upload
uv run python main_code/agent.py "What is the average purchase amount?" --file path/to/data.csvThe application uses a SQLite database named ecommerce.db with the following schema:
- customers: id, name, email, city, signup_date
- products: id, name, category, price, stock
- orders: id, customer_id, order_date, total_amount
- order_items: id, order_id, product_id, quantity, unit_price
- uploaded_data: (dynamically created from user-uploaded CSV/Excel files)
- File Upload (Optional): When a user uploads a CSV/Excel file, it's read with Pandas and stored as a table named
uploaded_datainecommerce.db - Question Processing: The user's natural language question is processed by the LangChain agent
- SQL Generation: The agent uses ChatGroq to generate a syntactically correct SQL query
- Query Execution: The query runs against
ecommerce.db(including any uploaded tables) - Result Formatting: The agent returns the answer along with the generated SQL query for transparency
- Display: Results are shown in the Streamlit UI with the SQL query in a code block
- All critical operations (file processing, database connections, agent creation, query execution) are wrapped in try-except blocks
- User-friendly error messages are displayed in the UI/CLI
- Detailed logs with timestamps, log levels, and tracebacks are written to
agent.log - The application gracefully handles invalid files, connection issues, and query errors
When configured via environment variables, every query is automatically traced in LangSmith, allowing you to view:
- Complete execution trace with all tool calls
- Token usage and costs
- Query execution time
- Generated SQL queries
- Error messages and retry attempts
MIT