Gemma2-STaR-MathHelper is a project that leverages the Gemma2 language model to efficiently solve mathematical problems. The project integrates a Retrieval-Augmented Generation (RAG) system and the Self-Teaching with Rationale (STaR) method along with mathematical tools to provide precise solutions and well-structured rationales.
- LLM Model: Uses the
Gemma2-9b
model from Ollama. - Information Retrieval System: Implemented with FAISS and
SentenceTransformer
for embeddings. - REACT Agent: Configured to use mathematical tools for problem-solving.
- Integrated Calculator: Tools for evaluating expressions, integration, differentiation, equation solving, and matrix operations.
- STaR Iterations: Generation and improvement of examples through STaR iterations.
- Python 3.8 or higher
- Python libraries:
langchain-community
sentence-transformers
faiss-cpu
dotenv
huggingface_hub
-
Clone the repository:
git clone https://github.com/your-username/Gemma2-STaR-MathHelper.git cd Gemma2-STaR-MathHelper
-
Create a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install the dependencies:
pip install -r requirements.txt
-
Configure the environment variables:
Create a
.env
file in the root directory of the project and add your OpenAI API key:OPENAI_API_KEY=your_api_key
-
Ensure the virtual environment is activated:
source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Run the main script:
python main.py
-
Modify
data/apuntes_matematicas.txt
to add your own mathematical notes.
main.py
: Main script that configures and runs the agent.utils.py
: Utilities for loading notes, setting up the QA chain, and calculator tools.calculator.py
: Implementation of theCalculator
class with mathematical functions.star_model.py
: Implementation of functions for generating and filtering rationales.data/apuntes_matematicas.txt
: Text file with mathematical notes for the retrieval system.
This project implements the concepts described in the paper "STaR: Self-Teaching with Rationales for Improved Explainability and Performance". Here's a brief overview of the implementation steps:
-
Retrieval-Augmented Generation (RAG):
- Uses FAISS for indexing and retrieving relevant mathematical notes.
- Utilizes
SentenceTransformer
to generate embeddings for the notes.
-
Self-Teaching with Rationales (STaR):
- Generates initial rationales for a set of example questions and answers.
- Filters and selects correct rationales to create a refined set of examples.
- Iteratively improves the examples through multiple iterations of generating and filtering rationales.
-
Integration with Tools:
- Configures an agent with access to various mathematical tools (calculator, integrator, differentiator, etc.) to solve complex mathematical problems.
- Uses the REACT framework to describe the steps and tools used in solving problems.
To solve a mathematical problem, you can modify main.py
to use the solve_math_problem
function with your desired question:
question = "What is the integral of x^2?"
answer = solve_math_problem(question)
print(answer)