diff --git a/examples/reasoning_function_calls.ipynb b/examples/reasoning_function_calls.ipynb index 88afb557e8..935eed283a 100644 --- a/examples/reasoning_function_calls.ipynb +++ b/examples/reasoning_function_calls.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -53,23 +53,22 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Of the last four Summer Olympic host cities—Beijing (2008), London (2012), Rio de Janeiro (2016) and Tokyo (2020)—Rio de Janeiro has by far the highest average annual temperature.\n", + "Among the last four Summer Olympic host cities—Tokyo (2020), Rio de Janeiro (2016), London (2012) and Beijing (2008)—Rio de Janeiro has by far the warmest climate. Average annual temperatures are roughly:\n", "\n", - "Approximate average annual temperatures: \n", - "• Rio de Janeiro: about 23 °C \n", - "• Tokyo: about 16 °C \n", - "• Beijing: about 12 °C \n", - "• London: about 11 °C \n", + "• Rio de Janeiro: ≈ 23 °C \n", + "• Tokyo: ≈ 16 °C \n", + "• Beijing: ≈ 13 °C \n", + "• London: ≈ 11 °C \n", "\n", - "So Rio de Janeiro is the warmest.\n", - "Of those four, London has the lowest average annual temperature, at around 11 °C.\n" + "So Rio de Janeiro has the highest average temperature.\n", + "Among those four, London has the lowest average annual temperature, at about 11 °C.\n" ] } ], @@ -102,7 +101,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -111,20 +110,20 @@ "text": [ "**Determining lowest temperatures**\n", "\n", - "The user previously mentioned four Summer Olympic host cities and found Rio to have the highest average temperature. Now they're asking about the lowest. Considering the averages: London has about 11°C, Beijing around 12°C, Tokyo about 16°C, and Rio roughly 23°C. So, it seems that London's average annual temperature is the lowest at approximately 11°C. I need to keep the focus on this answer since it fits their pattern of inquiry!\n" + "The user is asking about the lowest average temperatures of the last four Olympic host cities: Tokyo, Rio, London, and Beijing. I see London has the lowest average temperature at around 11°C. If I double-check the annual averages: Rio is about 23°C, Tokyo is around 16°C, and Beijing is approximately 13°C. So, my final answer is London with an average of roughly 11°C. I could provide those approximate values clearly for the user.\n" ] }, { "data": { "text/plain": [ - "{'input_tokens': 134,\n", + "{'input_tokens': 136,\n", " 'input_tokens_details': {'cached_tokens': 0},\n", " 'output_tokens': 89,\n", " 'output_tokens_details': {'reasoning_tokens': 64},\n", - " 'total_tokens': 223}" + " 'total_tokens': 225}" ] }, - "execution_count": 15, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -149,7 +148,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -207,17 +206,17 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[ResponseReasoningItem(id='rs_68236afb98748191bedd406ac09304d00650f58f696c6dc4', summary=[], type='reasoning', status=None),\n", - " ResponseFunctionToolCall(arguments='{\"city\":\"London\"}', call_id='call_MYB0swVrKRdFMM5zW4SWNj8m', name='get_city_uuid', type='function_call', id='fc_68236afc99b88191a8d1c616fa3b27790650f58f696c6dc4', status='completed')]" + "[ResponseReasoningItem(id='rs_68246219e8288191af051173b1d53b3f0c4fbdb0d4a46f3c', summary=[], type='reasoning', status=None),\n", + " ResponseFunctionToolCall(arguments='{\"city\":\"London\"}', call_id='call_Mx6pyTjCkSkmASETsVASogoC', name='get_city_uuid', type='function_call', id='fc_6824621b8f6c8191a8095df7230b611e0c4fbdb0d4a46f3c', status='completed')]" ] }, - "execution_count": 17, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -245,7 +244,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -260,23 +259,21 @@ " tool_output = target_tool(**arguments) # Invoke the tool with the arguments\n", " new_conversation_items.append({\n", " \"type\": \"function_call_output\",\n", - " \"call_id\": function_call.call_id, # We map the call_id back to the original function call\n", + " \"call_id\": function_call.call_id, # We map the response back to the original function call\n", " \"output\": tool_output\n", " })" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The internal ID for London is:\n", - "\n", - "eaf83b1b-8e3a-469c-a6a7-669c25cb1898\n" + "The internal ID for London is 816bed76-b956-46c4-94ec-51d30b022725.\n" ] } ], @@ -302,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -345,7 +342,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -393,52 +390,121 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Reasoning step: [Summary(text=\"**Identifying Olympic cities**\\n\\nThe user is looking for IDs of cities that have hosted the Olympics from 2005 to 2025, which includes both Summer and Winter events. The cities to list are: Turin 2006, Beijing 2008, Vancouver 2010, London 2012, Sochi 2014, Rio de Janeiro 2016, Pyeongchang 2018, and Tokyo 2020 (held in 2021). There's also Paris 2024, which is upcoming but still counts. I should clarify which cities have hosted, considering future hosts only as upcoming.\", type='summary_text'), Summary(text=\"**Organizing Olympic city data**\\n\\nI’m realizing that the range from 2005 to 2025 does include 2024, so Paris is part of the list. I need to avoid duplicating Beijing 2022. The final cities are Turin, Beijing, Vancouver, London, Sochi, Rio de Janeiro, Pyeongchang, Tokyo, and Paris. I should retrieve city UUIDs for each and then search for news stories regarding the Olympics in 2025 for these cities. I’ll do this sequentially, starting with Turin, then moving through the others in order, followed by the news searches. Let's get started!\", type='summary_text')]\n", - "Invoking tool: get_city_uuid({'city': 'Turin'})\n", - "More reasoning required, continuing...\n", + "Reasoning step: []\n", "Invoking tool: get_city_uuid({'city': 'Beijing'})\n", "More reasoning required, continuing...\n", - "Invoking tool: get_city_uuid({'city': 'Vancouver'})\n", - "More reasoning required, continuing...\n", + "Reasoning step: []\n", "Invoking tool: get_city_uuid({'city': 'London'})\n", "More reasoning required, continuing...\n", - "Invoking tool: get_city_uuid({'city': 'Sochi'})\n", - "More reasoning required, continuing...\n", + "Reasoning step: []\n", "Invoking tool: get_city_uuid({'city': 'Rio de Janeiro'})\n", "More reasoning required, continuing...\n", - "Invoking tool: get_city_uuid({'city': 'Pyeongchang'})\n", - "More reasoning required, continuing...\n", + "Reasoning step: []\n", "Invoking tool: get_city_uuid({'city': 'Tokyo'})\n", "More reasoning required, continuing...\n", + "Reasoning step: []\n", "Invoking tool: get_city_uuid({'city': 'Paris'})\n", "More reasoning required, continuing...\n", "Reasoning step: []\n", - "Invoking tool: web_search({'query': '2025 news Olympics Turin'})\n", + "Invoking tool: get_city_uuid({'city': 'Turin'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: get_city_uuid({'city': 'Vancouver'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: get_city_uuid({'city': 'Sochi'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: get_city_uuid({'city': 'Pyeongchang'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 Beijing Olympics news'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 London Olympics news'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 Rio de Janeiro Olympics news'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 Tokyo Olympics news'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 Paris Olympics news'})\n", "More reasoning required, continuing...\n", - "Reasoning step: [Summary(text=\"**Determining news relevance**\\n\\nThis is about the Special Olympics, not the regular Olympics, which makes things a bit tricky. The user asked for news stories about the Olympics in general, and while Special Olympics could fit, I think it’s best to focus on the traditional Olympic Games instead. Since there’s no new news on the Turin Olympics, I’ll skip that. Next stop is checking for updates on Beijing. Let's see what I find!\", type='summary_text')]\n", - "Invoking tool: web_search({'query': '2025 news Olympics Beijing'})\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 Turin Olympics news'})\n", "More reasoning required, continuing...\n", "Reasoning step: []\n", - "Invoking tool: web_search({'query': '2025 news Olympics Vancouver'})\n", + "Invoking tool: web_search({'query': '2025 Vancouver Olympics news'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: [Summary(text='**Focusing on Olympic News**\\n\\nI need to clarify that the Invictus Games are not related to the Olympics, so I should exclude them from my search. That leaves me with Olympic-specific news focusing on Paris. I also want to consider past events, like Sochi and Pyeongchang, so I think it makes sense to search for news related to Sochi as well. Let’s focus on gathering relevant Olympic updates to keep things organized.', type='summary_text')]\n", + "Invoking tool: web_search({'query': '2025 Sochi Olympics news'})\n", "More reasoning required, continuing...\n", "Reasoning step: []\n", - "Invoking tool: web_search({'query': '2025 news Olympics London'})\n", + "Invoking tool: web_search({'query': '2025 Pyeongchang Olympics news'})\n", "More reasoning required, continuing...\n", "Reasoning step: []\n", - "Invoking tool: web_search({'query': '2025 news Olympics Sochi'})\n", - "More reasoning required, continuing...\n" + "Here are the internal IDs for all cities that have hosted Olympic Games in the last 20 years (2005–2025), along with those cities that have notable 2025 news stories specifically about the Olympics:\n", + "\n", + "1. Beijing (2008 Summer; 2022 Winter) \n", + " • UUID: 5b058554-7253-4d9d-a434-5d4ccc87c78b \n", + " • 2025 Olympic News? No major Olympic-specific news in 2025\n", + "\n", + "2. London (2012 Summer) \n", + " • UUID: 9a67392d-c319-4598-b69a-adc5ffdaaba2 \n", + " • 2025 Olympic News? No\n", + "\n", + "3. Rio de Janeiro (2016 Summer) \n", + " • UUID: ad5eaaae-b280-4c1d-9360-3a38b0c348c3 \n", + " • 2025 Olympic News? No\n", + "\n", + "4. Tokyo (2020 Summer) \n", + " • UUID: 66c3a62a-840c-417a-8fad-ce87b97bb6a3 \n", + " • 2025 Olympic News? No\n", + "\n", + "5. Paris (2024 Summer) \n", + " • UUID: a2da124e-3fad-402b-8ccf-173f63b4ff68 \n", + " • 2025 Olympic News? Yes \n", + " – Olympic cauldron balloon to float annually over Paris into 2028 ([AP News]) \n", + " – IOC to replace defective Paris 2024 medals ([NDTV Sports]) \n", + " – IOC elects Kirsty Coventry as president at March 2025 session ([Wikipedia]) \n", + " – MLB cancels its planned 2025 Paris regular-season games ([AP News])\n", + "\n", + "6. Turin (2006 Winter) \n", + " • UUID: 3674750b-6b76-49dc-adf4-d4393fa7bcfa \n", + " • 2025 Olympic News? No (Host of Special Olympics World Winter Games, but not mainline Olympics)\n", + "\n", + "7. Vancouver (2010 Winter) \n", + " • UUID: 22517787-5915-41c8-b9dd-a19aa2953210 \n", + " • 2025 Olympic News? No\n", + "\n", + "8. Sochi (2014 Winter) \n", + " • UUID: f7efa267-c7da-4cdc-a14f-a4844f47b888 \n", + " • 2025 Olympic News? No\n", + "\n", + "9. Pyeongchang (2018 Winter) \n", + " • UUID: ffb19c03-5212-42a9-a527-315d35efc5fc \n", + " • 2025 Olympic News? No\n", + "\n", + "Summary of cities with 2025 Olympic-related news: \n", + "• Paris (a2da124e-3fad-402b-8ccf-173f63b4ff68)\n" ] } ], "source": [ - "initial_question = \"What are the internal IDs for the cities that have hosted the Olympics in the last 20 years, and which of those cities have recent news stories (in 2025) about the Olympics? Use your internal tools to look up the IDs and the web search tool to find the news stories.\"\n", + "initial_question = (\n", + " \"What are the internal IDs for the cities that have hosted the Olympics in the last 20 years, \"\n", + " \"and which of those cities have recent news stories (in 2025) about the Olympics? \"\n", + " \"Use your internal tools to look up the IDs and the web search tool to find the news stories.\"\n", + ")\n", "\n", "# We fetch a response and then kick off a loop to handle the response\n", "response = client.responses.create(\n", @@ -447,7 +513,6 @@ ")\n", "while True: \n", " function_responses = invoke_functions_from_response(response)\n", - " messages = [rx.to_dict() for rx in response.output if rx.type == 'message']\n", " if len(function_responses) == 0: # We're done reasoning\n", " print(response.output_text)\n", " break\n", @@ -486,7 +551,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -497,67 +562,70 @@ "User message: Of those cities that have hosted the summer Olympic games in the last 20 years - do any of them have IDs beginning with a number and a temperate climate? Use your available tools to look up the IDs for each city and make sure to search the web to find out about the climate.\n", "*******************************************************************************\n", "More reasoning required, continuing...\n", - "**Listing Summer Olympics cities**\n", + "**Clarifying Olympic Cities**\n", "\n", - "The user wants to identify the host cities of the Summer Olympics over the last 20 years. This would include 2024 in Paris, 2021 in Tokyo, 2016 in Rio, 2012 in London, and 2008 in Beijing. While considering the timeframe from 2005 to 2025, cities from 2004 should be excluded since it’s outside of the last 20 years. So, the final list is Beijing, London, Rio de Janeiro, Tokyo, and Paris. I'm checking their climates, noting that Paris and London have temperate climates, while Rio is tropical and Beijing has a monsoon-influenced climate.\n", - "Reasoning step: [Summary(text=\"**Listing Summer Olympics cities**\\n\\nThe user wants to identify the host cities of the Summer Olympics over the last 20 years. This would include 2024 in Paris, 2021 in Tokyo, 2016 in Rio, 2012 in London, and 2008 in Beijing. While considering the timeframe from 2005 to 2025, cities from 2004 should be excluded since it’s outside of the last 20 years. So, the final list is Beijing, London, Rio de Janeiro, Tokyo, and Paris. I'm checking their climates, noting that Paris and London have temperate climates, while Rio is tropical and Beijing has a monsoon-influenced climate.\", type='summary_text')]\n", - "Invoking tool: get_city_uuid({'city': 'Paris'})\n", + "The user is asking about cities that hosted the Summer Olympics in the last 20 years. The relevant years to consider are 2004 Athens, 2008 Beijing, 2012 London, 2016 Rio de Janeiro, and 2020 Tokyo. If we're considering 2025, then 2004 would actually be 21 years ago, so I should focus instead on the years from 2005 onwards. Therefore, the cities to include are Beijing, London, Rio, and Tokyo. I’ll exclude Paris since it hasn’t hosted yet.\n", + "Reasoning step: [Summary(text=\"**Clarifying Olympic Cities**\\n\\nThe user is asking about cities that hosted the Summer Olympics in the last 20 years. The relevant years to consider are 2004 Athens, 2008 Beijing, 2012 London, 2016 Rio de Janeiro, and 2020 Tokyo. If we're considering 2025, then 2004 would actually be 21 years ago, so I should focus instead on the years from 2005 onwards. Therefore, the cities to include are Beijing, London, Rio, and Tokyo. I’ll exclude Paris since it hasn’t hosted yet.\", type='summary_text')]\n", + "Invoking tool: get_city_uuid({'city': 'Beijing'})\n", "Invoking tool: get_city_uuid({'city': 'London'})\n", "Invoking tool: get_city_uuid({'city': 'Rio de Janeiro'})\n", "Invoking tool: get_city_uuid({'city': 'Tokyo'})\n", - "Invoking tool: get_city_uuid({'city': 'Beijing'})\n", - "More reasoning required, continuing...\n", - "\n", - "Reasoning step: []\n", - "Invoking tool: web_search({'query': 'Paris climate classification'})\n", "More reasoning required, continuing...\n", "\n", "Reasoning step: []\n", - "Invoking tool: web_search({'query': 'London climate classification'})\n", + "Invoking tool: web_search({'query': 'London climate'})\n", + "Invoking tool: web_search({'query': 'Tokyo climate'})\n", "More reasoning required, continuing...\n", "\n", - "Reasoning step: []\n", - "Invoking tool: web_search({'query': 'Beijing climate classification'})\n", - "More reasoning required, continuing...\n", - "**Evaluating climate classification**\n", + "I looked up the internal IDs and climates for each Summer-Olympics host of the last 20 years:\n", "\n", - "I’m looking into Beijing's climate and realizing it falls under Dwa, which is classified as humid continental. This means it’s not temperate, as temperate climates usually refer to C climates. So, I’m left with Paris and London as the temperate options. I check Tokyo but see it's a Cfa and doesn’t fit with my criteria. The final answer: Paris and London qualify as temperate, and I’ll include their IDs and climate types.\n", - "Among the last five Summer Olympic host cities (2008–2024), the only ones that\n", + "• Beijing \n", + " – ID: 937b336d-2708-4ad3-8c2f-85ea32057e1e (starts with “9”) \n", + " – Climate: humid continental (cold winters, hot summers) → not temperate\n", "\n", - "• have Köppen “temperate” (C) climates, and \n", - "• whose internal IDs begin with a digit \n", + "• London \n", + " – ID: ee57f35a-7d1b-4888-8833-4ace308fa004 (starts with “e”) \n", + " – Climate: temperate oceanic (mild, moderate rainfall)\n", "\n", - "are:\n", + "• Rio de Janeiro \n", + " – ID: 2a70c45e-a5b4-4e42-8d2b-6c1dbb2aa2d9 (starts with “2”) \n", + " – Climate: tropical (hot/wet)\n", "\n", - "1. Paris \n", - " – ID: 8c06d343-7532-4b7d-aefe-a434775f2bc1 \n", - " – Climate: Oceanic (Köppen Cfb) – mild temperatures year-round with moderate rainfall \n", + "• Tokyo \n", + " – ID: e5de3686-a7d2-42b8-aca5-6b6e436083ff (starts with “e”) \n", + " – Climate: humid subtropical (hot, humid summers; mild winters)\n", "\n", - "2. London \n", - " – ID: 71210de8-d578-4cbb-869e-2d63268e159e \n", - " – Climate: Temperate oceanic (Köppen Cfb) – cool, wet winters and mild, relatively dry summers \n", - "\n", - "Tokyo (Cfa) is temperate but its ID begins with “f.” Beijing (Dwa) and Rio de Janeiro (Aw) are not classified as temperate.\n", + "The only IDs that begin with a numeral are Beijing (“9…”) and Rio (“2…”), but neither city has a temperate climate. Therefore, none of the last-20-years hosts combine an ID starting with a number with a temperate climate.\n", "*******************************************************************************\n", "User message: Great thanks! We've just updated the IDs - could you please check again?\n", "*******************************************************************************\n", "More reasoning required, continuing...\n", "\n", "Reasoning step: []\n", - "Invoking tool: get_city_uuid({'city': 'Paris'})\n", + "Invoking tool: get_city_uuid({'city': 'Beijing'})\n", "Invoking tool: get_city_uuid({'city': 'London'})\n", - "Here are the updated IDs and their climates:\n", + "Invoking tool: get_city_uuid({'city': 'Rio de Janeiro'})\n", + "Invoking tool: get_city_uuid({'city': 'Tokyo'})\n", + "Here are the updated IDs along with their climates:\n", + "\n", + "• Beijing \n", + " – ID: 8819a1fd-a958-40e6-8ba7-9f450b40fb13 (starts with “8”) \n", + " – Climate: humid continental → not temperate\n", + "\n", + "• London \n", + " – ID: 50866ef9-6505-4939-90e7-e8b930815782 (starts with “5”) \n", + " – Climate: temperate oceanic\n", "\n", - "1. Paris \n", - " – New ID: abb950fe-1748-4d99-a341-5b6a3ed3e544 \n", - " – Climate: Oceanic (Köppen Cfb) – mild temperatures year-round with moderate rainfall \n", + "• Rio de Janeiro \n", + " – ID: 5bc1b2de-75da-4689-8bff-269e60af32cb (starts with “5”) \n", + " – Climate: tropical → not temperate\n", "\n", - "2. London \n", - " – New ID: dc73ca06-ba2d-4561-965e-5bec3d8b4d37 \n", - " – Climate: Temperate oceanic (Köppen Cfb) – cool, wet winters and mild summers \n", + "• Tokyo \n", + " – ID: 9d1c920e-e725-423e-b83c-ec7d97f2e79f (starts with “9”) \n", + " – Climate: humid subtropical → not temperate\n", "\n", - "Both remain temperate (Cfb) and their IDs still begin with a digit.\n", - "Total tokens used: 13217 (6.61% of o4-mini's context window)\n" + "Of these, the only city with a temperate climate is London, but its ID begins with “5” (a number) – so it does meet “ID beginning with a number AND temperate climate.” \n", + "Total tokens used: 17154 (8.58% of o4-mini's context window)\n" ] } ], @@ -565,7 +633,11 @@ "# Let's initialise our conversation with the first user message\n", "total_tokens_used = 0\n", "user_messages = [\n", - " \"Of those cities that have hosted the summer Olympic games in the last 20 years - do any of them have IDs beginning with a number and a temperate climate? Use your available tools to look up the IDs for each city and make sure to search the web to find out about the climate.\",\n", + " (\n", + " \"Of those cities that have hosted the summer Olympic games in the last 20 years - \"\n", + " \"do any of them have IDs beginning with a number and a temperate climate? \"\n", + " \"Use your available tools to look up the IDs for each city and make sure to search the web to find out about the climate.\"\n", + " ),\n", " \"Great thanks! We've just updated the IDs - could you please check again?\"\n", " ]\n", "\n",