File size: 8,881 Bytes
0a7d2c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# Math Problem Solver 
# A Streamlit application that solves math problems and searches for information using Google Gemma 2 model through Groq API

import streamlit as st
import os
from langchain_groq import ChatGroq
from langchain.agents import Tool, initialize_agent
from langchain.agents.agent_types import AgentType
from langchain.prompts import PromptTemplate
from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapper, DuckDuckGoSearchRun
from langchain_community.tools import WikipediaQueryRun, ArxivQueryRun
from langchain.callbacks import StreamlitCallbackHandler
from langchain.chains import LLMMathChain, LLMChain
from langchain.schema import SystemMessage
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Page configuration
st.set_page_config(
    page_title="Math Problem Solver",
    page_icon="🧮",
    layout="wide"
)

# App title and description
st.title("🧮 Math Problem Solver")
st.markdown("""
This application solves mathematical problems and provides step-by-step explanations using Groq's AI models.
It can handle various math topics including:
- Arithmetic calculations
- Algebra
- Geometry
- Statistics
- Word problems
- And more!
""")

# Sidebar configuration
st.sidebar.title("⚙️ Configuration")
st.sidebar.markdown("---")

# API key input - use environment variable if available
default_api_key = os.getenv("GROQ_API_KEY", "")
groq_api_key = st.sidebar.text_input(
    label="Groq API Key", 
    value=default_api_key,
    type="password",
    help="Get your API key from https://console.groq.com/keys"
)

# Model selection
model_options = {
    "gemma2-9b-it": "Gemma 2 9B (Fast)",
    "llama3-8b-8192": "Llama 3 8B (Balanced)",
    "llama3-70b-8192": "Llama 3 70B (Powerful)",
    "mixtral-8x7b-32768": "Mixtral 8x7B (Comprehensive)"
}
selected_model = st.sidebar.selectbox(
    "Select AI model:", 
    list(model_options.keys()), 
    format_func=lambda x: model_options[x],
    index=0
)

# Advanced options
with st.sidebar.expander("Advanced Options"):
    temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.2, step=0.1,
                           help="Lower values make responses more deterministic, higher values more creative")
    show_reasoning = st.checkbox("Show AI reasoning steps", value=True,
                                help="Display the AI's step-by-step reasoning process")

# Initialize LLM if API key is provided
if not groq_api_key:
    st.info("Please add your Groq API key in the sidebar to continue")
    st.sidebar.info("Don't have an API key? Sign up at [console.groq.com](https://console.groq.com)")
    
    # Example problem display
    st.markdown("### Example Problem")
    st.markdown("""
    ```
    I have 5 bananas and 7 grapes. I eat 2 bananas and give away 3 grapes. 
    Then I buy a dozen apples and 2 packs of blueberries. 
    Each pack of blueberries contains 25 berries. 
    How many total pieces of fruit do I have at the end?
    ```
    """)
    st.stop()

# Initialize LLM
try:
    llm = ChatGroq(
        model=selected_model, 
        groq_api_key=groq_api_key,
        temperature=temperature,
        streaming=True
    )
except Exception as e:
    st.error(f"Error initializing the AI model: {str(e)}")
    st.stop()

# Initialize tools
def setup_tools():
    """Initialize and return tools for the agent"""
    
    # Wikipedia tool
    wiki_wrapper = WikipediaAPIWrapper(top_k_results=2)
    wiki_tool = WikipediaQueryRun(api_wrapper=wiki_wrapper)
    
    # ArXiv tool for academic papers
    arxiv_wrapper = ArxivAPIWrapper(top_k_results=1)
    arxiv_tool = ArxivQueryRun(api_wrapper=arxiv_wrapper)
    
    # DuckDuckGo search tool
    search_tool = DuckDuckGoSearchRun()
    
    # Math calculation tool
    llm_math_chain = LLMMathChain.from_llm(llm=llm)
    calculator = Tool(
        func=llm_math_chain.run,
        name="Calculator",
        description="A tool for performing mathematical calculations. Input only mathematical expressions."
    )
    
    # Reasoning tool for detailed explanations
    prompt = """
    You're an expert mathematics teacher. Solve the following problem step by step:
    
    {question}
    
    First, identify what information is given and what is being asked.
    Then, lay out a clear strategy for solving the problem.
    Show your work carefully, with each step clearly labeled.
    Provide a final answer with appropriate units if applicable.
    
    Your solution:
    """
    prompt_template = PromptTemplate(
        input_variables=["question"],
        template=prompt
    )
    chain = LLMChain(llm=llm, prompt=prompt_template)
    reasoning_tool = Tool(
        name="MathReasoning",
        func=chain.run,
        description="A tool for solving math problems step-by-step with detailed explanations."
    )
    
    return [wiki_tool, arxiv_tool, search_tool, calculator, reasoning_tool]

# Set up the agent
def initialize_math_agent(tools):
    """Initialize the math problem solving agent"""
    system_message = SystemMessage(content="""
    You are an expert mathematics tutor and problem solver. Your goal is to:
    1. Solve mathematical problems accurately
    2. Provide clear, step-by-step explanations
    3. Use the appropriate tools for calculations and information gathering
    4. Organize your answers in a structured format with headings
    5. Include formulas and equations where relevant
    
    For math problems, always show your work and explain your thinking.
    For information queries, cite your sources where appropriate.
    """)
    
    return initialize_agent(
        tools=tools,
        llm=llm,
        agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
        verbose=show_reasoning,
        handle_parsing_errors=True,
        system_message=system_message,
        early_stopping_method="generate"
    )

# Initialize chat session state
if "messages" not in st.session_state:
    st.session_state["messages"] = [
        {"role": "assistant", "content": "Hi, I'm your Math Problem Solver! Ask me any math question or problem, and I'll solve it step-by-step."}
    ]

# Display chat history
for msg in st.session_state.messages:
    st.chat_message(msg["role"]).write(msg['content'])

# Example problems for the user to try
example_problems = [
    "Solve for x: 2x + 5 = 15",
    "Find the area of a circle with radius 4 cm",
    "If a train travels at 60 mph and takes 3 hours to reach its destination, how far did it travel?",
    "I have 5 bananas and 7 grapes. I eat 2 bananas and give away 3 grapes. Then I buy a dozen apples and 2 packs of blueberries. Each pack contains 25 berries. How many total pieces of fruit do I have at the end?"
]

# User input area with tabs for different input methods
tab1, tab2 = st.tabs(["Enter Your Question", "Try Examples"])

with tab1:
    question = st.text_area(
        "Type your math problem or question:",
        placeholder="Enter your math problem here...",
        height=100
    )
    solve_button = st.button("Solve Problem", type="primary")

with tab2:
    st.write("Select an example problem:")
    for i, example in enumerate(example_problems):
        if st.button(f"Example {i+1}", key=f"example_{i}"):
            question = example
            solve_button = True
            st.session_state.messages.append({"role": "user", "content": question})
            st.experimental_rerun()

# Process the request when button is clicked
if solve_button and question:
    # Add user message to chat
    st.session_state.messages.append({"role": "user", "content": question})
    st.chat_message("user").write(question)
    
    # Initialize tools and agent
    tools = setup_tools()
    assistant_agent = initialize_math_agent(tools)
    
    # Generate response with callback handler for streaming
    with st.chat_message("assistant"):
        st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=show_reasoning)
        try:
            response = assistant_agent.run(question, callbacks=[st_cb])
            st.write(response)
            
            # Add assistant response to chat
            st.session_state.messages.append({'role': 'assistant', "content": response})
        except Exception as e:
            error_msg = f"Error generating response: {str(e)}"
            st.error(error_msg)
            st.session_state.messages.append({'role': 'assistant', "content": error_msg})

# Add footer
st.sidebar.markdown("---")
st.sidebar.markdown("### About")
st.sidebar.info(
    "This app uses Groq's AI models to solve math problems "
    "and provide step-by-step explanations. It can handle a wide range "
    "of mathematical topics from basic arithmetic to advanced calculus."
)
st.sidebar.markdown("### Feedback")
st.sidebar.markdown("[Report an issue](https://github.com/yourusername/math-problem-solver/issues)")