gpt-4.1 and gpt-4o-suffix support
a powerful, built-in
web_search_preview
tool that enables real-time web access.
In this guide, you’ll learn how to integrate and use
web_search_preview within the AG2 framework.
Installation
Install AG2 with theopenai extra.
Note: If you have been usingYou’re all set! Now you can start usingautogenorag2, all you need to do is upgrade it using:orasautogen, andag2are aliases for the same PyPI package.
web_search_preview with AG2.
Imports
Using WebSearchPreviewTool
Now let’s create aWebSearchPreviewTool that will search the web and
return the OpenAPI specification for a desired API (e.g., GitHub,
WhatsApp, etc.).
The tool is configured to: - Search for a raw OpenAPI specification file
in JSON or YAML format. - Return a structured response that matches the
OpenAPISpec class, which includes: - The source page where the spec
was found. - A direct URL to the OpenAPI file. - A relevance score
(1–5).
This makes it easy to retrieve usable API definitions for further
automated processing or integration.
Start Searching
This loop demonstrates how to automatically search for OpenAPI specifications for a list of popular APIs. For each API name, the assistant runs a web search using the configuredWebSearchPreviewTool