"FLAML offers an experimental feature of interactive LLM agents, which can be used to solve various tasks with human or automatic feedback, including tasks that require using tools via code. Please find documentation about this feature [here](https://microsoft.github.io/FLAML/docs/Use-Cases/Auto-Generation#agents-experimental).\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."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"student (to assistant_for_student):\n",
"\n",
"Find $a + b + c$, given that $x+y \\neq -1$ and \n",
"This is a system of linear equations. We can solve it using Python's sympy library, which provides a function to solve systems of equations. Here is the Python code to solve it:\n",
"The output is the expression for $a + b + c$ in terms of $x$ and $y$. It's a bit complex, but it's the correct solution given the system of equations. \n",
"If you have specific values for $x$ and $y$ (with the condition $x + y \\neq -1$), you can substitute them into this expression to get a numerical value for $a + b + c$.\n",
"If you don't have specific values for $x$ and $y$, this is the most simplified form of $a + b + c$ that we can get from the given system of equations.\n",
" \"message\": \"The user has a system of equations and wants to find the sum of the variables a, b, and c. The system of equations is as follows:\\n\\nax + by + c = x + 7,\\na + bx + cy = 2x + 6y,\\nay + b + cx = 4x + y.\\n\\nI used sympy to solve the system of equations and got the following expression for a + b + c:\\n\\n(2*x**2 - 3*x + y)/(x**2 - x*y - x + y**2 - y + 1) + (x**2 - 3*x*y + 2*x + y**2 - y)/(x**2 - x*y - x + y**2 - y + 1) + (4*x**2 - 4*x*y - 6*x + 6*y**2 - 7*y + 7)/(x**2 - x*y - x + y**2 - y + 1)\\n\\nThe user is not sure if the answer is correct. Could you please verify this?\"\n",
"Sure, I can help you verify the solution. Let's first define the given system of equations and the expression for a + b + c using sympy, and then we'll check if the expression is correct by substituting it back into the original equations. Here's the code to do that:\n",
"Great! The output shows that the expression for a + b + c is indeed correct, as the abc_expression equals the abc_sum. You can trust the solution provided by sympy.\n",
"Great! The simplified expression for a + b + c is 7. This means that regardless of the values of x and y, the sum of the variables a, b, and c from the given system of equations will always be 7.\n",
"The user had a system of equations and an expression for the sum of the variables a, b, and c. The user wanted to verify if the expression was correct. The AI assistant used sympy to define the system of equations and the expression, and then checked if the expression was correct by comparing it with the sum of the solutions for a, b, and c. The result was True, indicating that the expression was correct.\n",
"The user then asked to simplify the expression. The AI assistant used sympy's simplify function to simplify the expression, and the result was 7. This means that regardless of the values of x and y, the sum of the variables a, b, and c from the given system of equations will always be 7.\n",
"The user had a system of equations and an expression for the sum of the variables a, b, and c. The user wanted to verify if the expression was correct. The AI assistant used sympy to define the system of equations and the expression, and then checked if the expression was correct by comparing it with the sum of the solutions for a, b, and c. The result was True, indicating that the expression was correct.\n",
"The user then asked to simplify the expression. The AI assistant used sympy's simplify function to simplify the expression, and the result was 7. This means that regardless of the values of x and y, the sum of the variables a, b, and c from the given system of equations will always be 7.\n",
"I apologize for the confusion earlier. After consulting with an expert, it appears that the sum of a, b, and c from the given system of equations is actually a constant value, not dependent on x and y. \n",
"Great! The output confirms that the sum of $a$, $b$, and $c$ from the given system of equations is indeed 7, regardless of the values of $x$ and $y$ (with the condition $x + y \\neq -1$). \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"