Using Web Search Tool with GPT-4o
In this lesson, we’ll explore how to enable the web_search_preview tool to allow GPT-4o to fetch up-to-date information from the internet.
Prerequisites
Ensure you have:
- Python 3.8 or later
- Installed OpenAI SDK:
pip install openai
The Code
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4o-mini",
tools=[{"type": "web_search_preview"}],
input="What was a positive news story from today?"
)
print(response.output_text)
Explanation
tools=[{"type": "web_search_preview"}]: Enables the GPT model to perform a web search.input=...: Your query will be supplemented with relevant information pulled from live web data.response.output_text: Displays the model’s summarized response.
Use Case
This is ideal for scenarios requiring:
- Real-time news summaries
- Current event awareness
- Dynamic content generation based on live information