diff --git a/examples/reasoning_function_calls.ipynb b/examples/reasoning_function_calls.ipynb index 127de07e11..88afb557e8 100644 --- a/examples/reasoning_function_calls.ipynb +++ b/examples/reasoning_function_calls.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -53,15 +53,23 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Among 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 mean annual temperature—around 23 °C, compared with about 16 °C in Tokyo, 13 °C in Beijing and 11 °C in London.\n", - "Of those four, London has the lowest mean annual temperature, at roughly 11 °C.\n" + "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", + "\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", + "\n", + "So Rio de Janeiro is the warmest.\n", + "Of those four, London has the lowest average annual temperature, at around 11 °C.\n" ] } ], @@ -94,29 +102,29 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "**Determining Olympic cities**\n", + "**Determining lowest temperatures**\n", "\n", - "The user is asking about the last four Olympic host cities, assuming it’s for the Summer Olympics. Those would be Beijing in 2008, London in 2012, Rio in 2016, and Tokyo in 2020. They’re interested in the lowest average temperature, which I see is London at around 11°C. Beijing is about 13°C, Tokyo 16°C, but London has the lowest. I should clarify it's the mean annual temperature. So, I'll present it neatly that London is the answer.\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" ] }, { "data": { "text/plain": [ - "{'input_tokens': 109,\n", + "{'input_tokens': 134,\n", " 'input_tokens_details': {'cached_tokens': 0},\n", " 'output_tokens': 89,\n", " 'output_tokens_details': {'reasoning_tokens': 64},\n", - " 'total_tokens': 198}" + " 'total_tokens': 223}" ] }, - "execution_count": 3, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -141,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -199,17 +207,17 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[ResponseReasoningItem(id='rs_680bcde645a08191bbb8b42ba4613aef07423969e3977116', summary=[], type='reasoning', status=None),\n", - " ResponseFunctionToolCall(arguments='{\"city\":\"London\"}', call_id='call_VcyIJQnP7HW2gge7Nh8HmPNG', name='get_city_uuid', type='function_call', id='fc_680bcde7cda48191ada496d462ca7c5407423969e3977116', status='completed')]" + "[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')]" ] }, - "execution_count": 5, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -237,7 +245,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -259,14 +267,16 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The internal ID for London is ce863d03-9c01-4de2-9af8-96b123852aec.\n" + "The internal ID for London is:\n", + "\n", + "eaf83b1b-8e3a-469c-a6a7-669c25cb1898\n" ] } ], @@ -285,6 +295,45 @@ "source": [ "This works great here - as we know that a single function call is all that is required for the model to respond - but we also need to account for situations where multiple tool calls might need to be executed for the reasoning to complete.\n", "\n", + "Let's add a second call to run a web search.\n", + "\n", + "OpenAI's web search tool is not available out of the box with reasoning models (as of May 2025 - this may soon change) but it's not too hard to create a custom web search function using 4o mini or another web search enabled model." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "def web_search(query: str) -> str:\n", + " \"\"\"Search the web for information and return back a summary of the results\"\"\"\n", + " result = client.responses.create(\n", + " model=\"gpt-4o-mini\",\n", + " input=f\"Search the web for '{query}' and reply with only the result.\",\n", + " tools=[{\"type\": \"web_search_preview\"}],\n", + " )\n", + " return result.output_text\n", + "\n", + "tools.append({\n", + " \"type\": \"function\",\n", + " \"name\": \"web_search\",\n", + " \"description\": \"Search the web for information and return back a summary of the results\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"query\": {\"type\": \"string\", \"description\": \"The query to search the web for.\"}\n", + " },\n", + " \"required\": [\"query\"]\n", + " }\n", + " })\n", + "tool_mapping[\"web_search\"] = web_search\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "## Executing multiple functions in series\n", "\n", "Some OpenAI models support the parameter `parallel_tool_calls` which allows the model to return an array of functions which we can then execute in parallel. However, reasoning models may produce a sequence of function calls that must be made in series, particularly as some steps may depend on the results of previous ones.\n", @@ -296,7 +345,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -344,13 +393,14 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "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", "Invoking tool: get_city_uuid({'city': 'Beijing'})\n", @@ -369,25 +419,26 @@ "More reasoning required, continuing...\n", "Invoking tool: get_city_uuid({'city': 'Paris'})\n", "More reasoning required, continuing...\n", - "Here are the internal IDs for the cities that have hosted the Olympics in the last 20 years:\n", - "\n", - "• Turin: 53c0e635-7a1c-478b-84ca-742a6f0df830 \n", - "• Beijing: 2c48757a-a1ed-48e7-897f-9edecf4909b5 \n", - "• Vancouver: cc8be1f1-5154-46f4-8879-451e97f771c7 \n", - "• London: a24addb0-4dd4-444c-a4a9-199612e0aca8 \n", - "• Sochi: da7386b3-2283-45cc-9244-c1e0f4121782 \n", - "• Rio de Janeiro: 01f60ec2-0efd-40b8-bb85-e63c2d2ddf4c \n", - "• Pyeongchang: f5d3687a-0097-4551-800c-aec66c37e8db \n", - "• Tokyo: 15aa0b12-7f7c-43d0-9ba3-b91250cafe48 \n", - "• Paris: 56d062f2-8835-4707-a826-5d68d8be9d3f \n", - "\n", - "Of these, the only city whose ID begins with “2” is:\n", - "• Beijing: 2c48757a-a1ed-48e7-897f-9edecf4909b5\n" + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 news Olympics Turin'})\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", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 news Olympics Vancouver'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 news Olympics London'})\n", + "More reasoning required, continuing...\n", + "Reasoning step: []\n", + "Invoking tool: web_search({'query': '2025 news Olympics Sochi'})\n", + "More reasoning required, continuing...\n" ] } ], "source": [ - "initial_question = \"What are the internal IDs for the cities that have hosted the Olympics in the last 20 years, and which cities have IDs beginning with the number '2'. Use your internal tools to look up the IDs?\"\n", + "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", "\n", "# We fetch a response and then kick off a loop to handle the response\n", "response = client.responses.create(\n", @@ -435,7 +486,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -443,56 +494,70 @@ "output_type": "stream", "text": [ "*******************************************************************************\n", - "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 prime number? Use your available tools to look up the IDs for each city.\n", + "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", "\n", - "Invoking tool: get_city_uuid({'city': 'Beijing'})\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", "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': 'Paris'})\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", + "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", + "\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", + "\n", + "• have Köppen “temperate” (C) climates, and \n", + "• whose internal IDs begin with a digit \n", + "\n", + "are:\n", "\n", - "Here are the UUIDs for each Summer Olympic host city since 2005, with the leading numeric prefix highlighted and assessed for primality:\n", + "1. Paris \n", + " – ID: 8c06d343-7532-4b7d-aefe-a434775f2bc1 \n", + " – Climate: Oceanic (Köppen Cfb) – mild temperatures year-round with moderate rainfall \n", "\n", - "• Beijing (2008): 11ab370c-2f59-4c35-b557-f845e22c847b \n", - " – Leading digits “11” → 11 is prime \n", - "• London (2012): 0fdff00b-cbfb-4b82-bdd8-2107c4100319 \n", - " – Leading digit “0” → 0 is not prime \n", - "• Rio de Janeiro (2016): 9c2202c4-00ab-46ee-a954-a17505e32d64 \n", - " – Leading digit “9” → 9 is not prime \n", - "• Tokyo (2020): c4bf0281-7e84-4489-88e4-750e07211334 \n", - " – No leading digit → N/A \n", - "• Paris (2024): b8c4b88e-dece-435d-b398-94f0ff762c88 \n", - " – No leading digit → N/A \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", - "Conclusion: Only Beijing’s ID begins with a prime number (“11”).\n", + "Tokyo (Cfa) is temperate but its ID begins with “f.” Beijing (Dwa) and Rio de Janeiro (Aw) are not classified as temperate.\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", - "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", + "Reasoning step: []\n", "Invoking tool: get_city_uuid({'city': 'Paris'})\n", - "Here are the updated UUIDs and their leading numeric prefixes:\n", + "Invoking tool: get_city_uuid({'city': 'London'})\n", + "Here are the updated IDs and their climates:\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", "\n", - "• Beijing (2008): 30b0886f-c4da-431c-8983-33e8bbb4c352 \n", - " – Leading “30” → 30 is not prime \n", - "• London (2012): 72ff5a9d-d147-4ba8-9a87-64e3572ba3bc \n", - " – Leading “72” → 72 is not prime \n", - "• Rio de Janeiro (2016): 7a45a392-b43a-41be-8eaf-07ec44d42a2b \n", - " – Leading “7” → 7 is prime \n", - "• Tokyo (2020): f725244f-079f-44e1-a91c-5c31c270c209 \n", - " – Leading “f” → no numeric prefix \n", - "• Paris (2024): b0230ad4-bc35-48be-a198-65a9aaf28fb5 \n", - " – Leading “b” → no numeric prefix \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", "\n", - "Conclusion: After the update, only Rio de Janeiro’s ID begins with a prime number (“7”).\n", - "Total tokens used: 9734 (4.87% of o4-mini's context window)\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" ] } ], @@ -500,7 +565,7 @@ "# 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 prime number? Use your available tools to look up the IDs for each city.\",\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", " \"Great thanks! We've just updated the IDs - could you please check again?\"\n", " ]\n", "\n", @@ -545,7 +610,7 @@ "metadata": {}, "source": [ "## Summary\n", - "In this cookbook, we identified how to combine function calling with OpenAI's reasoning models to demonstrate multi-step tasks that are dependent on external data sources. \n", + "In this cookbook, we identified how to combine function calling with OpenAI's reasoning models to demonstrate multi-step tasks that are dependent on external data sources., including searching the web.\n", "\n", "Importantly, we covered reasoning-model specific nuances in the function calling process, specifically that:\n", "* The model may choose to make multiple function calls or reasoning steps in series, and some steps may depend on the results of previous ones\n", @@ -565,7 +630,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "openai", "language": "python", "name": "python3" },