"`flaml.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/FLAML/docs/Use-Cases/Autogen#agents).\n",
"In this notebook, we demonstrate an application involving multiple agents and human users to work together and accomplish a task. `AssistantAgent` is an LLM-based agent that can write Python code (in a Python coding block) for a user to execute for a given task. `UserProxyAgent` is an agent which serves as a proxy for a user to execute the code written by `AssistantAgent`. We create multiple `UserProxyAgent` instances which can represent different human users.\n",
"\n",
"## Requirements\n",
"\n",
"FLAML requires `Python>=3.8`. To run this notebook example, please install flaml with the [autogen] option:\n",
"* The [`config_list_openai_aoai`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_openai_aoai) function tries to create a list of 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",
" - 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",
"* The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file. It first looks for environment variable `env_or_file` which needs to be a valid json string. If that variable is not found, it then looks for a json file with the same name. It filters the configs by filter_dict.\n",
"\n",
"It's OK to have only the OpenAI API key, or only the Azure OpenAI API key + base. 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"
" 'api_key': '<your Azure OpenAI API key here>',\n",
" 'api_base': '<your Azure OpenAI API base here>',\n",
" 'api_type': 'azure',\n",
" 'api_version': '2023-06-01-preview',\n",
" }, # Azure OpenAI API endpoint for gpt-4\n",
" {\n",
" 'model': 'gpt-4-32k',\n",
" 'api_key': '<your Azure OpenAI API key here>',\n",
" 'api_base': '<your Azure OpenAI API base here>',\n",
" 'api_type': 'azure',\n",
" 'api_version': '2023-06-01-preview',\n",
" }, # Azure OpenAI API endpoint for gpt-4-32k\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.\n",
"\n",
"## Construct Agents\n",
"\n",
"We define `ask_expert` function to start a conversation between two agents and return a summary of the result. We construct an assistant agent named \"assistant_for_expert\" and a user proxy agent named \"expert\". We specify `human_input_mode` as \"ALWAYS\" in the user proxy agent, which will always ask for feedback from the expert user."
"We construct another assistant agent named \"assistant_for_student\" and a user proxy agent named \"student\". We specify `human_input_mode` as \"TERMINATE\" in the user proxy agent, which will ask for feedback when it receives a \"TERMINATE\" signal from the assistant agent. We set the `functions` in `AssistantAgent` and `function_map` in `UserProxyAgent` to use the created `ask_expert` function.\n",
"\n",
"For simplicity, the `ask_expert` function is defined to run locally. For real applications, the function should run remotely to interact with an expert user."
" \"model\": \"gpt-4-0613\", # make sure the endpoint you use supports the model\n",
" \"temperature\": 0,\n",
" \"functions\": [\n",
" {\n",
" \"name\": \"ask_expert\",\n",
" \"description\": \"ask expert when you can't solve the problem satisfactorily.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"message\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"question to ask expert. Make sure the question include enough context, such as the code and the execution result. The expert does not know the conversation between you and the user, unless you share the conversation with the expert.\",\n",
"We invoke the `initiate_chat()` method of the student proxy agent to start the conversation. When you run the cell below, you will be prompted to provide feedback after the assistant agent sends a \"TERMINATE\" signal in the end of the message. If you don't provide any feedback (by pressing Enter directly), the conversation will finish. Before the \"TERMINATE\" signal, the student proxy agent will try to execute the code suggested by the assistant agent on behalf of the user."
"To solve this task, we can use the method of substitution or elimination in linear algebra. We have three equations and three unknowns (a, b, c). We can solve these equations step by step using Python's sympy library, which provides a powerful interface for symbolic computation.\n",
"\n",
"Here is the plan:\n",
"1. Define the symbols a, b, c, x, y using sympy.symbols.\n",
"2. Define the equations using sympy.Eq.\n",
"3. Use sympy.solve to solve the equations.\n",
"\n",
"Let's write the Python code to solve these equations.\n",
"The Python code has successfully solved the system of equations and found the values of a, b, c as well as their sum. The results are in terms of x and y. \n",
"\n",
"Here are the results:\n",
"\n",
"a = (x² - 3xy + 2x + y² - y) / (x² - xy - x + y² - y + 1)\n",
"\n",
"b = (2x² - 3x + y) / (x² - xy - x + y² - y + 1)\n",
" \"message\": \"The user is unsure about the correctness of the solution provided for the system of equations. The solution is in terms of x and y, and it looks complicated. Here are the equations and the solution:\\n\\nEquations:\\n1. ax + by + c = x + 7\\n2. a + bx + cy = 2x + 6y\\n3. ay + b + cx = 4x + y\\n\\nSolution:\\na = (x² - 3xy + 2x + y² - y) / (x² - xy - x + y² - y + 1)\\nb = (2x² - 3x + y) / (x² - xy - x + y² - y + 1)\\nc = (4x² - 4xy - 6x + 6y² - 7y + 7) / (x² - xy - x + y² - y + 1)\\na + b + c = (2x² - 3x + y) / (x² - xy - x + y² - y + 1) + (x² - 3xy + 2x + y² - y) / (x² - xy - x + y² - y + 1) + (4x² - 4xy - 6x + 6y² - 7y + 7) / (x² - xy - x + y² - y + 1)\\n\\nCould you please verify if the solution is correct?\"\n",
"The user is unsure about the correctness of the solution provided for the system of equations. The solution is in terms of x and y, and it looks complicated. Here are the equations and the solution:\n",
"To verify the solution, we can substitute the given values of a, b, and c into the original equations and check if both sides of the equations are equal. If they are, then the solution is correct. If not, then the solution is incorrect.\n",
"This code uses the sympy library to handle symbolic computation. It first defines the symbols x and y. Then it defines a, b, and c as given in the solution. It then defines the original equations and simplifies them. Finally, it checks if all the equations are satisfied (i.e., equal to zero). If they are, it prints True, indicating that the solution is correct. If not, it prints False, indicating that the solution is incorrect.\n",
"The Python code has returned True, which means that the given solution for the system of equations is correct. The values of a, b, and c, as provided in the solution, satisfy all the original equations when substituted back into them. Therefore, the solution is verified and correct. \n",
"The solution provided for the system of equations is already in its simplest form. The expressions for a, b, and c are fractions where the numerator and denominator are polynomials in x and y. These polynomials cannot be further simplified or factored to reduce the complexity of the expressions.\n",
"This code first defines the symbols x and y, and then defines a, b, and c as given in the solution. It then calculates the sum a + b + c and simplifies it using the sympy library's simplify function. Finally, it prints the simplified expression.\n",
"The Python code has returned 7, which means that the sum of a, b, and c, as provided in the solution, simplifies to 7. Therefore, the simplified expression for a + b + c is 7.\n",
"The solution provided for the system of equations is correct. The expressions for a, b, and c, when substituted back into the original equations, satisfy all of them. \n",
"\u001b[32m***** Response from calling function \"ask_expert\" *****\u001b[0m\n",
"The solution provided for the system of equations is correct. The expressions for a, b, and c, when substituted back into the original equations, satisfy all of them. \n",
"The expert has confirmed that the solution provided for the system of equations is correct. The expressions for a, b, and c, when substituted back into the original equations, satisfy all of them. \n",
"Moreover, the expert has pointed out that the sum of a, b, and c simplifies to 7. This means that regardless of the values of x and y (as long as x + y ≠ -1), the sum of a, b, and c will always be 7.\n",
"When the assistant needs to consult the expert, it suggests a function call to `ask_expert`. When this happens, a line like the following will be displayed:\n",
"\n",
"***** Suggested function Call: ask_expert *****\n"