"<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",
"\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_gpt4_gpt35`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_gpt4_gpt35) function tries to create a list of gpt-4 and gpt-3.5 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",
"\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",
"\n",
"It's OK to have only the OpenAI API key, or only the Azure OpenAI API key + base.\n"
"# the purpose of the following line is to log the conversation history\n",
"oai.ChatCompletion.start_logging()\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Perform a task\n",
"\n",
"We invoke the `receive()` method of the assistant 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."
"To find the values of $a$, $b$, and $c$ that satisfy the given equations, we can solve the system of equations using the method of matrix inversion. Here's the Python code to solve the system of equations:\n",
"Save the above code in a file named `equations.py` and execute it using Python. The code uses the NumPy library to perform the matrix inversion and solve the system of equations.\n",
"The variable `result` will contain the value of $a + b + c$.\n",
"I apologize for the confusion in the previous explanation. We can't solve this system of equations directly as a linear system because they are not written in the standard form. \n",
"\n",
"To solve these equations, we would first need to re-write them. Let's express 'a' from the first, 'b' from the second, and 'c' from the third equation, then sum up these expressions. This implementation will not give us the values of 'a', 'b', and 'c', but we can find the value of 'a + b + c', which is being asked.\n",
"\n",
"Here is a Python code that does exactly this:\n",
"You need to run the above code, then replace 'x' and 'y' with any appropriate values (keeping in mind the condition x + y ≠ -1) to get the numerical value of 'a + b + c'.\n",
"I apologize for the confusion. It seems there was an error in the code. To find the value of `a + b + c`, we need to substitute `x` and `y` into the expression for `a + b + c`. Here's an updated version of the code:\n",
"\n",
"```python\n",
"from sympy import symbols, Eq, solve\n",
"\n",
"# define symbols\n",
"x, y, a, b, c = symbols('x y a b c')\n",
"\n",
"# define equations\n",
"eq1 = Eq(a * x + b * y + c, x + 7)\n",
"eq2 = Eq(a + b * x + c * y, 2 * x + 6 * y)\n",
"eq3 = Eq(a * y + b + c * x, 4 * x + y)\n",
"\n",
"# express 'a' from eq1, 'b' from eq2, and 'c' from eq3\n",
"a_expr = solve(eq1, a)[0]\n",
"b_expr = solve(eq2, b)[0]\n",
"c_expr = solve(eq3, c)[0]\n",
"\n",
"# substitute x = 2 and y = 3\n",
"expr = a_expr + b_expr + c_expr\n",
"result = expr.subs([(x, 2), (y, 3)])\n",
"\n",
"# print the result\n",
"print(f\"a + b + c =\", result)\n",
"```\n",
"\n",
"Save the above code in a file named `equations.py` and execute it using Python. After running the code, it will print the value of `a + b + c` when `x = 2` and `y = 3`.\n",
"I apologize for the confusion previously. The issue was due to human error in forming correct expressions from given equations. Let's rewrite the equations correctly:\n",
"\n",
"From the first equation:\n",
"\n",
"c = 1 - a - 7b (This is gotten by subtracting ax and by from both sides, making c the subject)\n",
"\n",
"And from the second equation:\n",
"\n",
"a = 2x - 6y - bx - cy (This is gotten by subtracting bx and cy from both sides, making a the subject)\n",
"\n",
"And from the third equation:\n",
"\n",
"b = 4x - y - ay - cx (This is gotten by subtracting ay and cx from both sides, making b the subject)\n",
"\n",
"Then replace a, b and c in the equation a+b+c with the above expressions.\n",
"\n",
"Here's the corrected Python code:\n",
"\n",
"```python\n",
"from sympy import symbols, Eq, solve\n",
"\n",
"# define symbols\n",
"x, y, a, b, c = symbols('x y a b c')\n",
"\n",
"# define equations\n",
"eq1 = Eq(a * x + b * y, x + 7)\n",
"eq2 = Eq(b * x + c * y, 2 * x + 6 * y)\n",
"eq3 = Eq(a * y + c * x, 4 * x + y)\n",
"\n",
"# express 'a' from eq2, 'b' from eq3, and 'c' from eq1\n",
"a_expr = solve(eq2, a)[0]\n",
"b_expr = solve(eq3, b)[0]\n",
"c_expr = solve(eq1 - a_expr * x - b_expr * y, c)[0]\n",
"\n",
"# sum these expressions\n",
"abc_expr = a_expr + b_expr + c_expr\n",
"\n",
"# print a+b+c expression\n",
"print(f\"a + b + c = {abc_expr}\")\n",
"```\n",
"\n",
"Please run the above code to get the correct expression for `a + b + c`. I apologize for any confusion caused by the previous code blocks.\n",
"Save the above code in a file named `equations.py` and execute it using Python. The code uses SymPy to solve the system of equations and calculates the value of `a + b + c`.\n",
"Save the above code in a file named `equations.py` and execute it using Python. The code uses SymPy to solve the system of equations and calculates the value of `a + b + c`.\n",
"The output `a + b + c` is in terms of `x` and `y`. This is the correct expression for `a + b + c`. To get a numerical result, you can substitute any appropriate values for `x` and `y` (keeping in mind that x + y ≠ -1). For example, use `x = 2` and `y = 3`.\n",
"\n",
"Here's the updated code to substitute the values for `x` and `y`:\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",
" \"\"\"\n",
"\n",
"# the assistant receives a message from the user, which contains the task description\n",
"assistant.receive(math_problem_to_solve, user)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analyze the conversation\n",
"\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.\\\\n 1. When you need to ask the user for some info, use the code to output the info you need, for example, browse or search the web, download/read 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.\\\\n Verify your 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\": \"To find the values of $a$, $b$, and $c$ that satisfy the given equations, we can solve the system of equations using the method of matrix inversion. Here\\'s the Python code to solve the system of equations:\\\\n\\\\n```python\\\\nimport numpy as np\\\\n\\\\n# Set up the coefficients of the variables\\\\nA = np.array([[1, 1, 1], [1, 1, 7], [1, 2, 6]])\\\\nb = np.array([1, 2, 4])\\\\n\\\\n# Solve the system of equations\\\\nx = np.linalg.solve(A, b)\\\\n\\\\n# Extract the values of a, b, and c\\\\na = x[0]\\\\nb = x[1]\\\\nc = x[2]\\\\n\\\\n# Calculate a + b + c\\\\nresult = a + b + c\\\\nresult\\\\n```\\\\n\\\\nSave the above code in a file named `equations.py` and execute it using Python. The code uses the NumPy library to perform the matrix inversion and solve the system of equations.\\\\nThe variable `result` will contain the value of $a + b + c$.\", \"role\": \"assistant\"}, {\"content\": \"where is x and y?\", \"role\": \"user\"}, {\"content\": \"I apologize for the confusion in the previous explanation. We can\\'t solve this system of equations directly as a linear system because they are not written in the standard form. \\\\n\\\\nTo solve these equations, we would first need to re-write them. Let\\'s express \\'a\\' from the first, \\'b\\' from the second, and \\'c\\' from the third equation, then sum up these expressions. This implementation will not give us the values of \\'a\\', \\'b\\', and \\'c\\', but we can find the value of \\'a + b + c\\', which is being asked.\\\\n\\\\nHere is a Python code that does exactly this:\\\\n\\\\n```python\\\\nfrom sympy import symbols, Eq, solve\\\\n\\\\n# define symbols\\\\nx, y, a, b, c = symbols(\\'x y a b c\\')\\\\n\\\\n# define 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# express \\'a\\' from eq1, \\'b\\' from eq2, and \\'c\\' from eq3\\\\na_expr = solve(eq1, a)[0]\\\\nb_expr = solve(eq2, b)[0]\\\\nc_expr = solve(eq3, c)[0]\\\\n\\\\n# get a+b+c expression\\\\nabc_expr = a_expr + b_expr + c_expr\\\\n\\\\n# print a+b+c expression\\\\nprint(f\\\\\"a + b + c = {abc_expr}\\\\\")\\\\n```\\\\n\\\\nYou need to run the above code, then replace \\'x\\' and \\'y\\' with any appropriate values (keeping in mind the condition x + y \\\\u2260 -1) to get the numerical value of \\'a + b + c\\'.\", \"role\": \"assistant\"}, {\"content\": \"exitcode: 0 (executio