Skip to content

Commit

Permalink
Notebook text tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sze <[email protected]>
  • Loading branch information
marklysze committed Dec 5, 2024
1 parent 04c26fb commit 11fd1c2
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions notebook/agentchat_structured_outputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"source": [
"# Structured output\n",
"\n",
"OpenAI offers functionality for defining a structure of the messages generated by LLMs, AG2 enables this functionality by propagating `response_format` passed to your agents to the underlying client.\n",
"OpenAI offers functionality for defining a structure of the messages generated by LLMs, AG2 enables this functionality by propagating `response_format`, in the LLM configuration for your agents, to the underlying client. This is currently only supported by OpenAI.\n",
"\n",
"For more info on structured output, please check [here](https://platform.openai.com/docs/guides/structured-outputs)\n",
"\n",
Expand Down Expand Up @@ -43,7 +43,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/home/tvrtko/ag2/.venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
"/usr/local/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
Expand Down Expand Up @@ -108,6 +108,25 @@
" final_answer: str"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Applying the Response Format\n",
"\n",
"The `response_format` is added to the LLM configuration and then this configuration is applied to the agent."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"for config in config_list:\n",
" config[\"response_format\"] = MathReasoning"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -122,13 +141,10 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"for config in config_list:\n",
" config[\"response_format\"] = MathReasoning\n",
"\n",
"llm_config = {\"config_list\": config_list, \"cache_seed\": 42}\n",
"\n",
"user_proxy = autogen.UserProxyAgent(\n",
Expand All @@ -139,7 +155,7 @@
"\n",
"assistant = autogen.AssistantAgent(\n",
" name=\"Math_solver\",\n",
" llm_config=llm_config,\n",
" llm_config=llm_config, # Response Format is in the configuration\n",
")"
]
},
Expand All @@ -154,7 +170,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -168,18 +184,18 @@
"--------------------------------------------------------------------------------\n",
"\u001b[33mMath_solver\u001b[0m (to User_proxy):\n",
"\n",
"{\"steps\":[{\"explanation\":\"To solve the equation 8x + 7 = -23, we need to isolate x. Start by subtracting 7 from both sides to get rid of the constant term on the left side.\",\"output\":\"8x + 7 - 7 = -23 - 7\"},{\"explanation\":\"By performing the subtraction, we simplify the equation to 8x = -30.\",\"output\":\"8x = -30\"},{\"explanation\":\"Next, divide both sides by 8 to solve for x.\",\"output\":\"x = -30 / 8\"},{\"explanation\":\"Simplify the division to find x. -30 divided by 8 gives us -3.75.\",\"output\":\"x = -3.75\"}],\"final_answer\":\"x = -3.75\"}\n",
"{\"steps\":[{\"explanation\":\"To isolate the term with x, we first subtract 7 from both sides of the equation.\",\"output\":\"8x + 7 - 7 = -23 - 7 -> 8x = -30.\"},{\"explanation\":\"Now that we have 8x = -30, we divide both sides by 8 to solve for x.\",\"output\":\"x = -30 / 8 -> x = -3.75.\"}],\"final_answer\":\"x = -3.75\"}\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
},
{
"data": {
"text/plain": [
"ChatResult(chat_id=None, chat_history=[{'content': 'how can I solve 8x + 7 = -23', 'role': 'assistant', 'name': 'User_proxy'}, {'content': '{\"steps\":[{\"explanation\":\"To solve the equation 8x + 7 = -23, we need to isolate x. Start by subtracting 7 from both sides to get rid of the constant term on the left side.\",\"output\":\"8x + 7 - 7 = -23 - 7\"},{\"explanation\":\"By performing the subtraction, we simplify the equation to 8x = -30.\",\"output\":\"8x = -30\"},{\"explanation\":\"Next, divide both sides by 8 to solve for x.\",\"output\":\"x = -30 / 8\"},{\"explanation\":\"Simplify the division to find x. -30 divided by 8 gives us -3.75.\",\"output\":\"x = -3.75\"}],\"final_answer\":\"x = -3.75\"}', 'role': 'user', 'name': 'Math_solver'}], summary='{\"steps\":[{\"explanation\":\"To solve the equation 8x + 7 = -23, we need to isolate x. Start by subtracting 7 from both sides to get rid of the constant term on the left side.\",\"output\":\"8x + 7 - 7 = -23 - 7\"},{\"explanation\":\"By performing the subtraction, we simplify the equation to 8x = -30.\",\"output\":\"8x = -30\"},{\"explanation\":\"Next, divide both sides by 8 to solve for x.\",\"output\":\"x = -30 / 8\"},{\"explanation\":\"Simplify the division to find x. -30 divided by 8 gives us -3.75.\",\"output\":\"x = -3.75\"}],\"final_answer\":\"x = -3.75\"}', cost={'usage_including_cached_inference': {'total_cost': 0.003085, 'gpt-4o-2024-08-06': {'cost': 0.003085, 'prompt_tokens': 582, 'completion_tokens': 163, 'total_tokens': 745}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
"ChatResult(chat_id=None, chat_history=[{'content': 'how can I solve 8x + 7 = -23', 'role': 'assistant', 'name': 'User_proxy'}, {'content': '{\"steps\":[{\"explanation\":\"To isolate the term with x, we first subtract 7 from both sides of the equation.\",\"output\":\"8x + 7 - 7 = -23 - 7 -> 8x = -30.\"},{\"explanation\":\"Now that we have 8x = -30, we divide both sides by 8 to solve for x.\",\"output\":\"x = -30 / 8 -> x = -3.75.\"}],\"final_answer\":\"x = -3.75\"}', 'role': 'user', 'name': 'Math_solver'}], summary='{\"steps\":[{\"explanation\":\"To isolate the term with x, we first subtract 7 from both sides of the equation.\",\"output\":\"8x + 7 - 7 = -23 - 7 -> 8x = -30.\"},{\"explanation\":\"Now that we have 8x = -30, we divide both sides by 8 to solve for x.\",\"output\":\"x = -30 / 8 -> x = -3.75.\"}],\"final_answer\":\"x = -3.75\"}', cost={'usage_including_cached_inference': {'total_cost': 0.00015089999999999998, 'gpt-4o-mini-2024-07-18': {'cost': 0.00015089999999999998, 'prompt_tokens': 582, 'completion_tokens': 106, 'total_tokens': 688}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -208,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -244,7 +260,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -258,14 +274,10 @@
"--------------------------------------------------------------------------------\n",
"\u001b[33mMath_solver\u001b[0m (to User_proxy):\n",
"\n",
"Step 1: To solve the equation 8x + 7 = -23, we need to isolate x. Start by subtracting 7 from both sides to get rid of the constant term on the left side.\n",
" Output: 8x + 7 - 7 = -23 - 7\n",
"Step 2: By performing the subtraction, we simplify the equation to 8x = -30.\n",
" Output: 8x = -30\n",
"Step 3: Next, divide both sides by 8 to solve for x.\n",
" Output: x = -30 / 8\n",
"Step 4: Simplify the division to find x. -30 divided by 8 gives us -3.75.\n",
" Output: x = -3.75\n",
"Step 1: To isolate the term with x, we first subtract 7 from both sides of the equation.\n",
" Output: 8x + 7 - 7 = -23 - 7 -> 8x = -30.\n",
"Step 2: Now that we have 8x = -30, we divide both sides by 8 to solve for x.\n",
" Output: x = -30 / 8 -> x = -3.75.\n",
"\n",
"Final Answer: x = -3.75\n",
"\n",
Expand All @@ -275,10 +287,10 @@
{
"data": {
"text/plain": [
"ChatResult(chat_id=None, chat_history=[{'content': 'how can I solve 8x + 7 = -23', 'role': 'assistant', 'name': 'User_proxy'}, {'content': 'Step 1: To solve the equation 8x + 7 = -23, we need to isolate x. Start by subtracting 7 from both sides to get rid of the constant term on the left side.\\n Output: 8x + 7 - 7 = -23 - 7\\nStep 2: By performing the subtraction, we simplify the equation to 8x = -30.\\n Output: 8x = -30\\nStep 3: Next, divide both sides by 8 to solve for x.\\n Output: x = -30 / 8\\nStep 4: Simplify the division to find x. -30 divided by 8 gives us -3.75.\\n Output: x = -3.75\\n\\nFinal Answer: x = -3.75', 'role': 'user', 'name': 'Math_solver'}], summary='Step 1: To solve the equation 8x + 7 = -23, we need to isolate x. Start by subtracting 7 from both sides to get rid of the constant term on the left side.\\n Output: 8x + 7 - 7 = -23 - 7\\nStep 2: By performing the subtraction, we simplify the equation to 8x = -30.\\n Output: 8x = -30\\nStep 3: Next, divide both sides by 8 to solve for x.\\n Output: x = -30 / 8\\nStep 4: Simplify the division to find x. -30 divided by 8 gives us -3.75.\\n Output: x = -3.75\\n\\nFinal Answer: x = -3.75', cost={'usage_including_cached_inference': {'total_cost': 0.003085, 'gpt-4o-2024-08-06': {'cost': 0.003085, 'prompt_tokens': 582, 'completion_tokens': 163, 'total_tokens': 745}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
"ChatResult(chat_id=None, chat_history=[{'content': 'how can I solve 8x + 7 = -23', 'role': 'assistant', 'name': 'User_proxy'}, {'content': 'Step 1: To isolate the term with x, we first subtract 7 from both sides of the equation.\\n Output: 8x + 7 - 7 = -23 - 7 -> 8x = -30.\\nStep 2: Now that we have 8x = -30, we divide both sides by 8 to solve for x.\\n Output: x = -30 / 8 -> x = -3.75.\\n\\nFinal Answer: x = -3.75', 'role': 'user', 'name': 'Math_solver'}], summary='Step 1: To isolate the term with x, we first subtract 7 from both sides of the equation.\\n Output: 8x + 7 - 7 = -23 - 7 -> 8x = -30.\\nStep 2: Now that we have 8x = -30, we divide both sides by 8 to solve for x.\\n Output: x = -30 / 8 -> x = -3.75.\\n\\nFinal Answer: x = -3.75', cost={'usage_including_cached_inference': {'total_cost': 0.00015089999999999998, 'gpt-4o-mini-2024-07-18': {'cost': 0.00015089999999999998, 'prompt_tokens': 582, 'completion_tokens': 106, 'total_tokens': 688}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
]
},
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -305,12 +317,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Function calling still works alongside structured output"
"Normal function calling still works alongside structured output, so your agent can have a response format while still calling tools."
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand All @@ -324,7 +336,7 @@
"--------------------------------------------------------------------------------\n",
"\u001b[33mMath_solver\u001b[0m (to User_proxy):\n",
"\n",
"\u001b[32m***** Suggested tool call (call_cp953ANv0S6J6iwrIdMpvLFm): add *****\u001b[0m\n",
"\u001b[32m***** Suggested tool call (call_oTp96rVzs2kAOwGhBM5rJDcW): add *****\u001b[0m\n",
"Arguments: \n",
"{\"x\":3,\"y\":4}\n",
"\u001b[32m********************************************************************\u001b[0m\n",
Expand All @@ -335,10 +347,10 @@
{
"data": {
"text/plain": [
"ChatResult(chat_id=None, chat_history=[{'content': 'solve 3 + 4 by calling appropriate function', 'role': 'assistant', 'name': 'User_proxy'}, {'tool_calls': [{'id': 'call_cp953ANv0S6J6iwrIdMpvLFm', 'function': {'arguments': '{\"x\":3,\"y\":4}', 'name': 'add'}, 'type': 'function'}], 'content': None, 'role': 'assistant'}], summary='', cost={'usage_including_cached_inference': {'total_cost': 0.001715, 'gpt-4o-2024-08-06': {'cost': 0.001715, 'prompt_tokens': 618, 'completion_tokens': 17, 'total_tokens': 635}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
"ChatResult(chat_id=None, chat_history=[{'content': 'solve 3 + 4 by calling appropriate function', 'role': 'assistant', 'name': 'User_proxy'}, {'tool_calls': [{'id': 'call_oTp96rVzs2kAOwGhBM5rJDcW', 'function': {'arguments': '{\"x\":3,\"y\":4}', 'name': 'add'}, 'type': 'function'}], 'content': None, 'role': 'assistant'}], summary='', cost={'usage_including_cached_inference': {'total_cost': 0.0001029, 'gpt-4o-mini-2024-07-18': {'cost': 0.0001029, 'prompt_tokens': 618, 'completion_tokens': 17, 'total_tokens': 635}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -364,7 +376,7 @@
]
},
"kernelspec": {
"display_name": ".venv",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -378,7 +390,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.20"
"version": "3.11.10"
},
"orig_nbformat": 4
},
Expand Down

0 comments on commit 11fd1c2

Please sign in to comment.