mirror of
https://github.com/microsoft/FLAML.git
synced 2026-02-09 02:09:16 +08:00
simplify the initiation of chat (#1131)
* simplify the initiation of chat * version update * include openai * completion
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %pip install flaml[mathchat]==2.0.0rc2"
|
||||
"# %pip install flaml[mathchat]~=2.0.0rc4"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -122,14 +122,16 @@
|
||||
" system_message=\"You are a helpful assistant.\",\n",
|
||||
" request_timeout=600, \n",
|
||||
" seed=42, \n",
|
||||
" config_list=config_list)\n",
|
||||
" config_list=config_list,\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=\"MathChatAgent\", \n",
|
||||
" human_input_mode=\"NEVER\",\n",
|
||||
" use_docker=False)"
|
||||
" use_docker=False,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -283,11 +285,8 @@
|
||||
"# given a math problem, we use the mathproxyagent to generate a prompt to be sent to the assistant as the initial message.\n",
|
||||
"# the assistant receives the message and generates a response. The response will be sent back to the mathproxyagent for processing.\n",
|
||||
"# The conversation continues until the termination condition is met, in MathChat, the termination condition is the detect of \"\\boxed{}\" in the response.\n",
|
||||
"math_problem = \"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\"\n",
|
||||
"assistant.receive(\n",
|
||||
" message=mathproxyagent.generate_init_prompt(math_problem),\n",
|
||||
" sender=mathproxyagent,\n",
|
||||
")"
|
||||
"math_problem = \"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\"\n",
|
||||
"mathproxyagent.initiate_chat(assistant, math_problem)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -429,11 +428,8 @@
|
||||
"source": [
|
||||
"assistant.reset()\n",
|
||||
"\n",
|
||||
"math_problem = \"For what negative value of $k$ is there exactly one solution to the system of equations \\\\begin{align*}\\ny &= 2x^2 + kx + 6 \\\\\\\\\\ny &= -x + 4?\\n\\\\end{align*}\"\n",
|
||||
"assistant.receive(\n",
|
||||
" mathproxyagent.generate_init_prompt(math_problem),\n",
|
||||
" mathproxyagent,\n",
|
||||
")"
|
||||
"math_problem = \"For what negative value of $k$ is there exactly one solution to the system of equations \\\\begin{align*}\\ny &= 2x^2 + kx + 6 \\\\\\\\\\ny &= -x + 4?\\n\\\\end{align*}\"\n",
|
||||
"mathproxyagent.initiate_chat(assistant, math_problem)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -561,11 +557,8 @@
|
||||
"source": [
|
||||
"assistant.reset()\n",
|
||||
"\n",
|
||||
"math_problem = \"Find all positive integer values of $c$ such that the equation $x^2-7x+c=0$ only has roots that are real and rational. Express them in decreasing order, separated by commas.\"\n",
|
||||
"assistant.receive(\n",
|
||||
" mathproxyagent.generate_init_prompt(math_problem),\n",
|
||||
" mathproxyagent,\n",
|
||||
")"
|
||||
"math_problem = \"Find all positive integer values of $c$ such that the equation $x^2-7x+c=0$ only has roots that are real and rational. Express them in decreasing order, separated by commas.\"\n",
|
||||
"mathproxyagent.initiate_chat(assistant, math_problem)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -760,11 +753,8 @@
|
||||
"assistant.reset() # clear LLM assistant's message history\n",
|
||||
"\n",
|
||||
"# we set the prompt_type to \"python\", which is a simplied version of the default prompt.\n",
|
||||
"math_problem = \"Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\"\n",
|
||||
"assistant.receive(\n",
|
||||
" mathproxyagent.generate_init_prompt(math_problem, prompt_type=\"python\"),\n",
|
||||
" mathproxyagent,\n",
|
||||
")"
|
||||
"math_problem = \"Problem: If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\"\n",
|
||||
"mathproxyagent.initiate_chat(assistant, math_problem, prompt_type=\"python\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -904,11 +894,8 @@
|
||||
" os.environ[\"WOLFRAM_ALPHA_APPID\"] = open(\"wolfram.txt\").read().strip()\n",
|
||||
"\n",
|
||||
"# we set the prompt_type to \"two_tools\", which allows the assistant to select wolfram alpha when necessary.\n",
|
||||
"math_problem = \"Find all numbers $a$ for which the graph of $y=x^2+a$ and the graph of $y=ax$ intersect. Express your answer in interval notation.\"\n",
|
||||
"assistant.receive(\n",
|
||||
" mathproxyagent.generate_init_prompt(math_problem, prompt_type=\"two_tools\"),\n",
|
||||
" mathproxyagent,\n",
|
||||
")"
|
||||
"math_problem = \"Find all numbers $a$ for which the graph of $y=x^2+a$ and the graph of $y=ax$ intersect. Express your answer in interval notation.\"\n",
|
||||
"mathproxyagent.initiate_chat(assistant, math_problem, prompt_type=\"two_tools\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %pip install flaml[autogen]==2.0.0rc3"
|
||||
"# %pip install flaml[autogen]~=2.0.0rc4"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -209,9 +209,9 @@
|
||||
" use_docker=False, # set to True if you are using docker\n",
|
||||
")\n",
|
||||
"# the assistant receives a message from the user, which contains the task description\n",
|
||||
"assistant.receive(\n",
|
||||
"user.initiate_chat(\n",
|
||||
" assistant,\n",
|
||||
" \"\"\"What date is today? Compare the year-to-date gain for META and TESLA.\"\"\",\n",
|
||||
" user,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -314,9 +314,9 @@
|
||||
],
|
||||
"source": [
|
||||
"# followup of the previous question\n",
|
||||
"assistant.receive(\n",
|
||||
" \"\"\"Plot a chart of their stock price change YTD and save to stock_price_ytd.png.\"\"\",\n",
|
||||
" user\n",
|
||||
"user.send(\n",
|
||||
" recipient=assistant,\n",
|
||||
" message=\"\"\"Plot a chart of their stock price change YTD and save to stock_price_ytd.png.\"\"\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %pip install flaml[mathchat]==2.0.0rc3"
|
||||
"# %pip install flaml[mathchat]~=2.0.0rc4"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -79,11 +79,11 @@
|
||||
"source": [
|
||||
"## Making Function Calls\n",
|
||||
"\n",
|
||||
"In this example, we demonstrate function call execution with `AssistantAgent` and `UserProxyAgent`. With the default system prompt of `AssistantAgent`, we allow the LLM assistant to perform tasks with code, and the `UserProxyAgent` would extract code blocks from the LLM response and execute them. With the new \"function_call\" feature, we define a new function using the pre-defined `_execute_code` from `UserProxyAgent` and specify the description of the function in the OpenAI config. \n",
|
||||
"In this example, we demonstrate function call execution with `AssistantAgent` and `UserProxyAgent`. With the default system prompt of `AssistantAgent`, we allow the LLM assistant to perform tasks with code, and the `UserProxyAgent` would extract code blocks from the LLM response and execute them. With the new \"function_call\" feature, we define a new function using the pre-defined `execute_code` from `UserProxyAgent` and specify the description of the function in the OpenAI config. \n",
|
||||
"\n",
|
||||
"Then, the model has two paths to execute code:\n",
|
||||
"1. Put the code blocks in the response. `UserProxyAgent` will extract and execute the code through `_execute_code` method in the class.\n",
|
||||
"2. As we put a function description to OpenAI config and passed a function `execute_code_function` to `UserProxyAgent`, the model can also make function calls (will be put in `function_call` field of the API reply). `UserProxyAgent` will execute the function call through a `_execute_function` method."
|
||||
"1. Put the code blocks in the response. `UserProxyAgent` will extract and execute the code through `execute_code` method in the class.\n",
|
||||
"2. As we put a function description to OpenAI config and register a function `exec_code` in `UserProxyAgent`, the model can also make function calls (will be put in `function_call` field of the API reply). `UserProxyAgent` will execute the function call through the registered `exec_code` method."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -234,24 +234,26 @@
|
||||
"}\n",
|
||||
"chatbot = AssistantAgent(\"chatbot\", config_list=config_list, **oai_config)\n",
|
||||
"\n",
|
||||
"# use pre-defined execute_code function from a UserProxyAgent instance\n",
|
||||
"# for simplicity, we don't pass in `exec_func` directly to UserProxyAgent because it requires a list of tuple as parameter\n",
|
||||
"# instead, we define a wrapper function to call `exec_func`\n",
|
||||
"exec_func = UserProxyAgent(name=\"execute_code\", work_dir=\"coding\", use_docker=False)._execute_code\n",
|
||||
"\n",
|
||||
"def execute_code(code_type, code):\n",
|
||||
" return exec_func([(code_type, code)])\n",
|
||||
"\n",
|
||||
"# create a UserProxyAgent instance named \"user\"\n",
|
||||
"user = UserProxyAgent(\n",
|
||||
" \"user\",\n",
|
||||
" human_input_mode=\"NEVER\",\n",
|
||||
" function_map={\"execute_code\": execute_code},\n",
|
||||
" work_dir=\"coding\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# define an `execute_code` function according to the function desription\n",
|
||||
"def exec_code(code_type, code):\n",
|
||||
" # here we reuse the method in the user proxy agent\n",
|
||||
" # in general, this is not necessary\n",
|
||||
" return user.execute_code([(code_type, code)])\n",
|
||||
"\n",
|
||||
"# register the `execute_code` function\n",
|
||||
"user.register_function(function_map={\"execute_code\": exec_code})\n",
|
||||
"\n",
|
||||
"# start the conversation\n",
|
||||
"chatbot.receive(\n",
|
||||
"user.initiate_chat(\n",
|
||||
" chatbot,\n",
|
||||
" \"Draw a rocket and save to a file named 'rocket.svg'\",\n",
|
||||
" user,\n",
|
||||
")\n"
|
||||
]
|
||||
},
|
||||
@@ -289,7 +291,7 @@
|
||||
"source": [
|
||||
"## Another example with Wolfram Alpha API\n",
|
||||
"\n",
|
||||
"We give another example of query Wolfram Alpha API to solve math problem. We use the predefined function from `MathUserProxyAgent()`, we directly pass the class method, `MathUserProxyAgent()._execute_one_wolfram_query`, as the function to be called."
|
||||
"We give another example of query Wolfram Alpha API to solve math problem. We use the predefined function `MathUserProxyAgent().execute_one_wolfram_query` as the function to be called."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -389,19 +391,18 @@
|
||||
"}\n",
|
||||
"chatbot = AssistantAgent(\"chatbot\", sys_prompt, config_list=config_list, **oai_config)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# the key in `function_map` should match the function name passed to OpenAI\n",
|
||||
"# we pass a class instance directly\n",
|
||||
"# the key in `function_map` should match the function name in \"functions\" above\n",
|
||||
"# we register a class instance method directly\n",
|
||||
"user = UserProxyAgent(\n",
|
||||
" \"user\",\n",
|
||||
" human_input_mode=\"NEVER\",\n",
|
||||
" function_map={\"query_wolfram\": MathUserProxyAgent()._execute_one_wolfram_query},\n",
|
||||
" function_map={\"query_wolfram\": MathUserProxyAgent().execute_one_wolfram_query},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# start the conversation\n",
|
||||
"chatbot.receive(\n",
|
||||
"user.initiate_chat(\n",
|
||||
" chatbot,\n",
|
||||
" \"Problem: Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\",\n",
|
||||
" user,\n",
|
||||
")\n"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %pip install flaml[autogen]==2.0.0rc3"
|
||||
"# %pip install flaml[autogen]~=2.0.0rc4"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %pip install flaml[autogen]==2.0.0rc3"
|
||||
"# %pip install flaml[autogen]~=2.0.0rc4"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -419,9 +419,9 @@
|
||||
],
|
||||
"source": [
|
||||
"# the assistant receives a message from the user, which contains the task description\n",
|
||||
"assistant.receive(\n",
|
||||
"user.initiate_chat(\n",
|
||||
" assistant,\n",
|
||||
" \"\"\"Suggest a fix to an open good first issue of flaml\"\"\",\n",
|
||||
" user\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %pip install flaml[autogen]==2.0.0rc3"
|
||||
"# %pip install flaml[autogen]~=2.0.0rc4"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -82,7 +82,7 @@
|
||||
"source": [
|
||||
"from flaml import oai\n",
|
||||
"\n",
|
||||
"config_list = oai.config_list_openai_aoai(exclude=\"openai\")"
|
||||
"config_list = oai.config_list_openai_aoai()"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user