math-homework-helper

Math Agent with Human-in-the-Loop Feedback

A virtual math professor that provides step-by-step solutions to mathematical problems using an Agentic RAG (Retrieval-Augmented Generation) architecture, with human feedback for continuous improvement.


Deployed Agent at https://bcwti9cmjtjgovljrdxxer.streamlit.app/

Documentation at https://sarthakb11.github.io/math-homework-helper/

๐Ÿš€ Features


๐Ÿงฐ Tech Stack


๐Ÿ› ๏ธ Architecture

The system uses an Agentic RAG architecture with the following components:

  1. AI Gateway: Entry/exit point, enforcing guardrails.
  2. Routing Agent: Directs queries to either the knowledge base or web search.
  3. Knowledge Base: Vector database (Qdrant) storing math knowledge.
  4. Web Search Agent: Performs targeted web searches and extracts content.
  5. Generation Agent: Synthesizes information into step-by-step solutions.
  6. Human Feedback Loop: Collects and integrates user feedback.

๐Ÿ“ Project Structure

math-homework-helper/
โ”œโ”€โ”€ app.py                  # Main Streamlit app entry point
โ”œโ”€โ”€ requirements.txt        # Python dependencies
โ”œโ”€โ”€ env.sample              # Example environment variables
โ”œโ”€โ”€ .env                    # (Not committed) Your actual environment variables
โ”œโ”€โ”€ app/                    # Core application modules (agents, kb, feedback, etc.)
โ”œโ”€โ”€ scripts/
โ”‚   โ”œโ”€โ”€ init_db.py          # Script to initialize the vector DB
โ”‚   โ””โ”€โ”€ load_knowledge_base.py # Script to load sample data
โ”œโ”€โ”€ .github/workflows/
โ”‚   โ””โ”€โ”€ deploy.yml          # GitHub Actions workflow for deployment
โ””โ”€โ”€ ...

๐Ÿ”ง Installation & Local Development

Prerequisites

Setup

  1. Clone the repository:
    git clone https://github.com/your-username/math-homework-helper.git
    cd math-homework-helper
    
  2. Set up a Python virtual environment:
    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. Install dependencies:
    pip install --upgrade pip
    pip install -r requirements.txt
    
  4. Configure environment variables:
    cp env.sample .env
    

    Edit .env with your API keys and configuration.

  5. Start Qdrant (Vector Database) using Docker:
    docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant
    
  6. Initialize the database:
    python scripts/init_db.py
    
  7. Load sample data into the knowledge base:
    python scripts/load_knowledge_base.py
    
  8. Run the Streamlit app:
    streamlit run app.py
    

๐Ÿš€ Deployment (GitHub Actions)

A ready-to-use GitHub Actions workflow is provided at .github/workflows/deploy.yml. On push to main, it will:

To use Qdrant Cloud, set VECTOR_DB_URL and VECTOR_DB_API_KEY in your .env or as GitHub Actions secrets.

Required Secrets

Set these secrets in your GitHub repository (Settings โ†’ Secrets and variables โ†’ Actions):

(Reference your .env.sample for any additional secrets your app may require.)


๐Ÿ“ Environment Variables

See env.sample for all required environment variables:


๐Ÿงช Testing

To run tests:

pytest

๐Ÿ“ Project Structure

maths-homework-helper/
โ”œโ”€โ”€ app/                  # Core application code
โ”‚   โ”œโ”€โ”€ agents/           # Agent definitions and logic
โ”‚   โ”‚   โ”œโ”€โ”€ generation_agent.py  # LLM-based solution generation
โ”‚   โ”‚   โ””โ”€โ”€ routing_agent.py     # KB/Web routing logic
โ”‚   โ”œโ”€โ”€ gateway/          # AI Gateway implementation
โ”‚   โ”‚   โ””โ”€โ”€ ai_gateway.py        # Input/output validation
โ”‚   โ”œโ”€โ”€ kb/               # Knowledge Base integration
โ”‚   โ”‚   โ””โ”€โ”€ vector_db.py         # Vector database connector
โ”‚   โ”œโ”€โ”€ web_search/       # Web Search and extraction logic
โ”‚   โ”‚   โ””โ”€โ”€ search_agent.py      # Web search and content extraction
โ”‚   โ”œโ”€โ”€ feedback/         # Human-in-the-Loop feedback mechanism
โ”‚   โ”‚   โ””โ”€โ”€ feedback_loop.py     # Feedback collection and processing
โ”‚   โ””โ”€โ”€ models/           # Data models and schemas
โ”‚       โ””โ”€โ”€ database.py           # Database models
โ”œโ”€โ”€ scripts/              # Utility scripts
โ”‚   โ”œโ”€โ”€ init_db.py                # Initialize database tables
โ”‚   โ””โ”€โ”€ load_knowledge_base.py    # Load sample data into KB
โ”œโ”€โ”€ data/                 # Knowledge Base data and schemas
โ”‚   โ”œโ”€โ”€ kb_data/                  # Custom KB data files
โ”‚   โ””โ”€โ”€ feedback_logs/            # Feedback logs
โ”œโ”€โ”€ Instructions/         # Project documentation
โ”œโ”€โ”€ .env                  # Environment variables (not versioned)
โ”œโ”€โ”€ env.sample            # Sample environment variables
โ”œโ”€โ”€ requirements.txt      # Python dependencies
โ””โ”€โ”€ app.py                # Main application entry point

๐Ÿ”„ Workflow

  1. User submits a math question through the UI
  2. AI Gateway validates the input
  3. Routing Agent checks the Knowledge Base for relevant information
  4. If KB has good matches, the solution is generated from KB content
  5. If KB lacks information, Web Search is performed to find solutions
  6. Generation Agent creates a step-by-step solution
  7. User sees the solution and can provide feedback (helpful/needs improvement)
  8. Feedback is logged and used to improve future responses

๐Ÿค Contributing

We welcome contributions! Please see the development workflow in the documentation folder.

๐Ÿ“ License

This project is licensed under the MIT License.

๐Ÿ™ Acknowledgments