Skip to content

About

A lightweight Text-to-SQL web agent built with LangChain, ChatGroq, Streamlit, and SQLite.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

QueryCraft

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.

✨ Key Features

  • 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.log with timestamps and log levels
  • LangSmith Tracing: Optional integration for debugging and performance monitoring
  • Custom Agent Prompt: Formal, professional tone with SQL query transparency

📂 Project Structure

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

🔧 Prerequisites & Installation

Requirements

Installation Steps

  1. Clone the repository

    git clone https://github.com/yourusername/text-to-sql-agent.git
    cd text-to-sql-agent
  2. 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
  3. Install dependencies

    uv pip install -e .
    # Or using pip
    pip install -e .
  4. Configure environment variables

    cp .env.example .env
    # Edit .env and add your API keys

    Required in .env:

    GROQ_API_KEY=your_groq_api_key_here
    

    Optional 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
    

▶️ Running Instructions

Start the Streamlit web application:

streamlit run main_code/app.py

Then open your browser to http://localhost:8501.

Command Line Interface (Alternative)

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.csv

🗄️ Database

The 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)

🧠 How It Works

  1. File Upload (Optional): When a user uploads a CSV/Excel file, it's read with Pandas and stored as a table named uploaded_data in ecommerce.db
  2. Question Processing: The user's natural language question is processed by the LangChain agent
  3. SQL Generation: The agent uses ChatGroq to generate a syntactically correct SQL query
  4. Query Execution: The query runs against ecommerce.db (including any uploaded tables)
  5. Result Formatting: The agent returns the answer along with the generated SQL query for transparency
  6. Display: Results are shown in the Streamlit UI with the SQL query in a code block

🛠️ Error Handling & Logging

  • 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

📊 LangSmith Integration

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

📝 License

MIT

🙌 Acknowledgments

About

A lightweight Text-to-SQL web agent built with LangChain, ChatGroq, Streamlit, and SQLite.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages