Tonic commited on
Commit
74cb588
1 Parent(s): 419aa06

Upload agentchat_MathChat.ipynb

Browse files
Files changed (1) hide show
  1. agentchat_MathChat.ipynb +328 -0
agentchat_MathChat.ipynb ADDED
@@ -0,0 +1,328 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "attachments": {},
5
+ "cell_type": "markdown",
6
+ "metadata": {},
7
+ "source": [
8
+ "<a href=\"https://colab.research.google.com/github/microsoft/autogen/blob/main/notebook/agentchat_MathChat.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
9
+ ]
10
+ },
11
+ {
12
+ "attachments": {},
13
+ "cell_type": "markdown",
14
+ "metadata": {},
15
+ "source": [
16
+ "# Auto Generated Agent Chat: Using MathChat to Solve Math Problems\n",
17
+ "\n",
18
+ "AutoGen offers conversable agents powered by LLM, tool or human, which can be used to perform tasks collectively via automated chat. This framwork allows tool use and human participance through multi-agent conversation. Please find documentation about this feature [here](https://microsoft.github.io/autogen/docs/Use-Cases/agent_chat).\n",
19
+ "\n",
20
+ "MathChat is an experimental 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 (e.g., [Automated Task Solving with Code Generation, Execution & Debugging](https://github.com/microsoft/autogen/blob/main/notebook/agentchat_auto_feedback_from_code_execution.ipynb)). 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](https://arxiv.org/abs/2306.01337) or the [blogpost](https://microsoft.github.io/autogen/blog/2023/06/28/MathChat).\n",
21
+ "\n",
22
+ "## Requirements\n",
23
+ "\n",
24
+ "AutoGen requires `Python>=3.8`. To run this notebook example, please install the [mathchat] option.\n",
25
+ "```bash\n",
26
+ "pip install \"pyautogen[mathchat]\"\n",
27
+ "```"
28
+ ]
29
+ },
30
+ {
31
+ "cell_type": "code",
32
+ "execution_count": null,
33
+ "metadata": {},
34
+ "outputs": [],
35
+ "source": [
36
+ "# %pip install \"pyautogen[mathchat]~=0.1.1\""
37
+ ]
38
+ },
39
+ {
40
+ "attachments": {},
41
+ "cell_type": "markdown",
42
+ "metadata": {},
43
+ "source": [
44
+ "## Set your API Endpoint\n",
45
+ "\n",
46
+ "The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file.\n"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": null,
52
+ "metadata": {},
53
+ "outputs": [],
54
+ "source": [
55
+ "import autogen\n",
56
+ "\n",
57
+ "config_list = autogen.config_list_from_json(\n",
58
+ " \"OAI_CONFIG_LIST\",\n",
59
+ " filter_dict={\n",
60
+ " \"model\": {\n",
61
+ " \"gpt-4\",\n",
62
+ " \"gpt4\",\n",
63
+ " \"gpt-4-32k\",\n",
64
+ " \"gpt-4-32k-0314\",\n",
65
+ " \"gpt-4-32k-v0314\",\n",
66
+ " \"gpt-3.5-turbo\",\n",
67
+ " \"gpt-3.5-turbo-16k\",\n",
68
+ " \"gpt-3.5-turbo-0301\",\n",
69
+ " \"chatgpt-35-turbo-0301\",\n",
70
+ " \"gpt-35-turbo-v0301\",\n",
71
+ " \"gpt\",\n",
72
+ " }\n",
73
+ " }\n",
74
+ ")"
75
+ ]
76
+ },
77
+ {
78
+ "attachments": {},
79
+ "cell_type": "markdown",
80
+ "metadata": {},
81
+ "source": [
82
+ "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).\n",
83
+ "\n",
84
+ "The config list looks like the following:\n",
85
+ "```python\n",
86
+ "config_list = [\n",
87
+ " {\n",
88
+ " 'model': 'gpt-4',\n",
89
+ " 'api_key': '<your OpenAI API key here>',\n",
90
+ " },\n",
91
+ " {\n",
92
+ " 'model': 'gpt-4',\n",
93
+ " 'api_key': '<your Azure OpenAI API key here>',\n",
94
+ " 'api_base': '<your Azure OpenAI API base here>',\n",
95
+ " 'api_type': 'azure',\n",
96
+ " 'api_version': '2023-06-01-preview',\n",
97
+ " },\n",
98
+ " {\n",
99
+ " 'model': 'gpt-3.5-turbo',\n",
100
+ " 'api_key': '<your Azure OpenAI API key here>',\n",
101
+ " 'api_base': '<your Azure OpenAI API base here>',\n",
102
+ " 'api_type': 'azure',\n",
103
+ " 'api_version': '2023-06-01-preview',\n",
104
+ " },\n",
105
+ "]\n",
106
+ "```\n",
107
+ "\n",
108
+ "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.\n",
109
+ "\n",
110
+ "You can set the value of config_list in other ways you prefer, e.g., loading from a YAML file."
111
+ ]
112
+ },
113
+ {
114
+ "attachments": {},
115
+ "cell_type": "markdown",
116
+ "metadata": {},
117
+ "source": [
118
+ "## Construct agents for MathChat\n",
119
+ "\n",
120
+ "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_message` to combine the instructions and a math problem for an initial message to be sent to the LLM assistant."
121
+ ]
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "execution_count": null,
126
+ "metadata": {},
127
+ "outputs": [],
128
+ "source": [
129
+ "from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n",
130
+ "\n",
131
+ "autogen.ChatCompletion.start_logging()\n",
132
+ "\n",
133
+ "# 1. create an AssistantAgent instance named \"assistant\"\n",
134
+ "assistant = autogen.AssistantAgent(\n",
135
+ " name=\"assistant\", \n",
136
+ " system_message=\"You are a helpful assistant.\",\n",
137
+ " llm_config={\n",
138
+ " \"request_timeout\": 600,\n",
139
+ " \"seed\": 42,\n",
140
+ " \"config_list\": config_list,\n",
141
+ " }\n",
142
+ ")\n",
143
+ "\n",
144
+ "# 2. create the MathUserProxyAgent instance named \"mathproxyagent\"\n",
145
+ "# By default, the human_input_mode is \"NEVER\", which means the agent will not ask for human input.\n",
146
+ "mathproxyagent = MathUserProxyAgent(\n",
147
+ " name=\"mathproxyagent\", \n",
148
+ " human_input_mode=\"NEVER\",\n",
149
+ " code_execution_config={\"use_docker\": False},\n",
150
+ ")"
151
+ ]
152
+ },
153
+ {
154
+ "attachments": {},
155
+ "cell_type": "markdown",
156
+ "metadata": {},
157
+ "source": [
158
+ "### Example 1\n",
159
+ "\n",
160
+ "Problem: Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\n",
161
+ "\n",
162
+ "Correct Solution: \n",
163
+ "We have \\begin{align*} (2x+10)(x+3)&<(3x+9)(x+8) \\quad \\Rightarrow\n",
164
+ "\\\\ 2(x+5)(x+3)&<3(x+3)(x+8) \\quad \\Rightarrow\n",
165
+ "\\\\ 2(x+5)(x+3)-3(x+3)(x+8)&<0 \\quad \\Rightarrow\n",
166
+ "\\\\ (2x+10-(3x+24))(x+3)&<0 \\quad \\Rightarrow\n",
167
+ "\\\\ (-x-14)(x+3)&<0 \\quad \\Rightarrow\n",
168
+ "\\\\ (x+14)(x+3)&>0.\n",
169
+ "\\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)} $."
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": null,
175
+ "metadata": {},
176
+ "outputs": [],
177
+ "source": [
178
+ "# given a math problem, we use the mathproxyagent to generate a prompt to be sent to the assistant as the initial message.\n",
179
+ "# the assistant receives the message and generates a response. The response will be sent back to the mathproxyagent for processing.\n",
180
+ "# The conversation continues until the termination condition is met, in MathChat, the termination condition is the detect of \"\\boxed{}\" in the response.\n",
181
+ "math_problem = \"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\"\n",
182
+ "mathproxyagent.initiate_chat(assistant, problem=math_problem)"
183
+ ]
184
+ },
185
+ {
186
+ "attachments": {},
187
+ "cell_type": "markdown",
188
+ "metadata": {},
189
+ "source": [
190
+ "### Example 2\n",
191
+ "Problem: For what negative value of $k$ is there exactly one solution to the system of equations \\begin{align*}\n",
192
+ "y &= 2x^2 + kx + 6 \\\\\n",
193
+ "y &= -x + 4?\n",
194
+ "\\end{align*}\n",
195
+ "\n",
196
+ "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}$."
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": null,
202
+ "metadata": {},
203
+ "outputs": [],
204
+ "source": [
205
+ "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",
206
+ "mathproxyagent.initiate_chat(assistant, problem=math_problem)"
207
+ ]
208
+ },
209
+ {
210
+ "attachments": {},
211
+ "cell_type": "markdown",
212
+ "metadata": {},
213
+ "source": [
214
+ "### Example 3\n",
215
+ "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",
216
+ "\n",
217
+ "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}$."
218
+ ]
219
+ },
220
+ {
221
+ "cell_type": "code",
222
+ "execution_count": null,
223
+ "metadata": {},
224
+ "outputs": [],
225
+ "source": [
226
+ "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",
227
+ "mathproxyagent.initiate_chat(assistant, problem=math_problem)"
228
+ ]
229
+ },
230
+ {
231
+ "attachments": {},
232
+ "cell_type": "markdown",
233
+ "metadata": {},
234
+ "source": [
235
+ "------------------------------------\n",
236
+ "### Using other prompts\n",
237
+ "\n",
238
+ "MathChat allows different prompts that instruct assistant to solve the problem.\n",
239
+ "\n",
240
+ "Check out `MathUserProxyAgent.generate_init_message(problem, prompt_type='default', customized_prompt=None)`:\n",
241
+ "- You may choose from `['default', 'python', 'two_tools']` for parameter `prompt_type`. We include two more prompts in the paper: \n",
242
+ " 1. `'python'` is a simplified prompt from the default prompt that uses Python only. \n",
243
+ " 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",
244
+ "\n",
245
+ "- You can also input your customized prompt if needed: `mathproxyagent.generate_init_message(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",
246
+ "\n",
247
+ "\n",
248
+ "### Example 4 (Use the \"python\" prompt):\n",
249
+ "\n",
250
+ "Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\n",
251
+ "\n",
252
+ "Correct Solution: Subtracting the two equations gives: \n",
253
+ "\\begin{align*}\n",
254
+ "(729x+731y)-(725x+727y) &= 1508-1500\\\\\n",
255
+ "\\Rightarrow\\qquad 4x+4y &= 8\\\\\n",
256
+ "\\Rightarrow\\qquad x+y &= 2.\n",
257
+ "\\end{align*}\n",
258
+ "\n",
259
+ "Multiplying this equation by 725 and subtracting this equation from $725x+727y=1500$ gives \\begin{align*}\n",
260
+ "(725x+727y) - 725(x+y) &= 1500-725(x+y) \\implies \\\\\n",
261
+ "2y &= 50.\n",
262
+ "\\end{align*}So we can write $x-y$ as $(x+y) - 2y$, which equals $2 - 50 = \\boxed{-48}$.\n"
263
+ ]
264
+ },
265
+ {
266
+ "cell_type": "code",
267
+ "execution_count": null,
268
+ "metadata": {},
269
+ "outputs": [],
270
+ "source": [
271
+ "# we set the prompt_type to \"python\", which is a simplied version of the default prompt.\n",
272
+ "math_problem = \"Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\"\n",
273
+ "mathproxyagent.initiate_chat(assistant, problem=math_problem, prompt_type=\"python\")"
274
+ ]
275
+ },
276
+ {
277
+ "attachments": {},
278
+ "cell_type": "markdown",
279
+ "metadata": {},
280
+ "source": [
281
+ "## Example 5 (Use the \"two_tools\" prompt)\n",
282
+ "\n",
283
+ "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",
284
+ "\n",
285
+ "\n",
286
+ "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"
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": null,
292
+ "metadata": {},
293
+ "outputs": [],
294
+ "source": [
295
+ "# The wolfram alpha appid is required for this example (the assistant may choose to query Wolfram Alpha).\n",
296
+ "import os\n",
297
+ "if \"WOLFRAM_ALPHA_APPID\" not in os.environ:\n",
298
+ " os.environ[\"WOLFRAM_ALPHA_APPID\"] = open(\"wolfram.txt\").read().strip()\n",
299
+ "\n",
300
+ "# we set the prompt_type to \"two_tools\", which allows the assistant to select wolfram alpha when necessary.\n",
301
+ "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",
302
+ "mathproxyagent.initiate_chat(assistant, problem=math_problem, prompt_type=\"two_tools\")"
303
+ ]
304
+ }
305
+ ],
306
+ "metadata": {
307
+ "kernelspec": {
308
+ "display_name": "flaml_dev",
309
+ "language": "python",
310
+ "name": "python3"
311
+ },
312
+ "language_info": {
313
+ "codemirror_mode": {
314
+ "name": "ipython",
315
+ "version": 3
316
+ },
317
+ "file_extension": ".py",
318
+ "mimetype": "text/x-python",
319
+ "name": "python",
320
+ "nbconvert_exporter": "python",
321
+ "pygments_lexer": "ipython3",
322
+ "version": "3.9.17"
323
+ },
324
+ "orig_nbformat": 4
325
+ },
326
+ "nbformat": 4,
327
+ "nbformat_minor": 2
328
+ }