Web Search That Just Works
1. Web Search That Just Works
So we've laid the groundwork for tools by getting familiar with working with items, so let's begin with a web search tool!2. The Need for Up-to-Date Information
Being able to integrate web search with LLMs is incredibly powerful. Any model you work with has a knowledge cut-off date, that is, this latest information it was exposed to during training. Even for the latest models, this can be a full year into the past. Uses cases like coding and research require the latest information to ensure that their respective outputs are correct in the present moment. The importance of web search in modern LLM applications is why OpenAI provide this functionality out-of-the-box in the Responses API, no other third-party accounts or credentials required!3. Knowledge Cutoffs in Practice
Let's start by prompting a model for the current temperature in Tokyo. Here, the model truthfully informs us that it is unable to access live information. Let's change this!4. Getting Started with Web Search
To enable web search, we require just a single code change: adding a tools list with "type" set to "web_search". Let's re-run the request and see what we get now. The model responds with the information requested, retrieved using the web search tool. Let's look at the output items to see what's going on more clearly.5. Web Search Under-the-Hood
Looping through the output items, we first see a reasoning item where the model reasons about how to call the web search tool, in particular, the search query that would address the user input. Then, we get a new item of type 'web_search_call', where the web search tool is called with the query generated by the previous step. Then we get another reasoning item to interpret the results and determine if the user question has been addressed, and finally, the message item. In the 'web_search_call' item, you may have noticed a 'sources' field inside the .action attribute, which is None by default. It would be incredibly useful to know what sources had been consulted during the search, and fortunately, this is straightforward with the Responses API.6. Including All Sources
To add sources, we use the include parameter, passing it "web_search_call.action.sources", which recall, is the location of this particular field in the item. Now, if we loop over the items and extract only the items of type 'web_search_call', we can see that an API called 'oai-weather' was consulted to retrieve the weather information. OpenAI supports a few APIs for retrieving information on topics like sports, weather, and finance. Let's try a different query and inspect the sources.7. Other Source Types
We'll run the same code again, but requesting information about the latest Python version released.8. Other Source Types
In the sources, we can see that a bunch of URLs were consulted to retrieve relevant information. Notice that the 'type' is now 'url', not 'api', as these are regular web searches. What's also interesting about this example is that multiple web searches were performed. After the first search results were returned, during the reasoning step, the model determined that the user question hadn't been fully addressed, so it performed another web search.9. Let's practice!
Time to have a go combining internet searches and LLMs!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.