mirror of
https://github.com/microsoft/autogen.git
synced 2025-07-21 07:51:55 +00:00

* mathcaht implementation * code forrmat * update readme * update openai.yml * update openai.yml * update openai.yml
938 lines
42 KiB
Plaintext
938 lines
42 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<a href=\"https://colab.research.google.com/github/microsoft/FLAML/blob/main/notebook/autogen_agent_MathChat.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Using MathChat to solve math problems\n",
|
|
"\n",
|
|
"MathChat is a convesational 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 (More details in `autogen_agent_auto_feedback_from_code_execution.ipynb`). Essentially, `MathUserProxyAgent` implements a different auto reply mechanism corresponding to the MathChat prompts. The original implementation and exeperiments of MathChat are in this [branch](https://github.com/kevin666aa/FLAML/tree/gpt_math_solver/flaml/autogen/math), and you can find more details in our paper [An Empirical Study on Challenging Math Problem Solving with GPT-4](https://arxiv.org/abs/2306.01337).\n",
|
|
"\n",
|
|
"## Requirements\n",
|
|
"\n",
|
|
"FLAML requires `Python>=3.7`. To run this notebook example, please install flaml with the [mathchat] option.\n",
|
|
"```bash\n",
|
|
"pip install flaml[mathchat]\n",
|
|
"```"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# %pip install flaml[mathchat]==2.0.0rc1"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Set your API Endpoint\n",
|
|
"\n",
|
|
"The [`config_list_gpt4_gpt35`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_gpt4_gpt35) function tries to create a list of gpt-4 and gpt-3.5 configurations using Azure OpenAI endpoints and OpenAI endpoints. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n",
|
|
"\n",
|
|
"- OpenAI API key: os.environ[\"OPENAI_API_KEY\"] or `openai_api_key_file=\"key_openai.txt\"`.\n",
|
|
"- Azure OpenAI API key: os.environ[\"AZURE_OPENAI_API_KEY\"] or `aoai_api_key_file=\"key_aoai.txt\"`. Multiple keys can be stored, one per line.\n",
|
|
"- Azure OpenAI API base: os.environ[\"AZURE_OPENAI_API_BASE\"] or `aoai_api_base_file=\"base_aoai.txt\"`. Multiple bases can be stored, one per line.\n",
|
|
"\n",
|
|
"It's OK to have only the OpenAI API key, or only the Azure OpenAI API key + base.\n",
|
|
"\n",
|
|
"If you open this notebook in google colab, you can upload your files by click the file icon on the left panel and then choose \"upload file\" icon.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from flaml import oai\n",
|
|
"\n",
|
|
"config_list = oai.config_list_gpt4_gpt35()"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The config list looks like the following:\n",
|
|
"```python\n",
|
|
"config_list = [\n",
|
|
" {\n",
|
|
" 'model': 'gpt-4',\n",
|
|
" 'api_key': '<your OpenAI API key here>',\n",
|
|
" }, # only if OpenAI API key is found\n",
|
|
" {\n",
|
|
" 'model': 'gpt-4',\n",
|
|
" 'api_key': '<your first Azure OpenAI API key here>',\n",
|
|
" 'api_base': '<your first Azure OpenAI API base here>',\n",
|
|
" 'api_type': 'azure',\n",
|
|
" 'api_version': '2023-03-15-preview',\n",
|
|
" }, # only if the at least one Azure OpenAI API key is found\n",
|
|
" {\n",
|
|
" 'model': 'gpt-4',\n",
|
|
" 'api_key': '<your second Azure OpenAI API key here>',\n",
|
|
" 'api_base': '<your second Azure OpenAI API base here>',\n",
|
|
" 'api_type': 'azure',\n",
|
|
" 'api_version': '2023-03-15-preview',\n",
|
|
" }, # only if the second Azure OpenAI API key is found\n",
|
|
"]\n",
|
|
"```\n",
|
|
"\n",
|
|
"You can directly override it if the above function returns an empty list, i.e., it doesn't find the keys in the specified locations."
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Construct agents for MathChat\n",
|
|
"\n",
|
|
"We start by initialzing 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.generate_init_prompt` to combine the instructions and a math problem for an initial prompt to be sent to the LLM assistant."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from flaml.autogen.agent.assistant_agent import AssistantAgent\n",
|
|
"from flaml.autogen.agent.math_user_proxy_agent import MathUserProxyAgent\n",
|
|
"\n",
|
|
"oai.ChatCompletion.start_logging()\n",
|
|
"\n",
|
|
"# 1. create an AssistantAgent instance named \"assistant\"\n",
|
|
"assistant = AssistantAgent(\n",
|
|
" name=\"assistant\", \n",
|
|
" system_message=\"You are a helpful assistant.\",\n",
|
|
" request_timeout=600, \n",
|
|
" seed=42, \n",
|
|
" config_list=config_list)\n",
|
|
"\n",
|
|
"# 2. create the MathUserProxyAgent instance named \"mathproxyagent\"\n",
|
|
"# By default, the human_input_mode is \"NEVER\", which means the agent will not ask for human input.\n",
|
|
"mathproxyagent = MathUserProxyAgent(\n",
|
|
" name=\"MathChatAgent\", \n",
|
|
" human_input_mode=\"NEVER\",\n",
|
|
" use_docker=False)"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Example 1\n",
|
|
"\n",
|
|
"Problem: Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\n",
|
|
"\n",
|
|
"Correct Solution: \n",
|
|
"We have \\begin{align*} (2x+10)(x+3)&<(3x+9)(x+8) \\quad \\Rightarrow\n",
|
|
"\\\\ 2(x+5)(x+3)&<3(x+3)(x+8) \\quad \\Rightarrow\n",
|
|
"\\\\ 2(x+5)(x+3)-3(x+3)(x+8)&<0 \\quad \\Rightarrow\n",
|
|
"\\\\ (2x+10-(3x+24))(x+3)&<0 \\quad \\Rightarrow\n",
|
|
"\\\\ (-x-14)(x+3)&<0 \\quad \\Rightarrow\n",
|
|
"\\\\ (x+14)(x+3)&>0.\n",
|
|
"\\end{align*} This inequality is satisfied if and only if $(x+14)$ and $(x+3)$ are either both positive or both negative. Both factors are positive for $x>-3$ 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 $x$ that satisfies the inequality is $ \\boxed{(-\\infty, -14)\\cup(-3,\\infty)} $."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"Let's use Python to solve a math problem.\n",
|
|
"\n",
|
|
"Query requirements:\n",
|
|
"You should always use the 'print' function for the output and use fractions/radical forms instead of decimals.\n",
|
|
"You can use packages like sympy to help you.\n",
|
|
"You must follow the formats below to write your code:\n",
|
|
"```python\n",
|
|
"# your code\n",
|
|
"```\n",
|
|
"\n",
|
|
"First state the key idea to solve the problem. You may choose from three ways to solve the problem:\n",
|
|
"Case 1: If the problem can be solved with Python code directly, please write a program to solve it. You can enumerate all possible arrangements if needed.\n",
|
|
"Case 2: If the problem is mostly reasoning, you can solve it by yourself directly.\n",
|
|
"Case 3: If the problem cannot be handled in the above two ways, please follow this process:\n",
|
|
"1. Solve the problem step by step (do not over-divide the steps).\n",
|
|
"2. Take out any queries that can be asked through Python (for example, any calculations or equations that can be calculated).\n",
|
|
"3. Wait for me to give the results.\n",
|
|
"4. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.\n",
|
|
"\n",
|
|
"After all the queries are run and you get the answer, put the answer in \\boxed{}.\n",
|
|
"\n",
|
|
"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"The key idea to solve this problem is to find the critical points, which are the points where the inequality is equal. Then, test the intervals between the critical points to find the interval for which the inequality holds true.\n",
|
|
"\n",
|
|
"We start by finding the critical points:\n",
|
|
"\n",
|
|
"1. Set the given inequality to be equal: $(2x+10)(x+3)=(3x+9)(x+8)$\n",
|
|
"2. Expand both sides and simplify\n",
|
|
"3. Bring all terms to one side of the equation\n",
|
|
"4. Solve for `x`\n",
|
|
"\n",
|
|
"Then we'll test the intervals between the critical points.\n",
|
|
" 1. Test a point less than the smallest critical point\n",
|
|
" 2. Test a point between the critical points\n",
|
|
" 3. Test a point greater than the largest critical point\n",
|
|
"\n",
|
|
"Now we will find the critical points using Python:\n",
|
|
"```python\n",
|
|
"from sympy import symbols, Eq, solve\n",
|
|
"\n",
|
|
"x = symbols(\"x\")\n",
|
|
"lhs = (2*x + 10)*(x + 3)\n",
|
|
"rhs = (3*x + 9)*(x + 8)\n",
|
|
"equation = Eq(lhs, rhs)\n",
|
|
"\n",
|
|
"critical_points = solve(equation, x)\n",
|
|
"critical_points\n",
|
|
"```\n",
|
|
"\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"[-14, -3]\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"We found the critical points to be $x=-14$ and $x=-3$. Now we'll test the intervals between these critical points as well as points before and after the critical points.\n",
|
|
"\n",
|
|
"The intervals are:\n",
|
|
"\n",
|
|
"1. $x < -14$\n",
|
|
"2. $-14 < x < -3$\n",
|
|
"3. $x > -3$\n",
|
|
"\n",
|
|
"Next, we will test a point in each interval using Python and the original inequality:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"from sympy import symbols\n",
|
|
"\n",
|
|
"x = symbols(\"x\")\n",
|
|
"inequality = (2*x + 10)*(x + 3) < (3*x + 9)*(x + 8)\n",
|
|
"\n",
|
|
"test_points = [-15, -5, 0]\n",
|
|
"intervals = []\n",
|
|
"\n",
|
|
"for point in test_points:\n",
|
|
" if inequality.subs(x, point):\n",
|
|
" intervals.append(point)\n",
|
|
"\n",
|
|
"intervals\n",
|
|
"```\n",
|
|
"\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"[-15, 0]\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"We have tested the intervals and found that the inequality holds true for:\n",
|
|
"\n",
|
|
"1. $x < -14$ (test point: $-15$)\n",
|
|
"3. $x > -3$ (test point: $0$)\n",
|
|
"\n",
|
|
"Therefore, the solution to the inequality $(2x+10)(x+3)<(3x+9)(x+8)$ is:\n",
|
|
"\n",
|
|
"$x \\in \\boxed{(-\\infty, -14) \\cup (-3, \\infty)}$\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# reset the assistant. Always reset the assistant before starting a new conversation.\n",
|
|
"assistant.reset()\n",
|
|
"\n",
|
|
"# given a math problem, we use the mathproxyagent to generate a prompt to be sent to the assistant as the initial message.\n",
|
|
"# the assistant receives the message and generates a response. The response will be sent back to the mathproxyagent for processing.\n",
|
|
"# The conversation continues until the termination condition is met, in MathChat, the termination condition is the detect of \"\\boxed{}\" in the response.\n",
|
|
"math_problem = \"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\"\n",
|
|
"assistant.receive(\n",
|
|
" message=mathproxyagent.generate_init_prompt(math_problem),\n",
|
|
" sender=mathproxyagent,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Example 2\n",
|
|
"Problem: For what negative value of $k$ is there exactly one solution to the system of equations \\begin{align*}\n",
|
|
"y &= 2x^2 + kx + 6 \\\\\n",
|
|
"y &= -x + 4?\n",
|
|
"\\end{align*}\n",
|
|
"\n",
|
|
"Correct Solution: Setting the two expressions for $y$ equal to each other, it follows that $2x^2 + kx + 6 = -x + 4$. Re-arranging, $2x^2 + (k+1)x + 2 = 0$. For there to be exactly one solution for $x$, then the discriminant of the given quadratic must be equal to zero. Thus, $(k+1)^2 - 4 \\cdot 2 \\cdot 2 = (k+1)^2 - 16 = 0$, so $k+1 = \\pm 4$. Taking the negative value, $k = \\boxed{-5}$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 27,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"**** assistant received message from MathChatAgent ****\n",
|
|
"\n",
|
|
"\n",
|
|
"Let's use Python to solve a math problem.\n",
|
|
"\n",
|
|
"Query requirements:\n",
|
|
"You should always use the 'print' function for the output and use fractions/radical forms instead of decimals.\n",
|
|
"You can use packages like sympy to help you.\n",
|
|
"You must follow the formats below to write your code:\n",
|
|
"```python\n",
|
|
"# your code\n",
|
|
"```\n",
|
|
"\n",
|
|
"You may choose from three ways to solve the problem:\n",
|
|
"Case 1: If the problem can be solved with Python code directly, please write a program to solve it. You can enumerate all possible arrangements if needed.\n",
|
|
"Case 2: If the problem cannot be handled in the above two ways, please follow this process:\n",
|
|
"1. Solve the problem step by step (do not over-divide the steps).\n",
|
|
"2. Take out any queries that can be asked through Python (for example, any calculations or equations that can be calculated).\n",
|
|
"3. Wait for me to give the results.\n",
|
|
"4. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.\n",
|
|
"\n",
|
|
"After all the queries are run and you get the answer, put the answer in \\boxed{}.\n",
|
|
"\n",
|
|
"Problem: For what negative value of $k$ is there exactly one solution to the system of equations \\begin{align*}\n",
|
|
"y &= 2x^2 + kx + 6 \\\\\n",
|
|
"y &= -x + 4?\n",
|
|
"\\end{align*}\n",
|
|
"\n",
|
|
"**** MathChatAgent received message from assistant ****\n",
|
|
"\n",
|
|
"To find the value of k for which there is exactly one solution to the system, we need to solve the two equations for y and set them equal to each other. Then, we will find the discriminant and set it equal to 0. The discriminant will be equal to 0 if there is exactly one solution.\n",
|
|
"\n",
|
|
"Step 1: Set the equations equal to each other\n",
|
|
"$$2x^2 + kx + 6 = -x + 4$$\n",
|
|
"\n",
|
|
"Step 2: Rearrange the equation to get a quadratic equation\n",
|
|
"We can rearrange the equation to get a quadratic equation in terms of x:\n",
|
|
"$$2x^2 + (k+1)x + 2 = 0$$\n",
|
|
"\n",
|
|
"Step 3: Calculate the discriminant\n",
|
|
"The discriminant of a quadratic equation $ax^2 + bx + c = 0$ is given by $\\Delta = b^2 - 4ac$. In our case, $a=2$, $b=k+1$, and $c=2$.\n",
|
|
"\n",
|
|
"Now, let's compute the discriminant using Python:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"from sympy import symbols, Eq, solve\n",
|
|
"\n",
|
|
"a, b, c, k = 2, k+1, 2, symbols('k')\n",
|
|
"delta = b**2 - 4*a*c\n",
|
|
"delta\n",
|
|
"```\n",
|
|
"\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
"**** assistant received message from MathChatAgent ****\n",
|
|
"\n",
|
|
"Error: Traceback (most recent call last):\n",
|
|
" a, b, c, k = 2, k+1, 2, symbols('k')\n",
|
|
"NameError: name 'k' is not defined\n",
|
|
"\n",
|
|
"\n",
|
|
"**** MathChatAgent received message from assistant ****\n",
|
|
"\n",
|
|
"Apologies for the mistake. I made an error in the code. Let me correct it and compute the discriminant again. Here's the corrected code:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"from sympy import symbols, Eq, solve\n",
|
|
"\n",
|
|
"k = symbols('k')\n",
|
|
"a, b, c = 2, k + 1, 2\n",
|
|
"delta = b**2 - 4*a*c\n",
|
|
"delta\n",
|
|
"```\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
"**** assistant received message from MathChatAgent ****\n",
|
|
"\n",
|
|
"(k + 1)**2 - 16\n",
|
|
"\n",
|
|
"\n",
|
|
"**** MathChatAgent received message from assistant ****\n",
|
|
"\n",
|
|
"Yes, that's correct. So the discriminant is $\\Delta = (k+1)^2 - 16$. Since we want exactly one solution, the discriminant must be equal to 0:\n",
|
|
"\n",
|
|
"$$(k+1)^2 - 16 = 0$$\n",
|
|
"\n",
|
|
"Now let's solve this equation for k using Python:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"k_val = solve(delta, k)\n",
|
|
"k_val\n",
|
|
"```\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
"**** assistant received message from MathChatAgent ****\n",
|
|
"\n",
|
|
"[-5, 3]\n",
|
|
"\n",
|
|
"\n",
|
|
"**** MathChatAgent received message from assistant ****\n",
|
|
"\n",
|
|
"The solutions for k are $k=-5$ and $k=3$. However, we are looking for the negative value of k, so the answer is:\n",
|
|
"\n",
|
|
"$$\\boxed{k = -5}$$\n",
|
|
"\n",
|
|
"For k = -5, there is exactly one solution to the given system of equations.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"assistant.reset()\n",
|
|
"\n",
|
|
"math_problem = \"For what negative value of $k$ is there exactly one solution to the system of equations \\\\begin{align*}\\ny &= 2x^2 + kx + 6 \\\\\\\\\\ny &= -x + 4?\\n\\\\end{align*}\"\n",
|
|
"assistant.receive(\n",
|
|
" mathproxyagent.generate_init_prompt(math_problem),\n",
|
|
" mathproxyagent,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Example 3\n",
|
|
"Problem: Find all positive integer values of $c$ such that the equation $x^2-7x+c=0$ only has roots that are real and rational. Express them in decreasing order, separated by commas.\n",
|
|
"\n",
|
|
"Correct Solution: For the roots to be real and rational, the discriminant must be a perfect square. Therefore, $(-7)^2-4 \\cdot 1 \\cdot c = 49-4c$ must be a perfect square. The only positive perfect squares less than 49 are $1$, $4$, $9$, $16$, $25$, and $36$. The perfect squares that give a integer value of $c$ are $1$, $9$, and $25$. Thus, we have the equations $49-4c=1$, $49-4c=9$, and $49-4c=25$. Solving, we get that the positive integer values of c are $\\boxed{12, 10, 6}$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 30,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"**** assistant received message from MathChatAgent ****\n",
|
|
"\n",
|
|
"\n",
|
|
"Let's use Python to solve a math problem.\n",
|
|
"\n",
|
|
"Query requirements:\n",
|
|
"You should always use the 'print' function for the output and use fractions/radical forms instead of decimals.\n",
|
|
"You can use packages like sympy to help you.\n",
|
|
"You must follow the formats below to write your code:\n",
|
|
"```python\n",
|
|
"# your code\n",
|
|
"```\n",
|
|
"\n",
|
|
"You may choose from three ways to solve the problem:\n",
|
|
"Case 1: If the problem can be solved with Python code directly, please write a program to solve it. You can enumerate all possible arrangements if needed.\n",
|
|
"Case 2: If the problem cannot be handled in the above two ways, please follow this process:\n",
|
|
"1. Solve the problem step by step (do not over-divide the steps).\n",
|
|
"2. Take out any queries that can be asked through Python (for example, any calculations or equations that can be calculated).\n",
|
|
"3. Wait for me to give the results.\n",
|
|
"4. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.\n",
|
|
"\n",
|
|
"After all the queries are run and you get the answer, put the answer in \\boxed{}.\n",
|
|
"\n",
|
|
"Problem: Find all positive integer values of $c$ such that the equation $x^2-7x+c=0$ only has roots that are real and rational. Express them in decreasing order, separated by commas.\n",
|
|
"\n",
|
|
"**** MathChatAgent received message from assistant ****\n",
|
|
"\n",
|
|
"To solve this problem, we'll use the discriminant formula, which is $\\Delta = b^2 - 4ac$. For the roots to be real and rational, the discriminant must be non-negative and a perfect square. In our case, the equation is $x^2 - 7x + c = 0$, so $a = 1, b = -7, c = c$.\n",
|
|
"\n",
|
|
"The discriminant formula becomes $\\Delta = (-7)^2 - 4(1)(c) = 49 - 4c$. We want to find all possible positive integer values of $c$ such that $\\Delta \\geq 0$ and is a perfect square.\n",
|
|
"\n",
|
|
"```python\n",
|
|
"from sympy import *\n",
|
|
"\n",
|
|
"# List for storing c values that satisfy the conditions\n",
|
|
"c_values = []\n",
|
|
"\n",
|
|
"# Check for all c values from 1 to 49\n",
|
|
"for c in range(1, 50):\n",
|
|
" delta = 49 - 4*c\n",
|
|
" # Condition for the discriminant to be non-negative and a perfect square\n",
|
|
" if delta >= 0 and sqrt(delta).is_integer():\n",
|
|
" c_values.append(c)\n",
|
|
"\n",
|
|
"# Reverse the list to get decreasing order\n",
|
|
"c_values.reverse()\n",
|
|
"\n",
|
|
"# Printing the result\n",
|
|
"print(\"The values of c that make the roots real and rational are:\", \",\".join(map(str, c_values)))\n",
|
|
"```\n",
|
|
"\n",
|
|
"Let's run this code to find the valid values of $c$.\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
"**** assistant received message from MathChatAgent ****\n",
|
|
"\n",
|
|
"Error: Traceback (most recent call last):\n",
|
|
" if delta >= 0 and sqrt(delta).is_integer():\n",
|
|
"TypeError: 'bool' object is not callable\n",
|
|
"\n",
|
|
"\n",
|
|
"**** MathChatAgent received message from assistant ****\n",
|
|
"\n",
|
|
"I apologize for the error in my code. I used the wrong function to check if the square root is an integer. I will correct the code and try again.\n",
|
|
"\n",
|
|
"```python\n",
|
|
"import math\n",
|
|
"\n",
|
|
"# List for storing c values that satisfy the conditions\n",
|
|
"c_values = []\n",
|
|
"\n",
|
|
"# Check for all c values from 1 to 49\n",
|
|
"for c in range(1, 50):\n",
|
|
" delta = 49 - 4*c\n",
|
|
" # Condition for the discriminant to be non-negative and a perfect square\n",
|
|
" if delta >= 0 and (math.sqrt(delta) % 1 == 0):\n",
|
|
" c_values.append(c)\n",
|
|
"\n",
|
|
"# Reverse the list to get decreasing order\n",
|
|
"c_values.reverse()\n",
|
|
"\n",
|
|
"# Printing the result\n",
|
|
"print(\"The values of c that make the roots real and rational are:\", \",\".join(map(str, c_values)))\n",
|
|
"```\n",
|
|
"\n",
|
|
"Let's run this corrected code to find the valid values of $c$.\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
"**** assistant received message from MathChatAgent ****\n",
|
|
"\n",
|
|
"The values of c that make the roots real and rational are: 12,10,6\n",
|
|
"\n",
|
|
"\n",
|
|
"**** MathChatAgent received message from assistant ****\n",
|
|
"\n",
|
|
"Great! So, the positive integer values of $c$ such that the equation $x^2 - 7x + c = 0$ only has roots that are real and rational are $\\boxed{12, 10, 6}$ in decreasing order.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"assistant.reset()\n",
|
|
"\n",
|
|
"math_problem = \"Find all positive integer values of $c$ such that the equation $x^2-7x+c=0$ only has roots that are real and rational. Express them in decreasing order, separated by commas.\"\n",
|
|
"assistant.receive(\n",
|
|
" mathproxyagent.generate_init_prompt(math_problem),\n",
|
|
" mathproxyagent,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"------------------------------------\n",
|
|
"### Using other prompts\n",
|
|
"\n",
|
|
"MathChat allows different prompts that instruct assistant to solve the problem.\n",
|
|
"\n",
|
|
"Check out `MathUserProxyAgent.generate_init_prompt(problem, prompt_type='default', customized_prompt=None)`:\n",
|
|
"- You may choose from `['default', 'python', 'two_tools']` for parameter `prompt_type`. We include two more prompts in the paper: \n",
|
|
" 1. `'python'` is a simplified prompt from the default prompt that uses Python only. \n",
|
|
" 2. `'two_tools'` further allows the selection of Python or Wolfram Alpha based on this simplified `python` prompt. Note that this option requries a Wolfram Alpha API key and put it in `wolfram.txt`.\n",
|
|
"\n",
|
|
"- You can also input your customized prompt if needed: `mathproxyagent.generate_init_prompt(problem, customized_prompt=\"Your customized prompt\")`. Since this mathproxyagent detects '\\boxed{}' 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.\n",
|
|
"\n",
|
|
"\n",
|
|
"### Example 4 (Use the \"python\" prompt):\n",
|
|
"\n",
|
|
"Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\n",
|
|
"\n",
|
|
"Correct Solution: Subtracting the two equations gives: \n",
|
|
"\\begin{align*}\n",
|
|
"(729x+731y)-(725x+727y) &= 1508-1500\\\\\n",
|
|
"\\Rightarrow\\qquad 4x+4y &= 8\\\\\n",
|
|
"\\Rightarrow\\qquad x+y &= 2.\n",
|
|
"\\end{align*}\n",
|
|
"\n",
|
|
"Multiplying this equation by 725 and subtracting this equation from $725x+727y=1500$ gives \\begin{align*}\n",
|
|
"(725x+727y) - 725(x+y) &= 1500-725(x+y) \\implies \\\\\n",
|
|
"2y &= 50.\n",
|
|
"\\end{align*}So we can write $x-y$ as $(x+y) - 2y$, which equals $2 - 50 = \\boxed{-48}$.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"Let's use Python to solve a math problem.\n",
|
|
"\n",
|
|
"Query requirements:\n",
|
|
"You should always use the 'print' function for the output and use fractions/radical forms instead of decimals.\n",
|
|
"You can use packages like sympy to help you.\n",
|
|
"You must follow the formats below to write your code:\n",
|
|
"```python\n",
|
|
"# your code\n",
|
|
"```\n",
|
|
"\n",
|
|
"Please follow this process:\n",
|
|
"1. Solve the problem step by step (do not over-divide the steps).\n",
|
|
"2. Take out any queries that can be asked through Python (for example, any calculations or equations that can be calculated).\n",
|
|
"3. Wait for me to give the results.\n",
|
|
"4. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.\n",
|
|
"\n",
|
|
"After all the queries are run and you get the answer, put the answer in \\boxed{}.\n",
|
|
"\n",
|
|
"Problem: Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"First, let's solve these two linear equations step by step. We can start by eliminating one variable by subtracting one equation from the other.\n",
|
|
"\n",
|
|
"So, let's subtract the first equation from the second:\n",
|
|
"\n",
|
|
"(729x + 731y) - (725x + 727y) = (1508 - 1500)\n",
|
|
"\n",
|
|
"Now let's simplify this:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"from sympy import symbols, Eq, simplify\n",
|
|
"\n",
|
|
"x, y = symbols('x y')\n",
|
|
"eq1 = Eq(729*x + 731*y - (725*x + 727*y), 1508 - 1500)\n",
|
|
"eq1_simplified = simplify(eq1)\n",
|
|
"eq1_simplified\n",
|
|
"```\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"Eq(x + y, 2)\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"Now we have the simplified equation:\n",
|
|
"\n",
|
|
"x + y = 2\n",
|
|
"\n",
|
|
"Next, we need to use this equation to eliminate either x or y from one of the given equations. Let's eliminate y by expressing y in terms of x, and then substitute it into the second given equation.\n",
|
|
"\n",
|
|
"y = 2 - x\n",
|
|
"\n",
|
|
"Now let's substitute this into the second equation:\n",
|
|
"\n",
|
|
"729x + 731(2 - x) = 1508\n",
|
|
"\n",
|
|
"Now let's simplify this equation:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"eq2 = Eq(729*x + 731*(2 - x), 1508)\n",
|
|
"eq2_simplified = simplify(eq2)\n",
|
|
"eq2_simplified\n",
|
|
"```\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"Eq(x, -23)\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"Now we have found the value of x:\n",
|
|
"\n",
|
|
"x = -23\n",
|
|
"\n",
|
|
"We can now find the value of y using the simplified equation x + y = 2:\n",
|
|
"\n",
|
|
"y = 2 - x\n",
|
|
"\n",
|
|
"Let's calculate the value of y:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"y_value = 2 - (-23)\n",
|
|
"y_value\n",
|
|
"```\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"25\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"Now we have found the value of y:\n",
|
|
"\n",
|
|
"y = 25\n",
|
|
"\n",
|
|
"Now we can find the value of x - y:\n",
|
|
"\n",
|
|
"x - y = -23 - 25\n",
|
|
"\n",
|
|
"Let's calculate the value of x - y:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"x_minus_y = -23 - 25\n",
|
|
"x_minus_y\n",
|
|
"```\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"-48\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"So, the value of x - y is:\n",
|
|
"\n",
|
|
"x - y = -48\n",
|
|
"\n",
|
|
"Thus, the final answer is:\n",
|
|
"\n",
|
|
"$\\boxed{-48}$\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"assistant.reset() # clear LLM assistant's message history\n",
|
|
"\n",
|
|
"# we set the prompt_type to \"python\", which is a simplied version of the default prompt.\n",
|
|
"math_problem = \"Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\"\n",
|
|
"assistant.receive(\n",
|
|
" mathproxyagent.generate_init_prompt(math_problem, prompt_type=\"python\"),\n",
|
|
" mathproxyagent,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Example 5 (Use the \"two_tools\" prompt)\n",
|
|
"\n",
|
|
"Problem: Find all numbers $a$ for which the graph of $y=x^2+a$ and the graph of $y=ax$ intersect. Express your answer in interval notation.\n",
|
|
"\n",
|
|
"\n",
|
|
"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-4\\cdot1\\cdot a\\geq0.\\] This simplifies to \\[a(a-4)\\geq0.\\] This quadratic (in $a$) is nonnegative when $a$ and $a-4$ are either both $\\ge 0$ or both $\\le 0$. This is true for $a$ in $$(-\\infty,0]\\cup[4,\\infty).$$ Therefore the line and quadratic intersect exactly when $a$ is in $\\boxed{(-\\infty,0]\\cup[4,\\infty)}$.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"Let's use two tools (Python and Wolfram alpha) to solve a math problem.\n",
|
|
"\n",
|
|
"Query requirements:\n",
|
|
"You must follow the formats below to write your query:\n",
|
|
"For Wolfram Alpha:\n",
|
|
"```wolfram\n",
|
|
"# one wolfram query\n",
|
|
"```\n",
|
|
"For Python:\n",
|
|
"```python\n",
|
|
"# your code\n",
|
|
"```\n",
|
|
"When using Python, you should always use the 'print' function for the output and use fractions/radical forms instead of decimals. You can use packages like sympy to help you.\n",
|
|
"When using wolfram, give one query in each code block.\n",
|
|
"\n",
|
|
"Please follow this process:\n",
|
|
"1. Solve the problem step by step (do not over-divide the steps).\n",
|
|
"2. Take out any queries that can be asked through Python or Wolfram Alpha, select the most suitable tool to be used (for example, any calculations or equations that can be calculated).\n",
|
|
"3. Wait for me to give the results.\n",
|
|
"4. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.\n",
|
|
"\n",
|
|
"After all the queries are run and you get the answer, put the final answer in \\boxed{}.\n",
|
|
"\n",
|
|
"Problem: Find all numbers $a$ for which the graph of $y=x^2+a$ and the graph of $y=ax$ intersect. Express your answer in interval notation.\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"First, we need to find the intersection points of the two given equations, i.e., when $y = x^2 + a$ and $y = ax$ are equal. So, we will set the two equations equal to each other and solve for $x$:\n",
|
|
"\n",
|
|
"$x^2 + a = ax$\n",
|
|
"\n",
|
|
"Now, let's rearrange the equation to get a quadratic equation in terms of $x$:\n",
|
|
"\n",
|
|
"$x^2 - ax + a = 0$\n",
|
|
"\n",
|
|
"We can use the quadratic formula to solve for $x$ in terms of $a$:\n",
|
|
"\n",
|
|
"$x = \\frac{-(-a) \\pm \\sqrt{(-a)^2 - 4(1)(a)}}{2(1)}$\n",
|
|
"\n",
|
|
"```wolfram\n",
|
|
"x = (a + sqrt(a^2 - 4*a)) / 2, x = (a - sqrt(a^2 - 4*a)) / 2\n",
|
|
"```\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"Assumption: {x = 1/2 (a + sqrt(a^2 - 4 a)), x = 1/2 (a - sqrt(a^2 - 4 a))} \n",
|
|
"Answer: ans 0: a = 0, x = 0\n",
|
|
"ans 1: a = 4, x = 2\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"We found two solutions for $x$ in terms of $a$: \n",
|
|
"\n",
|
|
"$x = \\frac{1}{2}(a + \\sqrt{a^2 - 4a})$\n",
|
|
"\n",
|
|
"$x = \\frac{1}{2}(a - \\sqrt{a^2 - 4a})$\n",
|
|
"\n",
|
|
"And we have two possible answers: \n",
|
|
"\n",
|
|
"1) When $a=0$, $x=0$.\n",
|
|
"\n",
|
|
"2) When $a=4$, $x=2$.\n",
|
|
"\n",
|
|
"Now, we need to find the interval of $a$ for which the two graphs intersect. As we know the roots of the quadratic equation, we can look for the conditions under which the discriminant is greater or equal to zero:\n",
|
|
"\n",
|
|
"$D = a^2 - 4a \\ge 0$\n",
|
|
"\n",
|
|
"Let's solve this inequality to find the interval of $a$:\n",
|
|
"\n",
|
|
"```wolfram\n",
|
|
"solve a^2 - 4a >= 0 for a\n",
|
|
"```\n",
|
|
"\n",
|
|
">>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"MathChatAgent (to assistant):\n",
|
|
"Assumption: solve a^2 - 4 a>=0 for a \n",
|
|
"Answer: ans 0: a<=0\n",
|
|
"ans 1: a>=4\n",
|
|
"\n",
|
|
" -------------------------------------------------------------------------------- \n",
|
|
"\n",
|
|
"assistant (to MathChatAgent):\n",
|
|
"We found the intervals for $a$ that satisfy the inequality $a^2 - 4a \\ge 0$:\n",
|
|
"\n",
|
|
"1) $a \\le 0$\n",
|
|
"\n",
|
|
"2) $a \\ge 4$\n",
|
|
"\n",
|
|
"So, the graph of $y=x^2+a$ and the graph of $y=ax$ intersect when $a$ is in these intervals. Therefore, the final answer is:\n",
|
|
"\n",
|
|
"$\\boxed{(-\\infty, 0] \\cup [4, \\infty)}$\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"assistant.reset() # clear LLM assistant's message history\n",
|
|
"\n",
|
|
"# The wolfram alpha appid is required for this example (the assistant may choose to query Wolfram Alpha).\n",
|
|
"import os\n",
|
|
"if \"WOLFRAM_ALPHA_APPID\" not in os.environ:\n",
|
|
" os.environ[\"WOLFRAM_ALPHA_APPID\"] = open(\"wolfram.txt\").read().strip()\n",
|
|
"\n",
|
|
"# we set the prompt_type to \"two_tools\", which allows the assistant to select wolfram alpha when necessary.\n",
|
|
"math_problem = \"Find all numbers $a$ for which the graph of $y=x^2+a$ and the graph of $y=ax$ intersect. Express your answer in interval notation.\"\n",
|
|
"assistant.receive(\n",
|
|
" mathproxyagent.generate_init_prompt(math_problem, prompt_type=\"two_tools\"),\n",
|
|
" mathproxyagent,\n",
|
|
")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "flaml_dev",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.16"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|