"<a href=\"https://colab.research.google.com/github/microsoft/FLAML/blob/main/notebook/autogen_agent_human_feedback.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Interactive LLM Agent with Human Feedback\n",
"\n",
"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.\n",
"In this notebook, we demonstrate how to use `AssistantAgent` and `UserProxyAgent` to solve a challenging math problem with human feedback. Here `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`. By setting `human_input_mode` properly, the `UserProxyAgent` can also prompt the user for feedback to `AssistantAgent`. For example, when `human_input_mode` is set to \"ALWAYS\", the `UserProxyAgent` will always prompt the user for feedback. When user feedback is provided, the `UserProxyAgent` will directly pass the feedback to `AssistantAgent` without doing any additional steps. When no user feedback is provided, the `UserProxyAgent` will execute the code written by `AssistantAgent` directly and return the execution results (success or failure and corresponding outputs) to `AssistantAgent`.\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 \"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\".\n",
"We invoke the `initiate_chat()` method of the user proxy agent to start the conversation. When you run the cell below, you will be prompted to provide feedback after receving a message from the assistant agent. If you don't provide any feedback (by pressing Enter directly), the user proxy agent will try to execute the code suggested by the assistant agent on behalf of you, or terminate if the assistant agent sends a \"TERMINATE\" signal in the end of the message."
"The given system of equations is a linear system, which can be solved using linear algebra methods. We can represent this system in matrix form and solve it to find the values of a, b, and c. Finally, we add the values of a, b, and c to get the result.\n",
"You will need to run this python code block. Ensure numpy is installed in your environment. If it's not installed, you can install it by running `pip install numpy`\n",
"Sure, SymPy is a Python library used for symbolic mathematics. It aims to become a full-featured computer algebra system while keeping the code as simple as possible in order to be comprehensible and easily extensible. Here is the Python script using SymPy library for this:\n",
"You should run this python code block. Ensure sympy is installed in your environment. You can install it by running `pip install sympy` if it's not installed.\n",
"The output you are seeing is the expression of `a + b + c` in terms of `x` and `y`. Given that `x + y != -1`, it means that `y != -x -1`. Therefore, the denominator in your result `(x**2 - x*y - x + y**2 - y + 1)` will not be zero (it simplifies to `(x+y+1)**2`), so the solution is correct and valid.\n",
"If you have other values of `x` and `y` for which you would like to find `a + b + c`, you can substitute those values into the provided code by replacing `1` and `2` with your specific `x` and `y` values respectively.\n",
"The human user can provide feedback at each step. When the human user didn't provide feedback, the code was executed. The executed results and error messages are returned to the assistant, and the assistant was able to modify the code to rectify. In the end, the task is complete and a \"TERMINATE\" signal is sent from the assistant. The user skipped feedback in the end and the conversation is finished.\n",
"\n",
"After the conversation is finished, we can save the log of the conversation between the two agents. The log can be accessed from `oai.ChatCompletion.logged_history`."
"{'[{\"content\": \"You are a helpful AI assistant.\\\\n In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute. You must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can\\'t modify your code. So do not suggest incomplete code which requires users to modify. Don\\'t use a code block if it\\'s not intended to be executed by the user.\\\\n 1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file.\\\\n 2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly. Solve the task step by step if you need to. \\\\n If you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don\\'t include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use \\'print\\' function for the output when relevant. Check the execution result returned by the user.\\\\n If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can\\'t be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.\\\\n When you find an answer, verify the answer carefully. If a function for planning is provided, call the function to make plans and verify the execution.\\\\n Reply \\\\\"TERMINATE\\\\\" in the end when everything is done.\\\\n \", \"role\": \"system\"}, {\"content\": \"\\\\n Find $a + b + c$, given that $x+y \\\\\\\\neq -1$ and \\\\n \\\\\\\\begin{align}\\\\n\\\\tax + by + c & = x + 7,\\\\\\\\\\\\n\\\\ta + bx + cy & = 2x + 6y,\\\\\\\\\\\\n\\\\tay + b + cx & = 4x + y.\\\\n\\\\t\\\\\\\\end{align}.\\\\n \", \"role\": \"user\"}, {\"content\": \"The given system of equations is a linear system, which can be solved using linear algebra methods. We can represent this system in matrix form and solve it to find the values of a, b, and c. Finally, we add the values of a, b, and c to get the result.\\\\n\\\\nWe would use the Python Numpy library for solving this. Here is the Python script for this:\\\\n\\\\n```python\\\\nimport numpy as np\\\\n\\\\n# coefficients matrix for the left side of the equations\\\\nA = np.array([[1, 1, 1], \\\\n [1, 1, 1], \\\\n [1, 1, 1]])\\\\n\\\\n# constants on the right side of the equations\\\\nb = np.array([7, 6, -5])\\\\n\\\\n# solve the system of equations\\\\nx = np.linalg.solve(A, b)\\\\n\\\\n# print the result\\\\nprint(f\\\\\"a + b + c = {sum(x)}\\\\\")\\\\n```\\\\n\\\\nYou will need to run this python code block. Ensure numpy is installed in your environment. If it\\'s not installed, you can install it by running `pip install numpy`\", \"role\": \"assistant\"}, {\"content\": \"use sympy\", \"role\": \"user\"}, {\"content\": \"Sure, SymPy is a Python library used for symbolic mathematics. It aims to become a full-featured computer algebra system while keeping the code as simple as possible in order to be comprehensible and easily extensible. Here is the Python script using SymPy library for this:\\\\n\\\\n```python\\\\nfrom sympy import symbols, Eq, solve\\\\n\\\\na, b, c, x, y = symbols(\\'a b c x y\\')\\\\n\\\\n# system of equations\\\\neq1 = Eq(a*x + b*y + c, x + 7)\\\\neq2 = Eq(a + b*x + c*y, 2*x + 6*y)\\\\neq3 = Eq(a*y + b + c*x, 4*x + y)\\\\n\\\\n# solve the system of equations\\\\nsol = solve((eq1,eq2,eq3), (a, b, c))\\\\n\\\\n# print the result\\\\nprint(\\'a + b + c = \\', sum(sol.values()))\\\\n```\\\\n\\\\nYou should run this python code block. Ensure sympy is installed in your environment. You can install it