Ever struggled with a math problem? In this blog post, we will explore the process of creating a Mathematics Problem Solver using Lyzr Automata, a powerful library for building AI-driven applications, and OpenAI's state-of-the-art language model. This application allows users to input mathematical problems, and the system will generate solutions along with detailed explanations.
Building the Mathematics Problem Solver
The Lyzr Maths Application demonstrates the potential of combining Streamlit and OpenAI for creating interactive educational tools.
Let's break down the key components of our implementation:
Import Libraries:
The code begins by importing necessary libraries:
streamlit: for building the web app interface.
lyzr_automata libraries: for defining AI models, agents, and tasks.
dotenv: for loading environment variables (API key).
pip install lyzr_automata python-dotenv streamlit
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent,Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from dotenv import load_dotenv
import os
Load API Key:
API key for OpenAI is loaded from a .env file using load_dotenv and os.getenv.
load_dotenv()
api = os.getenv("OPENAI_API_KEY")
User Input:
st.text_input creates a text box for users to enter their math problem.
query=st.text_input("Enter your Maths Problem: ")
Define OpenAI Model:
An OpenAIModel object is created using the loaded API key.
Model parameters like "gpt-4-turbo-preview" (replace with your desired model), temperature, and maximum output length are specified.
open_ai_text_completion_model = OpenAIModel(
api_key=api,
parameters={
"model": "gpt-4-turbo-preview",
"temperature": 0.2,
"max_tokens": 1500,
},
)
Define math_problem_solver Function:
This function handles the problem-solving process:
A math_agent object is created, representing the "maths expert."
A prompt defining the agent's role and expertise is set.
The prompt instructs the model to act as a reasoning agent, solve the user's problem logically, and provide a step-by-step explanation with a final answer in bullet points.
A Task object is defined with the model, agent, and the prompt incorporating the user's query.
A LinearSyncPipeline is created to execute the problem-solving task.
The function runs the pipeline and returns the model's output (answer).
def math_problem_solver(query):
math_agent = Agent(
role="maths expert",
prompt_persona="You are an Expert MATHEMATICIAN. Your task is to ASSIST in solving complex mathematical problems and to PROVIDE detailed explanations for mathematical concepts."
)
prompt=f"""You are a reasoning agent tasked with solving
the user's logic-based questions. Logically arrive at the solution, and be
factual. In your answers, clearly detail the steps involved and give the
final answer. Provide the response in bullet points.
Question {query} Answer"""
math_task = Task(
name="Maths Problem Solver",
model=open_ai_text_completion_model,
agent=math_agent,
instructions=prompt,
)
output = LinearSyncPipeline(
name="Maths Pipline",
completion_message="pipeline completed",
tasks=[
math_task
],
).run()
answer = output[0]['task_output']
return answer
Function Calling:
A button labeled "Solve" triggers the problem-solving process when clicked.
Clicking the button calls math_problem_solver with the user's query.
The returned answer (solution steps and final answer) is displayed using st.markdown.
if st.button("Solve"):
solution = math_problem_solver(query)
st.markdown(solution)
Running the App:
streamlit run <app_name.py>
This blog post provides a starting point for exploring AI-powered math problem solving with Streamlit and OpenAI. Feel free to experiment and extend the functionalities to create a robust and comprehensive learning aid!
Try it out:https://lyzr-math.streamlit.app/
For more information explore the website: Lyzr
Github Repo
Top comments (0)