Project Overview: Guaranteeing Mathematical Accuracy
This project delivers a Tool-Augmented Generation (TAG) system designed to provide 100% mathematically accurate solutions to single-variable algebraic equations. The primary goal was to solve a critical engineering challenge: eliminating calculation errors and hallucinations—the most common failure points for general-purpose Large Language Models (LLMs) in mathematics.
This system runs entirely offline, demonstrating robust deployment capability outside of cloud infrastructure.
The system is strictly scoped to single-variable linear and quadratic equations. This limitation is a deliberate engineering decision made to prioritize reliability and verifiable accuracy over generalized, but unreliable, breadth of knowledge. We ensure the system does a few things perfectly, rather than many things poorly.
The core innovation lies in separating the natural language processing (handled by a neural network) from the formal computation (managed by a symbolic solver).
The neural component handles all interaction with the user, ensuring the system can communicate in human language while maintaining mathematical integrity.
(i) Parser/Classifier (src/language_model.py): This module uses a lightweight DistilBERT tokenizer and rule-based logic to perform Natural Language Understanding (NLU). Its job is to classify the user's intent (e.g., "Is this a quadratic equation?") and aggressively strip away all conversational noise and unnecessary words, leaving only a clean, machine-readable algebraic string (e.g., converting "Solve 4x minus 8" to "4*x - 8 == 0").
(ii) Output Formatter (src/chatbot_math.py): After the computation is complete, this module translates the raw, technical output from the solver (e.g., SymPy's list of rational numbers) into a clean, professional
This is the core of the Tool-Augmented Generation (TAG) strategy, ensuring verifiable truth.
(i) Computational Core (src/solver_engine.py): This wraps the SymPy library, making it the system's single source of truth for all computation. It guarantees
(ii) Role in TAG: The LLM is prohibited from calculating. Computation is offloaded to SymPy, which returns the provably correct result.
The MathChatbot class in main.py and src/chatbot_math.py serves as the central agent.
(i) It routes the cleaned query from the Parser to the Solver.
(ii) It implements the Graceful Refusal logic, ensuring that if a query falls outside the scope (e.g., "What is a derivative?"), the system responds politely and professionally with the defined scope message, rather than crashing or guessing.
(i) Python 3.8+ (ii) A stable internet connection is required for the initial model and dependency downloads.
(i) Clone the repository and navigate to the project root.
(ii) Create and activate a virtual environment (venv).
(iii) Install all required dependencies:
Execute the main entry point file:
The following test cases demonstrate the system's proficiency in handling key algebraic challenges, proving the successful integration of NLU and Symbolic Computation.
(i) Query: Solve 4x - 8 = 0
(ii) Verification Point: The system correctly parses the implicit multiplication (4x becomes 4*x) and solves the equation.
(iii) Expected Output: The single solution for x is: 2.
(i) Query: Find roots of x^2 + 2x - 3 = 0
(ii) Verification Point: Proves the parser correctly handles the power syntax (x^2 becomes x**2).
(iii) Expected Output: The solutions for x are: 1 and -3.
(i) Query: 2x^2 + 5x - 3 = 0
(ii) Verification Point: Confirms the SymPy solver handles fractional roots exactly, and the formatter cleanly presents the
(iii) Expected Output: The solutions for x are:
(i) Query: What is the history of the number pi?
(ii) Verification Point: Proves the orchestrator correctly identifies out-of-scope intent and provides a professional response.
(iii) Expected Output: I am currently a specialized algebraic solver. I can only solve single-variable linear and quadratic equations...