|
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | 7 | "# Managing Function Calls With Reasoning Models\n", |
8 | | - "OpenAI now offers [reasoning models](https://platform.openai.com/docs/guides/reasoning?api-mode=responses) which are trained to follow logical chains of thought, making them better suited for complex or multi-step tasks.\n", |
9 | | - "> \"_Reasoning models like o3 and o4-mini are LLMs trained with reinforcement learning to perform reasoning. Reasoning models think before they answer, producing a long internal chain of thought before responding to the user. Reasoning models excel in complex problem solving, coding, scientific reasoning, and multi-step planning for agentic workflows. They're also the best models for Codex CLI, our lightweight coding agent._\"\n", |
| 8 | + "OpenAI now offers function calling using [reasoning models](https://platform.openai.com/docs/guides/reasoning?api-mode=responses). Reasoning models are trained to follow logical chains of thought, making them better suited for complex or multi-step tasks.\n", |
| 9 | + "> _Reasoning models like o3 and o4-mini are LLMs trained with reinforcement learning to perform reasoning. Reasoning models think before they answer, producing a long internal chain of thought before responding to the user. Reasoning models excel in complex problem solving, coding, scientific reasoning, and multi-step planning for agentic workflows. They're also the best models for Codex CLI, our lightweight coding agent._\n", |
10 | 10 | "\n", |
11 | 11 | "For the most part, using these models via the API is very simple and comparable to using familiar classic 'chat' models. \n", |
12 | 12 | "\n", |
|
30 | 30 | "source": [ |
31 | 31 | "# pip install openai\n", |
32 | 32 | "# Import libraries \n", |
33 | | - "import json, openai\n", |
| 33 | + "import json\n", |
| 34 | + "from openai import OpenAI\n", |
34 | 35 | "from uuid import uuid4\n", |
35 | 36 | "from typing import Callable\n", |
36 | 37 | "\n", |
37 | | - "client = openai.OpenAI()\n", |
| 38 | + "client = OpenAI()\n", |
38 | 39 | "MODEL_DEFAULTS = {\n", |
39 | 40 | " \"model\": \"o4-mini\", # 200,000 token context window\n", |
40 | 41 | " \"reasoning\": {\"effort\": \"low\", \"summary\": \"auto\"}, # Automatically summarise the reasoning process. Can also choose \"detailed\" or \"none\"\n", |
|
65 | 66 | } |
66 | 67 | ], |
67 | 68 | "source": [ |
68 | | - "# Let's keep track of the response ids in a naive way, in case we want to reverse the conversation and pick up from a previous point\n", |
69 | | - "response = client.responses.create(input=\"Which of the last four Olympic host cities has the highest average temperature?\", **MODEL_DEFAULTS)\n", |
| 69 | + "response = client.responses.create(\n", |
| 70 | + " input=\"Which of the last four Olympic host cities has the highest average temperature?\",\n", |
| 71 | + " **MODEL_DEFAULTS\n", |
| 72 | + ")\n", |
70 | 73 | "print(response.output_text)\n", |
71 | | - "response = client.responses.create(input=\"what about the lowest?\", previous_response_id=response.id, **MODEL_DEFAULTS)\n", |
| 74 | + "\n", |
| 75 | + "response = client.responses.create(\n", |
| 76 | + " input=\"what about the lowest?\",\n", |
| 77 | + " previous_response_id=response.id,\n", |
| 78 | + " **MODEL_DEFAULTS\n", |
| 79 | + ")\n", |
72 | 80 | "print(response.output_text)" |
73 | 81 | ] |
74 | 82 | }, |
|
397 | 405 | "## Manual conversation orchestration\n", |
398 | 406 | "So far so good! It's really cool to watch the model pause execution to run a function before continuing. \n", |
399 | 407 | "In practice the example above is quite trivial, and production use cases may be much more complex:\n", |
400 | | - "* Our context window may grow too large and we may wish to prune older and less relevant messages\n", |
401 | | - "* We may not wish to proceed sequentially using the `previous_response_id` but allow users to navigate back and forth through the conversation and re-generate answers\n", |
| 408 | + "* Our context window may grow too large and we may wish to prune older and less relevant messages, or summarize the conversation so far\n", |
| 409 | + "* We may wish to allow users to navigate back and forth through the conversation and re-generate answers\n", |
402 | 410 | "* We may wish to store messages in our own database for audit purposes rather than relying on OpenAI's storage and orchestration\n", |
403 | 411 | "* etc.\n", |
404 | 412 | "\n", |
|
526 | 534 | "metadata": {}, |
527 | 535 | "source": [ |
528 | 536 | "## Summary\n", |
529 | | - "* Reasoning models can invoke custom functions during their reasoning process, allowing for complex workflows that require external data or operations.\n", |
530 | | - "* These models may require multiple function calls in series, as some steps depend on the results of previous ones, necessitating a loop to handle ongoing reasoning.\n", |
531 | | - "* It's essential to preserve reasoning and function call responses in the conversation history to maintain the chain-of-thought and avoid errors in the reasoning process.\n" |
| 537 | + "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", |
| 538 | + "\n", |
| 539 | + "Importantly, we covered reasoning-model specific nuances in the function calling process, specifically that:\n", |
| 540 | + "* 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", |
| 541 | + "* We cannot know how many of these steps there will be, so we must process responses with a loop\n", |
| 542 | + "* The responses API makes orchestration easy using the `previous_response_id` parameter, but where manual control is needed, it's important to maintain the correct order of conversation item to preserve the 'chain-of-thought'\n", |
| 543 | + "\n", |
| 544 | + "---\n", |
| 545 | + "\n", |
| 546 | + "The examples used here are rather simple, but you can imagine how this technique could be extended to more real-world use cases, such as:\n", |
| 547 | + "\n", |
| 548 | + "* Looking up a customer's transaction history and recent correspondence to determine if they are eligible for a promotional offer\n", |
| 549 | + "* Calling recent transaction logs, geolocation data, and device metadata to assess the likelihood of a transaction being fraudulent\n", |
| 550 | + "* Reviewing internal HR databases to fetch an employee’s benefits usage, tenure, and recent policy changes to answer personalized HR questions\n", |
| 551 | + "* Reading internal dashboards, competitor news feeds, and market analyses to compile a daily executive briefing tailored to their focus areas" |
532 | 552 | ] |
533 | | - }, |
534 | | - { |
535 | | - "cell_type": "markdown", |
536 | | - "metadata": {}, |
537 | | - "source": [] |
538 | 553 | } |
539 | 554 | ], |
540 | 555 | "metadata": { |
|
0 commit comments