{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Auto Generated Agent Chat: Using MathChat to Solve Math Problems\n", "\n", "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", "\n", "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", "\n", "## Requirements\n", "\n", "AutoGen requires `Python>=3.8`. To run this notebook example, please install the [mathchat] option.\n", "```bash\n", "pip install \"pyautogen[mathchat]\"\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %pip install \"pyautogen[mathchat]~=0.1.1\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Set your API Endpoint\n", "\n", "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import autogen\n", "\n", "config_list = autogen.config_list_from_json(\n", " \"OAI_CONFIG_LIST\",\n", " filter_dict={\n", " \"model\": {\n", " \"gpt-4\",\n", " \"gpt4\",\n", " \"gpt-4-32k\",\n", " \"gpt-4-32k-0314\",\n", " \"gpt-4-32k-v0314\",\n", " \"gpt-3.5-turbo\",\n", " \"gpt-3.5-turbo-16k\",\n", " \"gpt-3.5-turbo-0301\",\n", " \"chatgpt-35-turbo-0301\",\n", " \"gpt-35-turbo-v0301\",\n", " \"gpt\",\n", " }\n", " }\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "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", "\n", "The config list looks like the following:\n", "```python\n", "config_list = [\n", " {\n", " 'model': 'gpt-4',\n", " 'api_key': '',\n", " },\n", " {\n", " 'model': 'gpt-4',\n", " 'api_key': '',\n", " 'api_base': '',\n", " 'api_type': 'azure',\n", " 'api_version': '2023-06-01-preview',\n", " },\n", " {\n", " 'model': 'gpt-3.5-turbo',\n", " 'api_key': '',\n", " 'api_base': '',\n", " 'api_type': 'azure',\n", " 'api_version': '2023-06-01-preview',\n", " },\n", "]\n", "```\n", "\n", "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", "\n", "You can set the value of config_list in other ways you prefer, e.g., loading from a YAML file." ] }, { "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_message` to combine the instructions and a math problem for an initial message to be sent to the LLM assistant." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n", "\n", "autogen.ChatCompletion.start_logging()\n", "\n", "# 1. create an AssistantAgent instance named \"assistant\"\n", "assistant = autogen.AssistantAgent(\n", " name=\"assistant\", \n", " system_message=\"You are a helpful assistant.\",\n", " llm_config={\n", " \"request_timeout\": 600,\n", " \"seed\": 42,\n", " \"config_list\": config_list,\n", " }\n", ")\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=\"mathproxyagent\", \n", " human_input_mode=\"NEVER\",\n", " code_execution_config={\"use_docker\": False},\n", ")" ] }, { "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