Auto Generated Agent Chat: Using MathChat to Solve Math Problems
Using MathChat to Solve Math Problems
AG2 offers conversable agents powered by LLM, tool or human, which can be used to perform tasks collectively via automated chat. This framework allows tool use and human participation through multi-agent conversation. Please find documentation about this feature here.
MathChat is an experimental conversational framework for math problem
solving. In this notebook, we demonstrate how to use MathChat to solve
math problems. MathChat uses the AssistantAgent
and
MathUserProxyAgent
, which is similar to the usage of AssistantAgent
and UserProxyAgent
in other notebooks (e.g., Automated Task Solving
with Code Generation, Execution &
Debugging).
Essentially, MathUserProxyAgent
implements a different auto reply
mechanism corresponding to the MathChat prompts. You can find more
details in the paper An Empirical Study on Challenging Math Problem
Solving with GPT-4 or the
blogpost.
Some extra dependencies are needed for this notebook, which can be installed via pip:
For more information, please refer to the installation guide.
Set your API Endpoint
The
config_list_from_json
function loads a list of configurations from an environment variable or
a json file.
It first looks for environment variable “OAI_CONFIG_LIST” which needs to be a valid json string. If that variable is not found, it then looks for a json file named “OAI_CONFIG_LIST”. It filters the configs by models (you can filter by other keys as well).
The config list looks like the following:
If you open this notebook in colab, you can upload your files by clicking the file icon on the left panel and then choose “upload file” icon.
You can set the value of config_list in other ways you prefer, e.g., loading from a YAML file.
Construct agents for MathChat
We start by initializing the AssistantAgent
and MathUserProxyAgent
.
The system message needs to be set to “You are a helpful assistant.” for
MathChat. The detailed instructions are given in the user message. Later
we will use the MathUserProxyAgent.message_generator
to combine the
instructions and a math problem for an initial message to be sent to the
LLM assistant.
Example 1
Problem: Find all that satisfy the inequality
$(2x+10)(x+3)<(3x+9)(x+8)$
. Express your answer in interval notation.
Correct Solution: We have This inequality is satisfied if and only if
and are either both positive or both negative. Both
factors are positive for and both factors are negative for
$x<-14$
. When $-14<x<-3$
, one factor is positive and the other
negative, so their product is negative. Therefore, the range of that
satisfies the inequality is $ $.
Example 2
Problem: For what negative value of is there exactly one solution to the system of equations
Correct Solution: Setting the two expressions for equal to each other, it follows that . Re-arranging, . For there to be exactly one solution for , then the discriminant of the given quadratic must be equal to zero. Thus, , so . Taking the negative value, .
Example 3
Problem: Find all positive integer values of such that the equation only has roots that are real and rational. Express them in decreasing order, separated by commas.
Correct Solution: For the roots to be real and rational, the discriminant must be a perfect square. Therefore, must be a perfect square. The only positive perfect squares less than 49 are , , , , , and . The perfect squares that give a integer value of are , , and . Thus, we have the equations , , and . Solving, we get that the positive integer values of c are .
Using other prompts
MathChat allows different prompts that instruct the assistant to solve the problem.
Check out MathUserProxyAgent.message_generator
: - You may choose from
['default', 'python', 'two_tools']
for parameter prompt_type
. We
include two more prompts in the paper: 1. 'python'
is a simplified
prompt from the default prompt that uses Python only. 2. 'two_tools'
further allows the selection of Python or Wolfram Alpha based on this
simplified python
prompt. Note that this option requires a Wolfram
Alpha API key and put it in wolfram.txt
.
- You can also input your customized prompt if needed. Since this
mathproxyagent detects ‘’ as termination, you need to have a similar
termination sentence in the prompt: “If you get the answer, put the
answer in \boxed.”. If the customized is provided, the
prompt_type
will be ignored.
Example 4 (Use the “python” prompt):
Problem: If and , what is the value of ?
Correct Solution: Subtracting the two equations gives:
Multiplying this equation by 725 and subtracting this equation from gives So we can write as , which equals .
Example 5 (Use the “two_tools” prompt)
Problem: Find all numbers for which the graph of and the graph of intersect. Express your answer in interval notation.
Correct Solution: If these two graphs intersect then the points of
intersection occur when [x^2+a=ax,] or [x^2-ax+a=0.] This quadratic
has solutions exactly when the discriminant is nonnegative:
[(-a)^2-4a.] This simplifies to [a(a-4).] This quadratic (in ) is
nonnegative when and are either both or both .
This is true for in Therefore the
line and quadratic intersect exactly when is in
$\boxed{(-\infty,0]\cup[4,\infty)}$
.